package com.engine.htsc.subpayment.utils;

import com.engine.htsc.branchpayment.pojo.UfCash;
import com.engine.htsc.payment.pojo.*;
import com.engine.htsc.payment.util.BizUtils;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.general.Util;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.htsc.comInfo.PropBean;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * k1810012
 * 子公司工具类
 */
public class SubBizUtil {

  //子公司出纳表
  private final String uf = "uf_zgsfk";

  /**
   * 获取建模表里的数据
   */
  public List<UfCash> getUfCashList(String ids) {
    RecordSet rs = new RecordSet();
    String idJoin = String.join("','", Arrays.asList(ids.split(",")));
    rs.executeQuery("select ID,FKLX,REQUTID,LCBH from " + uf + " where REQUTID in ('" + idJoin + "')");
    List<UfCash> lists = new ArrayList<>();
    while (rs.next()) {
      UfCash ufCash = new UfCash();
      ufCash.setId(Util.null2String(rs.getString("ID")));
      ufCash.setType(Util.null2String(rs.getString("FKLX")));
      ufCash.setRid(Util.null2String(rs.getString("REQUTID")));
      ufCash.setrMark(Util.null2String(rs.getString("LCBH")));
      lists.add(ufCash);
    }
    return lists;
  }

  /**
   * 更新建模预算状态
   */
  public void updateBudgetStatus(String id, String discard) {
    RecordSetTrans rst = new RecordSetTrans();
    rst.setAutoCommit(false);
    try {
      if ("1".equals(discard)) {
        rst.executeUpdate("update " + uf + " set TBDLCYSZT=0 where ID=?", id);
      } else if ("-1".equals(discard)) {
        rst.executeUpdate("update " + uf + " set TBDLCYSZT=2 where ID=?", id);
      }
      rst.executeUpdate("update " + uf + " set FKZT=0 where TBDLCZT=0 and id =?", id);
      rst.commit();
    } catch (Exception e) {
      rst.rollback();
      rst.writeLog("error:" + e.getMessage());
    }
  }

  /**
   * 更新建模核算状态
   */
  public void updateCostStatus(String id, String rid) {
    RecordSetTrans rst = new RecordSetTrans();
    rst.setAutoCommit(false);
    try {
      rst.executeUpdate("update " + uf + " set TBDLCZT=0 where ID=?", id);
      rst.executeUpdate("update " + uf + " set FKZT=0 where TBDLCYSZT=0 and id =?", id);
      rst.executeUpdate("update UF_YFKMIDDLE set SFFKQR=1 where REQID=?", rid);
      //更新
      rst.commit();
    } catch (Exception e) {
      rst.rollback();
      rst.writeLog("error:" + e.getMessage());
    }
  }

  /**
   * 生成报销核算接口参数(子公司)--资管通用报销
   * @param rid 流程id
   * @param lb    lb 调用核算的类型 {@link com.engine.htsc.payment.enums.HsType}
   * @return CostAccountDto
   * @author wan
   */
  public CostAccountDto generateCommonReimbData(String rid, String lb) {
    CostAccountDto costAccDto = new CostAccountDto();
    BizUtils bizUtils = new BizUtils();
    RecordSet rs = new RecordSet();
    String tableName = bizUtils.getTableNameByRequestId(rid);
    rs.executeQuery(" select id,ngr,szbm,lcbh,djzs,ngsj from " + tableName + " where requestid=? ", rid);
    if (rs.next()) {
      try {
        String id = Util.null2String(rs.getString("id"));
        String ngr = Util.null2String(rs.getString("ngr"));
        String szbm = Util.null2String(rs.getString("szbm"));
        String lcbh = Util.null2String(rs.getString("lcbh"));
        String djzs = Util.null2String(rs.getString("djzs"));
        String ngsj = Util.null2String(rs.getString("ngsj")) + ":00";

        DepartmentComInfo deptComInfo = new DepartmentComInfo();
        String szbmmc = deptComInfo.getDepartmentName(szbm);
        String szbmbm = deptComInfo.getDepartmentCode(szbm);

        ResourceComInfo resourceComInfo = new ResourceComInfo();
        String ngrmc = resourceComInfo.getWorkcode(ngr);

        rs.executeQuery(" select requestname from workflow_requestbase where requestid=? ", rid);
        rs.next();
        String requestname = Util.null2String(rs.getString("requestname"));

        costAccDto.setType(lb);
        costAccDto.setNgrDept(szbmmc);
        costAccDto.setNgrDeptCode(szbmbm);
        costAccDto.setNgrName(ngrmc);
        costAccDto.setRequestMark(lcbh);
        costAccDto.setRequestId(lcbh);
        costAccDto.setAttachmentNum(djzs);
        costAccDto.setLocationName("");
        costAccDto.setTitle(requestname);
        costAccDto.setCurrency("RMB");
        costAccDto.setCreateDate(ngsj);
        costAccDto.setReimbursementWay("");
        costAccDto.setPayOrderNo(lcbh);

        List<KjDetail> kjdtList = new ArrayList<>();
        String dt12Sql = " select fysjcdbm,fysjcdr,hjkm,yskm,yskmmc,zhrmb,SY from " + tableName + "_dt12 where mainid=? ";
        rs.executeQuery(dt12Sql, id);
        while (rs.next()) {
          String fysjcdbm = Util.null2String(rs.getString("fysjcdbm"));
          String fysjcdr = Util.null2String(rs.getString("fysjcdr"));
          String hjkm = Util.null2String(rs.getString("hjkm"));
          String yskm = Util.null2String(rs.getString("yskm"));
          String yskmmc = Util.null2String(rs.getString("yskmmc"));
          String zhrmb = Util.null2String(rs.getString("zhrmb"));
          String sy = Util.null2String(rs.getString("SY"));

          String fysjcdbmbm = deptComInfo.getDepartmentCode(fysjcdbm);
          String fysjcdbmmc = deptComInfo.getDepartmentName(fysjcdbm);
          String fysjcdrbh = resourceComInfo.getWorkcode(fysjcdr);
          String fysjcdrmc = resourceComInfo.getLastname(fysjcdr);

          KjDetail kjitem = new KjDetail();
          kjitem.setExpenseDeptCode(fysjcdbmbm);
          kjitem.setExpenseDept(fysjcdbmmc);
          kjitem.setExpenseBadge(fysjcdrbh);
          kjitem.setExpensePerson(fysjcdrmc);

          SubjectData subject = new SubjectData();
          subject.setItemCode(hjkm);
          subject.setItemName(bizUtils.getKjkmmc(hjkm));
          kjitem.setSubjectData(subject);

          BudgetData budget = new BudgetData();
          budget.setRelationCode(hjkm);
          budget.setItemCode(yskm);
          budget.setItemName(yskmmc);
          kjitem.setBudgetData(budget);

          kjitem.setPriceRMB(zhrmb);
          kjitem.setMoney(zhrmb);
          kjitem.setProjectCode("");
          kjitem.setProject("");
          kjitem.setReason(sy);

          kjdtList.add(kjitem);
        }
        costAccDto.setKjDetails(kjdtList);

        List<PayDetail> zfdtList = new ArrayList<>();
        String dt11Sql = " select yxzfje,xjzfje,khxmc,yxzh,bm,ygxm from " + tableName + "_dt11 where mainid=? ";
        rs.executeQuery(dt11Sql, id);
        while (rs.next()) {
          String yxzfje = Util.null2String(rs.getString("yxzfje"));
          String xjzfje = Util.null2String(rs.getString("xjzfje"));
          String khxmc = Util.null2String(rs.getString("khxmc"));
          String yxzh = Util.null2String(rs.getString("yxzh"));
          String bm = Util.null2String(rs.getString("bm"));
          String ygxm = Util.null2String(rs.getString("ygxm"));

          String bmmc = deptComInfo.getDepartmentName(bm);
          String ygxmmc = resourceComInfo.getLastname(ygxm);
          String ygxmwk = resourceComInfo.getWorkcode(ygxm);

          PayDetail payitem = new PayDetail();
          payitem.setPayByBank(yxzfje);
          payitem.setPayByCash(xjzfje);
          payitem.setBankName(khxmc);
          payitem.setBankAccount(yxzh);
          payitem.setEmployeeDept(bmmc);
          payitem.setEmployeeName(ygxmmc);
          payitem.setEmployeeBadge(ygxmwk);

          zfdtList.add(payitem);

        }
        costAccDto.setPayDetails(zfdtList);


      } catch (Exception e) {
        e.printStackTrace();
      }


    }

    return costAccDto;
  }

  /**
   * 生成差旅核算参数(子公司)--资管差旅报销
   *
   * @param rid 流程id
   * @param lb  lb 调用核算的类型
   * @return CostAccountDto
   * @author wan
   */
  public CostAccountDto generateTravelReimbData(String rid, String lb) {
    CostAccountDto costAccDto = new CostAccountDto();
    RecordSet rs = new RecordSet();
    PropBean pb = new PropBean();
    BizUtils bizUtils = new BizUtils();
    String tablename = bizUtils.getTableNameByRequestId(rid);
    rs.executeQuery("select SFQDZ,id,ngr,szbm,lcbh,djzs,ngsj from " + tablename + " where requestid=? ",
            rid);
    if (rs.next()) {
      try {
        String id = Util.null2String(rs.getString("id"));
        String ngr = Util.null2String(rs.getString("ngr"));
        String szbm = Util.null2String(rs.getString("szbm"));
        String lcbh = Util.null2String(rs.getString("lcbh"));
        String djzs = Util.null2String(rs.getString("djzs"));
        String ngsj = Util.null2String(rs.getString("ngsj")) + ":00";
        String SFQDZ = Util.null2String(rs.getString("SFQDZ"));

        DepartmentComInfo deptComInfo = new DepartmentComInfo();
        String szbmmc = deptComInfo.getDepartmentName(szbm);
        String szbmbm = deptComInfo.getDepartmentCode(szbm);

        ResourceComInfo resourceComInfo = new ResourceComInfo();
        String ngrmc = resourceComInfo.getLastname(ngr);

        rs.executeQuery(" select requestname from workflow_requestbase where requestid=? ",
                rid);
        rs.next();
        String requestname = Util.null2String(rs.getString("requestname"));

        costAccDto.setElectronicBill(SFQDZ);
        costAccDto.setType(lb);
        costAccDto.setNgrDept(szbmmc);
        costAccDto.setNgrDeptCode(szbmbm);
        costAccDto.setNgrName(ngrmc);
        costAccDto.setRequestMark(lcbh);
        costAccDto.setRequestId(lcbh);
        costAccDto.setAttachmentNum(djzs);
        costAccDto.setLocationName("");
        costAccDto.setTitle(requestname);
        costAccDto.setCurrency("RMB");
        costAccDto.setCreateDate(ngsj);
        costAccDto.setReimbursementWay("");
        costAccDto.setPayOrderNo(lcbh);

        List<KjDetail> kjdtList = new ArrayList<>();
        String dt12Sql = " select fycdbm,ccr,hjkm,zhrmb  from " + tablename + "_dt11 where mainid=? ";
        rs.executeQuery(dt12Sql, id);
        while (rs.next()) {
          String fycdbm = Util.null2String(rs.getString("fycdbm"));
          String fycdr = Util.null2String(rs.getString("ccr"));
          String hjkm = Util.null2String(rs.getString("hjkm"));
          if (!"".equals(hjkm)) {
            hjkm = hjkm.split("_")[1];
          }
          String zhrmb = Util.null2String(rs.getString("zhrmb"));

          String fysjcdbmbm = deptComInfo.getDepartmentCode(fycdbm);
          String fysjcdbmmc = deptComInfo.getDepartmentName(fycdbm);
          String fysjcdrbh = resourceComInfo.getWorkcode(fycdr);
          String fysjcdrmc = resourceComInfo.getLastname(fycdr);

          KjDetail kjitem = new KjDetail();
          kjitem.setExpenseDeptCode(fysjcdbmbm);
          kjitem.setExpenseDept(fysjcdbmmc);
          kjitem.setExpenseBadge(fysjcdrbh);
          kjitem.setExpensePerson(fysjcdrmc);

          SubjectData subject = new SubjectData();
          subject.setItemCode(hjkm);
          subject.setItemName(bizUtils.getKjkmmc(hjkm));
          kjitem.setSubjectData(subject);

          BudgetData budget = new BudgetData();
          budget.setRelationCode(hjkm);
          String yskmbm = pb.getPropName(hjkm);
          budget.setItemCode(yskmbm);
          budget.setItemName(bizUtils.getYskmmc(yskmbm));
          kjitem.setBudgetData(budget);

          kjitem.setPriceRMB(zhrmb);
          kjitem.setMoney(zhrmb);
          kjitem.setProjectCode("");
          kjitem.setProject("");
          kjitem.setReason("");

          kjdtList.add(kjitem);
        }
        costAccDto.setKjDetails(kjdtList);

        List<PayDetail> zfdtList = new ArrayList<>();
        String dt11Sql = " select yxzfje,xjzfje,khx,yxzh,bm,ccr from " + tablename + "_dt10 where mainid=? ";
        rs.executeQuery(dt11Sql, id);
        while (rs.next()) {
          String yxzfje = Util.null2String(rs.getString("yxzfje"));
          String xjzfje = Util.null2String(rs.getString("xjzfje"));
          String khxmc = Util.null2String(rs.getString("khx"));
          String yxzh = Util.null2String(rs.getString("yxzh"));
          String bm = Util.null2String(rs.getString("bm"));
          String ygxm = Util.null2String(rs.getString("ccr"));

          String bmmc = deptComInfo.getDepartmentName(bm);
          String ygxmmc = resourceComInfo.getLastname(ygxm);
          String ygxmwk = resourceComInfo.getWorkcode(ygxm);

          PayDetail payitem = new PayDetail();
          payitem.setPayByBank(yxzfje);
          payitem.setPayByCash(xjzfje);
          payitem.setBankName(khxmc);
          payitem.setBankAccount(yxzh);
          payitem.setEmployeeDept(bmmc);
          payitem.setEmployeeName(ygxmmc);
          payitem.setEmployeeBadge(ygxmwk);

          zfdtList.add(payitem);

        }
        costAccDto.setPayDetails(zfdtList);


      } catch (Exception e) {
        e.printStackTrace();
      }

    }

    return costAccDto;
  }
}
