package com.customization.htsc.cus;

import com.engine.core.cfg.annotation.CommandDynamicProxy;
import com.engine.core.interceptor.AbstractCommandProxy;
import com.engine.core.interceptor.Command;
import com.engine.htsc.trip.util.WeaverUtil;
import com.engine.workflow.cmd.requestForm.RequestSubmitCmd;
import com.engine.workflow.entity.requestForm.RequestOperationResultBean;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.htsc.cus.HTUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by qlk
 */
@CommandDynamicProxy(target = RequestSubmitCmd.class, desc = "报销流程提交处理业务")
public class ReiRequestSubCmd extends AbstractCommandProxy<Map<String, Object>> {
    @Override
    public Map<String, Object> execute(Command<Map<String, Object>> targetCommand) {
        BaseBean baseBean = new BaseBean();
        //联合报销workflowid
        String unionReiWfid = Util.null2String(new PropBean().getPropName("unionReWfid"));
        String wfidstr = Util.null2String(new PropBean().getWorkflowIdsByUnNumber(unionReiWfid));
        List<String> lisWF = Arrays.asList(wfidstr.split(","));
        //获取代理对象
        RequestSubmitCmd requestSubmitCmd = (RequestSubmitCmd) targetCommand;
        HttpServletRequest request = requestSubmitCmd.getRequest();
        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 type = Util.null2String(resultBean.getType()); //流程处理结果
        if (lisWF.contains(workflowid) && "0".equals(nodetype)) { // 联合报销创建节点
            if ("SUCCESS".equalsIgnoreCase(type)) { //成功
                if ("save".equalsIgnoreCase(src) || "dostopc".equalsIgnoreCase(src)) { //保存 终止
                    Map<String, Object> paras = new HashMap<>(); //行程接口请求参数
                    HTUtil htUtil = new HTUtil();
                    baseBean.writeLog("unionReWfid====" + unionReiWfid);
                    baseBean.writeLog("requestid====" + requestid);
                    //状态
                    if ("save".equalsIgnoreCase(src)) {
                        paras.put("status", "DRAFT");
                    } else {
                        paras.put("status", "TERMINATION");
                    }
                    Map<String, Object> submitParams = resultBean.getSubmitParams();
                    if ("-1".equals(requestid)) {
                        requestid = Util.null2String(submitParams.get("requestid"));
                    }
                    //流程id
                    paras.put("processId", requestid);
                    String tableName = htUtil.getTableNameByWfid(Util.getIntValue(lisWF.get(0))); //获取表单名
                    String sql = "select t3.requestname,lcbh,ngrgh,ngrq,glcclc,zfmxhj,lastname from " +tableName+" t1\n" +
                            " left join hrmresource t2\n" +
                            " on t1.ngr=t2.id\n" +
                            " left join workflow_requestbase t3\n" +
                            " on t1.requestid=t3.requestid\n" +
                            " where t1.requestid="+requestid;
                    RecordSet rs = new RecordSet();
                    List<String> bizNos = new ArrayList<>();
                    rs.execute(sql);
                    String glcclc = "";
                    if (rs.next()) {
                        paras.put("draftTime", Util.null2String(rs.getString("ngrq")));
                        paras.put("drafterId", Util.null2String(rs.getString("ngrgh")));
                        paras.put("reimbursementBizNo", Util.null2String(rs.getString("lcbh")));
                        paras.put("drafterName", Util.null2String(rs.getString("lastname")));
                        paras.put("totalAmount", Util.null2String(rs.getString("zfmxhj")));
                        paras.put("title", Util.null2String(rs.getString("requestname")));
                        glcclc = WeaverUtil.getLcBhbyrequestId(rs.getString("glcclc"));
                        if (!"".equals(glcclc)) {
                            bizNos = Arrays.asList(glcclc.split(","));
                        }

                    }
                    // 关联出差流程
                    paras.put("travelApplicationBizNo", bizNos);
                    baseBean.writeLog("paras", "参数====" + paras);
                    //调用updateReimbursement方法
                    String res = htUtil.updateReimbursement(paras);
                    baseBean.writeLog("res", "返回值:" + res);
                }
            }
        }
        return result;
    }
}
