package com.customization.htsc.cus;

import com.alibaba.fastjson.JSONObject;
import com.engine.core.cfg.annotation.CommandDynamicProxy;
import com.engine.core.interceptor.AbstractCommandProxy;
import com.engine.core.interceptor.Command;
import com.engine.workflow.cmd.requestForm.RequestSubmitCmd;
import com.engine.workflow.entity.requestForm.RequestOperationResultBean;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.htsc.cus.HTUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author:DTY-PC
 * Date:2020/10/21 13:53
 * Description: 自定义流程提交代理
 * Copyright:上海泛微网络科技股份有限公司
 */
@CommandDynamicProxy(target = RequestSubmitCmd.class, desc = "流程提交处理业务")
public class RequestSubCmd extends AbstractCommandProxy<Map<String, Object>> {
    private Log log = LogFactory.getLog(this.getClass());
    @Override
    public Map<String, Object> execute(Command<Map<String, Object>> targetCommand) {
        BaseBean baseBean = new BaseBean();
        //获取到被代理对象
        RequestSubmitCmd requestSubmitCmd = (RequestSubmitCmd) targetCommand;
        HttpServletRequest request = requestSubmitCmd.getRequest();//从request中可以取到参数，不用以前的方法了
        String requestid = Util.null2String(request.getParameter("requestid"));
        String workflowid = Util.null2String(request.getParameter("workflowid"));
        String nodetype = Util.null2String(request.getParameter("nodetype"));
        String src = Util.null2String(request.getParameter("src"));
        //执行标准的业务处理
        Map<String, Object> result = nextExecute(targetCommand);
        RequestOperationResultBean resultBean = (RequestOperationResultBean) result.get("data");
        //联合出差申请创建节点
        String unionwfidNum = Util.null2String(new PropBean().getPropName("unionwfid"));
        String wfidStr = Util.null2String(new PropBean().getWorkflowIdsByUnNumber(unionwfidNum));
        List<String> listWF = Arrays.asList(wfidStr.split(","));
        if (listWF.contains(workflowid) && nodetype.equals("0")) {// 联合出差申请创建节点
            String type = Util.null2String(resultBean.getType());
            if ("SUCCESS".equalsIgnoreCase(type)) {//成功
                HTUtil htUtil = new HTUtil();
                Map<String, Object> paras = new HashMap<>();
                if ("save".equalsIgnoreCase(src) || "dostopc".equalsIgnoreCase(src)) {//保存 终止
                    Map<String, Object> submitParams = resultBean.getSubmitParams();
                    if ("-1".equals(requestid)) {
                        requestid = Util.null2String(submitParams.get("requestid"));//
                    }
                    if ("save".equalsIgnoreCase(src)) {
                        paras.put("status", "DRAFT");
                    } else {
                        paras.put("status", "TERMINATION");
                    }
                    paras.put("requestid", requestid);
                    String res = htUtil.getSaveTripReqString(paras);
                    if (!"".equals(res) && !"-1".equals(res)) {
                        //开始调用保存行程接口 requestid
                        log.info("=====RequestSubCmd======");
                        log.info("=====开始调用保存行程接口, requestid是: "+requestid+"======");
                        boolean isSucc = htUtil.saveTrip(res);
                    }
                }
            }
        }
        //联合差旅报销
        String unionReWfidNum = Util.null2String(new PropBean().getPropName("unionReWfid"));
        String ReWfidStr = Util.null2String(new PropBean().getWorkflowIdsByUnNumber(unionReWfidNum));
        List<String> listREWF = Arrays.asList(ReWfidStr.split(","));
        if (listREWF.contains(workflowid) && "dostopc".equalsIgnoreCase(src)) {
            HTUtil ht = new HTUtil();
            String tableName = ht.getTableNameByWfid(Util.getIntValue(listREWF.get(0)));
            if (!"".equals(tableName)) {
                ResourceComInfo rc = null;
                try {
                    rc = new ResourceComInfo();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                String tableDt4Name = tableName + "_dt4";
                String tableDt5Name = tableName + "_dt5";
                String tableDt6Name = tableName + "_dt6";
                String tableDt7Name = tableName + "_dt7";
                String sql = "select id,lcbh,ngr from " + tableName + " where requestid = " + requestid;
                RecordSet rs = new RecordSet();
                rs.execute(sql);
                rs.next();
                int mianid = Util.getIntValue(rs.getString("id"));//主表id
                String lcbh = Util.null2String(rs.getString("lcbh"));//流程编号
                String ngr = Util.null2String(rs.getString("ngr"));//拟稿人
                String workcode = rc.getWorkcode(ngr);//工号
                sql = "select wjid,fphm,fpdm from " + tableDt4Name + " where mainid = " + mianid;
                rs.execute(sql);
                while (rs.next()) {
                    String wjid = Util.null2String(rs.getString("wjid"));//文件id
                    String fphm = Util.null2String(rs.getString("fphm"));//发票号码
                    String fpdm = Util.null2String(rs.getString("fpdm"));//发票代码
                    Map<String, Object> params = new HashMap<>();
                    params.put("UserCode", workcode);
                    params.put("FileID", wjid);
                    params.put("InvoiceNum", fphm);
                    params.put("InvoiceCode", fpdm);
                    params.put("BillID", lcbh);
                    String res = ht.resetImageAndBill(params);
                    JSONObject resObj = JSONObject.parseObject(res);
                    String code = Util.null2String(resObj.get("ReCode"));//1：成功
                    String msg = Util.null2String(resObj.get("ReMsg"));
                    if (!"1".equals(code)) {
                        baseBean.writeLog("明细4文件ID为：" + wjid + "的重置单据关系失败！-->"+msg);
                    }
                }

                sql = "select wjid,fphm,fpdm from " + tableDt5Name + " where mainid = " + mianid;
                rs.execute(sql);
                while (rs.next()) {
                    String wjid = Util.null2String(rs.getString("wjid"));//文件id
                    String fphm = Util.null2String(rs.getString("fphm"));//发票号码
                    String fpdm = Util.null2String(rs.getString("fpdm"));//发票代码
                    Map<String, Object> params = new HashMap<>();
                    params.put("UserCode", workcode);
                    params.put("FileID", wjid);
                    params.put("InvoiceNum", fphm);
                    params.put("InvoiceCode", fpdm);
                    params.put("BillID", lcbh);
                    String res = ht.resetImageAndBill(params);
                    JSONObject resObj = JSONObject.parseObject(res);
                    String code = Util.null2String(resObj.get("ReCode"));//1：成功
                    String msg = Util.null2String(resObj.get("ReMsg"));
                    if (!"1".equals(code)) {
                        baseBean.writeLog("明细5文件ID为：" + wjid + "的重置单据关系失败！-->"+msg);
                    }
                }

                sql = "select wjid,fphm,fpdm from " + tableDt6Name + " where mainid = " + mianid;
                rs.execute(sql);
                while (rs.next()) {
                    String wjid = Util.null2String(rs.getString("wjid"));//文件id
                    String fphm = Util.null2String(rs.getString("fphm"));//发票号码
                    String fpdm = Util.null2String(rs.getString("fpdm"));//发票代码
                    Map<String, Object> params = new HashMap<>();
                    params.put("UserCode", workcode);
                    params.put("FileID", wjid);
                    params.put("InvoiceNum", fphm);
                    params.put("InvoiceCode", fpdm);
                    params.put("BillID", lcbh);
                    String res = ht.resetImageAndBill(params);
                    JSONObject resObj = JSONObject.parseObject(res);
                    String code = Util.null2String(resObj.get("ReCode"));//1：成功
                    String msg = Util.null2String(resObj.get("ReMsg"));
                    if (!"1".equals(code)) {
                        baseBean.writeLog("明细6文件ID为：" + wjid + "的重置单据关系失败！-->"+msg);
                    }
                }

                sql = "select wjid from " + tableDt7Name + " where mainid = " + mianid;
                rs.execute(sql);
                while (rs.next()) {
                    String wjid = Util.null2String(rs.getString("wjid"));//文件id
                    Map<String, Object> params = new HashMap<>();
                    params.put("UserCode", workcode);
                    params.put("FileID", wjid);
                    params.put("InvoiceNum", "");
                    params.put("InvoiceCode", "");
                    params.put("BillID", lcbh);
                    String res = ht.resetImageAndBill(params);
                    JSONObject resObj = JSONObject.parseObject(res);
                    String code = Util.null2String(resObj.get("ReCode"));//1：成功
                    String msg = Util.null2String(resObj.get("ReMsg"));
                    if (!"1".equals(code)) {
                        baseBean.writeLog("明细7文件ID为：" + wjid + "的重置单据关系失败！-->"+msg);
                    }
                }

            }
        }
        return result;
    }
}
