package weaver.interfaces.htsc.payment;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.engine.htsc.payment.pojo.*;
import com.engine.htsc.payment.service.impl.HeadPaymentBudgetCommand;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.resource.ResourceComInfo;
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.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * k1810012
 * 预付款流程,办结节点前
 * 调用接口:
 * 1.预占
 * 2.实占
 * 3.核算
 */
public class PrePaymentInterfaceInvoke implements Action {

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

	@Override
	public String execute(RequestInfo requestInfo) {
		RequestManager requestManager = requestInfo.getRequestManager();
		String requestId = requestInfo.getRequestid();
		String tableName = requestManager.getBillTableName();
		log.info("预付款流程,办结前对接预算核算接口");
		try {
			doBudgetCost(requestId, tableName);
		} catch (Exception e) {
			requestManager.setMessagecontent("预算核算对接失败!请联系管理员:" + e);
			requestManager.setMessageid("5000");
			return FAILURE_AND_CONTINUE;
		}
		return SUCCESS;
	}

	public void doBudgetCost(String requestId, String tableName) throws Exception{
			HeadPaymentBudgetCommand command = new HeadPaymentBudgetCommand();
			log.info(requestId + "@预算开始");
			String submitResp = command.submit(requestId, "3");
			log.info("执行预算预占,返回值@" + submitResp);
			String finishResp = command.finish(requestId);
			log.info("执行预算实占,返回值@" + finishResp);
			log.info(requestId + "@核算开始");
			String hsResp = doCost(requestId, tableName);
			log.info("执行核算,返回值@" + hsResp);
	}

	/**
	 * 预付款核算
	 *
	 * @param requestNo 流程id
	 * @param tableName 流程表单
	 * @return 处理核算的返回值
	 */
	public String doCost(String requestNo, String tableName) throws Exception {
		RecordSet rs = new RecordSet();
		//获取主表数据
		String mainSql = "SELECT t1.SFQDZ,t1.REQUESTID,t1.NGR,t1.SZBM,t1.LCBH,t1.REQUESTID,t1.DJZS,t1.NGSJ,t2.REQUESTNAME\n" +
				"FROM " + tableName + " t1\n" +
				"LEFT OUTER JOIN WORKFLOW_REQUESTBASE t2 on t1.REQUESTID=t2.REQUESTID\n" +
				"WHERE (t1.REQUESTID=?)";
		log.info(String.format("mainSql= %s; requestNo= %s; tableName= %s", mainSql, requestNo, tableName));
		ResourceComInfo rc = new ResourceComInfo();
		DepartmentComInfo dc = new DepartmentComInfo();
		CostAccountDto costAccountDto = new CostAccountDto();

		rs.executeQuery(mainSql, requestNo);
		if (rs.next()) {
			String ngr = Util.null2String(rs.getString("NGR"));
			String bm = Util.null2String(rs.getString("SZBM")); //部门
			String requestMark = Util.null2String(rs.getString("LCBH"));
			String requestId = Util.null2String(rs.getString("REQUESTID"));
			String fjzs = Util.null2String(rs.getString("DJZS")); //附件张数
			String ngrq = Util.null2String(rs.getString("NGSJ")); //拟稿日期
			String requestName = Util.null2String(rs.getString("REQUESTNAME"));
			String SFQDZ = Util.null2String(rs.getString("SFQDZ"));

			costAccountDto.setElectronicBill(SFQDZ);
			costAccountDto.setType("htfk"); //预付款费用核算类别
			costAccountDto.setBz2("0");
			costAccountDto.setNgrDept(dc.getDepartmentmark(bm));
			costAccountDto.setNgrDeptCode(dc.getDepartmentCode(bm));
			costAccountDto.setNgrName(rc.getWorkcode(ngr));
			costAccountDto.setRequestMark(requestMark);
			costAccountDto.setRequestId(requestMark); //djid传流程编码,流程id总部不需要
			costAccountDto.setAttachmentNum(fjzs);
			costAccountDto.setLocationName("");
			costAccountDto.setTitle(requestName);
			costAccountDto.setCurrency("RMB");
			costAccountDto.setCreateDate(ngrq);
			costAccountDto.setReimbursementWay("");
			costAccountDto.setPayOrderNo(requestMark); //总部付款付款单号直接传流程编码
		} else {
			log.info(String.format("doCost方法没有找到主表数据, %s", mainSql));
			throw new Exception("doCost方法没有找到主表数据");
		}

		//获取明细11,支付信息
		String dt11Sql = "select t2.YXZFJE,t2.XJZFJE,t2.KHXMC,t2.YXZH,t2.SKDW\n" +
				"FROM " + tableName + "  t1\n" +
				"left join " + tableName + "_DT11 t2 on t1.ID=t2.MAINID\n" +
				"WHERE(t1.REQUESTID=?)";
		log.info(String.format("dt11Sql= %s; requestNo= %s; tableName= %s", dt11Sql, requestNo, tableName));
		List<PayDetail> payDetailList = new ArrayList<>();

		rs.executeQuery(dt11Sql, requestNo);
		while (rs.next()) {
			PayDetail payDetail = new PayDetail();
			String payByBank = Util.null2String(rs.getString("YXZFJE"));
			String payByCash = Util.null2String(rs.getString("XJZFJE"));
			String bankName = Util.null2String(rs.getString("KHXMC"));
			String bankAccount = Util.null2String(rs.getString("YXZH"));
			String SKDW = Util.null2String(rs.getString("SKDW"));
			payDetail.setPayByBank(payByBank);
			payDetail.setPayByCash(payByCash);
			payDetail.setBankName(bankName);
			payDetail.setBankAccount(bankAccount);
			payDetail.setEmployeeDept("");
			payDetail.setEmployeeName("");
			payDetail.setPayee(SKDW);
			payDetailList.add(payDetail);
		}
		costAccountDto.setPayDetails(payDetailList);

		//获取预算明细12
		String dt12Sql = "SELECT t2.YSKM,t2.YSKMMC,t2.HJKM,t2.FYSJCDBM,t2.JE,t2.SY,t2.ZHRMB \n" +
				"FROM " + tableName + "  t1\n" +
				"left join " + tableName + "_DT12 t2 on t1.ID=t2.MAINID\n" +
				"WHERE t1.REQUESTID=? " +
				" order by t2.ID";
		List<KjDetail> kjDetailList = new ArrayList<>();

		rs.executeQuery(dt12Sql, requestNo);
		while (rs.next()) {
			KjDetail kjDetail = new KjDetail();
			BudgetData budgetData = new BudgetData(); //预算科目
			SubjectData subjectData = new SubjectData(); //会计科目

			String fysjcdbm = Util.null2String(rs.getString("FYSJCDBM")); //费用承担部门
			String je = Util.null2String(rs.getString("JE"));
			String zhrmb = Util.null2String(rs.getString("ZHRMB"));
			String sy = Util.null2String(rs.getString("SY"));
			String hjkmmc = Util.null2String("");
			String hjkmbm = Util.null2String(rs.getString("HJKM")); //会计科目编码
			subjectData.setItemCode(hjkmbm);
			subjectData.setItemName(hjkmmc);

			String dyyskm = Util.null2String(rs.getString("YSKM")); //预算科目编码
			String dyysmc = Util.null2String(rs.getString("YSKMMC")); //预算科目名称
			budgetData.setRelationCode(hjkmbm);
			budgetData.setItemCode(dyyskm);
			budgetData.setItemName(dyysmc);

			kjDetail.setExpenseDeptCode(dc.getDepartmentCode(fysjcdbm));
			kjDetail.setExpenseDept(dc.getDepartmentmark(fysjcdbm));
			kjDetail.setExpenseBadge("");
			kjDetail.setExpensePerson("");
			kjDetail.setSubjectData(subjectData);
			kjDetail.setBudgetData(budgetData);
			kjDetail.setPriceRMB(zhrmb);
			kjDetail.setMoney(zhrmb);
			kjDetail.setProjectCode("");
			kjDetail.setProject("");
			kjDetail.setReason(sy);
			kjDetailList.add(kjDetail);
		}
		costAccountDto.setKjDetails(kjDetailList);

		String costAccountDtoJsonStr = JSON.toJSONString(costAccountDto);
		@SuppressWarnings("unchecked")
		Map<String, Object> stringObjectMap = JSONObject.parseObject(costAccountDtoJsonStr);
		HTUtil ht = new HTUtil();
		log.info("开始执行核算接口");
		log.info("PrePaymentInterfaceInvoke核算入参@request=\n" + stringObjectMap + "@");
		String response = ht.saveFydj(stringObjectMap);
		log.info("PrePaymentInterfaceInvoke核算返回@response=\n" + response + "@");
		return response;
	}

}
