package com.njcn.prepare.harmonic.service.mysql.Impl.line;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.njcn.harmonic.pojo.po.RMpSurplusHarmonicDetailM;
import com.njcn.prepare.harmonic.mapper.mysql.line.RMpSurplusHarmonicDetailMMapper;
import com.njcn.prepare.harmonic.pojo.bo.CalculatedParam;
import com.njcn.prepare.harmonic.service.mysql.line.IRMpSurplusHarmonicDetailMService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.ListUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 类的介绍：
 *
 * @author xuyang
 * @version 1.0.0
 * @createTime 2023/4/23 16:15
 */
@Slf4j
@Service
public class RMpSurplusHarmonicDetailMServiceImpl extends MppServiceImpl<RMpSurplusHarmonicDetailMMapper, RMpSurplusHarmonicDetailM> implements IRMpSurplusHarmonicDetailMService {

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void insertHarmonicDetailMonthTwo(CalculatedParam calculatedParam) {
        log.info(LocalDateTime.now()+"===>监测点稳态指标超标明细月表2开始执行");
        DateTime beginMonth = DateUtil.beginOfMonth(DateUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN));
        DateTime endMonth = DateUtil.endOfMonth(DateUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN));
        List<String> lineIds = calculatedParam.getIdList();
        List<List<String>> pendingIds = ListUtils.partition(lineIds, 100);
        for (List<String> pendingId : pendingIds) {
            List<RMpSurplusHarmonicDetailM> list = this.baseMapper.getHarmonicDetailMonthTwo(DateUtil.format(beginMonth, DatePattern.NORM_DATE_PATTERN),beginMonth,endMonth,pendingId);
            if (CollectionUtil.isNotEmpty(list)){
                this.saveOrUpdateBatchByMultiId(list);
            }
        }
    }
}
