package com.njcn.device.pq.service.impl;

import cn.hutool.core.bean.BeanUtil;
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.common.DataStateEnum;
import com.njcn.device.line.mapper.LineDetailMapper;
import com.njcn.device.line.mapper.LineMapper;
import com.njcn.device.pq.enums.LineBaseEnum;
import com.njcn.device.pq.pojo.dto.SubstationDTO;
import com.njcn.device.pq.pojo.po.Line;
import com.njcn.device.pq.pojo.po.LineDetail;
import com.njcn.device.pq.pojo.po.Substation;
import com.njcn.device.pq.pojo.po.Voltage;
import com.njcn.device.pq.pojo.vo.LedgerSelectVO;
import com.njcn.device.pq.service.ISubstationService;
import com.njcn.device.substation.mapper.SubstationMapper;
import com.njcn.device.subvoltage.mapper.VoltageMapper;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.enums.DicDataTypeEnum;
import com.njcn.system.pojo.po.DictData;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author hongawen
 * @version 1.0.0
 * @date 2022年10月13日 20:11
 */
@Service
@RequiredArgsConstructor
public class SubstationServiceImpl extends ServiceImpl<SubstationMapper, Substation> implements ISubstationService {

    private final LineMapper lineMapper;

    private final LineDetailMapper lineDetailMapper;

    private final VoltageMapper voltageMapper;

    private final SubstationMapper substationMapper;

    private final DicDataFeignClient dicDataFeignClient;

    @Override
    public List<SubstationDTO> getSubstationById(List<String> subId) {
        return this.baseMapper.getSubstationById(subId);
    }

    @Override
    public List<LedgerSelectVO> getSubstationSelect() {
        List<LedgerSelectVO> result = new ArrayList<>();
        LambdaQueryWrapper<Line> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Line::getLevel, LineBaseEnum.SUB_LEVEL.getCode())
                .eq(Line::getState, DataStateEnum.ENABLE.getCode()).orderByAsc(Line::getCreateTime);
        List<Line> stationList = lineMapper.selectList(lambdaQueryWrapper);
        List<String> ids = stationList.stream().map(Line::getId).collect(Collectors.toList());
        List<Substation> substationList = substationMapper.selectBatchIds(ids);
        Map<String,Substation> stationMap = substationList.stream().collect(Collectors.toMap(Substation::getId,Function.identity()));

        List<DictData> dictDataList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.DEV_VOLTAGE_STAND.getCode()).getData();
        Map<String,DictData> dictDataMap = dictDataList.stream().collect(Collectors.toMap(DictData::getId,Function.identity()));
        stationList.forEach(item->{
            if(stationMap.containsKey(item.getId())){
                LedgerSelectVO ledgerSelectVO = new LedgerSelectVO();
                BeanUtil.copyProperties(item,ledgerSelectVO);
                ledgerSelectVO.setVoltageLevel(stationMap.get(item.getId()).getScale());
                ledgerSelectVO.setVoltageName(dictDataMap.get(ledgerSelectVO.getVoltageLevel()).getName());
                result.add(ledgerSelectVO);
            }
        });
        return result;
    }

    @Override
    public List<LedgerSelectVO> getBusBarSelect(String stationId) {
        List<LedgerSelectVO> result = new ArrayList<>();
        LambdaQueryWrapper<Line> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Line::getPids,stationId).eq(Line::getLevel,LineBaseEnum.SUB_V_LEVEL.getCode());
        List<Line> busBarList = lineMapper.selectList(lambdaQueryWrapper);
        List<String> ids = busBarList.stream().map(Line::getId).collect(Collectors.toList());
        List<Voltage> voltageList = voltageMapper.selectBatchIds(ids);
        Map<String,Voltage> voltageMap = voltageList.stream().collect(Collectors.toMap(Voltage::getId,Function.identity()));

        List<DictData> dictDataList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.DEV_VOLTAGE_STAND.getCode()).getData();
        Map<String,DictData> dictDataMap = dictDataList.stream().collect(Collectors.toMap(DictData::getId,Function.identity()));
        busBarList.forEach(item->{
            LedgerSelectVO ledgerSelectVO = new LedgerSelectVO();
            BeanUtil.copyProperties(item,ledgerSelectVO);
            ledgerSelectVO.setVoltageLevel(voltageMap.get(item.getId()).getScale());
            ledgerSelectVO.setVoltageName(dictDataMap.get(ledgerSelectVO.getVoltageLevel()).getName());
            result.add(ledgerSelectVO);
        });
        return result;
    }

    @Override
    public List<Line> getMonitorSelect(String stationId) {
        LambdaQueryWrapper<Line> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Line::getPids,stationId).eq(Line::getLevel,LineBaseEnum.LINE_LEVEL.getCode());
        List<Line> lineList = lineMapper.selectList(lambdaQueryWrapper);
        return lineList;
    }

    @Override
    public List<LineDetail> getPowerByStationId(String stationId, Integer powerFlag) {
        List<LineDetail> lineDetailList = new ArrayList<>();
        LambdaQueryWrapper<Line> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(Line::getId,Line::getName).like(Line::getPids,stationId).eq(Line::getLevel,LineBaseEnum.LINE_LEVEL.getCode()).eq(Line::getState,DataStateEnum.ENABLE.getCode());
        List<Line> lineList = lineMapper.selectList(lambdaQueryWrapper);
        if(CollUtil.isNotEmpty(lineList)){
            List<String> ids = lineList.stream().map(Line::getId).collect(Collectors.toList());
            LambdaQueryWrapper<LineDetail> detailQuery = new LambdaQueryWrapper<>();
            if(Objects.nonNull(powerFlag)){
                detailQuery.eq(LineDetail::getPowerFlag,powerFlag);
            }
            detailQuery.in(LineDetail::getId,ids);
            lineDetailList = lineDetailMapper.selectList(detailQuery);
            if(CollUtil.isNotEmpty(lineDetailList)){
               Map<String,Line> lineMap = lineList.stream().collect(Collectors.toMap(Line::getId, Function.identity()));
                lineDetailList.forEach(item->item.setMonitorName(lineMap.getOrDefault(item.getId(),new Line()).getName()));
            }
        }
        return lineDetailList;
    }
}
