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

import cn.hutool.core.collection.CollUtil;
import com.njcn.common.pojo.constant.BizParamConstant;
import com.njcn.common.pojo.dto.SimpleDTO;
import com.njcn.common.pojo.param.StatisticsBizBaseParam;
import com.njcn.device.pms.mapper.majornetwork.RStatAccountCheckDataMapper;
import com.njcn.device.pms.mapper.majornetwork.RStatTargetCheckDataMapper;
import com.njcn.device.pms.pojo.dto.PmsGeneralDeviceDTO;
import com.njcn.device.pms.pojo.param.PmsDeviceInfoParam;
import com.njcn.device.pms.pojo.vo.RStatAccountCheckDataVO;
import com.njcn.device.pms.pojo.vo.RStatTargetCheckDataVO;
import com.njcn.device.pms.service.majornetwork.DataQualityCheckService;
import com.njcn.device.pms.service.majornetwork.IPmsGeneralDeviceService;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.enums.DicDataEnum;
import com.njcn.user.api.DeptFeignClient;
import com.njcn.user.pojo.dto.DeptDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 主网数据质量-数据质量核查
 *
 * @author yzh
 * @date 2022/11/2
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class DataQualityCheckServiceImpl implements DataQualityCheckService {

    private final IPmsGeneralDeviceService iPmsGeneralDeviceService;

    private final RStatAccountCheckDataMapper rStatAccountCheckDataMapper;

    private final RStatTargetCheckDataMapper rStatTargetCheckDataMapper;

    private final DicDataFeignClient dicDataFeignClient;
    private final DeptFeignClient deptFeignClient;

    /**
     * 获取主网数据质量-台帐类数据质量核查
     *
     * @param param 条件参数
     * @return java.util.List<com.njcn.event.pojo.vo.RStatAccountCheckDataVO>
     * @author yzh
     * @date 2022/11/2
     */
    @Override
    public List<RStatAccountCheckDataVO> getQualityCheckOfAccountData(StatisticsBizBaseParam param) {
        // 获取当前用户的部门的子部门信息
//        List<PmsGeneralDeviceDTO> data = getPmsGeneralDeviceDTOList(param);
//        if (CollUtil.isEmpty(data)) {
//            return Collections.emptyList();
//        }
//        List<String> deptIdList = data.stream().map(PmsGeneralDeviceDTO::getIndex).collect(Collectors.toList());
        /*获取下级子部门信息*/
        List<DeptDTO> deptDTOList = deptFeignClient.getDepSonDetailByDeptId (param.getId ()).getData ( );
        if (CollUtil.isEmpty(deptDTOList)) {
            return Collections.emptyList();
        }
        List<String> deptIdList = deptDTOList.stream().map(DeptDTO::getCode).collect(Collectors.toList());
        Map<String, DeptDTO> deptDTOMap = deptDTOList.stream().collect(Collectors.toMap(DeptDTO::getCode, deptDTO -> deptDTO));

        // 创建集合封装查询数据
        List<RStatAccountCheckDataVO> result = new ArrayList<>();
        // 获取主网字典id
        String mainnetPoint = dicDataFeignClient.getDicDataByCode(DicDataEnum.MAINNET_POINT.getCode()).getData().getId();
        // 获取主网数据质量-台帐类数据质量核查
        switch (param.getType().toString()) {
            // 年
            case BizParamConstant.STAT_BIZ_YEAR:
                result = rStatAccountCheckDataMapper.getRStatAccountCheckDataYearInfo(param, deptIdList, mainnetPoint);
                break;
            // 季
            case BizParamConstant.STAT_BIZ_QUARTER:
                result = rStatAccountCheckDataMapper.getRStatAccountCheckDataSeasonInfo(param, deptIdList, mainnetPoint);
                break;
            // 月
            case BizParamConstant.STAT_BIZ_MONTH:
                result = rStatAccountCheckDataMapper.getRStatAccountCheckDataMonthInfo(param, deptIdList, mainnetPoint);
                break;
            default:
                break;
        }
        if (CollUtil.isEmpty(result)) {
            return Collections.emptyList();
        }
        //将单位信息转为map集合 key: 单位id  value: 单位实体
//        Map<String, PmsGeneralDeviceDTO> dataMap = getDataMap(data);
        return result.stream().peek(vo -> {
            vo.setOrgName(deptDTOMap.get(vo.getOrgNo()).getName());
        }).collect(Collectors.toList());
    }

    /**
     * 获取主网数据质量-监测指标数据质量核查
     *
     * @param param 条件参数
     * @return java.util.List<com.njcn.event.pojo.vo.RStatTargetCheckDataVO>
     * @author yzh
     * @date 2022/11/2
     */
    @Override
    public List<RStatTargetCheckDataVO> getMonitoringIndexDataQualityVerification(StatisticsBizBaseParam param) {
        // 获取当前用户的部门的子部门信息
//        List<PmsGeneralDeviceDTO> data = getPmsGeneralDeviceDTOList(param);
//        if (CollUtil.isEmpty(data)) {
//            return Collections.emptyList();
//        }
//        List<String> deptIdList = data.stream().map(PmsGeneralDeviceDTO::getIndex).collect(Collectors.toList());
        List<DeptDTO> deptDTOList = deptFeignClient.getDepSonDetailByDeptId (param.getId ()).getData ( );
        if (CollUtil.isEmpty(deptDTOList)) {
            return Collections.emptyList();
        }
        List<String> deptIdList = deptDTOList.stream().map(DeptDTO::getCode).collect(Collectors.toList());
        Map<String, DeptDTO> deptDTOMap = deptDTOList.stream().collect(Collectors.toMap(DeptDTO::getCode, deptDTO -> deptDTO));


        // 获取主网字典id
        String mainnetPoint = dicDataFeignClient.getDicDataByCode(DicDataEnum.MAINNET_POINT.getCode()).getData().getId();
        // 创建集合封装数据
        List<RStatTargetCheckDataVO> result = new ArrayList<>();
        // 获取主网数据质量-台帐类数据质量核查
        switch (param.getType().toString()) {
            // 年
            case BizParamConstant.STAT_BIZ_YEAR:
                result = rStatTargetCheckDataMapper.getRStatTargetCheckDataYearInfo(param, deptIdList, mainnetPoint);
                break;
            // 季
            case BizParamConstant.STAT_BIZ_QUARTER:
                result = rStatTargetCheckDataMapper.getRStatTargetCheckDataSeasonInfo(param, deptIdList, mainnetPoint);
                break;
            // 月
            case BizParamConstant.STAT_BIZ_MONTH:
                result = rStatTargetCheckDataMapper.getRStatTargetCheckDataMonthInfo(param, deptIdList, mainnetPoint);
                break;
            default:
                break;
        }
        if (CollUtil.isEmpty(result)) {
            return Collections.emptyList();
        }
        //将单位信息转为map集合 key: 单位id  value: 单位实体
//        Map<String, PmsGeneralDeviceDTO> dataMap = getDataMap(data);
        return result.stream().peek(vo -> {
            vo.setOrgName(deptDTOMap.get(vo.getOrgNo()).getName());
        }).collect(Collectors.toList());
    }

    /**
     * 将单位信息转为map集合 key: 单位id  value: 单位实体
     */
    private Map<String, PmsGeneralDeviceDTO> getDataMap(List<PmsGeneralDeviceDTO> data) {
        return data
                .stream()
                .collect(Collectors.toMap(PmsGeneralDeviceDTO::getIndex, PmsGeneralDeviceDTO -> PmsGeneralDeviceDTO));
    }

    /**
     * 获取当前用户的部门的子部门信息
     *
     * @param param 条件参数
     * @return java.util.List<com.njcn.device.pms.pojo.dto.PmsGeneralDeviceDTO>
     * @author yzh
     * @date 2022/11/2
     */
    private List<PmsGeneralDeviceDTO> getPmsGeneralDeviceDTOList(StatisticsBizBaseParam param) {
        // 获取当前用户的部门的子部门信息
        PmsDeviceInfoParam pmsDeviceInfoParam = new PmsDeviceInfoParam();
        pmsDeviceInfoParam.setDeptIndex(param.getId());
        pmsDeviceInfoParam.setStatisticalType(new SimpleDTO());
        return iPmsGeneralDeviceService.getPmsDeviceInfoWithInOrg(pmsDeviceInfoParam);
    }
}
