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

import cn.cecep.talroad.common.core.utils.StringUtils;
import cn.cecep.talroad.domain.analysis.SRaEmissionsStandard;
import cn.cecep.talroad.domain.analysis.SRaRealTimeAnalysisCoal;
import cn.cecep.talroad.mapper.analysis.SRaEmissionsStandardMapper;
import cn.cecep.talroad.service.analysis.ISRaEmissionsStandardService;
import cn.cecep.talroad.vo.query.analysis.SRaEmissionsStandardQuery;
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.*;


/**
 * 企业排放水平分析Service业务层处理
 *
 * @author szhpt
 * @date 2023-03-24*/
@Service
public class SRaEmissionsStandardServiceImpl extends ServiceImpl<SRaEmissionsStandardMapper, SRaEmissionsStandard> implements ISRaEmissionsStandardService
{
    @Autowired
    private SRaEmissionsStandardMapper sRaEmissionsStandardMapper;

    /**
     * 查询企业排放水平分析列表
     *
     * @param sRaEmissionsStandard 企业排放水平分析
     * @return 企业排放水平分析
     */
    @Override
    public List<SRaEmissionsStandard> selectSRaEmissionsStandardList(SRaEmissionsStandardQuery sRaEmissionsStandard)
    {
        BigDecimal start=null;
        BigDecimal end=null;
        if(StringUtils.isNotEmpty(sRaEmissionsStandard.getEmissionsScale())){
             start=new BigDecimal(sRaEmissionsStandard.getEmissionsScale().split("-")[0]);
             end=new BigDecimal(sRaEmissionsStandard.getEmissionsScale().split("-")[1]);
        }
        Page<SRaRealTimeAnalysisCoal> page=new Page<>(sRaEmissionsStandard.getPageNum(),sRaEmissionsStandard.getPageSize());
        return sRaEmissionsStandardMapper.selectSRaEmissionsStandardList(page,sRaEmissionsStandard,start,end);
    }

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

    @Override
    public List<Map<String, String>> pullEmissionScaleOptions() {
        List<Map<String, String>> list=new ArrayList<>();
        Map<String, BigDecimal> map= sRaEmissionsStandardMapper.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;
    }
}
