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

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.event.pojo.po.RMpEventDetailD;
import com.njcn.event.pojo.po.RmpEventDetailPO;
import com.njcn.prepare.harmonic.mapper.mysql.line.RMpEventDetailMapper;
import com.njcn.prepare.harmonic.pojo.bo.CalculatedParam;
import com.njcn.prepare.harmonic.service.mysql.line.RMpEventDetailDService;
import com.njcn.prepare.harmonic.service.mysql.line.RMpEventDetailService;
import com.njcn.redis.utils.RedisUtil;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.enums.DicDataEnum;
import com.njcn.system.pojo.po.DictData;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.ListUtils;
import org.springframework.stereotype.Service;

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

/**
 * Description:
 * Date: 2023/4/23  13:50【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
public class RMpEventDetailServiceImpl extends ServiceImpl<RMpEventDetailMapper, RmpEventDetailPO> implements RMpEventDetailService {

    private final RMpEventDetailDService rMpEventDetailDService;

    private final DicDataFeignClient dicDataFeignClient;

    private final RedisUtil redisUtil;


    /***
     * 监测点暂态指标明细--日表算法
     */
    @Override
    public void handleDay(CalculatedParam calculatedParam) {
        List<String> lineIds = calculatedParam.getIdList();
        //以尺寸1000分片
        List<List<String>> pendingIds =  ListUtils.partition(lineIds,1000);
        LocalDateTime beginDay = LocalDateTimeUtil.beginOfDay(LocalDateTimeUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN));
        LocalDateTime endDay = LocalDateTimeUtil.endOfDay(LocalDateTimeUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN));
        for (List<String> pendingId : pendingIds) {
            //查询
            List<RMpEventDetailD> rMpEventDetailDList = this.baseMapper.handleDay(
                    pendingId
                    ,LocalDateTimeUtil.format(beginDay,DatePattern.NORM_DATE_PATTERN)
                    ,beginDay
                    ,endDay
                    ,getEventTypeId(DicDataEnum.VOLTAGE_DIP.getCode())
                    ,getEventTypeId(DicDataEnum.VOLTAGE_RISE.getCode())
                    ,getEventTypeId(DicDataEnum.SHORT_INTERRUPTIONS.getCode()));
            //入库
            rMpEventDetailDService.saveOrUpdateBatchByMultiId(rMpEventDetailDList);
        }
    }



    public String getEventTypeId(String code) {
        String eventType = redisUtil.getStringByKey(code);
        eventType = Optional.ofNullable(eventType).orElseGet(() -> {
            DictData data = dicDataFeignClient.getDicDataByCode(code).getData();
            redisUtil.saveByKey(data.getCode(), data.getId());
            return data.getId();
        });

        return eventType;

    }

}
