package cn.cecep.talroad.data.analyse.task.factory.label.change;

import cn.cecep.talroad.common.core.utils.StringUtils;
import cn.cecep.talroad.data.analyse.task.entity.FactoryActivePowerQuantityVO;
import cn.cecep.talroad.domain.dto.FactoryLabelChangeDTO;
import cn.cecep.talroad.enums.LabelFactoryEnum;
import cn.cecep.talroad.mapper.MonElecOtherMapper;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author: Frank
 * @createTime: 2023年06月28日 16:37:44
 * @version:
 * @Description:
 */
@Service
@AllArgsConstructor
public class ActivePowerQuantityChangeService extends AbstractFactoryLabelChangeService {
    private static final Logger logger = LoggerFactory.getLogger(ActivePowerQuantityChangeService.class);

    private final MonElecOtherMapper monElecOtherMapper;

    @Override
    public boolean support(String enumId) {
        return LabelFactoryEnum.PF016.getEnumId().equalsIgnoreCase(enumId);
    }

    @Override
    public void execute(FactoryLabelChangeDTO factoryLabelChangeDTO) {
        List<FactoryActivePowerQuantityVO> result = selectFactoryActivePowerQuantity(factoryLabelChangeDTO);
        if(CollUtil.isNotEmpty(result)){
            segmentationAndUpdate(result);
            logger.info("企业用电量标签划分完毕 !");
        }
    }

    public List<FactoryActivePowerQuantityVO> selectFactoryActivePowerQuantity(FactoryLabelChangeDTO factoryLabelChangeDTO) {
        String startTime = factoryLabelChangeDTO.getStartTime();
        String endTime = factoryLabelChangeDTO.getEndTime();
        if(StringUtils.isEmpty(startTime)){
            startTime = LocalDate.now().withDayOfYear(1).toString();
        }
        if(StringUtils.isEmpty(endTime)){
            endTime = LocalDate.now().toString();
        }
        //查询出本年的企业用电量

        List<FactoryActivePowerQuantityVO> result = monElecOtherMapper.selectFactoryActivePowerQuantity(startTime, endTime);
        logger.info("查询出本年的企业用电量结果:{}", JSON.toJSONString(result));
        if (CollUtil.isEmpty(result)) Lists.newArrayList();
        return result;
    }

    public void segmentationAndUpdate(List<FactoryActivePowerQuantityVO> result) {
        final String highLevel ="1039" ,lowLevel ="1041" ,generalLevel ="1040";
        int n = result.size();
        int k = 5; // k = 5%
        int m = (int) Math.ceil(k * n / 100.0);
        // 按用电量排序
        result.sort(Comparator.comparing(FactoryActivePowerQuantityVO::getActivePowerQuantitySum).reversed());
        logger.info("本年的企业用电量按用电量排序后:{},size:{}", JSON.toJSONString(result),result.size());
        // 获取前 5% 的企业
        List<FactoryActivePowerQuantityVO> topResult = result.subList(0, m);
        logger.info("前 5% 的企业:{},size:{}", JSON.toJSONString(topResult),topResult.size());
        executeUpdate(topResult, highLevel);
        // 获取后 5% 的企业
        List<FactoryActivePowerQuantityVO> bottomResult = result.subList(n - m, n);
        logger.info("后 5% 的企业:{},size:{}", JSON.toJSONString(bottomResult),bottomResult.size());
        executeUpdate(bottomResult, lowLevel);
        // 获取中间段企业
        List<FactoryActivePowerQuantityVO> middleResult = result.subList(m, n - m);
        logger.info("获取中间段企业:{},size:{}", JSON.toJSONString(middleResult),middleResult.size());
        executeUpdate(middleResult, generalLevel);

    }

    public void executeUpdate(List<FactoryActivePowerQuantityVO> result, String argumentsCode) {
        if (CollUtil.isNotEmpty(result)) {
            insertOrUpdateFactoryLabel(result.stream().map(FactoryActivePowerQuantityVO::getFactoryId).filter(Objects::nonNull).collect(Collectors.toSet()), argumentsCode, LabelFactoryEnum.PF016.getEnumId());
        }
    }

}
