package com.njcn.energy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.energy.mapper.EleEpdMapper;
import com.njcn.energy.mapper.EleMdMapper;
import com.njcn.energy.mapper.EleStatisticalSetMapper;
import com.njcn.energy.pojo.enums.EleStatisticalSetEnum;
import com.njcn.energy.pojo.param.EleParam;
import com.njcn.energy.pojo.po.EleEpdPqd;
import com.njcn.energy.pojo.po.EleMd;
import com.njcn.energy.pojo.po.EleStatisticalSet;
import com.njcn.energy.pojo.vo.EleMdVO;
import com.njcn.energy.pojo.vo.EleStatisticalTreeVO;
import com.njcn.energy.pojo.vo.EleStatisticalVO;
import com.njcn.energy.service.EleStatisticalSetService;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.enums.DicDataTypeEnum;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * 用能统计类型配置
 *
 * @author denghuajun
 * @version 1.0.0
 * @date 2022年04月14日 15:16
 */
@Service
@AllArgsConstructor
public class EleStatisticalSetServiceImpl extends ServiceImpl<EleStatisticalSetMapper, EleStatisticalSet> implements EleStatisticalSetService {

    private final EleMdMapper eleMdMapper;

    private final EleEpdMapper eleEpdMapper;

    private final EleStatisticalSetMapper eleStatisticalSetMapper;

    private final DicDataFeignClient dicDataFeignClient;

    @Override
    public List<EleStatisticalVO> getEleStatisticalDetail() {
        return this.baseMapper.getEleStatisticalDetail(DicDataTypeEnum.ELE_STATISTICAL_TYPE.getName());
    }

    @Override
    public EleStatisticalTreeVO getEleStatisticalTree(EleStatisticalVO eleStatisticalVO) {
        EleStatisticalTreeVO eleStatisticalTreeVO = new EleStatisticalTreeVO();
        //根据id去查询相对应的类型
        if (EleStatisticalSetEnum.LOAD_STATISTICS.getName().equals(eleStatisticalVO.getStatisticalName())) {
            //查询已绑指标
            List<EleMd> eleMdList = eleMdMapper.getEleMdList(dicDataFeignClient.getDicDataByName(eleStatisticalVO.getStatisticalName()).getData().getId());
            List<EleMdVO> eleMdVOList = new ArrayList<>();
            List<EleMdVO> eleMdVONoList = new ArrayList<>();
            for (EleMd eleMd : eleMdList) {
                EleMdVO eleMdVO = new EleMdVO();
                eleMdVO.setId(eleMd.getId());
                eleMdVO.setName(("T".equals(eleMd.getPhase())) || "M".equals(eleMd.getPhase()) ? eleMd.getAnotherName() : (eleMd.getPhase() + "相" + eleMd.getAnotherName()));
                eleMdVOList.add(eleMdVO);
            }
            //查询未绑指标
            List<EleMd> eleMdNoList = eleMdMapper.getEleMdNoList(dicDataFeignClient.getDicDataByName(eleStatisticalVO.getStatisticalName()).getData().getId());
            for (EleMd eleMd : eleMdNoList) {
                EleMdVO eleMdVO = new EleMdVO();
                eleMdVO.setId(eleMd.getId());
                eleMdVO.setName(("T".equals(eleMd.getPhase())) || "M".equals(eleMd.getPhase()) ? eleMd.getAnotherName() : (eleMd.getPhase() + "相" + eleMd.getAnotherName()));
                eleMdVONoList.add(eleMdVO);
            }
            eleStatisticalTreeVO.setBinds(eleMdVOList);
            eleStatisticalTreeVO.setNoBinds(eleMdVONoList);
        } else {
            //查询已绑指标
            List<EleEpdPqd> eleEpdList = eleEpdMapper.getEleEpdPqdList(dicDataFeignClient.getDicDataByName(eleStatisticalVO.getStatisticalName()).getData().getId());
            List<EleMdVO> eleEpdVOList = new ArrayList<>();
            List<EleMdVO> eleEpdVONoList = new ArrayList<>();
            for (EleEpdPqd eleEpdPqd : eleEpdList) {
                EleMdVO eleMdVO = new EleMdVO();
                eleMdVO.setId(eleEpdPqd.getId());
                eleMdVO.setName(("T".equals(eleEpdPqd.getPhase())) || "M".equals(eleEpdPqd.getPhase()) ? eleEpdPqd.getAnotherName() : (eleEpdPqd.getPhase() + "相" + eleEpdPqd.getAnotherName()));
                eleEpdVOList.add(eleMdVO);
            }
            //查询未绑指标
            List<EleEpdPqd> eleEpdNoList = eleEpdMapper.getEleEpdPqdNoList(dicDataFeignClient.getDicDataByName(eleStatisticalVO.getStatisticalName()).getData().getId());
            for (EleEpdPqd eleEpdPqd : eleEpdNoList) {
                EleMdVO eleMdVO = new EleMdVO();
                eleMdVO.setId(eleEpdPqd.getId());
                eleMdVO.setName(("T".equals(eleEpdPqd.getPhase())) || "M".equals(eleEpdPqd.getPhase()) ? eleEpdPqd.getAnotherName() : (eleEpdPqd.getPhase() + "相" + eleEpdPqd.getAnotherName()));
                eleEpdVONoList.add(eleMdVO);
            }
            eleStatisticalTreeVO.setBinds(eleEpdVOList);
            eleStatisticalTreeVO.setNoBinds(eleEpdVONoList);
        }
        return eleStatisticalTreeVO;
    }

    @Override
    public Boolean add(EleParam eleParam) {
        QueryWrapper<EleStatisticalSet> eleMdQueryWrapper = new QueryWrapper<>();
        eleMdQueryWrapper.eq("ele_statistical_set.id", eleParam.getId());
        this.eleStatisticalSetMapper.delete(eleMdQueryWrapper);
        for (String index : eleParam.getIds()) {
            EleStatisticalSet eleStatisticalSet = new EleStatisticalSet();
            eleStatisticalSet.setId(eleParam.getId());
            eleStatisticalSet.setConId(index);
            this.eleStatisticalSetMapper.insert(eleStatisticalSet);
        }
        return true;
    }

}
