package weaver.interfaces.htsc.cus;

import com.engine.htsc.trip.util.WeaverUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.*;

/**
 * Author:qlk
 * Date:2020/11/17 20:14
 * Description: 差旅报销
 * Copyright:上海泛微网络科技股份有限公司
 */
public class UpdateReimbursement4SubAction extends BaseBean implements Action {
    public String status; //自定义参数

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    @Override
    public String execute(RequestInfo requestInfo) {
        try {
            HTUtil htUtil = new HTUtil();
            Map<String, Object> para = new HashMap<>();
            String requestid = Util.null2String(requestInfo.getRequestid());//请求ID
            String requestName = Util.null2String(requestInfo.getRequestManager().getRequestname());
            String tableName = Util.null2String(htUtil.getTableNameByRequestid(requestid,"0"));//主表名
            String sql = "select lcbh,ngrgh,ngrq,glcclc,zfmxhj,lastname from "+tableName+" t1\n" +
                    " left join hrmresource t2\n" +
                    " on t1.ngr=t2.id\n" +
                    " where requestid=" + requestid;
            RecordSet rs = new RecordSet();
            List<String> bizNos = new ArrayList<>();
            String glcclc = "";
            String ccqk = "";
            rs.execute(sql);
            if (rs.next()) {
                para.put("draftTime", Util.null2String(rs.getString("ngrq")));
                para.put("drafterId", Util.null2String(rs.getString("ngrgh")));
                para.put("reimbursementBizNo", Util.null2String(rs.getString("lcbh")));
                para.put("drafterName", Util.null2String(rs.getString("lastname")));
                para.put("totalAmount", Util.null2String(rs.getString("zfmxhj")));
                glcclc = WeaverUtil.getLcBhbyrequestId(rs.getString("glcclc"));
                if (!"".equals(glcclc)) {
                    bizNos = Arrays.asList(glcclc.split(","));
                }
            }
            para.put("travelApplicationBizNo", bizNos);
            writeLog("UpdateReimbursement4SubAction参数travelApplicationBizNo", bizNos);
            para.put("title", requestName);
            para.put("processId", requestid);
            para.put("status", status);
            //调用updateReimbursement方法
            String res = htUtil.updateReimbursement(para);
            writeLog("UpdateReimbursement4SubAction接口调用返回值" + res);
            if ("".equals(res)) { //token校验
                requestInfo.getRequestManager().setMessageid("111111");
                requestInfo.getRequestManager().setMessagecontent("调用保存行程接口出错，请联系管理员！");
                return Action.FAILURE_AND_CONTINUE;
            }
        }catch (Exception e){
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("更新差旅报销接口捕获异常，e="+e.getMessage());
            return Action.FAILURE_AND_CONTINUE;
        }
        return Action.SUCCESS;
    }

}
