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

import cn.hutool.core.collection.CollectionUtil;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.njcn.harmonic.pojo.po.RMpPartHarmonicDetailD;
import com.njcn.prepare.harmonic.mapper.mysql.line.RMpPartHarmonicDetailDMapper;
import com.njcn.prepare.harmonic.pojo.bo.CalculatedParam;
import com.njcn.prepare.harmonic.service.mysql.line.IRMpPartHarmonicDetailDService;
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 org.springframework.util.CollectionUtils;

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

/**
 * 类的介绍：
 * r_mp_part_harmonic_detail_d算法
 * @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})
    public void insertHarmonicDetailDayOne(CalculatedParam calculatedParam) {
        log.info(LocalDateTime.now()+"===>监测点稳态指标超标明细日表1开始执行");
        List<String> lineIds = calculatedParam.getIdList();
        //以尺寸100分片,查询数据
        List<List<String>> pendingIds =  ListUtils.partition(lineIds,5);
        for (List<String> pendingId : pendingIds) {
            List<RMpPartHarmonicDetailD> list = getData(calculatedParam.getDataDate(),pendingId);
            if (CollectionUtil.isNotEmpty(list)){
                this.saveOrUpdateBatchByMultiId(list);
            }
        }
    }

    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;
    }
    /***********************************************新算法结束********************************************************************/

}
