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

import com.alibaba.fastjson.JSONObject;
import com.engine.htsc.payment.dao.BudgetDao;
import com.engine.htsc.payment.pojo.BudgetList;
import com.engine.htsc.payment.pojo.BxBudgetDTO;
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 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 java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.UUID;

public class HeadClBxBudgetStrategy implements BudgetStrategy<BxBudgetDTO> {

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

    private String ZSYSKM = "8601050303";
    private String YSYSKM = "8601050301";
    private String QTYSKM = "8601050302";

    @Override
    public BxBudgetDTO buildNewBudgetParamFromWorkflowDB(String requestNo, String billStatus) throws Exception {
        log.info("====差旅报销流程预算开始");
        log.info("====差旅报销流程预算流程id@" + requestNo + "@");
        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);
        log.info("====差旅报销流程表名@" + table + "@");
        BudgetDao budgetDao = new BudgetDao();
        String sql = budgetDao.mainBXFieldParam(table, requestNo);
        log.info("查询差旅报销SQL为" + sql);
        rs.executeQuery(sql);
        BxBudgetDTO budgetDTO = new BxBudgetDTO();
        String lcbh = "";
        String requestname = "";
        String ngr = "";
        String mainId = "";
        if (rs.next()) {
            lcbh = Util.null2String(rs.getString("lcbh"));
            ngr = Util.null2String(rs.getString("ngr"));
            mainId = Util.null2String(rs.getString("id"));
            requestname = Util.null2String(rs.getString("requestname"));
        }
        budgetDTO.setBillNum(requestNo);
        budgetDTO.setBillName(lcbh);
        budgetDTO.setWorkFlowNum(lcbh);
        budgetDTO.setBillStatus(billStatus);
        budgetDTO.setBillTitle(requestname);
        budgetDTO.setBizDate(df.format(LocalDateTime.now()));
        budgetDTO.setPesonName(resourceComInfo.getLastname(ngr));
        //获取明细表 dt9和
        String yskmTable = table + "_dt9";
        String clbxYskmTableFieldParam = budgetDao.clbxBBYskmTableFieldParam(yskmTable, mainId);
        rs.executeQuery(clbxYskmTableFieldParam);
        //uuid现在放在循环外
        String uuid = UUID.randomUUID().toString();
        int i = 1;
        ArrayList<BudgetList> list = new ArrayList<>();
        while (rs.next()) {
            String fysjcdbm = Util.null2String(rs.getString("fycdbm")); //费用承担部门
            String JSHJJE = Util.null2String(rs.getString("jshjje"));
            String BHSJE = Util.null2String(rs.getString("bhsje"));
            String SJ = Util.null2String(rs.getString("sj"));
            String qzzsbhsje = Util.null2String(rs.getString("qzzsbhsje"));
            String zssj = Util.null2String(rs.getString("zssj"));
            String lkysbhsje = Util.null2String(rs.getString("lkysbhsje"));
            String lkyssj = Util.null2String(rs.getString("lkyssj"));
            String qt = Util.null2String(rs.getString("qt"));

            BudgetList budgetListZS = new BudgetList();
            budgetListZS.setDeptNum(departmentComInfo.getDepartmentCode(fysjcdbm));    //费用承担部门编号
            budgetListZS.setBgItem(PayUtil.superBudgetSubject(ZSYSKM)); //项目编码
            budgetListZS.setItemEntryNum(ZSYSKM);   //明细项目编码
            budgetListZS.setBgElement("0101");    //费用类型编码
            budgetListZS.setBillEntryID(uuid + "#" + i);
            Float amount = Float.parseFloat(qzzsbhsje) + Float.parseFloat(zssj);
            budgetListZS.setAmount(amount.toString());//金额
            budgetListZS.setNoTaxAmount(qzzsbhsje); // 不含价税金额
            budgetListZS.setTaxAmount(zssj); //税金
            list.add(budgetListZS);
            i++;
            BudgetList budgetListYS = new BudgetList();
            budgetListYS.setDeptNum(departmentComInfo.getDepartmentCode(fysjcdbm));    //费用承担部门编号
            budgetListYS.setBgItem(PayUtil.superBudgetSubject(YSYSKM)); //项目编码
            budgetListYS.setItemEntryNum(YSYSKM);   //明细项目编码
            budgetListYS.setBgElement("0101");    //费用类型编码
            budgetListYS.setBillEntryID(uuid + "#" + i);
            Float amountYS = Float.parseFloat(lkysbhsje) + Float.parseFloat(lkyssj);
            budgetListYS.setAmount(amountYS.toString());//金额
            budgetListYS.setNoTaxAmount(lkysbhsje); // 不含价税金额
            budgetListYS.setTaxAmount(lkyssj); //税金
            list.add(budgetListYS);
            i++;
            BudgetList budgetListQT = new BudgetList();
            budgetListQT.setDeptNum(departmentComInfo.getDepartmentCode(fysjcdbm));    //费用承担部门编号;    //费用承担部门编号
            budgetListQT.setBgItem(PayUtil.superBudgetSubject(QTYSKM)); //项目编码
            budgetListQT.setItemEntryNum(QTYSKM);   //明细项目编码
            budgetListQT.setBgElement("0101");    //费用类型编码
            budgetListQT.setBillEntryID(uuid + "#" + i);
            budgetListQT.setAmount(qt);//金额
            budgetListQT.setNoTaxAmount(qt); // 不含价税金额
            budgetListQT.setTaxAmount("0"); //税金
            list.add(budgetListQT);
            i++;
        }
        budgetDTO.setList(list); //分录汇总
        return budgetDTO;
    }

    @Override
    public String queryBudgetSqlFromDetailTable(String bz, String table, String detailId) {
        String sql = "";
//        if ("0".equals(bz)) {
//            //dt8
//            sql = "select FYCDBMFZR,FYSJCDBM,YSKM,BHSJE,SJ,JSHJJE from " + table + "_DT8 where MAINID=" + detailId;
//        } else if ("1".equals(bz)) {
//            //dt9
//            sql = "select FYCDBMFZR,FYSJCDBM,YSKM,RMBJE from " + table + "_DT9 where MAINID=" + detailId;
//        }
        sql = "select FYCDBMFZR,FYSJCDBM,YSKM,RMBJE from " + table + "_DT9 where MAINID=" + detailId;

        return sql;
    }

    @Override
    public BxBudgetDTO 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
        BxBudgetDTO budgetDTO = null;
        try {
            budgetDTO = JSONObject.parseObject(lastOperateBudgetParam, BxBudgetDTO.class);
        } catch (Exception e) {
            new BizUtils().log(e.getMessage());
        }
        return budgetDTO;
    }
}
