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.prepare.harmonic.mapper.mysql.line.RMpPartHarmonicDetailMMapper;
import com.njcn.prepare.harmonic.pojo.param.LineParam;
import com.njcn.prepare.harmonic.service.mysql.line.IRMpPartHarmonicDetailMService;
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.List;

/**
 * 类的介绍：
 *
 * @author xuyang
 * @version 1.0.0
 * @createTime 2023/4/23 16:15
 */
@Slf4j
@Service
public class RMpPartHarmonicDetailMServiceImpl extends MppServiceImpl<RMpPartHarmonicDetailMMapper, RMpPartHarmonicDetailM> implements IRMpPartHarmonicDetailMService {

    @Override
    @Transactional(rollbackFor = {Exception.class})
    @Async("asyncExecutor")
    public void insertHarmonicDetailMonthOne(LineParam lineParam) {
        log.info(LocalDateTime.now()+"===>监测点稳态指标超标明细月表1开始执行");
        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<RMpPartHarmonicDetailM> list = this.baseMapper.getHarmonicDetailMonthOne(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);
        }
    }
}
