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.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.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.UUID;

public class ZjBxBudgetStrategy implements BudgetStrategy<BxBudgetDTO> {
    private final Log log = LogFactory.getLog(ZjBxBudgetStrategy.class);

    @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));
        //获取明细表 dt8和
        String yskmTable = table + "_dt8";
        String bbYskmTableFieldParam = budgetDao.bxBBYskmTableFieldParam(yskmTable, mainId);
        log.info("====查询报销明细表SQL" + bbYskmTableFieldParam);
        rs.executeQuery(bbYskmTableFieldParam);
        //uuid现在放在循环外
        String uuid = UUID.randomUUID().toString();
        int i = 1;
        ArrayList<BudgetList> list = new ArrayList<>();
        while (rs.next()) {
            BudgetList budgetList = new BudgetList();
            String fysjcdbm = Util.null2String(rs.getString("fysjcdbm")); //费用承担部门
            String yskm = Util.null2String(rs.getString("yskm")); //预算科目
            String yskmid = Util.null2String(rs.getString("yskmid")); //预算科目
            String[] yskmidArray = yskmid.split("_");
            if (null==yskm||"".equals(yskm)){
                yskm = yskmidArray[1];
            }
            String JSHJJE = Util.null2String(rs.getString("jshjje"));
            String BHSJE = Util.null2String(rs.getString("bhsje"));
            String SJ = Util.null2String(rs.getString("sj"));
         //   budgetList.setDeptNum(departmentComInfo.getDepartmentCode(fysjcdbm));    //费用承担部门编号
            budgetList.setDeptNum(fysjcdbm);    //费用承担部门编号

            budgetList.setBgItem(PayUtil.superBudgetSubject(yskm)); //项目编码
            budgetList.setItemEntryNum(yskm);   //明细项目编码
            budgetList.setBgElement("0101");    //费用类型编码
            budgetList.setBillEntryID(uuid);
            budgetList.setAmount(JSHJJE);//金额
            budgetList.setNoTaxAmount(BHSJE); // 不含价税金额
            budgetList.setTaxAmount(SJ); //税金
            i++;
            list.add(budgetList);
        }
        log.info("====查询报销SQL" + JSONObject.toJSONString(list));
        //外币
        String wbYskmTable = table + "_dt9";
        String wbYskmTableFieldParam = budgetDao.bxWBYskmTableFieldParam(wbYskmTable, mainId);
        log.info("====查询报销明细表SQL" + wbYskmTableFieldParam);
        rs.executeQuery(wbYskmTableFieldParam);
        while (rs.next()) {
            BudgetList budgetList = new BudgetList();
            String fysjcdbm = Util.null2String(rs.getString("fysjcdbm")); //费用承担部门
            String yskm = Util.null2String(rs.getString("yskm")); //预算科目
            String yskmid = Util.null2String(rs.getString("yskmid")); //预算科目
            String[] yskmidArray = yskmid.split("_");
            if (null==yskm||"".equals(yskm)){
                yskm = yskmidArray[1];
            }
            String rmbje = Util.null2String(rs.getString("rmbje"));
           // budgetList.setDeptNum(departmentComInfo.getDepartmentCode(fysjcdbm));    //费用承担部门编号
            budgetList.setDeptNum(fysjcdbm);    //费用承担部门编号
            budgetList.setBgItem(PayUtil.superBudgetSubject(yskm)); //项目编码
            budgetList.setItemEntryNum(yskm);   //明细项目编码
            budgetList.setBgElement("0101");    //费用类型编码
            budgetList.setBillEntryID(uuid);
            budgetList.setAmount(rmbje);//金额
            budgetList.setNoTaxAmount(rmbje); // 不含价税金额
            budgetList.setTaxAmount("0"); //税金
            i++;
            list.add(budgetList);
        }
        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;
        }
        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;
    }

}
