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


import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.njcn.harmonic.pojo.po.RMpSurplusHarmonicDetailD;
import com.njcn.harmonic.pojo.po.RMpVThd;

import com.njcn.prepare.harmonic.mapper.mysql.line.RMpVThdMapper;
import com.njcn.prepare.harmonic.pojo.param.LineParam;
import com.njcn.prepare.harmonic.service.mysql.line.IRMpVThdService;
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.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 类的介绍：
 *
 * @author xuyang
 * @version 1.0.0
 * @createTime 2023/4/24 17:58
 */
@Service
@Slf4j
public class RMpVThdServiceImpl extends MppServiceImpl<RMpVThdMapper, RMpVThd> implements IRMpVThdService {

    @Override
    @Transactional(rollbackFor = {Exception.class})
    @Async("asyncExecutor")
    public void insertHarmonicVThdDay(LineParam lineParam) {
        log.info(LocalDateTime.now()+"===>监测点谐波畸变率开始执行");
        List<RMpVThd> result = new ArrayList<>();
        if (Objects.equals(lineParam.getType(),5)){
            result = this.baseMapper.getVThdData(lineParam.getDataDate(),lineParam.getLineIds());
        } else if (Objects.equals(lineParam.getType(),3)){
            int year = Integer.parseInt(lineParam.getDataDate().split("-")[0]);
            int month = Integer.parseInt(lineParam.getDataDate().split("-")[1]);
            List<String> dayList = PublicUtil.getDayByMonth(year,month);
            for (String item : dayList) {
                result.addAll(this.baseMapper.getVThdData(item,lineParam.getLineIds()));
            }
        }
        this.saveOrUpdateBatchByMultiId(result,50);
    }
}
