package com.ruoyi.project.production.service.impl.thread;

import com.ruoyi.project.production.domain.TMonthlyProductionPrice;
import com.ruoyi.project.production.domain.TMonthlyProductionReport;
import com.ruoyi.project.production.mapper.TMonthlyProductionPriceMapper;
import com.ruoyi.project.production.mapper.TMonthlyProductionReportMapper;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 修改月报后同步更新数据
 *
 * @author: Wang Zi Wen
 * @email: wangggziwen@163.com
 * @datetime: 2022/11/15 10:24
 */
public class SyncAfterMonthlyUpdateThread extends Thread {

    private int reportYear;

    private int reportMonth;

    private TMonthlyProductionReportMapper tMonthlyProductionReportMapper;

    private TMonthlyProductionPriceMapper tMonthlyProductionPriceMapper;

    public SyncAfterMonthlyUpdateThread() {
        super();
    }

    public SyncAfterMonthlyUpdateThread(
            int reportYear,
            int reportMonth,
            TMonthlyProductionReportMapper tMonthlyProductionReportMapper) {
        super();
        this.reportYear = reportYear;
        this.reportMonth = reportMonth;
        this.tMonthlyProductionReportMapper = tMonthlyProductionReportMapper;
    }

    public SyncAfterMonthlyUpdateThread(
            int reportYear,
            int reportMonth,
            TMonthlyProductionReportMapper tMonthlyProductionReportMapper,
            TMonthlyProductionPriceMapper tMonthlyProductionPriceMapper) {
        super();
        this.reportYear = reportYear;
        this.reportMonth = reportMonth;
        this.tMonthlyProductionReportMapper = tMonthlyProductionReportMapper;
        this.tMonthlyProductionPriceMapper = tMonthlyProductionPriceMapper;
    }

    @Override
    public void run() {
        this.syncMonthlyData();
    }

    /**
     * 同步更新月报数据
     */
    private void syncMonthlyData() {
        TMonthlyProductionReport report = tMonthlyProductionReportMapper.selectTMonthlyProductionReportByDate(this.reportYear, this.reportMonth);
        if (report == null) {
            return;
        }

        String ethylenProducedStr = report.getEthylenProduced();
        String fuelGasOffgasCrackerStr = report.getFuelGasOffgasCracker();
        String shpCrackerStr = report.getShpCracker();
        String hpCrackerStr = report.getHpCracker();
        String mpCrackerStr = report.getMpCracker();
        String lpCrackerStr = report.getLpCracker();
        String electricityCrackerStr = report.getElectricityCracker();
        String coolingWaterCrackerStr = report.getCoolingWaterCracker();
        String n2CrackerStr = report.getN2Cracker();
        String hpBfwCrackerStr = report.getHpBfwCracker();
        String polishedConCrackerStr = report.getPolishedConCracker();
        String turbineConCrackerStr = report.getTurbineConCracker();
        String crackerConsumptionIaStr = report.getCrackerConsumptionIa();
        String paIaCrackerStr = report.getPaIaCracker();
        String crackerConsumptionPwStr = report.getCrackerConsumptionPw();
        String prodWaterCrackerStr = report.getProdWaterCracker();
        BigDecimal ethylenProduced = new BigDecimal(ethylenProducedStr == null ? "0" : ethylenProducedStr);
        BigDecimal fuelGasOffgasCracker = new BigDecimal(fuelGasOffgasCrackerStr == null ? "0" : fuelGasOffgasCrackerStr);
        BigDecimal shpCracker = new BigDecimal(shpCrackerStr == null ? "0" : shpCrackerStr);
        BigDecimal hpCracker = new BigDecimal(hpCrackerStr == null ? "0" : hpCrackerStr);
        BigDecimal mpCracker = new BigDecimal(mpCrackerStr == null ? "0" : mpCrackerStr);
        BigDecimal lpCracker = new BigDecimal(lpCrackerStr == null ? "0" : lpCrackerStr);
        BigDecimal electricityCracker = new BigDecimal(electricityCrackerStr == null ? "0" : electricityCrackerStr);
        BigDecimal coolingWaterCracker = new BigDecimal(coolingWaterCrackerStr == null ? "0" : coolingWaterCrackerStr);
        BigDecimal n2Cracker = new BigDecimal(n2CrackerStr == null ? "0" : n2CrackerStr);
        BigDecimal hpBfwCracker = new BigDecimal(hpBfwCrackerStr == null ? "0" : hpBfwCrackerStr);
        BigDecimal polishedConCracker = new BigDecimal(polishedConCrackerStr == null ? "0" : polishedConCrackerStr);
        BigDecimal turbineConCracker = new BigDecimal(turbineConCrackerStr == null ? "0" : turbineConCrackerStr);
        BigDecimal crackerConsumptionIa = new BigDecimal(crackerConsumptionIaStr == null ? "0" : crackerConsumptionIaStr);
        BigDecimal paIaCracker = new BigDecimal(paIaCrackerStr == null ? "0" : paIaCrackerStr);
        BigDecimal crackerConsumptionPw = new BigDecimal(crackerConsumptionPwStr == null ? "0" : crackerConsumptionPwStr);
        BigDecimal prodWaterCracker = new BigDecimal(prodWaterCrackerStr == null ? "0" : prodWaterCrackerStr);
        BigDecimal crackerEnergyTeTeo = new BigDecimal("0");
        crackerEnergyTeTeo = (fuelGasOffgasCracker.multiply(new BigDecimal("1.2")))
                .add(shpCracker.multiply(new BigDecimal("0.092")))
                .add(hpCracker.multiply(new BigDecimal("0.088")))
                .add(mpCracker.multiply(new BigDecimal("0.08")))
                .add(lpCracker.multiply(new BigDecimal("0.066")))
                .add(electricityCracker.multiply(new BigDecimal("0.233")))
                .add(coolingWaterCracker.multiply(new BigDecimal("0.1")))
                .add((crackerConsumptionIa.add(paIaCracker)).multiply(new BigDecimal("0.038")))
                .add(n2Cracker.multiply(new BigDecimal("0.15")))
                .add(hpBfwCracker.multiply(new BigDecimal("0.0092")))
                .add(polishedConCracker.multiply(new BigDecimal("0.0023")))
                .add(turbineConCracker.multiply(new BigDecimal("0.00365")))
                .add((crackerConsumptionPw.add(prodWaterCracker)).multiply(new BigDecimal("0.00017")));
        crackerEnergyTeTeo = crackerEnergyTeTeo.divide(new BigDecimal("1"), 2, RoundingMode.HALF_UP);
        BigDecimal crackerEnergyTeTec = new BigDecimal("0");
        crackerEnergyTeTec = crackerEnergyTeTeo.divide(new BigDecimal("0.7"), 2, RoundingMode.HALF_UP);
        BigDecimal crackerEnergyEeKgeot = new BigDecimal("0");
        if (ethylenProduced.compareTo(BigDecimal.ZERO) != 0) {
            crackerEnergyEeKgeot = crackerEnergyTeTeo.divide(ethylenProduced, 2, RoundingMode.HALF_UP)
                    .multiply(new BigDecimal("1000"));
        }
        BigDecimal crackerEnergyEeKgect = new BigDecimal("0");
        if (ethylenProduced.compareTo(BigDecimal.ZERO) != 0) {
            crackerEnergyEeKgect = crackerEnergyTeTec.divide(ethylenProduced, 2, RoundingMode.HALF_UP)
                    .multiply(new BigDecimal("1000"));
        }
        report.setCrackerEnergyTeTeo(crackerEnergyTeTeo.toString());
        report.setCrackerEnergyTeTec(crackerEnergyTeTec.toString());
        report.setCrackerEnergyEeKgeot(crackerEnergyEeKgeot.toString());
        report.setCrackerEnergyEeKgect(crackerEnergyEeKgect.toString());

        String shutdouwnOptStr = report.getShutdouwnOpt();
        String shutdouwnOphStr = report.getShutdouwnOph();
        String shutdouwnEqtStr = report.getShutdouwnEqt();
        String shutdouwnEqhStr = report.getShutdouwnEqh();
        String shutdouwnItStr = report.getShutdouwnIt();
        String shutdouwnIthStr = report.getShutdouwnIth();
        String shutdouwnEltStr = report.getShutdouwnElt();
        String shutdouwnElthStr = report.getShutdouwnElth();
        String shutdouwnOtStr = report.getShutdouwnOt();
        String shutdouwnOthStr = report.getShutdouwnOth();
        BigDecimal shutdouwnOpt = new BigDecimal(shutdouwnOptStr == null ? "0" : shutdouwnOptStr);
        BigDecimal shutdouwnOph = new BigDecimal(shutdouwnOphStr == null ? "0" : shutdouwnOphStr);
        BigDecimal shutdouwnEqt = new BigDecimal(shutdouwnEqtStr == null ? "0" : shutdouwnEqtStr);
        BigDecimal shutdouwnEqh = new BigDecimal(shutdouwnEqhStr == null ? "0" : shutdouwnEqhStr);
        BigDecimal shutdouwnIt = new BigDecimal(shutdouwnItStr == null ? "0" : shutdouwnItStr);
        BigDecimal shutdouwnIth = new BigDecimal(shutdouwnIthStr == null ? "0" : shutdouwnIthStr);
        BigDecimal shutdouwnElt = new BigDecimal(shutdouwnEltStr == null ? "0" : shutdouwnEltStr);
        BigDecimal shutdouwnElth = new BigDecimal(shutdouwnElthStr == null ? "0" : shutdouwnElthStr);
        BigDecimal shutdouwnOt = new BigDecimal(shutdouwnOtStr == null ? "0" : shutdouwnOtStr);
        BigDecimal shutdouwnOth = new BigDecimal(shutdouwnOthStr == null ? "0" : shutdouwnOthStr);
        BigDecimal shutdouwnTt = new BigDecimal("0");
        BigDecimal shutdouwnTh = new BigDecimal("0");
        shutdouwnTt = shutdouwnOpt.add(shutdouwnEqt).add(shutdouwnIt).add(shutdouwnElt).add(shutdouwnOt);
        shutdouwnTh = shutdouwnOph.add(shutdouwnEqh).add(shutdouwnIth).add(shutdouwnElth).add(shutdouwnOth);
        report.setShutdouwnTt(shutdouwnTt.toString());
        report.setShutdouwnTh(shutdouwnTh.toString());

        String calendarDayStr = report.getCalendarDay();
        BigDecimal runHour = new BigDecimal("0");
        BigDecimal runRate = new BigDecimal("0");
        BigDecimal calendarHour = new BigDecimal("0");
        BigDecimal calendarDay = new BigDecimal(calendarDayStr == null ? "0" : calendarDayStr);
        calendarHour = calendarDay.multiply(new BigDecimal("24"));
        runHour = calendarHour.subtract(shutdouwnTh);
        if (calendarHour.compareTo(BigDecimal.ZERO) != 0) {
            runRate = runHour.multiply(new BigDecimal("100")).divide(calendarHour, 2, RoundingMode.HALF_UP);
        }
        report.setRunHour(runHour.toString());
        report.setRunRate(runRate.toString());
        report.setCalendarHour(calendarHour.toString());

        String propyleneProducedStr = report.getPropyleneProduced();
        String aeuBenzeneProducedStr = report.getAeuBenzeneProduced();
        String aeuTolueneProducedStr = report.getAeuTolueneProduced();
        String aeuXyleneProducedStr = report.getAeuXyleneProduced();
        BigDecimal propyleneProduced = new BigDecimal(propyleneProducedStr == null ? "0" : propyleneProducedStr);
        BigDecimal aeuBenzeneProduced = new BigDecimal(aeuBenzeneProducedStr == null ? "0" : aeuBenzeneProducedStr);
        BigDecimal aeuTolueneProduced = new BigDecimal(aeuTolueneProducedStr == null ? "0" : aeuTolueneProducedStr);
        BigDecimal aeuXyleneProduced = new BigDecimal(aeuXyleneProducedStr == null ? "0" : aeuXyleneProducedStr);
        BigDecimal crackerLoad = new BigDecimal("0");
        BigDecimal c2C3Capacity = new BigDecimal("0");
        BigDecimal ethyleneLoad = new BigDecimal("0");
        BigDecimal ethyleneCapacity = new BigDecimal("0");
        BigDecimal aeuLoad = new BigDecimal("0");
        BigDecimal btxCapacity = new BigDecimal("0");
        c2C3Capacity = calendarHour.multiply(new BigDecimal("94").add(new BigDecimal(51.625)))
                .divide(new BigDecimal("1"), 2, RoundingMode.HALF_UP);
        if (c2C3Capacity.compareTo(BigDecimal.ZERO) != 0) {
            crackerLoad = (ethylenProduced.add(propyleneProduced))
                    .multiply(new BigDecimal("100"))
                    .divide(c2C3Capacity, 2, RoundingMode.HALF_UP);
        }
        ethyleneCapacity = calendarHour.multiply(new BigDecimal("94"));
        if (ethyleneCapacity.compareTo(BigDecimal.ZERO) != 0) {
            ethyleneLoad = ethylenProduced
                    .multiply(new BigDecimal("100"))
                    .divide(ethyleneCapacity, 2, RoundingMode.HALF_UP);
        }
        btxCapacity = calendarHour.multiply(new BigDecimal("45"));
        if (btxCapacity.compareTo(BigDecimal.ZERO) != 0) {
            aeuLoad = (aeuBenzeneProduced.add(aeuTolueneProduced).add(aeuXyleneProduced))
                    .multiply(new BigDecimal("100"))
                    .divide(btxCapacity, 2, RoundingMode.HALF_UP);
        }
        report.setCrackerLoad(crackerLoad.toString());
        report.setC2C3Capacity(c2C3Capacity.toString());
        report.setEthyleneLoad(ethyleneLoad.toString());
        report.setEthyleneCapacity(ethyleneCapacity.toString());
        report.setAeuLoad(aeuLoad.toString());
        report.setBtxCapacity(btxCapacity.toString());

        String subConHpSteamFrSubNetStr = report.getSubConHpSteamFrSubNet();
        String subConHhpSteamStr = report.getSubConHhpSteam();
        String subConHpProductStr = report.getSubConHpProduct();
        BigDecimal subConHpSteamFrSubNet = new BigDecimal(subConHpSteamFrSubNetStr == null ? "0" : subConHpSteamFrSubNetStr);
        BigDecimal subConHhpSteam = new BigDecimal(subConHhpSteamStr == null ? "0" : subConHhpSteamStr);
        BigDecimal subConHpProduct = new BigDecimal(subConHpProductStr == null ? "0" : subConHpProductStr);
        BigDecimal subConGljcl = new BigDecimal("0")
                .subtract(subConHpSteamFrSubNet.add(subConHhpSteam));
        BigDecimal subConCc = new BigDecimal("0");
        BigDecimal subConSc = new BigDecimal("0");
        if (calendarHour.compareTo(BigDecimal.ZERO) != 0) {
            subConCc = subConHpProduct
                    .multiply(new BigDecimal("0"))
                    .divide(calendarHour, 2, RoundingMode.HALF_UP);
            subConSc = (subConSc.subtract(subConHpSteamFrSubNet.add(subConHhpSteam)))
                    .multiply(new BigDecimal("0"))
                    .divide(calendarHour, 2, RoundingMode.HALF_UP);
        }
        report.setSubConGljcl(subConGljcl.toString());
        if (calendarHour.compareTo(BigDecimal.ZERO) != 0) {
            subConCc = subConHpProduct.divide(calendarHour, 2, RoundingMode.HALF_UP);
        }
        report.setSubConCc(subConCc.toString());
        if (calendarHour.compareTo(BigDecimal.ZERO) != 0) {
            subConSc = new BigDecimal("0").subtract((subConHhpSteam.add(subConHpSteamFrSubNet)).divide(calendarHour, 2, RoundingMode.HALF_UP));
        }
        report.setSubConSc(subConSc.toString());

        String subConElectricStr = report.getSubConElectric();
        String subConNgStr = report.getSubConNg();
        String subConFgFrEuStr = report.getSubConFgFrEu();
        String subConLpgStr = report.getSubConLpg();
        String subConPCondensateStr = report.getSubConPCondensate();
        String subConCwStr = report.getSubConCw();
        String subConIaPaStr = report.getSubConIaPa();
        String subConBfwStr = report.getSubConBfw();
        String subConEoEgPurgeGasStr = report.getSubConEoEgPurgeGas();
        String subConH2FrSyngasAsFuleStr = report.getSubConH2FrSyngasAsFule();
        BigDecimal subConElectric = new BigDecimal(subConElectricStr == null ? "0" : subConElectricStr);
        BigDecimal subConNg = new BigDecimal(subConNgStr == null ? "0" : subConNgStr);
        BigDecimal subConFgFrEu = new BigDecimal(subConFgFrEuStr == null ? "0" : subConFgFrEuStr);
        BigDecimal subConLpg = new BigDecimal(subConLpgStr == null ? "0" : subConLpgStr);
        BigDecimal subConPCondensate = new BigDecimal(subConPCondensateStr == null ? "0" : subConPCondensateStr);
        BigDecimal subConCw = new BigDecimal(subConCwStr == null ? "0" : subConCwStr);
        BigDecimal subConIaPa = new BigDecimal(subConIaPaStr == null ? "0" : subConIaPaStr);
        BigDecimal subConBfw = new BigDecimal(subConBfwStr == null ? "0" : subConBfwStr);
        BigDecimal subConEoEgPurgeGas = new BigDecimal(subConEoEgPurgeGasStr == null ? "0" : subConEoEgPurgeGasStr);
        BigDecimal subConH2FrSyngasAsFule = new BigDecimal(subConH2FrSyngasAsFuleStr == null ? "0" : subConH2FrSyngasAsFuleStr);
        BigDecimal subConCostOutput = new BigDecimal("0");
        // 查询单价
        TMonthlyProductionPrice price = tMonthlyProductionPriceMapper.selectTMonthlyProductionPriceByDate(this.reportYear, this.reportMonth);
        String subConElectricPriceStr = null;
        String subConNgPriceStr = null;
        String subConFgFrEuPriceStr = null;
        String subConLpgPriceStr = null;
        String subConHhpSteamPriceStr = null;
        String subConPCondensatePriceStr = null;
        String subConCwPriceStr = null;
        String subConIaPaPriceStr = null;
        String subConBfwPriceStr = null;
        String subConEoEgPurgeGasPriceStr = null;
        String subConH2FulePriceStr = null;
        if (price != null) {
            subConElectricPriceStr = price.getSubConElectricPrice();
            subConNgPriceStr = price.getSubConNgPrice();
            subConFgFrEuPriceStr = price.getSubConFgFrEuPrice();
            subConLpgPriceStr = price.getSubConLpgPrice();
            subConHhpSteamPriceStr = price.getSubConHhpSteamPrice();
            subConPCondensatePriceStr = price.getSubConPCondensatePrice();
            subConCwPriceStr = price.getSubConCwPrice();
            subConIaPaPriceStr = price.getSubConIaPaPrice();
            subConBfwPriceStr = price.getSubConBfwPrice();
            subConEoEgPurgeGasPriceStr = price.getSubConEoEgPurgeGasPrice();
            subConH2FulePriceStr = price.getSubConH2FulePrice();
        }
        BigDecimal subConElectricPrice = new BigDecimal(subConElectricPriceStr == null ? "0" : subConElectricPriceStr);
        BigDecimal subConNgPrice = new BigDecimal(subConNgPriceStr == null ? "0" : subConNgPriceStr);
        BigDecimal subConFgFrEuPrice = new BigDecimal(subConFgFrEuPriceStr == null ? "0" : subConFgFrEuPriceStr);
        BigDecimal subConLpgPrice = new BigDecimal(subConLpgPriceStr == null ? "0" : subConLpgPriceStr);
        BigDecimal subConHhpSteamPrice = new BigDecimal(subConHhpSteamPriceStr == null ? "0" : subConHhpSteamPriceStr);
        BigDecimal subConPCondensatePrice = new BigDecimal(subConPCondensatePriceStr == null ? "0" : subConPCondensatePriceStr);
        BigDecimal subConCwPrice = new BigDecimal(subConCwPriceStr == null ? "0" : subConCwPriceStr);
        BigDecimal subConIaPaPrice = new BigDecimal(subConIaPaPriceStr == null ? "0" : subConIaPaPriceStr);
        BigDecimal subConBfwPrice = new BigDecimal(subConBfwPriceStr == null ? "0" : subConBfwPriceStr);
        BigDecimal subConEoEgPurgeGasPrice = new BigDecimal(subConEoEgPurgeGasPriceStr == null ? "0" : subConEoEgPurgeGasPriceStr);
        BigDecimal subConH2FulePrice = new BigDecimal(subConH2FulePriceStr == null ? "0" : subConH2FulePriceStr);
        if (subConHpSteamFrSubNet.compareTo(BigDecimal.ZERO) != 0 && subConHhpSteam.compareTo(BigDecimal.ZERO) != 0) {
            subConCostOutput = (new BigDecimal("0").subtract(
                                        (subConElectricPrice.multiply(subConElectric))
                                        .add(subConNgPrice.multiply(subConNg))
                                        .add(subConFgFrEuPrice.multiply(subConFgFrEu))
                                        .add(subConLpgPrice.multiply(subConLpg))
                                        .add(subConPCondensatePrice.multiply(subConPCondensate))
                                        .add(subConCwPrice.multiply(subConCw))
                                        .add(subConIaPaPrice.multiply(subConIaPa))
                                        .add(subConBfwPrice.multiply(subConBfw))
                                        .add(subConEoEgPurgeGasPrice.multiply(subConEoEgPurgeGas))
                                        .add(subConH2FulePrice.multiply(subConH2FrSyngasAsFule))
                                    )
                               ).divide((subConHpSteamFrSubNet.add(subConHhpSteam)), 2, RoundingMode.HALF_UP);
        }
        BigDecimal subConZfy = subConCostOutput.multiply(subConGljcl).divide(new BigDecimal("1"), 2, RoundingMode.HALF_UP);
        report.setSubConCostOutput(subConCostOutput.toString());
        report.setSubConZfy(subConZfy.toString());

        report.setKbiPcur(crackerLoad.toString());
        report.setKbiEnergyConsumption(crackerEnergyEeKgeot.toString());
        report.setKbiAeuEnergyConsumption(report.getAromaticsEnergyEeKgect());

        tMonthlyProductionReportMapper.updateTMonthlyProductionReport(report);

    }

}