package com.engine.htsc.payment.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.engine.htsc.payment.pojo.BudgetDTO;
import com.engine.htsc.payment.pojo.BudgetList;
import com.engine.htsc.payment.service.BudgetStrategy;
import com.engine.htsc.payment.util.BizUtils;
import com.engine.htsc.payment.util.PayUtil;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.resource.ResourceComInfo;

import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.UUID;

/**
 * 总部通用付款预算参数构建
 */
public class HeadCgscPaymentBudgetStrategy implements BudgetStrategy<BudgetDTO> {


	@Override
	public BudgetDTO buildNewBudgetParamFromWorkflowDB(String requestNo, String billStatus) throws Exception {
		RecordSet rs = new RecordSet();
		BizUtils bizUtils = new BizUtils();
		DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		ResourceComInfo resourceComInfo = new ResourceComInfo();
		DepartmentComInfo departmentComInfo = new DepartmentComInfo();
		String table = bizUtils.getTableNameByRequestId(requestNo);
		//预算基本数据信息
		String sql = "select t1.ID,t1.REQUESTID,t1.LCBH,t1.NGR,t2.REQUESTNAME from " + table + " t1\n" +
				"left join WORKFLOW_REQUESTBASE t2\n" +
				"on t1.REQUESTID=t2.REQUESTID\n" +
				"where t1.REQUESTID=?";
		rs.executeQuery(sql, requestNo);
		BudgetDTO budgetDTO = new BudgetDTO();
//		String bz = "0"; // 0 本币
		String detailId = "";
		if (rs.next()) {
			budgetDTO.setBillNum(rs.getString("REQUESTID"));
			budgetDTO.setBizDate(df.format(LocalDateTime.now()));
			budgetDTO.setBillStatus(billStatus);
			budgetDTO.setBillName(rs.getString("LCBH"));
			budgetDTO.setBillTitle(rs.getString("REQUESTNAME"));
			budgetDTO.setPesonName(resourceComInfo.getLastname(rs.getString("NGR")));
			detailId = rs.getString("ID");
		}

		String queryBudgetList = queryBudgetSqlFromDetailTable("0", table, detailId);
		rs.writeLog("requestId:"+requestNo+" 查询付款明细sql:"+queryBudgetList);
		String uuid = UUID.randomUUID().toString();
		int i = 1;
		ArrayList<BudgetList> list = new ArrayList<>();
		rs.executeQuery(queryBudgetList);
		while (rs.next()) {
			BudgetList budgetList = new BudgetList();
			String fysjcdbm = rs.getString("FYSJCDBM");
			String yskm = rs.getString("YSKM");
			String superBudgetCode = PayUtil.getSuperBudgetCode(yskm);
			budgetList.setDeptNum(departmentComInfo.getDepartmentCode(fysjcdbm));
			budgetList.setBgItem(superBudgetCode);
			budgetList.setBgITElement(superBudgetCode);
			budgetList.setBgElement("0101"); //费用类型编码
			budgetList.setBillEntryID(uuid + "#" + i);
			budgetList.setItemEntryNum(yskm);
			//明细8
			String jshjje = Util.null2String(rs.getString("JSHJJE"));
			String bhsje = Util.null2String(rs.getString("BHSJE"));
			String sj = Util.null2String(rs.getString("SJ"));
			jshjje = new DecimalFormat("0.00").format(NumberUtils.toDouble(jshjje));
			budgetList.setAmount(jshjje);
			budgetList.setNoTaxAmount(bhsje);
			budgetList.setTaxAmount(sj);
			if (StringUtils.isNotBlank(jshjje) && !"0".equals(jshjje) && !"0.00".equals(jshjje) && (
					StringUtils.isBlank(bhsje) || "0".equals(bhsje) || "0.00".equals(bhsje))) {
				// 不含价税金额
				budgetList.setNoTaxAmount(jshjje);
			}
			i++;
			list.add(budgetList);
		}
		budgetDTO.setList(list); //分录汇总
		return budgetDTO;
	}

	@Override
	public String queryBudgetSqlFromDetailTable(String bz, String table, String detailId) {
		String sql = "";
		sql = "select FYCDBMFZR,FYSJCDBM,YSKM,BHSJE,SJ,JSHJJE from " + table + "_DT8 where MAINID=" + detailId;
		return sql;
	}

	@Override
	public BudgetDTO getLastBudgetParamFromModeDB(String requestNo, String itFlag) {
		RecordSet rs = new RecordSet();
		rs.executeQuery("select YSQQCS from UF_YSJLB where ITFLAG=? and LCID=?", itFlag, requestNo);
		String lastOperateBudgetParam = "";
		if (rs.next()) {
			lastOperateBudgetParam = Util.null2String(rs.getString("YSQQCS"));
		}
		//decode
		BudgetDTO budgetDTO = null;
		try {
			budgetDTO = JSONObject.parseObject(lastOperateBudgetParam, BudgetDTO.class);
		} catch (Exception e) {
			new BizUtils().log(e.getMessage());
		}
		return budgetDTO;
	}
}
