package com.njcn.device.pms.service.distribution.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.distribution.PwDataQualityCheckMapper;
import com.njcn.device.pms.mapper.distribution.PwRStatTargetCheckDataMapper;
import com.njcn.device.pms.pojo.dto.PmsGeneralDeviceDTO;
import com.njcn.device.pms.pojo.dto.PmsTractionStationDTO;
import com.njcn.device.pms.pojo.param.PmsDeviceInfoParam;
import com.njcn.device.pms.pojo.vo.PwRStatAccountCheckDataVO;
import com.njcn.device.pms.pojo.vo.RStatTargetCheckDataVO;
import com.njcn.device.pms.service.distribution.PwDataQualityCheckService;
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/8
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class PwDataQualityCheckServiceImpl implements PwDataQualityCheckService {

    private final IPmsGeneralDeviceService iPmsGeneralDeviceService;

    private final DicDataFeignClient dicDataFeignClient;

    private final DeptFeignClient deptFeignClient;

    private final PwDataQualityCheckMapper pwDataQualityCheckMapper;

    private final PwRStatTargetCheckDataMapper pwRStatTargetCheckDataMapper;

    /**
     * 获取配网数据质量-台帐类数据质量核查
     *
     * @param param 条件参数
     * @return java.util.List<com.njcn.event.pojo.vo.RStatAccountCheckDataVO>
     * @author yzh
     * @date 2022/11/8
     */
    @Override
    public List<PwRStatAccountCheckDataVO> getPwQualityCheckOfAccountData(StatisticsBizBaseParam param) {
        // 获取单位的子单位id
//        List<PmsGeneralDeviceDTO> data = getDeptIds(param);
//        if (CollUtil.isEmpty(data)) {
//            return Collections.emptyList();
//        }
//        // 取出单位id
//        List<String> deptIds = 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> deptIds = deptDTOList.stream().map(DeptDTO::getCode).collect(Collectors.toList());
        Map<String, DeptDTO> deptDTOMap = deptDTOList.stream().collect(Collectors.toMap(DeptDTO::getCode, deptDTO -> deptDTO));

        // 获取配网字典信息
        String distributionPoint = dicDataFeignClient.getDicDataByCode(DicDataEnum.DISTRIBUTION_POINT.getCode()).getData().getId();
        // 创建集合封装返回数据
        List<PwRStatAccountCheckDataVO> result = new ArrayList<>();
        // 获取配网数据质量-台帐类数据质量核查
        switch (param.getType().toString()) {
            // 年
            case BizParamConstant.STAT_BIZ_YEAR:
                result = pwDataQualityCheckMapper.getYearInfo(deptIds, param, distributionPoint);
                break;
            // 季
            case BizParamConstant.STAT_BIZ_QUARTER:
                result = pwDataQualityCheckMapper.getSeasonInfo(deptIds, param, distributionPoint);
                break;
            // 月
            case BizParamConstant.STAT_BIZ_MONTH:
                result = pwDataQualityCheckMapper.getMonthInfo(deptIds, param, distributionPoint);
                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/8
     */
    @Override
    public List<RStatTargetCheckDataVO> getPwMonitoringIndexDataQualityVerification(StatisticsBizBaseParam param) {
        // 获取单位的子单位id
//        List<PmsGeneralDeviceDTO> data = getDeptIds(param);
//        if (CollUtil.isEmpty(data)) {
//            return Collections.emptyList();
//        }
//        // 取出单位id
//        List<String> deptIds = 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> deptIds = deptDTOList.stream().map(DeptDTO::getCode).collect(Collectors.toList());
        Map<String, DeptDTO> deptDTOMap = deptDTOList.stream().collect(Collectors.toMap(DeptDTO::getCode, deptDTO -> deptDTO));

        // 获取配网字典信息
        String distributionPoint = dicDataFeignClient.getDicDataByCode(DicDataEnum.DISTRIBUTION_POINT.getCode()).getData().getId();
        // 创建集合封装返回数据
        List<RStatTargetCheckDataVO> result = new ArrayList<>();
        // 获取配网数据质量-监测指标数据质量核查
        switch (param.getType().toString()) {
            // 年
            case BizParamConstant.STAT_BIZ_YEAR:
                result = pwRStatTargetCheckDataMapper.getYearInfo(deptIds, param, distributionPoint);
                break;
            // 季
            case BizParamConstant.STAT_BIZ_QUARTER:
                result = pwRStatTargetCheckDataMapper.getSeasonInfo(deptIds, param, distributionPoint);
                break;
            // 月
            case BizParamConstant.STAT_BIZ_MONTH:
                result = pwRStatTargetCheckDataMapper.getMonthInfo(deptIds, param, distributionPoint);
                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));
    }

    /**
     * 获取单位的子单位id
     */
    private List<PmsGeneralDeviceDTO> getDeptIds(StatisticsBizBaseParam param) {
        PmsDeviceInfoParam pmsDeviceInfoParam = new PmsDeviceInfoParam();
        pmsDeviceInfoParam.setDeptIndex(param.getId());
        pmsDeviceInfoParam.setStatisticalType(new SimpleDTO());
        return iPmsGeneralDeviceService.getPmsDeviceInfoWithInOrg(pmsDeviceInfoParam);
    }
}
