package com.njcn.device.pms.service.majornetwork.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.device.pms.enums.PmsDeviceResponseEnum;
import com.njcn.device.pms.mapper.majornetwork.*;
import com.njcn.device.pms.pojo.param.TaiZhangParam;
import com.njcn.device.pms.pojo.po.*;
import com.njcn.device.pms.pojo.vo.PmsPowerTreeMonitorVO;
import com.njcn.device.pms.service.majornetwork.IMonitorService;
import com.njcn.device.pms.service.majornetwork.ITerminalEliminateDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.enums.DicDataEnum;
import com.njcn.system.enums.DicDataTypeEnum;
import com.njcn.system.enums.SystemResponseEnum;
import com.njcn.system.pojo.constant.DicDataConstant;
import com.njcn.system.pojo.po.DictData;
import com.njcn.user.api.DeptFeignClient;
import com.njcn.user.pojo.dto.DeptDTO;
import com.njcn.web.factory.PageFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hongawen
 * @since 2022-10-14
 */
@Service
@RequiredArgsConstructor
public class TerminalEliminateDataServiceImpl extends ServiceImpl<TerminalEliminateDataMapper, TerminalEliminateData> implements ITerminalEliminateDataService {

    private final MonitorMapper monitorMapper;

    private final DicDataFeignClient dicDataFeignClient;

    private final DeptFeignClient deptFeignClient;

    private final DistributionMonitorMapper distributionMonitorMapper;

    private final PowerDistributionareaMapper powerDistributionareaMapper;

    private final PowerClientMapper powerClientMapper;

    private final PowerGenerationUserMapper powerGenerationUserMapper;

    @Override
    public Page<Monitor> getOneMonitorByOrgId(TaiZhangParam taiZhangParam) {
        Page<Monitor> monitorPage = new Page<>();
        List<String> deptIds = deptFeignClient.getDepSonSelfCodetByDeptId(taiZhangParam.getOrgId()).getData();
        if(taiZhangParam.getDataType() == 0){
            //主网
            LambdaQueryWrapper<Monitor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.in(Monitor::getOrgId,deptIds)
                    .like(StrUtil.isNotBlank(taiZhangParam.getSearchValue()),Monitor::getName,taiZhangParam.getSearchValue())
                    .eq(StrUtil.isNotBlank(taiZhangParam.getMonitorState()),Monitor::getMonitorState,taiZhangParam.getMonitorState())
            .eq(Monitor::getStatus, DataStateEnum.ENABLE.getCode()).orderByDesc(Monitor::getCreateTime);
            monitorPage = monitorMapper.selectPage(new Page<>(PageFactory.getPageNum(taiZhangParam),PageFactory.getPageSize(taiZhangParam)),lambdaQueryWrapper);
        }else {
            //配网
                DictData one = dicDataFeignClient.getDicDataByCode(DicDataEnum.ONE_LINE.getCode()).getData();
                taiZhangParam.setLineType(one.getId());
                monitorPage = distributionMonitorMapper.getMonitorByOrg(new Page<>(PageFactory.getPageNum(taiZhangParam),PageFactory.getPageSize(taiZhangParam)),taiZhangParam,deptIds);
        }
        return monitorPage;
    }

    @Override
    public Page<PowerDistributionarea> getTwoMonitorByOrgId(TaiZhangParam taiZhangParam) {
        Page<PowerDistributionarea> monitorPage = new Page<>();
        List<String> deptIds = deptFeignClient.getDepSonSelfCodetByDeptId(taiZhangParam.getOrgId()).getData();
        if(taiZhangParam.getDataType() == 0){

        }else {
                DictData two = dicDataFeignClient.getDicDataByCode(DicDataEnum.TWO_LINE.getCode()).getData();
                taiZhangParam.setLineType(two.getId());
                monitorPage = distributionMonitorMapper.getPowerDisAreaMonitorByOrg(new Page<>(PageFactory.getPageNum(taiZhangParam),PageFactory.getPageSize(taiZhangParam)),taiZhangParam,deptIds);
        }
        return monitorPage;
    }

    @Override
    public Page<PmsPowerTreeMonitorVO> getThreeMonitorByOrgId(TaiZhangParam taiZhangParam) {
        Page<PmsPowerTreeMonitorVO> monitorPage = new Page<>();
        List<String> deptIds = deptFeignClient.getDepSonSelfCodetByDeptId(taiZhangParam.getOrgId()).getData();
        if(taiZhangParam.getDataType() == 0){

        }else {
            if(Objects.isNull(taiZhangParam.getTreeType())){
                throw new BusinessException(PmsDeviceResponseEnum.POWER_CLIENT_DIS_EMPTY);
            }
            //配网
                DictData three = dicDataFeignClient.getDicDataByCode(DicDataEnum.THREE_LINE.getCode()).getData();
                taiZhangParam.setLineType(three.getId());
                if(taiZhangParam.getTreeType().equals(0)){
                    //用电
                    monitorPage = distributionMonitorMapper.getThreeMonitorByOrgIdYong(new Page<>(PageFactory.getPageNum(taiZhangParam),PageFactory.getPageSize(taiZhangParam)),taiZhangParam,deptIds);
                }else {
                    //
                    monitorPage = distributionMonitorMapper.getThreeMonitorByOrgIdFa(new Page<>(PageFactory.getPageNum(taiZhangParam),PageFactory.getPageSize(taiZhangParam)),taiZhangParam,deptIds);
                }
        }
        return monitorPage;
    }
}
