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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.device.pms.mapper.majornetwork.ROperatingIndexMMapper;
import com.njcn.device.pms.pojo.param.ROperatingIndexParam;
import com.njcn.device.pms.pojo.po.ROperatingIndexM;
import com.njcn.device.pms.pojo.vo.ROperatingIndexCommonVO;
import com.njcn.device.pms.service.majornetwork.ROperatingIndexMService;
import com.njcn.user.api.DeptFeignClient;
import com.njcn.user.pojo.dto.DeptDTO;
import com.njcn.web.utils.WebUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author jianghf
* @description 针对表【r_operating_index_m(主网运行指标统计-月表,主网监测指标数据质量统计-月表 )】的数据库操作Service实现
* @createDate 2022-11-09 19:56:01
*/
@Service
@RequiredArgsConstructor
public class ROperatingIndexMServiceImpl extends ServiceImpl<ROperatingIndexMMapper, ROperatingIndexM>
    implements ROperatingIndexMService{

    private final DeptFeignClient deptFeignClient;

    @Override
    public List<ROperatingIndexCommonVO> getOperatingList(ROperatingIndexParam rOperatingIndexParam) {
        //提取参数
        String id = rOperatingIndexParam.getId(); //单位id
        String startTime = rOperatingIndexParam.getStartTime(); //开始时间
        String endTime = rOperatingIndexParam.getEndTime(); //结束时间

        //获取所有子部门信息
//        List<DeptDTO> deptDTOList = deptFeignClient.getDeptDescendantIndexes(id, WebUtil.filterDeptType()).getData();
        /*获取下一级子部门信息*/
        List<DeptDTO> deptDTOList = deptFeignClient.getDepSonDetailByDeptId (id).getData ( );
        if (CollUtil.isEmpty(deptDTOList)) {
            throw new BusinessException(CommonResponseEnum.NO_DATA, "部门不存在");
        }
        //单位code集合
        List<String> orgCodeList = 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));

        //组装查询条件
        LambdaQueryWrapper<ROperatingIndexM> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(ROperatingIndexM::getOrgNo, orgCodeList)
                .ge(StringUtils.isNotBlank(startTime), ROperatingIndexM::getDataDate, startTime)
                .le(StringUtils.isNotBlank(endTime), ROperatingIndexM::getDataDate, endTime);
        //查询数据
        List<ROperatingIndexM> list = this.list(lambdaQueryWrapper);

        //封装返回数据
        DecimalFormat df = new DecimalFormat("###.00");
        List<ROperatingIndexCommonVO> resultList = list.stream().map(item -> {
            ROperatingIndexCommonVO rOperatingIndexCommonVO = new ROperatingIndexCommonVO();
            BeanUtils.copyProperties(item, rOperatingIndexCommonVO);

            rOperatingIndexCommonVO.setOrgName(deptDTOMap.get(item.getOrgNo()).getName()); //单位名称
            rOperatingIndexCommonVO.setSort(deptDTOMap.get(item.getOrgNo()).getSort());
            //在线监测率
            Integer measurementRunPoints = item.getMeasurementRunPoints(); //监测点数
            Integer transitMeasurementPoints = item.getTransitMeasurementPoints(); //在线监测点数
            if(transitMeasurementPoints!=null&&transitMeasurementPoints!=0){
                Double transitMeasurementRate = Double.parseDouble(df.format(transitMeasurementPoints / (measurementRunPoints * 1.0) * 100));
                rOperatingIndexCommonVO.setTransitMeasurementRate(transitMeasurementRate); //在线监测率
            }else{
                rOperatingIndexCommonVO.setTransitMeasurementRate(0.0);
            }
            return rOperatingIndexCommonVO;
        }).sorted(Comparator.comparing(ROperatingIndexCommonVO::getSort)).collect(Collectors.toList());
        return resultList;
    }
}




