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

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.harmonic.mapper.specialanalysis.RStatOrgPvPermeabilityMMapper;
import com.njcn.harmonic.pojo.param.StreamReverseParam;
import com.njcn.harmonic.pojo.vo.PermeabilityVO;
import com.njcn.harmonic.service.specialanalysis.PermeabilityService;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.enums.DicDataTypeEnum;
import com.njcn.system.pojo.po.DictData;
import com.njcn.user.api.DeptFeignClient;
import com.njcn.user.pojo.dto.DeptDTO;
import com.njcn.user.pojo.po.Dept;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * @Title PermeabilityServiceImpl
 * @Package com.njcn.harmonic.service.specialanalysis.impl
 * @Author jianghaifei
 * @Date 2022-11-28 11:29
 * @Version V1.0
 */
@Service
@RequiredArgsConstructor
public class PermeabilityServiceImpl implements PermeabilityService {

    private final RStatOrgPvPermeabilityMMapper rStatOrgPvPermeabilityMMapper;

    private final DeptFeignClient deptFeignClient;

    private final DicDataFeignClient dicDataFeignClient;
    /***
     * 渗透率分析
     * @author jianghaifei
     * @date 2022-11-28 16:22
     * @param streamReverseParam
     * @return com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.njcn.harmonic.pojo.vo.PermeabilityVO>
     */
    @Override
    public Page<PermeabilityVO> getPermeabilityList(StreamReverseParam streamReverseParam) {
        //提取参数
        String id = streamReverseParam.getId(); //单位
        String startTime = streamReverseParam.getStartTime(); //开始时间
        String endTime = streamReverseParam.getEndTime(); //结束时间
        Integer pageNum = streamReverseParam.getPageNum() != null && streamReverseParam.getPageNum() != 0 ? streamReverseParam.getPageNum() : 1; //页码
        Integer pageSize = streamReverseParam.getPageSize() != null && streamReverseParam.getPageSize() != 0 ? streamReverseParam.getPageSize() : 10; //页面尺寸

        //获取部门信息
//        List<DeptDTO> data = deptFeignClient.getDeptDescendantIndexes(id, WebUtil.filterDeptType()).getData();
        List<DeptDTO> data = deptFeignClient.getDepSonDetailByDeptId(id).getData(); //获取当前部门和子部门信息
        //剔除本部
        Map<String, DeptDTO> deptMap = data.stream().collect(Collectors.toMap(DeptDTO::getCode, dept -> dept));
        List<String> deptCodes = data.stream().map(DeptDTO::getCode).collect(Collectors.toList());
        //获取各渗透率字典
        List<DictData> rateList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.PERMEABILITY_TYPE.getCode()).getData();
        //将渗透率字典转换为查询条件
        Map<String, Object> condMap = rateList.stream().collect(Collectors.toMap(DictData::getCode, DictData::getId));
        condMap.put("orgIdList", deptCodes);
        condMap.put("startTime", startTime);
        condMap.put("endTime", endTime);
        Page<PermeabilityVO> permeabilityList = rStatOrgPvPermeabilityMMapper.getPermeabilityList(new Page<>(pageNum, pageSize), condMap);
        List<PermeabilityVO> records = permeabilityList.getRecords();
        records.forEach(item -> {
            item.setOrgName(deptMap.get(item.getOrgId()).getName());
        });
        return permeabilityList;
    }
}
