/*
 * 文 件 名:  EmsEcoAnalysis
 * 版    权:  华设设计集团股份有限公司
 * 描    述:  <描述>
 * 修 改 人:  lvwenbin
 * 修改时间:  2025/6/21
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */
package com.ruoyi.ems.service.analysis;

import com.huashe.common.utils.DateUtils;
import com.ruoyi.ems.domain.EmsEcoD;
import com.ruoyi.ems.domain.EnergyMeter;
import com.ruoyi.ems.mapper.EmsEcoDMapper;
import com.ruoyi.ems.model.QueryMeter;
import com.ruoyi.ems.service.IElecMeterHService;
import com.ruoyi.ems.service.IWaterMeterHService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.format.DateTimeFormatter;

/**
 * 节能分析
 * <功能详细描述>
 *
 * @author lvwenbin
 * @version [版本号, 2025/6/21]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Service
public class EmsEcoAnalysisService {
    private static final Logger log = LoggerFactory.getLogger(EmsEcoAnalysisService.class);

    private final DateTimeFormatter dateForm = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    @Resource
    private EmsEcoDMapper emsEcoDMapper;

    @Resource
    private IElecMeterHService elecMeterHService;

    @Resource
    private IWaterMeterHService waterMeterHService;

    public void executePvForecast(String date, QueryMeter reference) {
        try {
            EmsEcoD emsEcoD = new EmsEcoD();
            emsEcoD.setAreaCode(reference.getAreaCode());
            emsEcoD.setDate(DateUtils.stringToDate(date, "yyyy-MM-dd"));

            // 节电计算
            EnergyMeter yestElecMeter = elecMeterHService.qryElecMeterByDate(date, reference.getAreaCode());

            if (null != yestElecMeter) {
                // 参照对比数据提取
                EnergyMeter referenceMeter = elecMeterHService.selectElecDayAvg(reference);
                referenceMeter = referenceMeter != null ? referenceMeter : yestElecMeter;

                double quantity = subtractAndRound(referenceMeter.getQuantity(), yestElecMeter.getQuantity(), 2);
                double cost = subtractAndRound(referenceMeter.getUseCost(), yestElecMeter.getUseCost(), 2);
                emsEcoD.setElecEcoQuantity(quantity);
                emsEcoD.setElecEcoCost(cost);
            }

            EnergyMeter yestWaterMeter = waterMeterHService.qryWaterMeterByDate(date, reference.getAreaCode());

            if (null != yestWaterMeter) {
                // 参照对比数据提取
                EnergyMeter referenceMeter = waterMeterHService.selectWaterDayAvg(reference);
                referenceMeter = referenceMeter != null ? referenceMeter : yestWaterMeter;

                double quantity = subtractAndRound(referenceMeter.getQuantity(), yestWaterMeter.getQuantity(), 2);
                double cost = subtractAndRound(referenceMeter.getUseCost(), yestWaterMeter.getUseCost(), 2);
                emsEcoD.setWaterEcoQuantity(quantity);
                emsEcoD.setWaterEcoCost(cost);
            }

            emsEcoDMapper.deleteEmsEcoD(date, reference.getAreaCode());
            emsEcoDMapper.insertEmsEcoD(emsEcoD);
        }
        catch (Exception e) {
            log.error("executePvForecast fail! adcode:{}, date:{}", reference.getAreaCode(), date, e);
        }
    }

    /**
     * 减法运算并保留指定小数位数
     *
     * @param a     乘数
     * @param b     乘数
     * @param scale 保留小数位数
     * @return 四舍五入后的结果
     */
    private double subtractAndRound(double a, double b, int scale) {
        BigDecimal bd1 = new BigDecimal(Double.toString(a));
        BigDecimal bd2 = new BigDecimal(Double.toString(b));

        return bd1.subtract(bd2).setScale(scale, RoundingMode.HALF_UP).doubleValue();
    }
}