package com.engine.htsc.fundbusinesspay.utils;


import com.engine.htsc.payment.util.PayUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.Util;
import weaver.interfaces.htsc.comInfo.PropBean;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

/**
 * k1810012
 * 资金业务付款工具类
 */
public class FundBizUtil {
	private final Log log = LogFactory.getLog(FundBizUtil.class);

	/**
	 * 生成资金业务付款流程编号
	 *
	 * @return string
	 */
	public synchronized String createFundBusinessPayRequestMark() {
		try {
			PropBean pp = new PropBean();
			String workFlowId = pp.getPropName("FundBusinessPayRequestMark"); //流程编号
			if (StringUtils.isBlank(workFlowId)) {
				log.info("配置文件,FundBusinessPayRequestMark没有维护");
				return "";
			}

			RecordSet rs = new RecordSet();
			int formModeId = PayUtil.getFormModeIdByCubeName("uf_fklcbh");
			String sql = "select WORKFLOWNAME,UNNUMBER from workflow_base where isvalid=1 and ID=?";
			rs.executeQuery(sql, workFlowId);
			String name = ""; //流程中文名称
			String unNumber = ""; //流程唯一字符标识
			if (rs.next()) {
				name = rs.getString("WORKFLOWNAME");
				unNumber = rs.getString("UNNUMBER");
			}

			StringBuilder mark = new StringBuilder(); //流程编码
			String modeUuid = ""; //建模同步uuid

			LocalDateTime now = LocalDateTime.now();
			String yM = now.format(DateTimeFormatter.ofPattern("yyyyMM"));
			rs.executeQuery("select id,lsh,MODEUUID from uf_fklcbh where ny = '" + yM + "' and LCLX='" + unNumber + "'");
			if (rs.next()) {
				//更新
				int num = Util.getIntValue(rs.getString("lsh")) + 1;
				int id = Util.getIntValue(rs.getString("id"));
				modeUuid = Util.null2String(rs.getString("MODEUUID"));
				String updateSql = "update uf_fklcbh set lsh=? where id=?";
				rs.executeUpdate(updateSql, num, id);
				mark = new StringBuilder(num + "");
				while (mark.length() < 6) {
					mark.insert(0, "0");
				}
				mark = new StringBuilder(name + now.format(DateTimeFormatter.ofPattern("yyyyMM")) + mark);
			} else {
				String userId = "";
				String userType = "0";
				String createDate = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
				String createTime = now.format(DateTimeFormatter.ofPattern("HH:mm:ss"));
				String insertSql = "insert into uf_fklcbh" +
						"(FORMMODEID,MODEDATACREATER,MODEDATACREATERTYPE,MODEDATACREATEDATE,MODEDATACREATETIME,MODEUUID,LSH,NY,LCLX) \n" +
						"values(?,?,?,?,?,?,?,?,?)";
				String uuid = UUID.randomUUID().toString();
				modeUuid = uuid;
				rs.executeUpdate(insertSql, formModeId, userId, userType, createDate, createTime, uuid, "1", yM, unNumber);
				mark = new StringBuilder(name + now.format(DateTimeFormatter.ofPattern("yyyyMM")) + "000001");
			}

			//建模权限重构
			int dataId = PayUtil.getDataIdByUUID(modeUuid);
			ModeRightInfo modeRightInfo = new ModeRightInfo();
			modeRightInfo.editModeDataShare(1, formModeId, dataId);

			return mark.toString();
		} catch (Exception e) {
			log.error("生成资金业务付款流程编号失败,catch:" + e.getMessage());
			e.printStackTrace();
			return "";
		}

	}


}
