package com.njcn.harmonic.service.majornetwork.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.njcn.common.pojo.param.StatisticsBizBaseParam;
import com.njcn.harmonic.pojo.param.RStatOrgParam;
import com.njcn.harmonic.pojo.po.RStatOrgM;
import com.njcn.harmonic.pojo.po.RStatOrgQ;
import com.njcn.harmonic.mapper.majornetwork.RStatOrgQMapper;
import com.njcn.harmonic.pojo.po.RStatOrgY;
import com.njcn.harmonic.pojo.vo.RStatOrgVO;
import com.njcn.harmonic.service.majornetwork.RStatOrgQService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.harmonic.utils.DeptUtil;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.enums.DicDataEnum;
import com.njcn.system.pojo.po.DictData;
import com.njcn.user.api.DeptFeignClient;
import com.njcn.user.pojo.dto.DeptDTO;
import com.njcn.web.utils.WebUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 区域分类统计季表 服务实现类
 * </p>
 *
 * @author rui.wu
 * @since 2022-10-17
 */
@Service
@RequiredArgsConstructor
public class RStatOrgQServiceImpl extends ServiceImpl<RStatOrgQMapper, RStatOrgQ> implements RStatOrgQService {

    private final DeptFeignClient deptFeignClient;
    private final DicDataFeignClient dicDataFeignClient;
    private final RStatOrgQMapper rStatOrgQMapper;

    @Override
    public List<RStatOrgVO> getRStatHarmonicQAll(RStatOrgParam.Info param) {
        //获取部门信息
        List<DeptDTO> deptList = deptFeignClient.getDeptDescendantIndexes(param.getId(), WebUtil.filterDeptType()).getData();
        List<DeptDTO> deptDTOList = DeptUtil.getDeptSubsetVOList(deptList, param.getId());
        List<String> deptIds = deptDTOList.stream().map(DeptDTO::getCode).collect(Collectors.toList());

        Map<String, DeptDTO> deptMap = deptDTOList.stream().collect(Collectors.toMap(DeptDTO::getCode, Function.identity()));

        //获取主网id信息
        DictData mainnetData = dicDataFeignClient.getDicDataByCode(DicDataEnum.MAINNET_POINT.getCode()).getData();

        //数据库查询
        List<RStatOrgQ> list = rStatOrgQMapper.selectList(new LambdaQueryWrapper<RStatOrgQ>()
                .eq(RStatOrgQ::getDataType, mainnetData.getId())
                .in(RStatOrgQ::getOrgNo, deptIds)
                .in(CollUtil.isNotEmpty(param.getMeasurementTypeClass()), RStatOrgQ::getMeasurementTypeClass, param.getMeasurementTypeClass())
                .ge(StrUtil.isNotBlank(param.getStartTime()), RStatOrgQ::getDataDate, param.getStartTime())
                .le(StrUtil.isNotBlank(param.getEndTime()), RStatOrgQ::getDataDate, param.getEndTime()));
        Map<String, List<RStatOrgQ>> collect = list.stream().collect(Collectors.groupingBy(RStatOrgQ::getOrgNo));

        List<RStatOrgVO> newList = new ArrayList<>();
        collect.forEach((key, value) -> {
            if (deptMap.containsKey(key)) {
                for (RStatOrgQ rStatOrgQ : value) {
                    DeptDTO deptDTO = deptMap.get(key);
                    RStatOrgVO rStatOrgVO = new RStatOrgVO();
                    rStatOrgVO.setOrgName(deptDTO.getName());
                    rStatOrgVO.setAverageOverDay(rStatOrgQ.getAverageOverDay());
                    rStatOrgVO.setMeasurementTypeClass(rStatOrgQ.getMeasurementTypeClass());
                    rStatOrgVO.setEffectiveMeasurementAverage(rStatOrgQ.getEffectiveMeasurementAverage());
                    rStatOrgVO.setEffectiveMeasurementAccrued(rStatOrgQ.getEffectiveMeasurementAccrued());
                    rStatOrgVO.setOverLimitMeasurementAverage(rStatOrgQ.getHarmonicMeasurementAverage());
                    rStatOrgVO.setOverLimitMeasurementAccrued(rStatOrgQ.getHarmonicMeasurementAccrued());
                    DecimalFormat df = new DecimalFormat("###.00");
                    if(rStatOrgQ.getHarmonicMeasurementAverage()!=null&&rStatOrgQ.getEffectiveMeasurementAverage()!=null&&
                            rStatOrgQ.getHarmonicMeasurementAverage()!=0&&rStatOrgQ.getEffectiveMeasurementAverage()!=0){
                        Float harmonicMeasurementRatioAverage = Float.parseFloat(
                                df.format(rStatOrgQ.getHarmonicMeasurementAverage()*100.0 / rStatOrgQ.getEffectiveMeasurementAverage() ));
                        rStatOrgVO.setHarmonicMeasurementRatioAverage(harmonicMeasurementRatioAverage);
                    }else{
                        rStatOrgVO.setHarmonicMeasurementRatioAverage(0.0f);
                    }
                    if(rStatOrgQ.getHarmonicMeasurementAccrued()!=null&&rStatOrgQ.getEffectiveMeasurementAccrued()!=null&&
                            rStatOrgQ.getHarmonicMeasurementAccrued()!=0&&rStatOrgQ.getEffectiveMeasurementAccrued()!=0){
                        Float harmonicMeasurementRatioAccrued = Float.parseFloat(
                                df.format(rStatOrgQ.getHarmonicMeasurementAccrued()*100.0 / rStatOrgQ.getEffectiveMeasurementAccrued()));
                        rStatOrgVO.setHarmonicMeasurementRatioAccrued(harmonicMeasurementRatioAccrued);
                    }else{
                        rStatOrgVO.setHarmonicMeasurementRatioAccrued(0.0f);
                    }


                    newList.add(rStatOrgVO);
                }
            }
        });
        return newList;
    }
}
