package weaver.interfaces.htsc.payment;


import org.apache.commons.collections4.CollectionUtils;
import weaver.conn.RecordSet;
import weaver.general.GCONST;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.RequestManager;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 拟稿和费用会计节点,拉取付款明细费用承担部门,赋值到支付明细的费用承担部门
 * <p>
 * 总部付款流程
 * <p>
 * 通用,it,工会
 */
public class SaveFycdbmForPaymentAction implements Action {

	@Override
	public String execute(RequestInfo requestInfo) {
		RequestManager re = requestInfo.getRequestManager();
		String tableName = re.getBillTableName();
		String rid = requestInfo.getRequestid();
		String dt8 = tableName + "_DT8";
		String dt9 = tableName + "_DT9";
		String dt11 = tableName + "_DT11";
		try {
			RecordSet rs = new RecordSet();
			String sql = "select t1.fysjcdbm,t2.ID from " + dt8 + " t1\n" +
					"left join " + tableName + " t2\n" +
					"on t2.ID=t1.MAINID\n" +
					"where t2.REQUESTID=" + rid;
			List<String> depts = new ArrayList<>();
			rs.executeQuery(sql);
			String mid = "";
			while (rs.next()) {
				mid = Util.null2String(rs.getString("ID"));
				depts.add(Util.null2String(rs.getString("fysjcdbm")));
			}
			if (CollectionUtils.isEmpty(depts)) {
				//明细8没有数据,则查找明细9数据
				sql = "select t1.fysjcdbm,t2.ID from " + dt9 + " t1\n" +
						"left join " + tableName + " t2\n" +
						"on t2.ID=t1.MAINID\n" +
						"where t2.REQUESTID=" + rid;
				rs.executeQuery(sql);
				while (rs.next()) {
					mid = Util.null2String(rs.getString("ID"));
					depts.add(Util.null2String(rs.getString("fysjcdbm")));
				}
			}
			String deptStr = depts.stream().distinct().filter(dept -> !"".equals(dept)).collect(Collectors.joining(","));
			String szjg = "";
			if (CollectionUtils.isNotEmpty(depts)) {
				String firstDept = depts.get(0);
				String _sql = "select id from ( select t.* from HrmDepartment t start with t.id=( select a.id from HRMDEPARTMENT a,HRMRESOURCE b where a.id=b.DEPARTMENTID and b.id=?) connect by prior t.supdepid=t.id) where SUPDEPID is null or SUPDEPID=0";
				rs.executeQuery(_sql, firstDept);
				if (rs.next()) {
					szjg = Util.null2String(rs.getString("id"));
				} else {
					szjg = "728"; //没有对应的则默认给华泰总部
				}
			}
			String updateSql = "update " + dt11 + " set fycdbm=?,szjg=? where MAINID=?";
			rs.executeQuery(updateSql, deptStr, szjg, mid);
			return SUCCESS;
		} catch (Exception e) {
			requestInfo.getRequestManager().setMessageid("11111111");
			requestInfo.getRequestManager().setMessagecontent("节点后附加操作异常,请联系管理员!");
			e.printStackTrace();
			return Action.FAILURE_AND_CONTINUE;
		}
	}

	public static void main(String[] args) {
		GCONST.setRootPath("WEB-INF/");
		GCONST.setServerName("ecology");

		SaveFycdbmForPaymentAction action = new SaveFycdbmForPaymentAction();
		RequestManager requestManager = new RequestManager();
		requestManager.setBilltablename("formtable_main_171");
		requestManager.setBillid(785);

		RequestInfo requestInfo = new RequestInfo();
		requestInfo.setRequestManager(requestManager);
		action.execute(requestInfo);
	}
}
