package com.njcn.prepare.harmonic.service.mysql.upload.impl;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;

import com.njcn.prepare.harmonic.mapper.mysql.upload.PmsRunStatisticDMapper;
import com.njcn.prepare.harmonic.mapper.mysql.upload.PmsRunStatisticMMapper;
import com.njcn.harmonic.pojo.po.upload.PmsRunStatisticD;
import com.njcn.harmonic.pojo.po.upload.PmsRunStatisticM;
import com.njcn.prepare.harmonic.service.mysql.upload.IPmsRunStatisticMService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cdf
 * @since 2024-04-17
 */
@Service
@RequiredArgsConstructor
public class PmsRunStatisticMServiceImpl extends MppServiceImpl<PmsRunStatisticMMapper, PmsRunStatisticM> implements IPmsRunStatisticMService {

    private final PmsRunStatisticDMapper pmsRunStatisticDMapper;


    @Override
    public void statisticLedgerRunMonth(String date) {
        List<PmsRunStatisticM> poList = new ArrayList<>();
        DateTime begin = DateUtil.beginOfMonth(DateUtil.parse(date));
        DateTime end = DateUtil.parse(date);

        ZoneId zoneId = ZoneId.systemDefault();
        LocalDate localDate = begin.toInstant().atZone(zoneId).toLocalDate();

        LambdaQueryWrapper<PmsRunStatisticD> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.between(PmsRunStatisticD::getStatisticDate,begin,end);
        List<PmsRunStatisticD> pmsRunStatisticDList = pmsRunStatisticDMapper.selectList(lambdaQueryWrapper);
        Map<String,List<PmsRunStatisticD>> map = pmsRunStatisticDList.stream().collect(Collectors.groupingBy(PmsRunStatisticD::getDeptId));

        map.forEach((key,val)->{
            PmsRunStatisticM pmsRunStatisticM = new PmsRunStatisticM();
            pmsRunStatisticM.setDeptId(key);
            pmsRunStatisticM.setStatisticDate(localDate);

            Set<String> pointSet = new HashSet<>();
            Set<String> pointOnlineSet = new HashSet<>();

            Set<String> busSet = new HashSet<>();
            Set<String> busMidSet = new HashSet<>();
            Set<String> busRunSet = new HashSet<>();
            Set<String> stationSet = new HashSet<>();

            Set<String> devRunSet = new HashSet<>();

            Set<String> typicalSet = new HashSet<>();
            val.forEach(item->{
                if(StrUtil.isNotBlank(item.getRunMonitorIds())){
                    pointSet.addAll(Arrays.asList(item.getRunMonitorIds().split(StrUtil.COMMA)));
                }
                if(StrUtil.isNotBlank(item.getOnlineMonitorIds())){
                    pointOnlineSet.addAll(Arrays.asList(item.getOnlineMonitorIds().split(StrUtil.COMMA)));
                }
                if(StrUtil.isNotBlank(item.getOnlineBusIds())){
                    busSet.addAll(Arrays.asList(item.getOnlineBusIds().split(StrUtil.COMMA)));
                }

                if(StrUtil.isNotBlank(item.getOnlineBusMidIds())){
                    busMidSet.addAll(Arrays.asList(item.getOnlineBusMidIds().split(StrUtil.COMMA)));
                }

                if(StrUtil.isNotBlank(item.getOnlineStationIds())){
                    stationSet.addAll(Arrays.asList(item.getOnlineStationIds().split(StrUtil.COMMA)));
                }

                if(StrUtil.isNotBlank(item.getTypicalOnlineIds())){
                    typicalSet.addAll(Arrays.asList(item.getTypicalOnlineIds().split(StrUtil.COMMA)));
                }

                if(StrUtil.isNotBlank(item.getRunBusIds())){
                    busRunSet.addAll(Arrays.asList(item.getRunBusIds().split(StrUtil.COMMA)));
                }

                if(StrUtil.isNotBlank(item.getRunDevIds())){
                    devRunSet.addAll(Arrays.asList(item.getRunDevIds().split(StrUtil.COMMA)));
                }
            });
            pmsRunStatisticM.setRunMonitorIds(String.join(StrUtil.COMMA,pointSet));
            pmsRunStatisticM.setOnlineMonitorIds(String.join(StrUtil.COMMA,pointOnlineSet));
            pmsRunStatisticM.setOnlineBusIds(String.join(StrUtil.COMMA,busSet));
            pmsRunStatisticM.setOnlineBusMidIds(String.join(StrUtil.COMMA,busMidSet));
            pmsRunStatisticM.setOnlineStationIds(String.join(StrUtil.COMMA,stationSet));
            pmsRunStatisticM.setRunDevIds(String.join(StrUtil.COMMA,devRunSet));
            pmsRunStatisticM.setTypicalOnlineIds(String.join(StrUtil.COMMA,typicalSet));

            //月在运母线，电站数量取日的最大值
            int temStation = val.stream().map(PmsRunStatisticD::getRunStationNum).filter(Objects::nonNull).max(Integer::compareTo).orElse(0);
            pmsRunStatisticM.setRunBusNum(busRunSet.size());
            pmsRunStatisticM.setRunBusIds(String.join(StrUtil.COMMA,busRunSet));
            pmsRunStatisticM.setRunStationNum(temStation);

            poList.add(pmsRunStatisticM);

        });
        this.saveOrUpdateBatchByMultiId(poList);
    }

    @Override
    public List<PmsRunStatisticM> selectListByDate(String startTime, String endTime, String dataDate) {
        return this.list(new LambdaQueryWrapper<PmsRunStatisticM>()
                .ge(StrUtil.isNotBlank(startTime), PmsRunStatisticM::getStatisticDate, DateUtil.beginOfDay(DateUtil.parse(startTime)))
                .le(StrUtil.isNotBlank(endTime), PmsRunStatisticM::getStatisticDate, DateUtil.endOfDay(DateUtil.parse(endTime))));
    }


}
