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

import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.njcn.common.pojo.constant.PatternRegex;
import com.njcn.harmonic.pojo.po.RMpPartHarmonicDetailD;
import com.njcn.prepare.harmonic.mapper.mysql.line.RMpPartHarmonicDetailDMapper;
import com.njcn.prepare.harmonic.pojo.param.LineParam;
import com.njcn.prepare.harmonic.service.mysql.line.IRMpPartHarmonicDetailDService;
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 org.springframework.util.CollectionUtils;

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/23 15:08
 */
@Slf4j
@Service
public class RMpPartHarmonicDetailDServiceImpl extends MppServiceImpl<RMpPartHarmonicDetailDMapper, RMpPartHarmonicDetailD> implements IRMpPartHarmonicDetailDService {

    @Override
    @Transactional(rollbackFor = {Exception.class})
    @Async("asyncExecutor")
    public void insertHarmonicDetailDayOne(LineParam lineParam) {
        log.info(LocalDateTime.now()+"===>监测点稳态指标超标明细日表1开始执行");
        List<RMpPartHarmonicDetailD> result = new ArrayList<>();
        if (Objects.equals(lineParam.getType(),5)){
            System.out.println(lineParam.getDataDate());
            System.out.println(lineParam.getLineIds());
            result = getData(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(getData(item,lineParam.getLineIds()));
            }
        }
        this.saveOrUpdateBatchByMultiId(result,50);
    }

    public List<RMpPartHarmonicDetailD> getData(String time,List<String> lineList) {
        List<RMpPartHarmonicDetailD> list = this.baseMapper.getHarmonicDetailDayOne(time,lineList);
        if (!CollectionUtils.isEmpty(list)){
            list.forEach(item->{
                if (item.getIsFreq()>0 || item.getIsVDev()>0 || item.getIsV()>0 || item.getIsI()>0 || item.getIsUnbalance()>0 || item.getIsINeg()>0 || item.getIsFlicker()>0 || item.getIsInuharm()>0){
                    item.setIsHarmonic(1);
                } else {
                    item.setIsHarmonic(0);
                }
            });
        }
        return list;
    }
}
