package weaver.interfaces.htsc.cus;

import com.alibaba.fastjson.JSONObject;
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.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.RequestManager;
import java.util.HashMap;
import java.util.Map;

/**
 * Author:DTY-PC
 * Date:2020/10/21 18:26
 * Description: 提交时调用保存行程接口
 * Copyright:上海泛微网络科技股份有限公司
 */
public class SaveTrip4SubAction extends BaseBean implements Action {

	private final Log log = LogFactory.getLog(SaveTrip4SubAction.class.getName());

	public String status; //自定义参数

	public String getStatus() {
		return status;
	}

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

	@Override
	public String execute(RequestInfo requestInfo) {
		String requestid = Util.null2String(requestInfo.getRequestid());
		RequestManager requestManager = requestInfo.getRequestManager();
		try {
			HTUtil htUtil = new HTUtil();
			Map<String, Object> para = new HashMap<>();
			para.put("requestid", requestid);
			para.put("status", this.getStatus());
			RecordSet rs = new RecordSet();
			String tableName = requestManager.getBillTableName(); //表名
			rs.executeQuery("select CCQK,LCBH,NGRGH from " + tableName + " where REQUESTID=" + requestid);
			String ccqkValue = "";
			if (rs.next()) {
				ccqkValue = rs.getString("CCQK");
			}
			//取消行程到办结的行程不调用保存行程接口
			if ("3".equals(ccqkValue)) {
				log.info("流程" + requestid + "已被取消");
				//覆盖FINISH
				para.put("status", "TERMINATION");
			}
			//去除通知阅读人
			String res = htUtil.getSaveTripReqString(para);

			log.info("入参: " + res);
			if (!"".equals(res)) {
				if (!"-1".equals(res)) {
					boolean isSucc = htUtil.saveTrip(res);
					if (!isSucc) {
						requestInfo.getRequestManager().setMessageid("1111111");
						requestInfo.getRequestManager().setMessagecontent("调用保存行程接口出错，请联系管理员！");
						writeLog("拼接保存行程接口请求字符串是：" + res);
						return Action.FAILURE_AND_CONTINUE;
					}
				}
			} else {
				requestInfo.getRequestManager().setMessageid("1111111");
				requestInfo.getRequestManager().setMessagecontent("调用保存行程接口出错，请联系管理员！");
				writeLog("拼接保存行程接口请求字符串是：" + res);
				return Action.FAILURE_AND_CONTINUE;
			}
		} catch (Exception e) {
			e.printStackTrace();
			requestInfo.getRequestManager().setMessageid("1111111");
			requestInfo.getRequestManager().setMessagecontent("调用保存行程接口捕获异常，请联系管理员！");
			return Action.FAILURE_AND_CONTINUE;
		}
		return Action.SUCCESS;
	}
}
