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

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.project.production.controller.vo.MonthlyUpdateVO;
import com.ruoyi.project.production.domain.TMonthlyProductionReport;
import com.ruoyi.project.production.mapper.TMonthlyProductionReportMapper;
import com.ruoyi.project.production.service.impl.thread.SyncAfterMonthlyUpdateThread;
import com.ruoyi.project.production.service.impl.vo.MonthlyProductionReportVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.production.mapper.TMonthlyProductionPriceMapper;
import com.ruoyi.project.production.domain.TMonthlyProductionPrice;
import com.ruoyi.project.production.service.ITMonthlyProductionPriceService;

/**
 * 生产月报单价维护Service业务层处理
 *
 * @author ruoyi
 * @date 2022-11-18
 */
@Service
public class TMonthlyProductionPriceServiceImpl implements ITMonthlyProductionPriceService
{
    @Autowired
    private TMonthlyProductionPriceMapper tMonthlyProductionPriceMapper;

    @Autowired
    private TMonthlyProductionReportMapper tMonthlyProductionReportMapper;

    /**
     * 查询生产月报单价维护
     *
     * @param id 生产月报单价维护ID
     * @return 生产月报单价维护
     */
    @Override
    public TMonthlyProductionPrice selectTMonthlyProductionPriceById(Long id)
    {
        return tMonthlyProductionPriceMapper.selectTMonthlyProductionPriceById(id);
    }

    /**
     * 查询生产月报单价维护列表
     *
     * @param tMonthlyProductionPrice 生产月报单价维护
     * @return 生产月报单价维护
     */
    @Override
    public List<TMonthlyProductionPrice> selectTMonthlyProductionPriceList(TMonthlyProductionPrice tMonthlyProductionPrice)
    {
        return tMonthlyProductionPriceMapper.selectTMonthlyProductionPriceList(tMonthlyProductionPrice);
    }

    /**
     * 新增生产月报单价维护
     *
     * @param tMonthlyProductionPrice 生产月报单价维护
     * @return 结果
     */
    @Override
    public int insertTMonthlyProductionPrice(TMonthlyProductionPrice tMonthlyProductionPrice)
    {
        return tMonthlyProductionPriceMapper.insertTMonthlyProductionPrice(tMonthlyProductionPrice);
    }

    /**
     * 修改生产月报单价维护
     *
     * @param tMonthlyProductionPrice 生产月报单价维护
     * @return 结果
     */
    @Override
    public int updateTMonthlyProductionPrice(TMonthlyProductionPrice tMonthlyProductionPrice)
    {
        return tMonthlyProductionPriceMapper.updateTMonthlyProductionPrice(tMonthlyProductionPrice);
    }

    /**
     * 批量删除生产月报单价维护
     *
     * @param ids 需要删除的生产月报单价维护ID
     * @return 结果
     */
    @Override
    public int deleteTMonthlyProductionPriceByIds(Long[] ids)
    {
        return tMonthlyProductionPriceMapper.deleteTMonthlyProductionPriceByIds(ids);
    }

    /**
     * 删除生产月报单价维护信息
     *
     * @param id 生产月报单价维护ID
     * @return 结果
     */
    @Override
    public int deleteTMonthlyProductionPriceById(Long id)
    {
        return tMonthlyProductionPriceMapper.deleteTMonthlyProductionPriceById(id);
    }

    @Override
    public List<MonthlyProductionReportVO> selectPriceByYear(Long year) {
        TMonthlyProductionReportServiceImpl tMonthlyProductionReportService = new TMonthlyProductionReportServiceImpl();
        // 当前日期
        Date currentDate = new Date();
        // 当前年份
        Long currentYear = Long.parseLong(currentDate.getYear() + 1900 + "");
        // 当前月份
        Long currentMonth = Long.parseLong(currentDate.getMonth() + 1 + "");
        // 从数据库中查出的当前year的月报数据集合，每个元素对应当前year某个月的数据
        List<TMonthlyProductionPrice> tMonthlyProductionPrices = tMonthlyProductionPriceMapper.selectPriceByYear(year);
        // 前端月报数据集合，每个元素对应当前某个指标的title、unit、1~12月的数据以及年度汇总数据
        List<MonthlyProductionReportVO> monthlyProductionReportVOs = new ArrayList<>();
        monthlyProductionReportVOs.add(new MonthlyProductionReportVO("sub_con_electric_price","Electric","cny/MWh"));
        monthlyProductionReportVOs.add(new MonthlyProductionReportVO("sub_con_ng_price","NG","cny/KM3"));
        monthlyProductionReportVOs.add(new MonthlyProductionReportVO("sub_con_fg_fr_eu_price","FG fr EU","cny/tom"));
        monthlyProductionReportVOs.add(new MonthlyProductionReportVO("sub_con_lpg_price","LPG","cny/Ton"));
        monthlyProductionReportVOs.add(new MonthlyProductionReportVO("sub_con_hhp_steam_price","HHP Steam","cny/Ton"));
        monthlyProductionReportVOs.add(new MonthlyProductionReportVO("sub_con_p_condensate_price","P. Condensate","cny/Ton"));
        monthlyProductionReportVOs.add(new MonthlyProductionReportVO("sub_con_cw_price","CW","cny/KT"));
        monthlyProductionReportVOs.add(new MonthlyProductionReportVO("sub_con_ia_pa_price","IA&PA","cny/KM3"));
        monthlyProductionReportVOs.add(new MonthlyProductionReportVO("sub_con_bfw_price","BFW","cny/Ton"));
        monthlyProductionReportVOs.add(new MonthlyProductionReportVO("sub_con_eo_eg_purge_gas_price","EO/EG purge gas","cny/Km3"));
        monthlyProductionReportVOs.add(new MonthlyProductionReportVO("sub_con_h2_fule_price","H2 fr Syngas as fule","cny/km3"));
        // 遍历从数据库中查出的月报数据集合，结果为0~12条数据不等
        for (int i = 0; i < tMonthlyProductionPrices.size(); i++) {
            TMonthlyProductionPrice tMonthlyProductionPrice = tMonthlyProductionPrices.get(i);
            String subConElectricPrice = tMonthlyProductionPrice.getSubConElectricPrice();
            String subConNgPrice = tMonthlyProductionPrice.getSubConNgPrice();
            String subConFgFrEuPrice = tMonthlyProductionPrice.getSubConFgFrEuPrice();
            String subConLpgPrice = tMonthlyProductionPrice.getSubConLpgPrice();
            String subConHhpSteamPrice = tMonthlyProductionPrice.getSubConHhpSteamPrice();
            String subConPCondensatePrice = tMonthlyProductionPrice.getSubConPCondensatePrice();
            String subConCwPrice = tMonthlyProductionPrice.getSubConCwPrice();
            String subConIaPaPrice = tMonthlyProductionPrice.getSubConIaPaPrice();
            String subConBfwPrice = tMonthlyProductionPrice.getSubConBfwPrice();
            String subConEoEgPurgeGasPrice = tMonthlyProductionPrice.getSubConEoEgPurgeGasPrice();
            String subConH2FulePrice = tMonthlyProductionPrice.getSubConH2FulePrice();
            Long reportMonth = tMonthlyProductionPrice.getReportMonth();   // 当前元素的所属月份
            Long reportYear = tMonthlyProductionPrice.getReportYear();     // 当前元素的所属年份
            // 前端数据集合的class
            Class clazz = null;
            // 当前元素调用的set方法
            Method method = null;
            try {
                clazz = Class.forName("com.ruoyi.project.production.service.impl.vo.MonthlyProductionReportVO");
                method = tMonthlyProductionReportService.getSetMethod(reportMonth, clazz);
                // 按照当前元素的所属月份调取相应set方法
                method.invoke(monthlyProductionReportVOs.get(0), subConElectricPrice);
                method.invoke(monthlyProductionReportVOs.get(1), subConNgPrice);
                method.invoke(monthlyProductionReportVOs.get(2), subConFgFrEuPrice);
                method.invoke(monthlyProductionReportVOs.get(3), subConLpgPrice);
                method.invoke(monthlyProductionReportVOs.get(4), subConHhpSteamPrice);
                method.invoke(monthlyProductionReportVOs.get(5), subConPCondensatePrice);
                method.invoke(monthlyProductionReportVOs.get(6), subConCwPrice);
                method.invoke(monthlyProductionReportVOs.get(7), subConIaPaPrice);
                method.invoke(monthlyProductionReportVOs.get(8), subConBfwPrice);
                method.invoke(monthlyProductionReportVOs.get(9), subConEoEgPurgeGasPrice);
                method.invoke(monthlyProductionReportVOs.get(10), subConH2FulePrice);
                // 当前年份=所属年份，当前月份=所属月份
                if (currentMonth.equals(reportMonth) && currentYear.equals(reportYear)) {
                    monthlyProductionReportVOs.get(0).setCurrently(subConElectricPrice);
                    monthlyProductionReportVOs.get(1).setCurrently(subConNgPrice);
                    monthlyProductionReportVOs.get(2).setCurrently(subConFgFrEuPrice);
                    monthlyProductionReportVOs.get(3).setCurrently(subConLpgPrice);
                    monthlyProductionReportVOs.get(4).setCurrently(subConHhpSteamPrice);
                    monthlyProductionReportVOs.get(5).setCurrently(subConPCondensatePrice);
                    monthlyProductionReportVOs.get(6).setCurrently(subConCwPrice);
                    monthlyProductionReportVOs.get(7).setCurrently(subConIaPaPrice);
                    monthlyProductionReportVOs.get(8).setCurrently(subConBfwPrice);
                    monthlyProductionReportVOs.get(9).setCurrently(subConEoEgPurgeGasPrice);
                    monthlyProductionReportVOs.get(10).setCurrently(subConH2FulePrice);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        tMonthlyProductionReportService.calcAvg(monthlyProductionReportVOs);
        return monthlyProductionReportVOs;
    }

    @Override
    public int updatePriceList(MonthlyUpdateVO monthlyUpdateVO) {
        // 受影响行数
        int affectedRows = 0;
        List<MonthlyProductionReportVO> tableData = monthlyUpdateVO.getTableData();
        long year = monthlyUpdateVO.getYear();
        Class clazz = null;
        try {
            clazz = Class.forName("com.ruoyi.project.production.service.impl.vo.MonthlyProductionReportVO");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        for (MonthlyProductionReportVO tableDatum : tableData) {
            for (int i = 0; i < 12; i++) {
                TMonthlyProductionPrice price = new TMonthlyProductionPrice();
                price.setReportMonth((long)(i + 1));
                price.setReportYear(year);
                Method getMethod = null;
                String monthValue = null;
                try {
                    if (i == 0) { getMethod =  clazz.getDeclaredMethod("getJan"); }
                    if (i == 1) { getMethod =  clazz.getDeclaredMethod("getFeb"); }
                    if (i == 2) { getMethod =  clazz.getDeclaredMethod("getMar"); }
                    if (i == 3) { getMethod =  clazz.getDeclaredMethod("getApr"); }
                    if (i == 4) { getMethod =  clazz.getDeclaredMethod("getMay"); }
                    if (i == 5) { getMethod =  clazz.getDeclaredMethod("getJun"); }
                    if (i == 6) { getMethod =  clazz.getDeclaredMethod("getJul"); }
                    if (i == 7) { getMethod =  clazz.getDeclaredMethod("getAug"); }
                    if (i == 8) { getMethod =  clazz.getDeclaredMethod("getSep"); }
                    if (i == 9) { getMethod =  clazz.getDeclaredMethod("getOct"); }
                    if (i == 10) { getMethod =  clazz.getDeclaredMethod("getNov"); }
                    if (i == 11) { getMethod =  clazz.getDeclaredMethod("getDec"); }
                    monthValue = (String) getMethod.invoke(tableDatum);
                } catch (NoSuchMethodException e1){
                    e1.printStackTrace();
                } catch (IllegalAccessException e2) {
                    e2.printStackTrace();
                } catch (InvocationTargetException e3) {
                    e3.printStackTrace();
                }
                if (monthValue == null) {
                    continue;
                }
                if ("sub_con_electric_price".equals(tableDatum.getDictLabel())) {price.setSubConElectricPrice(monthValue);}
                if ("sub_con_ng_price".equals(tableDatum.getDictLabel())) {price.setSubConNgPrice(monthValue);}
                if ("sub_con_fg_fr_eu_price".equals(tableDatum.getDictLabel())) {price.setSubConFgFrEuPrice(monthValue);}
                if ("sub_con_lpg_price".equals(tableDatum.getDictLabel())) {price.setSubConLpgPrice(monthValue);}
                if ("sub_con_hhp_steam_price".equals(tableDatum.getDictLabel())) {price.setSubConHhpSteamPrice(monthValue);}
                if ("sub_con_p_condensate_price".equals(tableDatum.getDictLabel())) {price.setSubConPCondensatePrice(monthValue);}
                if ("sub_con_cw_price".equals(tableDatum.getDictLabel())) {price.setSubConCwPrice(monthValue);}
                if ("sub_con_ia_pa_price".equals(tableDatum.getDictLabel())) {price.setSubConIaPaPrice(monthValue);}
                if ("sub_con_bfw_price".equals(tableDatum.getDictLabel())) {price.setSubConBfwPrice(monthValue);}
                if ("sub_con_eo_eg_purge_gas_price".equals(tableDatum.getDictLabel())) {price.setSubConEoEgPurgeGasPrice(monthValue);}
                if ("sub_con_h2_fule_price".equals(tableDatum.getDictLabel())) {price.setSubConH2FulePrice(monthValue);}
                // 当前报告年份
                int reportYear = Integer.parseInt(price.getReportYear().toString());
                // 当前报告月份
                int reportMonth = Integer.parseInt(price.getReportMonth().toString());
                // 查询年份=reportYear，月份=reportMonth的单价数据
                int count = tMonthlyProductionPriceMapper.selectTMonthlyProductionPriceCountByDate(reportYear, reportMonth);
                if (count == 0) {   // 新增月报数据
                    affectedRows += tMonthlyProductionPriceMapper.insertTMonthlyProductionPrice(price);
                } else {    // 更新月报数据
                    affectedRows += tMonthlyProductionPriceMapper.updateTMonthlyProductionPrice(price);
                }
                // 同步更新月报数据
                SyncAfterMonthlyUpdateThread thread = new SyncAfterMonthlyUpdateThread(
                        (int)year,
                        (i + 1),
                        this.tMonthlyProductionReportMapper,
                        this.tMonthlyProductionPriceMapper
                );
                thread.run();
            }
        }
        return affectedRows;
    }
}
