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

import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.njcn.harmonic.pojo.po.RMpPartHarmonicDetailM;
import com.njcn.harmonic.pojo.po.RMpSurplusHarmonicDetailD;
import com.njcn.harmonic.pojo.po.RMpSurplusHarmonicDetailM;
import com.njcn.prepare.harmonic.mapper.mysql.line.RMpSurplusHarmonicDetailMMapper;
import com.njcn.prepare.harmonic.pojo.param.LineParam;
import com.njcn.prepare.harmonic.service.mysql.line.IRMpSurplusHarmonicDetailMService;
import com.njcn.prepare.harmonic.utils.PublicUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 类的介绍：
 *
 * @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})
    @Async("asyncExecutor")
    public void insertHarmonicDetailMonthTwo(LineParam lineParam) {
        log.info(LocalDateTime.now()+"===>监测点稳态指标超标明细月表2开始执行");
        int year = Integer.parseInt(lineParam.getDataDate().split("-")[0]);
        int month = Integer.parseInt(lineParam.getDataDate().split("-")[1]);
        String startTime = PublicUtil.getFisrtDayOfMonth(year,month);
        String endTime = PublicUtil.getLastDayOfMonth(year,month);
        List<RMpSurplusHarmonicDetailM> list = this.baseMapper.getHarmonicDetailMonthTwo(startTime,endTime,lineParam.getLineIds());
        if (!CollectionUtils.isEmpty(list)){
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate date = LocalDate.parse(startTime, df);
            list.forEach(item->{
                item.setDataDate(date);
            });
            this.saveOrUpdateBatchByMultiId(list,50);
        }
    }
}
