package cn.cecep.talroad.service.impl.analysis;

import cn.cecep.talroad.common.core.utils.StringUtils;
import cn.cecep.talroad.domain.analysis.SRaRealTimeAnalysisCoal;
import cn.cecep.talroad.domain.analysis.SRaRealTimeAnalysisKwh;
import cn.cecep.talroad.mapper.analysis.SRaRealTimeAnalysisKwhMapper;
import cn.cecep.talroad.service.analysis.ISRaRealTimeAnalysisKwhService;
import cn.cecep.talroad.vo.query.analysis.SRaRealTimeAnalysisKwhQuery;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 企业用电水平分析Service业务层处理
 *
 * @author szhpt
 * @date 2023-03-24*/
@Service
public class SRaRealTimeAnalysisKwhServiceImpl extends ServiceImpl<SRaRealTimeAnalysisKwhMapper, SRaRealTimeAnalysisKwh> implements ISRaRealTimeAnalysisKwhService
{
    @Autowired
    private SRaRealTimeAnalysisKwhMapper sRaRealTimeAnalysisKwhMapper;

    /**
     * 查询企业用电水平分析列表
     *
     * @param sRaRealTimeAnalysisKwh 企业用电水平分析
     * @return 企业用电水平分析
     */
    @Override
    public List<SRaRealTimeAnalysisKwh> selectSRaRealTimeAnalysisKwhList(SRaRealTimeAnalysisKwhQuery sRaRealTimeAnalysisKwh)
    {  BigDecimal start=null;
        BigDecimal end=null;
        if(StringUtils.isNotEmpty(sRaRealTimeAnalysisKwh.getScale())){
            start=new BigDecimal(sRaRealTimeAnalysisKwh.getScale().split("-")[0]);
            end=new BigDecimal(sRaRealTimeAnalysisKwh.getScale().split("-")[1]);
        }
        Page<SRaRealTimeAnalysisKwh> page=new Page<>(1,999999);
        return sRaRealTimeAnalysisKwhMapper.selectSRaRealTimeAnalysisKwhList(page,sRaRealTimeAnalysisKwh,start,end);
    }

    @Override
    public List<Map<String, String>> pullPollutantOptions() {
        return sRaRealTimeAnalysisKwhMapper.pullPollutantOptions();
    }

    @Override
    public List<Map<String, String>> pullEmissionScaleOptions() {
        List<Map<String, String>> list=new ArrayList<>();
        Map<String, BigDecimal> map= sRaRealTimeAnalysisKwhMapper.pullEmissionScaleOptions();
        if(MapUtil.isNotEmpty(map)){
            BigDecimal maxValue= map.get("maxvalue");
            BigDecimal minValue=map.get("minvalue");
            BigDecimal subtract = maxValue.subtract(minValue);
            BigDecimal divide = subtract.divide(BigDecimal.valueOf(3), 0, BigDecimal.ROUND_HALF_UP);
            BigDecimal value1= minValue.add(divide);
            BigDecimal value2= value1.add(divide);
            Map<String, String> map1=new HashMap<>();
            map1.put("label",String.join("-",minValue.toString(),value1.toString()));
            map1.put("value",String.join("-",minValue.toString(),value1.toString()));
            list.add(map1);
            if(maxValue.compareTo(minValue)!=0){
                Map<String, String> map2=new HashMap<>();
                map2.put("label",String.join("-",value1.toString(),value2.toString()));
                map2.put("value",String.join("-",value1.toString(),value2.toString()));
                list.add(map2);
                Map<String, String> map3=new HashMap<>();
                map3.put("label",String.join("-",value2.toString(),maxValue.toString()));
                map3.put("value",String.join("-",value2.toString(),maxValue.toString()));
                list.add(map3);
            }
        }
        return list;
    }
}
