package weaver.interfaces.htsc.payment;

import com.alibaba.fastjson.JSONObject;
import com.engine.htsc.payment.enums.ReiState;
import com.engine.htsc.trip.util.WeaverUtil;
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.Util;
import weaver.interfaces.htsc.cus.HTUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.RequestManager;

import java.util.*;
import java.util.stream.Collectors;

/**
 * k1810012
 * 同步给华泰差旅报销信息
 * 拟稿前(退回) RETURN
 * 拟稿后 RUNNING
 * 办结前 FINISH
 */
public class UpdateTravelReiDataToHt implements Action {

	private String code = "";

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

	@Override
	public String execute(RequestInfo requestInfo) {
		String rid = requestInfo.getRequestid();
		RequestManager rm = requestInfo.getRequestManager();
		String tableName = rm.getBillTableName();

		if (StringUtils.isBlank(code)) {
			rm.setMessageid("500");
			rm.setMessagecontent("同步差旅信息失败,当前节点缺失同步状态的配置,请联系管理员!");
			return FAILURE_AND_CONTINUE;
		}

		try {
			log.info("总部差旅报销流程,同步给华泰差旅报销信息,code:" + code);
			HTUtil ht = new HTUtil();
			Map<String, Object> reqs = getDataToUpdate(tableName, rid, code);
			log.info("总部差旅报销流程,同步给华泰差旅报销信息,入参" + JSONObject.toJSONString(reqs));
			String resp = ht.updateReimbursement(reqs);
			log.info("总部差旅报销流程,同步给华泰差旅报销信息,返回值" + resp);

			return SUCCESS;
		} catch (Exception e) {
			rm.setMessageid("500");
			rm.setMessagecontent("同步差旅信息失败,请联系管理员!");
			return FAILURE_AND_CONTINUE;
		}

	}

	/**
	 * 生成数据
	 *
	 * @return map
	 */
	private Map<String, Object> getDataToUpdate(String tableName, String requestId, String code) {

		RecordSet rs = new RecordSet();

		String sql = "select t1.lcbh,t1.ngrgh,t1.ngsj,t1.glcclc,t1.zfmxhj,t2.lastname,t3.REQUESTNAME\n" +
				"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 =?";
		rs.executeQuery(sql, requestId);

		Map<String, Object> para = new HashMap<>();
		if (rs.next()) {
			para.put("draftTime", Util.null2String(rs.getString("ngsj")));
			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")));
			para.put("title", Util.null2String(rs.getString("REQUESTNAME")));
			para.put("processId", requestId);
			para.put("travelApplicationBizNo", splitTravelBizNo(rs.getString("glcclc")));
			para.put("status", ReiState.getStateByCode(code));
		}

		return para;
	}

	private List<String> splitTravelBizNo(String glcclc) {
		List<String> bizNos = new ArrayList<>();
		if (StringUtils.isNotBlank(glcclc)) {
			bizNos = Arrays.stream(glcclc.split(",")).map(
					i-> "出差申请" + i
			).collect(Collectors.toList());
		}
		return bizNos;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

}
