package weaver.interfaces.htsc.cus;

import com.alibaba.fastjson.JSONObject;
import com.engine.htsc.payment.util.HttpUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.general.GCONST;
import weaver.general.Util;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.RequestManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 保存行程信息-出差返回节点-出差返回情况选择差旅取消,配置在节点后
 */
public class SaveTrip4Cancel implements Action {

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

	@Override
	public String execute(RequestInfo requestInfo) {
		RequestManager requestManager = requestInfo.getRequestManager();
		String tableName = requestManager.getBillTableName(); //表名
		int requestId = requestManager.getRequestid(); //流程id
		RecordSet rs = new RecordSet();
		rs.executeQuery("select CCQK,LCBH,NGRGH from " + tableName + " where REQUESTID=" + requestId);
		String ccqkValue = "";
		String requestMark = "";
		String userCode = "";
		if (rs.next()) {
			ccqkValue = Util.null2String(rs.getString("CCQK"));
			requestMark = Util.null2String(rs.getString("LCBH"));
			userCode = Util.null2String(rs.getString("NGRGH"));
		}
		log.info("出差情况状态@ccqkValue=" + ccqkValue + "@");
		if ("3".equals(ccqkValue)) {
			log.info("保存行程信息-出差返回节点-出差返回情况选择差旅取消");
			log.info("SaveTrip4Cancel@reqeuestId=" + requestId + "@");
			try {
				Map<String, Object> params = new HashMap<>();
				List<String> travelApplicationBizNoList = new ArrayList<>();
				travelApplicationBizNoList.add(requestMark);
				params.put("travelApplicationBizNoList", travelApplicationBizNoList);
				params.put("userCode", userCode);
				HTUtil htUtil = new HTUtil();
				String token = htUtil.getAppToken();
				if (StringUtils.isEmpty(token)) {
					requestInfo.getRequestManager().setMessageid("500");
					requestInfo.getRequestManager().setMessagecontent("调用取消行程接口出差，获取第三方token失败");
					return Action.FAILURE_AND_CONTINUE;
				}
				Map<String, String> headers = new HashMap<>();
				headers.put("JTXT-AUTH-TOKEN", token);
				headers.put("JTXT-AUTH-TYPE", "APP");
				String cancelJourneyUrl = Util.null2String(new PropBean().getPropName("cancelJourney"));
				log.info("取消行程请求参数@" + params + "@");
				String response = HttpUtil.postApplicationJson(cancelJourneyUrl, params, headers);
				log.info("取消行程返回参数@" + response + "@");
				JSONObject jsonObject = JSONObject.parseObject(response);
				String result = (String) jsonObject.get("result");
				if ("success".equals(result)) {
					log.info("行程已取消");
					return SUCCESS;
				} else {
					requestInfo.getRequestManager().setMessageid("1111111");
					requestInfo.getRequestManager().setMessagecontent("调用取消行程接口出差，请联系管理员！");
					return Action.FAILURE_AND_CONTINUE;
				}
			} catch (Exception e) {
				e.printStackTrace();
				requestInfo.getRequestManager().setMessageid("500");
				requestInfo.getRequestManager().setMessagecontent("调用取消行程接口捕获异常，请联系管理员！" + e);
				return Action.FAILURE_AND_CONTINUE;
			}
		}
		return SUCCESS;
	}

}
