package weaver.interfaces.htsc.payment;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.engine.htsc.payment.dao.PaymentDao;
import com.engine.htsc.payment.enums.HsType;
import com.engine.htsc.payment.pojo.*;
import org.apache.commons.collections4.CollectionUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.GCONST;
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;

/**
 * @author K1810012
 * 特殊流程预付款流程
 * 核算接口调用
 */
public class PaymentCostInterface extends BaseBean implements Action {
	@Override
	public String execute(RequestInfo requestInfo) {
		writeLog("====预付款流程节点后核算开始");
		writeLog("====预付款流程节点后核算开始");
		RequestManager requestManager = requestInfo.getRequestManager();
		String tableName = requestManager.getBillTableName();
		int id = requestManager.getBillid();
		String requestId = requestInfo.getRequestid();
		try {
			ResourceComInfo rc = new ResourceComInfo();
			DepartmentComInfo dc = new DepartmentComInfo();
			RecordSet rs = new RecordSet();
			rs.executeQuery("select " +
					" fm.SFQDZ,fm.fklx,fm.NGR,fm.SZBM,fm.LCBH,fm.DJZS,fm.NGSJ,wr.REQUESTNAME " +
					" from " + tableName + " fm\n" +
					"left join WORKFLOW_REQUESTBASE wr\n" +
					"on wr.REQUESTID=fm.REQUESTID where fm.ID=" + id);
			if (rs.next()) {
				writeLog("====数据存在,开始执行核算");
				String fklx = rs.getString("fklx"); //付款类型
				String ngr = Util.null2String(rs.getString("NGR"));
				String bm = Util.null2String(rs.getString("SZBM")); //部门
				String requestMark = Util.null2String(rs.getString("LCBH"));
				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 costAccountDto = new CostAccountDto();
				costAccountDto.setElectronicBill(SFQDZ);
				costAccountDto.setType(HsType.ZBFK.getType()); // lb=htfk
				costAccountDto.setBz2("0");
				costAccountDto.setNgrDept(dc.getDepartmentname(bm));
				costAccountDto.setNgrDeptCode(dc.getDepartmentCode(bm));
				costAccountDto.setNgrName(rc.getLastname(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); //总部付款付款单号直接传流程编码

				//核算入参 支付明细payDetail 取自明细11
				PaymentDao pd = new PaymentDao();
				String getPayInfoFromDt11 = pd.getPayInfoFromDt11(tableName, requestId);
				List<PayDetail> payDetailList = new ArrayList<>();
				rs.executeQuery(getPayInfoFromDt11);
				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"));
					payDetail.setPayByBank(payByBank);
					payDetail.setPayByCash(payByCash);
					payDetail.setBankName(bankName);
					payDetail.setBankAccount(bankAccount);
					payDetail.setEmployeeDept("");
					payDetail.setEmployeeName("");

					payDetailList.add(payDetail);
				}
				if (CollectionUtils.isEmpty(payDetailList)) {
					requestManager.setMessagecontent("流程执行核算接口失败,支付明细数据不能为空!");
					return FAILURE_AND_CONTINUE;
				}
				costAccountDto.setPayDetails(payDetailList);

				//
				//核算入参 会计明细KjDetail 取自明细12
				String subjectInfoFromDt8AndDt12 = pd.getSubjectInfoFromDt8AndDt12(tableName, requestId);
				List<KjDetail> kjDetailList = new ArrayList<>();
				rs.executeQuery(subjectInfoFromDt8AndDt12);
				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(rs.getString("HJKMMC"));
					String hjkmbm = Util.null2String(rs.getString("HJKMBM"));
					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.getDepartmentName(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);
				}
				if (CollectionUtils.isEmpty(kjDetailList)) {
					requestManager.setMessagecontent("流程执行核算接口失败,会计明细数据不能为空!");
					return FAILURE_AND_CONTINUE;
				}
				costAccountDto.setKjDetails(kjDetailList);


				String costAccountDtoJsonStr = JSON.toJSONString(costAccountDto);
				@SuppressWarnings("unchecked")
				Map<String, Object> stringObjectMap = JSONObject.parseObject(costAccountDtoJsonStr);
				writeLog("====ht.saveFydj@request=" + stringObjectMap + "@");
				HTUtil ht = new HTUtil();
				String response = ht.saveFydj(stringObjectMap);
				writeLog("ht.saveFydj@response=" + response + "@");
				if (!"success".equals(response)) {
					requestManager.setMessagecontent("流程执行核算接口失败,接口请求失败,请联系管理员处理!");
					return FAILURE_AND_CONTINUE;
				}
				return SUCCESS;

			} else {
				requestManager.setMessagecontent("流程执行核算接口失败,执行sql出错,请联系管理员处理!");
				return FAILURE_AND_CONTINUE;
			}
		} catch (Exception e) {
			e.printStackTrace();
			requestManager.setMessagecontent("流程执行核算接口失败,接口异常,请联系管理员处理!");
			return FAILURE_AND_CONTINUE;
		}
	}

	public static void main(String[] args) {
		GCONST.setRootPath("WEB-INF/");
		GCONST.setServerName("ecology");
		RequestManager requestManager = new RequestManager();
		requestManager.setBilltablename("FORMTABLE_MAIN_210");
		requestManager.setBillid(304);
		RequestInfo requestInfo = new RequestInfo();
		requestInfo.setRequestid("2207220");
		requestInfo.setRequestManager(requestManager);
		PaymentCostInterface p = new PaymentCostInterface();
		p.execute(requestInfo);
	}
}
