package com.seeyon.apps.src_hxinvoice.event;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.seeyon.apps.common.config.ICstConfigApi;
import com.seeyon.apps.common.plugin.vo.ConfigVo;
import com.seeyon.apps.common.workflow.constants.WorkFlowType;
import com.seeyon.apps.common.workflow.event.ACommonWorkflowEvent;
import com.seeyon.apps.ext.workflow.vo.FieldDataVo;
import com.seeyon.apps.ext.workflow.vo.FormDataVo;
import com.seeyon.apps.ext.workflow.vo.WorkflowEventContext;
import com.seeyon.apps.src_hxinvoice.constants.HxinvoiceConstants;
import com.seeyon.apps.src_hxinvoice.dao.IHxinvoiceDao;
import com.seeyon.apps.src_hxinvoice.dao.impl.HxinvoiceDaoImpl;
import com.seeyon.apps.src_hxinvoice.util.FileUtil;
import com.seeyon.apps.src_hxinvoice.util.FourDigitUniqueIdGenerator;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.SystemEnvironment;
import www.seeyon.com.utils.StringUtil;

import java.math.RoundingMode;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class FileDataValidateBeforeStartEvent extends ACommonWorkflowEvent {

    protected ICstConfigApi cstConfigApi = (ICstConfigApi) AppContext.getBean("cstConfigApi");


    @Override
    protected WorkflowEventContext proceed(String request, FormDataVo formDataVo, WorkFlowType workFlowType, FormDataMasterBean formDataMasterBean) throws Exception {
        WorkflowEventContext context = new WorkflowEventContext();

        ConfigVo configVo = cstConfigApi.getConfig(getPluginId());
        NumberFormat nf = NumberFormat.getNumberInstance();
        nf.setMaximumFractionDigits(2);    // 保留2位
        nf.setRoundingMode(RoundingMode.HALF_UP); // 四舍五入
        nf.setGroupingUsed(false);
        context.setNeedSave(true);

        System.out.println("进行附件处理");
        FileUtil fileUtil = new FileUtil();
        IHxinvoiceDao hxinvoiceDao = new HxinvoiceDaoImpl();
//        参数转JSON
        JSONObject jsonObject = JSONObject.parseObject(request);
//        获取参数名称
        String DDZXXName = jsonObject.getString("DDZXX");
        System.out.println("获取参数附件ID"+DDZXXName);
//        根据参数名称获取附件控件对象
//        设置附件下载路径
        String path = SystemEnvironment.getApplicationFolder()+"/hxinvoiceFile/"+DDZXXName+"/";
        System.out.println("文件下载路径"+path);
        Thread.sleep(3000);
//        将附件下载到服务器
        List<String> filePaths = fileUtil.fieldFileDownload(Long.parseLong(DDZXXName),path);
//        创建DDZXX对象
        JSONArray DDZXXS = new JSONArray();

//        税率判断
        String taxRes = "";
        double zje = 0;
        double sj = 0;
        double bhsje = 0;
        for(String filePath : filePaths){
            JSONArray jsonArray = fileUtil.getFileContent(filePath);
//            System.out.println("获取附件内容"+jsonArray);
            for(int i = 0 ; i < jsonArray.size();i++){
                JSONObject fileJson = jsonArray.getJSONObject(i);
                JSONArray SPS = fileJson.getJSONArray("SPS");
                for (int n = 0 ; n < SPS.size() ; n++){
                    JSONObject SP = SPS.getJSONObject(n);
                    String SPBM = SP.getString("SPBM");
                    String XMMC = SP.getString("XMMC");
                    String SL = SP.getString("SL");
                    List<String> taxRates = hxinvoiceDao.getTaxRateByTaxItemCode(SPBM);
                    if(!taxRates.contains(SL)){
                        taxRes = taxRes+SPBM+XMMC+"、";
                    }
                    String JE = SP.getString("JE");
                    zje = zje +Double.parseDouble(JE);
                    sj = sj+(Double.parseDouble(JE)*Double.parseDouble(SL));
                }
            }
        }
        if(taxRes.length()==0){
            context.setSuccess(true);
            context.setErrMsg("税率校验成功。发起流程");
        }else{
            taxRes = taxRes.substring(0,taxRes.length()-1)+"税目编码与税率不匹配";
            context.setSuccess(false);
            context.setErrMsg(taxRes);
        }
//        总金额=金额累计和
//        税金 = （金额*税率）累计和
//                不含税金额 = 总金额 - 税金
        System.out.println("总金额"+nf.format(zje));
        System.out.println("税金"+nf.format(sj));
        System.out.println("不含税金额"+nf.format(zje-sj));

        formDataVo.getNewFieldDataMap().put("总金额合计", nf.format(zje));
        formDataVo.getNewFieldDataMap().put("税金合计", nf.format(sj));
        formDataVo.getNewFieldDataMap().put("不含税金额合计", nf.format(zje-sj));

        return context;
    }

    @Override
    public String getPluginId() {
        return HxinvoiceConstants.getPluginId();
    }

    @Override
    public String getFormParse() {
        return "json";
    }

    @Override
    public WorkFlowType[] getTypes() {
        return new WorkFlowType[] { WorkFlowType.onBeforeStart };
    }

    @Override
    public String getId() {
        return "FileDataValidateBeforeStartEvent";
    }

    @Override
    public String getLabel() {
        return "批量数电发票开票附件校验发起前监听";
    }
}
