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.PmsRunStatisticYMapper;
import com.njcn.harmonic.pojo.po.upload.PmsRunStatisticD;
import com.njcn.harmonic.pojo.po.upload.PmsRunStatisticY;
import com.njcn.prepare.harmonic.service.mysql.upload.IPmsRunStatisticYService;
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 PmsRunStatisticYServiceImpl extends MppServiceImpl<PmsRunStatisticYMapper, PmsRunStatisticY> implements IPmsRunStatisticYService {

    private final PmsRunStatisticDMapper pmsRunStatisticDMapper;





    @Override
    public void statisticLedgerRunYear(String date) {
        List<PmsRunStatisticY> poList = new ArrayList<>();
        DateTime begin = DateUtil.beginOfYear(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)->{
            PmsRunStatisticY pmsRunStatisticY = new PmsRunStatisticY();
            pmsRunStatisticY.setDeptId(key);
            pmsRunStatisticY.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)));
                }
            });
            pmsRunStatisticY.setRunMonitorIds(String.join(StrUtil.COMMA,pointSet));
            pmsRunStatisticY.setOnlineMonitorIds(String.join(StrUtil.COMMA,pointOnlineSet));
            pmsRunStatisticY.setOnlineBusIds(String.join(StrUtil.COMMA,busSet));
            pmsRunStatisticY.setOnlineBusMidIds(String.join(StrUtil.COMMA,busMidSet));
            pmsRunStatisticY.setOnlineStationIds(String.join(StrUtil.COMMA,stationSet));
            pmsRunStatisticY.setRunDevIds(String.join(StrUtil.COMMA,devRunSet));

            pmsRunStatisticY.setTypicalOnlineIds(String.join(StrUtil.COMMA,typicalSet));

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

            pmsRunStatisticY.setRunStationNum(temStation);
            poList.add(pmsRunStatisticY);
        });


        this.saveOrUpdateBatchByMultiId(poList);


    }

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

}
