package com.engine.htsc.trip.web;

import com.engine.common.util.ParamUtil;
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.BaseBean;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

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


	@GET
	@Path("/saveAgain")
	@Produces(MediaType.TEXT_PLAIN)
	public String saveAgain(@Context HttpServletRequest request,
						  @Context HttpServletResponse response) {
		Map<String, Object> paras = ParamUtil.request2Map(request);
		String requestIdList = Util.null2String(Util.null2String(paras.get("requestIdList")));
		String tableName = Util.null2String(Util.null2String(paras.get("tableMain")));
		String status = Util.null2String(Util.null2String(paras.get("status")));
		String[] requestIdLists = requestIdList.split(",");
		for(String requestid:requestIdLists) {
			try {
				HTUtil htUtil = new HTUtil();
				Map<String, Object> para = new HashMap<>();
				para.put("requestid", requestid);
				para.put("status", status);
				RecordSet rs = new RecordSet();
				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 + "已被取消");
					return null;
				}
				String res = htUtil.getSaveTripReqString(para);
				if(StringUtils.isNotEmpty(res) && !"-1".equals(res)) {
					boolean saveTrip = htUtil.saveTrip(res);
					if (!saveTrip) {
						log.info("流程" + requestid + "写入失败");
					}
				}
				log.info("入参: " + res);
			} catch (Exception e) {
				e.printStackTrace();
				return Action.FAILURE_AND_CONTINUE;
			}
		}
		return Action.SUCCESS;
	}
}
