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.ROperatingIndexMapper;
import com.njcn.device.pms.mapper.majornetwork.RQualityParameterMapper;
import com.njcn.device.pms.pojo.dto.PmsGeneralDeviceDTO;
import com.njcn.device.pms.pojo.dto.PmsMonitorInfoDTO;
import com.njcn.device.pms.pojo.param.PmsDeviceInfoParam;
import com.njcn.device.pms.pojo.po.RQualityParameterPO;
import com.njcn.device.pms.pojo.vo.ROperatingIndexVO;
import com.njcn.device.pms.pojo.vo.RQualityParameterPollVO;
import com.njcn.device.pms.pojo.vo.RQualityParameterVO;
import com.njcn.device.pms.service.majornetwork.DataQualityStatService;
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.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
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/1
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class DataQualityStatServiceImpl implements DataQualityStatService {

    private final IPmsGeneralDeviceService iPmsGeneralDeviceService;

    private final DicDataFeignClient dicDataFeignClient;

    private final RQualityParameterMapper rQualityParameterMapper;

    private final ROperatingIndexMapper rOperatingIndexMapper;

    private final DeptFeignClient deptFeignClient;

    private final DecimalFormat df = new DecimalFormat("###.00");

    /**
     * 获取主网数据质量-台账数据质量统计
     *
     * @param param 条件参数
     * @return java.util.List<RQualityParameterVO>
     * @author yzh
     * @date 2022/11/1
     */
    @Override
    public List<RQualityParameterVO> getLedgerDataQualityStat(StatisticsBizBaseParam param) {
        // 获取当前用户的部门的子部门信息

        /*获取下级子部门信息*/
        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集合 key: 单位code  value: 单位实体
        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<RQualityParameterPO> pos = rQualityParameterMapper.getLedgerDataQualityStat(
                param,
                deptIdList,
                mainnetPoint
        );
        if (CollUtil.isEmpty(pos)) {
            return Collections.emptyList();
        }
//        //将单位信息转为map集合 key: 单位id  value: 单位实体
//        Map<String, PmsGeneralDeviceDTO> dataMap = getDataMap(data);
        // 属性赋值
        return pos.stream().map(po -> {
            RQualityParameterVO vo = new RQualityParameterVO();
            BeanUtils.copyProperties(po, vo);
            vo.setOrgName(deptDTOMap.get(vo.getOrgNo()).getName());
            return vo;
        }).collect(Collectors.toList());
    }

    /**
     * 获取主网数据质量-监测指标数据质量统计
     *
     * @param param 条件参数
     * @return java.util.List<com.njcn.event.pojo.vo.ROperatingIndexVO>
     * @author yzh
     * @date 2022/11/1
     */
    @Override
    public List<ROperatingIndexVO> getMonitoringIndexDataQualityStat(StatisticsBizBaseParam param) {

        /*获取下级子部门信息*/
        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));

        //将单位信息转为map集合 key: 单位id  value: 单位实体
//        Map<String, PmsGeneralDeviceDTO> dataMap = getDataMap(data);
        //将单位信息转为map集合 key: 单位code  value: 单位实体

        // 创建集合封装返回数据
        List<ROperatingIndexVO> result = new ArrayList<>();
        // 获取主网数据质量-监测指标数据质量统计
        switch (param.getType().toString()) {
            // 年
            case BizParamConstant.STAT_BIZ_YEAR:
                result = rOperatingIndexMapper.getYearInfo(param, deptIdList);
                break;
            // 季
            case BizParamConstant.STAT_BIZ_QUARTER:
                result = rOperatingIndexMapper.getSeasonInfo(param, deptIdList);
                break;
            // 月
            case BizParamConstant.STAT_BIZ_MONTH:
                result = rOperatingIndexMapper.getMonthInfo(param, deptIdList);
                break;
            default:
                break;
        }


        // 处理数据
        // 计算有效接入率【有效接入监测点数量/在运监测点数】
        return result.stream().peek(vo -> {
            vo.setOrgName(deptDTOMap.get(vo.getOrgNo()).getName());
            if(vo.getMeasurementCount()!=0&&vo.getMeasurementCount()!=null){
                vo.setEffectiveAccessRate(
                        Double.parseDouble(
                                df.format((vo.getEffectiveAccessMeasurementCount() * 1.0)
                                        / (vo.getMeasurementCount() * 1.0))
                        ) * 100);
            }else{
                vo.setEffectiveAccessRate(0.0);
            }

        }).collect(Collectors.toList());
    }

    /**
     * 获取主网数据质量-监测指标数据质量问题汇总统计
     *
     * @param param 条件参数
     * @return java.util.List<?>
     * @author yzh
     * @date 2022/11/1
     */
    @Override
    public List<RQualityParameterPollVO> getSummaryStatOfMonitoringIndexDataQualityProblems(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<RQualityParameterPO> pos = rQualityParameterMapper.getLedgerDataQualityStat(param, deptIdList, mainnetPoint);
//        // 将单位信息转为map集合 key: 单位id  value: 单位实体
//        Map<String, PmsGeneralDeviceDTO> dataMap = getDataMap(data);
        // 属性赋值
        return pos.stream().map(po -> {
            RQualityParameterPollVO vo = new RQualityParameterPollVO();
            BeanUtils.copyProperties(po, vo);
            vo.setOrgId(po.getOrgNo());
            vo.setOrgName(deptDTOMap.get(vo.getOrgId()).getName());
            if(po.getEffectiveAccessMeasurementCount()!=0&&po.getEffectiveAccessMeasurementCount()!=null){
                vo.setProblemProportion(
                        Double.parseDouble(
                                df.format((po.getProblem() * 1.0)
                                        / (po.getEffectiveAccessMeasurementCount() * 1.0))
                        ) * 100);
            }else{
                vo.setProblemProportion(0.0);
            }

            if (po.getProblemYearOnYear() == null) {
                vo.setProblemYearOnYear("3.14159");
            } else {
                vo.setProblemYearOnYear(po.getProblemYearOnYear());
            }
            return vo;
        }).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);
    }
}
