package com.steerinfo.ems.foldthevalues.service.impl;

import com.steerinfo.ems.Utils.DateUtils;
import com.steerinfo.ems.emscalorificvalue.mapper.EmsCalorificValueMapper;
import com.steerinfo.ems.emscalorificvalue.model.EmsCalorificValue;
import com.steerinfo.ems.njformula.mapper.NjFormulaMapper;
import com.steerinfo.ems.njformula.model.NjFormula;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.ems.foldthevalues.model.FoldTheValues;
import com.steerinfo.ems.foldthevalues.mapper.FoldTheValuesMapper;
import com.steerinfo.ems.foldthevalues.service.IFoldTheValuesService;
import com.steerinfo.framework.service.pagehelper.PageHelper;
import com.steerinfo.framework.service.pagehelper.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.math.BigDecimal;

/**
 * FoldTheValues服务实现：
 * @author generator
 * @version 1.0-SNAPSHORT 2021-10-14 02:33
 * 类描述
 * 修订历史：
 * 日期：2021-10-14
 * 作者：generator
 * 参考：
 * 描述：FoldTheValues服务实现
 * @see null
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 */
@Service(value = "foldTheValuesService")
public class FoldTheValuesServiceImpl extends BaseServiceImpl<FoldTheValues, String> implements IFoldTheValuesService {
    @Autowired
    private FoldTheValuesMapper foldTheValuesMapper;
    @Autowired
    private EmsCalorificValueMapper emsCalorificValueMapper;
    @Autowired
    private NjFormulaMapper njFormulaMapper;


    @Override
    protected IBaseMapper<FoldTheValues, String> getMapper() {
        return foldTheValuesMapper;
    }

    // 获取每日得动力折标值
    public void getData() throws ScriptException {
        List<FoldTheValues> newDate = foldTheValuesMapper.getNewData();
        DecimalFormat df   = new DecimalFormat("0.000");
        FoldTheValues foldTheValue = new FoldTheValues();
        for (FoldTheValues foldTheValues : newDate) {
            foldTheValue.setId(foldTheValues.getId());
            foldTheValue.setMaterialsource(foldTheValues.getMaterialsource());
            foldTheValue.setMaterialno(this.getTrmenergyid(foldTheValues.getMaterialno()));
            foldTheValue.setClock(foldTheValues.getClock());
            foldTheValue.setCode(df.format(Double.valueOf(String.valueOf(foldTheValues.getCode()))));
            foldTheValue.setTimegranid("DAY");
            foldTheValue.setUnitid("032");
            foldTheValue.setDescription("低热值");
            foldTheValue.setBz("采用当天热值进行换算,当天无检测结果，则取最近热值进行换算");
            foldTheValue.setEstimatedvalue(foldTheValues.getEstimatedvalue());
            FoldTheValues byClockAndMaterialno = foldTheValuesMapper.getByClockAndMaterialno(foldTheValue);
            if(byClockAndMaterialno != null){
                foldTheValuesMapper.updateByClockAndMaterialno(foldTheValue);
                foldTheValuesMapper.updateByClock(foldTheValues);
            } else {
                foldTheValuesMapper.insert(foldTheValue);
            }
        }
    }
    //读取昨天数据
    public void insertNewDate(){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        String clock  = DateUtils.dateStr(calendar.getTime(),"yyyy-MM-dd");
        String id  = DateUtils.dateStr(calendar.getTime(),"yyyyMMdd");
        List<FoldTheValues> foldTheValues = foldTheValuesMapper.getNozbzData(clock);
        for (FoldTheValues foldTheValue : foldTheValues) {
            FoldTheValues newOne = foldTheValuesMapper.getNewOne(foldTheValue.getMaterialno());
            newOne.setId(id+foldTheValue.getMaterialno());
            newOne.setClock(clock);
            foldTheValuesMapper.insert(newOne);
        }
    }
    @Override
    public PageList<Map<String, Object>> getDateForPage(HashMap<String, Object> parmas, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Map<String, Object>> dateLc = foldTheValuesMapper.getDateForPage(parmas);
        PageList<Map<String, Object>> pageInfo = new PageList<Map<String, Object>>(dateLc);
        return pageInfo;
    }

    //高炉煤气P005 转炉煤气P007 焦炉煤气P006
    public String getTrmenergyid(String materialno){
        if(materialno.equals("P00006")){
           return "P007";
        }
        if(materialno.equals("P00007")){
            return  "P005";
        }
        if(materialno.equals("P00008")){
           return "P006";
        }
        return null;
    }

    //获取动力每月的得折标值
    public void getMonthData() throws ParseException, ScriptException {
        HashMap<String,Object> hashMap =  new HashMap<>();
        //SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar  cal_1=Calendar.getInstance();//获取当前日期
        cal_1.add(Calendar.MONTH, -1);
        cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
        String startTime = DateUtils.dateStr(cal_1.getTime(),"yyyy-MM-dd");//format.format(cal_1.getTime());
        String month  = DateUtils.dateStr(cal_1.getTime(),"yyyyMM");
        String clock  = DateUtils.dateStr(cal_1.getTime(),"yyyy-MM");
        //获取前月的最后一天
        Calendar cale = Calendar.getInstance();
        cale.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天
        String endTime = DateUtils.dateStr(cale.getTime(),"yyyy-MM-dd");//format.format(cale.getTime());
        int days = DateUtils.getDaysOfMonth(cal_1.getTime());
        hashMap.put("startTime",startTime);
        hashMap.put("endTime",endTime);
        List<FoldTheValues> dateForMoth = foldTheValuesMapper.getDataForMoth(hashMap);
        ScriptEngine jse = new ScriptEngineManager().getEngineByName("JavaScript");
        DecimalFormat df   = new DecimalFormat("0.000");
        FoldTheValues foldTheValue =  new FoldTheValues();
        for (FoldTheValues foldTheValues : dateForMoth) {
            foldTheValue.setId(month+foldTheValues.getMaterialno());
            foldTheValue.setClock(clock);
            if(foldTheValues.getMaterialsource().equals("三电厂")){
                foldTheValue.setDescription("热值");
                foldTheValue.setCode(df.format(jse.eval(foldTheValues.getEstimatedvalue()+"/"+days+"*0.03412/100")).toString());
            }
            if(foldTheValues.getMaterialsource().equals("机焦厂")){
                foldTheValue.setDescription("灰分");
                foldTheValue.setCode(df.format(jse.eval("(29727+(10-"+foldTheValues.getEstimatedvalue()+"/"+days+")*334)/29307.6")));
            }
            foldTheValue.setIsavailable("0");
            foldTheValue.setUnitid("032");
            hashMap.put("materialno",foldTheValues.getMaterialno());
            foldTheValue.setBz("取本月热值检测结果平均值("+foldTheValuesMapper.getavgDate(hashMap)+")进行热值换算");
            foldTheValue.setMaterialno(foldTheValues.getMaterialno());
            foldTheValue.setMaterialsource(foldTheValues.getMaterialsource());
            foldTheValue.setTimegranid("MONTH");
            foldTheValue.setEstimatedvalue(df.format(jse.eval(foldTheValues.getEstimatedvalue()+"/"+days)));
            foldTheValuesMapper.insert(foldTheValue);
        }

    }

//    //对于不同能介给不同的计算方式
//    public String getjsz(FoldTheValues foldTheValues){
//        String str = "";
//        if(foldTheValues.getMaterialsource().equals("三电厂")){
//            str = ""
//        }
//    }

    //获取物料每日得折标值
    public void getDataForwl(){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        String clock  = DateUtils.dateStr(calendar.getTime(),"yyyy-MM-dd");
        HashMap<String,Object> hashMap =  new HashMap<>();
          hashMap.put("clock",clock);
        List<FoldTheValues> dateForwl = foldTheValuesMapper.getDateForwl(hashMap);
        FoldTheValues foldTheValue =  new FoldTheValues();
        for (FoldTheValues foldTheValues : dateForwl) {
            foldTheValue.setId(foldTheValues.getId());
            foldTheValue.setMaterialsource(foldTheValues.getMaterialsource());
            foldTheValue.setMaterialno(foldTheValues.getMaterialno());
            foldTheValue.setCode(foldTheValues.getCode());
            foldTheValue.setClock(foldTheValues.getClock());
            foldTheValue.setEstimatedvalue(foldTheValues.getEstimatedvalue());
            foldTheValue.setTimegranid("DAY");
            foldTheValue.setUnitid("032");
            foldTheValue.setDescription("灰分");
            foldTheValue.setBz("采用当天灰分进行换算,当天无检测结果，则取最近灰分进行换算");
            foldTheValuesMapper.insert(foldTheValue);
        }

    }
//    //获取折标值 （该方法已经弃用）
//    public String getzbz(EmsCalorificValue emsCalorificValue) throws ScriptException {
//        ScriptEngine jse = new ScriptEngineManager().getEngineByName("JavaScript");
//        //首先获取公式
//        NjFormula njFormula = njFormulaMapper.getByMateralNo(emsCalorificValue.getMaterialno());
//        String formula = njFormula.getCode();
//        if (null == formula ){
//            return  null;
//        }
//        HashMap map =  new HashMap();
//        map.put("wtbh",emsCalorificValue.getWtbh());
//        if(formula.contains("CO") == true){
//            map.put("item","CO");
//            String value = emsCalorificValueMapper.getValue(map) == null ? "0.00" : emsCalorificValueMapper.getValue(map);
//            formula = formula.replace("CO", value);
//        }
//        if (formula.contains("H2") == true){
//            map.put("item","H2");
//            String value = emsCalorificValueMapper.getValue(map) == null ? "0.00" : emsCalorificValueMapper.getValue(map);
//            formula = formula.replace("H2", value);
//        }
//        if (formula.contains("CH4") == true){
//            map.put("item","CH4");
//            String value = emsCalorificValueMapper.getValue(map) == null ? "0.00" : emsCalorificValueMapper.getValue(map);
//            formula = formula.replace("CH4", value);
//        }
//        if (formula.contains("N2") == true){
//            map.put("item","N2");
//            String value = emsCalorificValueMapper.getValue(map) == null ? "0.00" :emsCalorificValueMapper.getValue(map);
//            formula = formula.replace("N2", value);
//        }
//        DecimalFormat df   = new DecimalFormat("0.00");
//        return  df.format(jse.eval(formula)).toString();
//    }
}
