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


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.njcn.device.biz.pojo.dto.DeptGetChildrenDTO;
import com.njcn.harmonic.pojo.po.dim.RDimMonitorRateD;
import com.njcn.harmonic.pojo.po.dim.RDimMonitorRateM;
import com.njcn.harmonic.pojo.po.dim.RDimMonitorRateY;
import com.njcn.prepare.harmonic.mapper.mysql.dim.RDimMonitorRateYMapper;
import com.njcn.prepare.harmonic.pojo.bo.CalculatedParam;
import com.njcn.prepare.harmonic.service.mysql.dim.IRDimMonitorRateDService;
import com.njcn.prepare.harmonic.service.mysql.dim.IRDimMonitorRateMService;
import com.njcn.prepare.harmonic.service.mysql.dim.IRDimMonitorRateYService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cdf
 * @since 2023-11-23
 */
@Service
@RequiredArgsConstructor
public class RDimMonitorRateYServiceImpl extends MppServiceImpl<RDimMonitorRateYMapper, RDimMonitorRateY> implements IRDimMonitorRateYService {

    private final IRDimMonitorRateMService irDimMonitorRateMService;

    @Override
    public void monitorRateYear(CalculatedParam<DeptGetChildrenDTO> calculatedParam) {
        List<RDimMonitorRateY> poList = new ArrayList<>();
        DateTime start = DateUtil.beginOfYear(DateUtil.parse(calculatedParam.getDataDate(),DatePattern.NORM_DATE_FORMAT));

        QueryWrapper<RDimMonitorRateM> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().between(RDimMonitorRateM::getStatisDate,start,DateUtil.endOfYear(DateUtil.parse(calculatedParam.getDataDate(),DatePattern.NORM_DATE_FORMAT)));
        queryWrapper.select("Org_Id as orgId,avg(Bus_Monitor_Rate) as busMonitorRate,avg(Station_Monitor_Rate) as stationMonitorRate").groupBy("Org_Id");
        List<RDimMonitorRateM> rDimMonitorRateMDList = irDimMonitorRateMService.list(queryWrapper);

        if(CollUtil.isNotEmpty(rDimMonitorRateMDList)){
            for(RDimMonitorRateM rDimMonitorRateM : rDimMonitorRateMDList){
                RDimMonitorRateY rDimMonitorRateY = new RDimMonitorRateY();
                rDimMonitorRateY.setBusMonitorRate(rDimMonitorRateM.getBusMonitorRate());
                rDimMonitorRateY.setStationMonitorRate(rDimMonitorRateM.getStationMonitorRate());
                rDimMonitorRateY.setOrgId(rDimMonitorRateM.getOrgId());
                rDimMonitorRateY.setStatisDate(LocalDate.parse(DateUtil.format(start,DatePattern.NORM_DATE_PATTERN)));
                poList.add(rDimMonitorRateY);
            }
            this.saveOrUpdateBatchByMultiId(poList,100);
        }
    }
}
