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.RStatAreaAlarmCountMMapper;
import com.njcn.device.pms.pojo.param.RStatAreaAlarmCountMParam;
import com.njcn.device.pms.pojo.po.RStatAreaAlarmCountM;
import com.njcn.device.pms.pojo.vo.RStatAreaAlarmCountMVO;
import com.njcn.device.pms.service.majornetwork.RStatAreaAlarmCountMService;
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 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.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author jianghf
* @description 针对表【r_stat_area_alarm_count_m】的数据库操作Service实现
* @createDate 2022-10-10 14:36:46
*/
@Service
@RequiredArgsConstructor
public class RStatAreaAlarmCountMServiceImpl extends ServiceImpl<RStatAreaAlarmCountMMapper, RStatAreaAlarmCountM>
implements RStatAreaAlarmCountMService{

    private final DeptFeignClient deptFeignClient;

    private final DicDataFeignClient dicDataFeignClient;

    /***
     * 根据条件查询所有区域告警统计(月)(主网)
     * @author jianghaifei
     * @date 2022-10-10 15:20
     * @param rStatAreaAlarmCountMParam
     * @return java.util.List<com.njcn.device.pms.pojo.vo.RStatAreaAlarmCountMVO>
     */
    @Override
    public List<RStatAreaAlarmCountMVO> getAllRStatAreaAlarmCountMList(RStatAreaAlarmCountMParam rStatAreaAlarmCountMParam) {
        //获取查询条件
        String id = rStatAreaAlarmCountMParam.getId(); //单位id
        if (StringUtils.isBlank(id)) {
            throw new BusinessException(CommonResponseEnum.NO_DATA, "单位id不可为空");
        }
        String startTime = rStatAreaAlarmCountMParam.getStartTime(); //开始时间 yyyy-MM-dd
        String endTime = rStatAreaAlarmCountMParam.getEndTime(); //截止时间 yyyy-MM-dd
        //获取所有子部门信息
//        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, "部门不存在");
        }
        //单位id集合
        List<String> orgCodeList = deptDTOList.stream().map(DeptDTO::getCode).collect(Collectors.toList());
        //将单位信息转为map集合 key: 单位id  value: 单位实体
        Map<String, DeptDTO> deptDTOMap = deptDTOList.stream().collect(Collectors.toMap(DeptDTO::getCode, deptDTO -> deptDTO));
        //获取主网id
        String dataType = dicDataFeignClient.getDicDataByCode(DicDataEnum.MAINNET_POINT.getCode()).getData().getId();

        LambdaQueryWrapper<RStatAreaAlarmCountM> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //条件组装: where org_no in (orgNoList) and data_date >= startTime and data_date <= endTime
        lambdaQueryWrapper.in(CollUtil.isNotEmpty(orgCodeList), RStatAreaAlarmCountM::getOrgNo, orgCodeList)
                .ge(StringUtils.isNotBlank(startTime), RStatAreaAlarmCountM::getDataDate, startTime)
                .le(StringUtils.isNotBlank(endTime), RStatAreaAlarmCountM::getDataDate, endTime)
                .eq(RStatAreaAlarmCountM::getDataType, dataType);

        //查询区域告警统计(月)集合
        List<RStatAreaAlarmCountM> records = this.list(lambdaQueryWrapper);

        List<RStatAreaAlarmCountMVO> resultList;
        if (CollUtil.isNotEmpty(records)) {
            //填充返回数据
            resultList = records.stream().map(item -> {
                RStatAreaAlarmCountMVO rStatAreaAlarmCountMVO = new RStatAreaAlarmCountMVO();
                BeanUtils.copyProperties(item, rStatAreaAlarmCountMVO);
                //设置单位名称
                rStatAreaAlarmCountMVO.setOrgName(deptDTOMap.get(rStatAreaAlarmCountMVO.getOrgNo()).getName());

                return rStatAreaAlarmCountMVO;
            }).collect(Collectors.toList());
        } else {
            resultList = new ArrayList<>();
        }
        return resultList;
    }
}
