package com.project.zcustom.service.service.basics.impl;

import com.project.zcustom.domain.basics.PlatBasicsServer;
import com.project.zcustom.mapper.service.basics.PlatBasicsServerMapper;
import com.project.zcustom.service.service.basics.IPlatBasicsServerService;
import com.project.common.core.domain.search.PageParamVo;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.common.utils.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 服务信息Service业务层处理
 *
 * @author change
 * @date 2024-10-21
 */
@Service
public class PlatBasicsServerServiceImpl extends ServiceImpl<PlatBasicsServerMapper, PlatBasicsServer> implements IPlatBasicsServerService {

    /**
     * 组装查询条件
     */
    private LambdaQueryWrapper<PlatBasicsServer> getLambdaQueryWrapper(PlatBasicsServer platBasicsServer) {
        LambdaQueryWrapper<PlatBasicsServer> lqw = new LambdaQueryWrapper<PlatBasicsServer>();
        if (StringUtils.isNotBlank(platBasicsServer.getAppOrg())) {
            lqw.eq(PlatBasicsServer::getAppOrg, platBasicsServer.getAppOrg());
        }
        if (StringUtils.isNotBlank(platBasicsServer.getServerName())) {
            lqw.like(PlatBasicsServer::getServerName, platBasicsServer.getServerName());
        }
        return lqw;
    }

    /**
     * 分页查询服务信息列表
     *
     * @param platBasicsServer 服务信息
     * @return 服务信息集合
     */
    @Override
    public IPage<PlatBasicsServer> pagingPlatBasicsServerList(PlatBasicsServer platBasicsServer) {
        IPage<PlatBasicsServer> page = new Page<>(platBasicsServer.getPageNum(), platBasicsServer.getPageSize());
        LambdaQueryWrapper<PlatBasicsServer> lqw = getLambdaQueryWrapper(platBasicsServer);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatBasicsServer::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

    /**
     * 分页查询服务信息列表
     *
     * @param lqw
     * @return 服务信息集合
     */
    @Override
    public IPage<PlatBasicsServer> pagingPlatBasicsServerList(PageParamVo pageParamVo, LambdaQueryWrapper<PlatBasicsServer> lqw) {
        IPage<PlatBasicsServer> page = new Page<>(pageParamVo.getPageNum(), pageParamVo.getPageSize());
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatBasicsServer::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

    /**
     * 查询服务信息列表
     *
     * @param platBasicsServer 服务信息
     * @return 服务信息
     */
    @Override
    public List<PlatBasicsServer> selectPlatBasicsServerList(PlatBasicsServer platBasicsServer) {
        LambdaQueryWrapper<PlatBasicsServer> lqw = getLambdaQueryWrapper(platBasicsServer);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatBasicsServer::getCreateTime);
        return baseMapper.selectList(lqw);
    }

    /**
     * 查询服务信息列表
     *
     * @param platBasicsServer 服务信息
     * @return 服务信息
     */
    @Override
    public List<PlatBasicsServer> selectPlatBasicsServerList(PlatBasicsServer platBasicsServer, Long limitNum) {
        LambdaQueryWrapper<PlatBasicsServer> lqw = getLambdaQueryWrapper(platBasicsServer);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatBasicsServer::getCreateTime);
        lqw.last("limit " + limitNum);
        return baseMapper.selectList(lqw);
    }

    /**
     * 根据指定字段查询信息
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 服务信息
     */
    @Override
    public PlatBasicsServer getPlatBasicsServerByField(String field, String filedValue) {
        QueryWrapper<PlatBasicsServer> lqw = new QueryWrapper<PlatBasicsServer>();
        lqw.eq(field, filedValue);
        lqw.last("limit 1");
        return this.getOne(lqw, false);
    }

    /**
     * 根据指定字段查询信息列表
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 服务信息
     */
    @Override
    public List<PlatBasicsServer> selectPlatBasicsServerListByField(String field, String filedValue, Long limitNum) {
        QueryWrapper<PlatBasicsServer> lqw = new QueryWrapper<PlatBasicsServer>();
        lqw.eq(field, filedValue);
        lqw.last("limit " + limitNum);
        return this.list(lqw);
    }

    /**
     * 根据指定字段查询信息列表
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 服务信息
     */
    @Override
    public List<PlatBasicsServer> selectPlatBasicsServerListByField(String field, String filedValue) {
        QueryWrapper<PlatBasicsServer> lqw = new QueryWrapper<PlatBasicsServer>();
        if(!(Objects.equals(field,"app_org") && Objects.equals(filedValue,"0000"))){
            lqw.eq(field, filedValue);
        }else{
            lqw.groupBy("server_name");
            lqw.select("server_name,sum(server_num) as server_num");
        }
        lqw.orderByAsc("server_name");
        return this.list(lqw);
    }

    /**
     * 判断指定字段值是否存在
     *
     * @param field      字段名称
     * @param filedValue 字段值
     */
    @Override
    public void checkFieldExist(String field, String filedValue, String tips, Long id) {
        QueryWrapper<PlatBasicsServer> lqw = new QueryWrapper<PlatBasicsServer>();
        lqw.eq(field, filedValue);
        lqw.ne("id", id);
        if (this.getOne(lqw, false) != null) {
            throw new RuntimeException(tips + "已存在");
        }
    }

    /**
     *  真删接口
     */
    @Override
    public void reallyDeletePlatBasicsServer(LambdaQueryWrapper<PlatBasicsServer> lqw){
        baseMapper.reallyDeleteByWrapper(lqw);
    }

    /**
     * 获取服务器列表
     *
     * @param appOrg 字段名称
     * @return 服务信息
     */
    @Override
    public List<Map<String,Object>> selectServerListByAppOrg(String appOrg){
        List<PlatBasicsServer> list = this.selectPlatBasicsServerListByField("app_org", appOrg);
        if(list.isEmpty()){
            return new ArrayList<>();
        }
        return list.stream().map(item -> {
            Map<String,Object> map = new HashMap<>();
            map.put("serverName", item.getServerName());
            map.put("serverNum", item.getServerNum());
            return map;
        }).collect(Collectors.toList());
    }
}
