package weaver.interfaces.htsc.cus;

import com.alibaba.fastjson.JSONObject;
import com.engine.htsc.payment.util.PayUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSetTrans;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * 报表查询字段更新
 * 更新表单：
 * 1.投行项目code（cl）
 * 2.投行项目name（cl）
 * 3.出发地，目的地
 */
public class UpdateReportDt4Action extends BaseBean implements Action {
	private final Log log = LogFactory.getLog(this.getClass());

	@Override
	public String execute(RequestInfo requestInfo) {
		RecordSetTrans rst = new RecordSetTrans();
		RecordSetTrans rs = new RecordSetTrans();
		rst.setAutoCommit(false);
		String requestid = Util.null2String(requestInfo.getRequestid());//流程请求ID
		String tableName = Util.null2String(requestInfo.getRequestManager().getBillTableName());//表名
		try {
			//1.获取xmjc
			String xmjc = "";
			String mainid = "";
			Map<String, Object> parms = new HashMap<>();
			String getXmjcSql = "select id,xmjc from " + tableName + " where requestid = ?";
			rst.executeQuery(getXmjcSql, requestid);
			if (rst.next()) {
				xmjc = Util.null2String(rst.getString("xmjc"));
				mainid = Util.null2String(rst.getString("id"));
				parms.put("projectId", xmjc);
			}
			log.info("获取项目简称" + parms.toString());
			//3.出发地，目的地
			rst.executeQuery("select id,cfdgn,mddgn from " + tableName + "_dt4 where mainid=?", mainid);
			log.info("select id,cfdgn,mddgn from " + tableName + "_dt4 where mainid=?" + mainid);
			String id;
			String cfdgn;
			String mddgn;
			String cfdbb;
			String mddbb;
			while (rst.next()) {
				id = Util.null2String(rst.getString("id"));
				log.info("明细4id，" + id);
				if (!"".equals(id)) {
					cfdgn = Util.null2String(rst.getString("cfdgn"));
					mddgn = Util.null2String(rst.getString("mddgn"));
					cfdbb = PayUtil.getProvinceAndCityNameByCode(cfdgn);
					mddbb = PayUtil.getProvinceAndCityNameByCode(mddgn);
					log.info("明细4cfdbb，" + cfdbb);
					log.info("明细4mddbb，" + mddbb);
					rs.executeUpdate("update " + tableName + "_dt4 set cfdbb=?,mddbb=? where id=?",
							cfdbb, mddbb, id);
				}
			}
			rs.commit();
			String res = new HTUtil().getProjectById(parms);
			if ("".equals(res)) {
				log.info("返回结果为空" + res);
				return null;
			}
			//2.返回结果解析
			JSONObject jsonObject = JSONObject.parseObject(res);
			log.info("获取返回结果" + jsonObject);
			String code = jsonObject.getString("code");//投行项目编号
			String shortName = jsonObject.getString("shortName");

			String updateMain = "update " + tableName + " set txxmjc = ?,txxmbh = ? where requestid = ?";
			rst.executeUpdate(updateMain, shortName, code, requestid);
			log.info("更新主表语句" + updateMain);
			rst.commit();
		} catch (Exception e) {
			e.printStackTrace();
			// 事务回滚
			rs.rollback();
			rst.rollback();
			requestInfo.getRequestManager().setMessageid("111111");
			requestInfo.getRequestManager().setMessagecontent("获取投行项目简称和编号失败");
		}

		return Action.SUCCESS;
	}
}
