package com.engine.htsc.payment.util;

import com.engine.htsc.common.utils.DoubleUtil;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.htsc.comInfo.PropBean;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

public class StaffCodUtil extends BaseBean {

  private static final String tableName;

  static {
    tableName = PayUtil.getTableNameByUnNumber("ygwwsq");
  }

  public static List<Map<String, String>> getDt5s(String rid) throws Exception {
    RecordSet rs = new RecordSet();
    List<Map<String, String>> resp = new ArrayList<>();
    String sql = String.format("select " +
            "t1.fplx, t1.fpdm, t1.fphm, t1.jshj, t1.xfmc, t1.gfmc, t1.fpfl, t1.fpmc, t1.se, t1.kpsj, t1.yz, t1.pjly, t1.fplxid, t1.wjid, t1.htdelete, t1.fpckid\n" +
            "from %s t1\n" +
            "left join %s t2\n" +
            "on t1.mainid=t2.id\n" +
            "where t2.requestid=?", tableName + "_dt5", tableName);
    rs.executeQuery(sql, rid);
    while (rs.next()) {
      Map<String, String> dt5 = new HashMap<>();
      dt5.put("fplx", Util.null2String(rs.getString("fplx")));
      dt5.put("fpdm", Util.null2String(rs.getString("fpdm")));
      dt5.put("fphm", Util.null2String(rs.getString("fphm")));
      dt5.put("jshj", Util.null2String(rs.getString("jshj")));
      dt5.put("xfmc", Util.null2String(rs.getString("xfmc")));
      dt5.put("gfmc", Util.null2String(rs.getString("gfmc")));
      dt5.put("fpfl", Util.null2String(rs.getString("fpfl")));
      dt5.put("fpmc", Util.null2String(rs.getString("fpmc")));
      dt5.put("se", Util.null2String(rs.getString("se")));
      dt5.put("kpsj", Util.null2String(rs.getString("kpsj")));
      dt5.put("yz", Util.null2String(rs.getString("yz")));
      dt5.put("pjly", Util.null2String(rs.getString("pjly")));
      dt5.put("fplxid", Util.null2String(rs.getString("fplxid")));
      dt5.put("wjid", Util.null2String(rs.getString("wjid")));
      dt5.put("htdelete", Util.null2String(rs.getString("htdelete")));
      dt5.put("fpckid", Util.null2String(rs.getString("fpckid")));
      resp.add(dt5);
    }
    return resp;
  }

  public static List<Map<String, String>> getDt6s(String rid) throws Exception {
    RecordSet rs = new RecordSet();
    List<Map<String, String>> resp = new ArrayList<>();
    String sql = String.format("select " +
            "t1.wjid, t1.fpdm, t1.fphm, t1.kpje, t1.kpf, t1.fpfl, t1.htdelete, t1.fpckid, t1.zt, t1.kpsj, t1.fplx, t1.se, t1.wsje, t1.fplxid, t1.dyfp, t1.sj, t1.zjid, t1.pjly\n" +
            "from %s t1\n" +
            "left join %s t2\n" +
            "on t1.mainid=t2.id\n" +
            "where t2.requestid=?", tableName + "_dt6", tableName);
    rs.executeQuery(sql, rid);
    while (rs.next()) {
      Map<String, String> dt6 = new HashMap<>();
      dt6.put("wjid", Util.null2String(rs.getString("wjid")));
      dt6.put("fpdm", Util.null2String(rs.getString("fpdm")));
      dt6.put("fphm", Util.null2String(rs.getString("fphm")));
      dt6.put("kpje", Util.null2String(rs.getString("kpje")));
      dt6.put("kpf", Util.null2String(rs.getString("kpf")));
      dt6.put("fpfl", Util.null2String(rs.getString("fpfl")));
      dt6.put("htdelete", Util.null2String(rs.getString("htdelete")));
      dt6.put("fpckid", Util.null2String(rs.getString("fpckid")));
      dt6.put("zt", Util.null2String(rs.getString("zt")));
      dt6.put("kpsj", Util.null2String(rs.getString("kpsj")));
      dt6.put("fplx", Util.null2String(rs.getString("fplx")));
      dt6.put("se", Util.null2String(rs.getString("se")));
      dt6.put("wsje", Util.null2String(rs.getString("wsje")));
      dt6.put("fplxid", Util.null2String(rs.getString("fplxid")));
      dt6.put("dyfp", Util.null2String(rs.getString("dyfp")));
      dt6.put("sj", Util.null2String(rs.getString("sj")));
      dt6.put("zjid", Util.null2String(rs.getString("zjid")));
      dt6.put("pjly", Util.null2String(rs.getString("pjly")));
      resp.add(dt6);
    }
    return resp;
  }

  public static List<Map<String, String>> getDt7s(String rid) throws Exception {
    RecordSet rs = new RecordSet();
    List<Map<String, String>> resp = new ArrayList<>();
    String sql = String.format("select t1.jshjze, t1.sjhj, t1.bhshj, t1.pjlx\n" +
            "from %s t1\n" +
            "left join %s t2\n" +
            "on t1.mainid=t2.id\n" +
            "where t2.requestid=?", tableName + "_dt7", tableName);
    rs.executeQuery(sql, rid);
    while (rs.next()) {
      Map<String, String> dt6 = new HashMap<>();
      dt6.put("jshjze", Util.null2String(rs.getString("jshjze")));
      dt6.put("sjhj", Util.null2String(rs.getString("sjhj")));
      dt6.put("bhshj", Util.null2String(rs.getString("bhshj")));
      dt6.put("pjlx", Util.null2String(rs.getString("pjlx")));
      resp.add(dt6);
    }
    return resp;
  }

  /**
   * 明细8只汇总一条数据
   *
   * @param rids 流程id,逗号分隔
   */
  public static List<LinkedHashMap<String, String>> getDt8s(String rids) throws Exception {
    RecordSet rs = new RecordSet();
    PropBean pp = new PropBean();
    List<LinkedHashMap<String, String>> resp = new ArrayList<>();
    String replace = rids.replaceAll(",", "','");
    String sql = "select SQJE from UF_WWSQHZ where REQUID in ('" + replace + "')";
    rs.executeQuery(sql);
    BigDecimal sum = BigDecimal.ZERO;
    while (rs.next()) {
      String sqje = Util.null2String(rs.getString("SQJE"));
      sum = DoubleUtil.add(sum, new BigDecimal(sqje), 2);
    }
    ResourceComInfo rc = new ResourceComInfo();
    String creatorUid = pp.getPropName("StaffCodCreator");
    String prefix = pp.getPropName("UnionBudgetAccountPrefix");
    if (StringUtils.isEmpty(prefix)) {
      throw new Exception("预算前缀值没有维护,请检查配置项:UnionBudgetAccountPrefix");
    }
    String departmentID = rc.getDepartmentID(creatorUid);
    LinkedHashMap<String, String> dt8 = new LinkedHashMap<>();
    dt8.put("bhsje", sum.toString());
    dt8.put("sj", "0");
    dt8.put("jshjje", sum.toString());
    dt8.put("sy", "");
//    dt8.put("fysjcdbm", departmentID);
    dt8.put("fysjcdbm", "1001"); //总部工会
    dt8.put("sl", "10");
    dt8.put("slz", "0.00");
    dt8.put("htbh", "");
    dt8.put("fycdbmfzr", getDepartLeader("1001"));
//    dt8.put("fycdbmfzr", "35775");
    dt8.put("cys", "");
    dt8.put("mxfzzd", "");
    dt8.put("yskm", "50104"); //职工集体福利支出
    dt8.put("yskmid", prefix + "_50104");
    dt8.put("yskz", "");
    resp.add(dt8);
    return resp;
  }

  /**
   * 只汇总一次明细13
   *
   * @param rids 流程id,逗号分隔
   */
  public static List<LinkedHashMap<String, String>> getDt13s(String rids) throws Exception {
    RecordSet rs = new RecordSet();
    String replace = rids.replaceAll(",", "','");
    String sql = "select t1.SQR,t2.LOGINID from UF_WWSQHZ t1\n" +
            "    left join HRMRESOURCE t2\n" +
            "    on t1.SQR=t2.ID where t1.REQUID in ('" + replace + "')";
    rs.executeQuery(sql);
    List<String> applies = new ArrayList<>();
    while (rs.next()) {
      applies.add(Util.null2String(rs.getString("LOGINID")));
    }
    return applies.stream()
            .filter(StringUtils::isNotBlank)
            .distinct()
            .map(item -> StaffCodUtil.getBanks(item, rids))
            .collect(Collectors.toList());
  }

  public static String getDepartLeader(String departmentId) {
    String sql = "select f.deptdirectors from hrmdepartmentdefined f where f.deptid = ?";
    RecordSet rs = new RecordSet();
    rs.executeQuery(sql, departmentId);
    if (rs.next()) {
      return rs.getString("deptdirectors");
    }
    return "";
  }

  public static LinkedHashMap<String, String> getBanks(String workCode, String rids) {
    RecordSet rs = new RecordSet();
    LinkedHashMap<String, String> resp = new LinkedHashMap<>();
    try {
      ResourceComInfo rc = new ResourceComInfo();
      String uid = rc.getUserIdByLoginId(workCode);
      String departmentID = rc.getDepartmentID(uid);
      String replace = rids.replaceAll(",", "','");
      String sqlje = "select SQJE from UF_WWSQHZ where REQUID in ('" + replace + "') and sqr=?";
      rs.executeQuery(sqlje, uid);
      BigDecimal sum = BigDecimal.ZERO;
      while (rs.next()) {
        String sqje = Util.null2String(rs.getString("SQJE"));
        sum = DoubleUtil.add(sum, new BigDecimal(sqje), 2);
      }

      String sql = "select GH,YXZH,KHX,SS,KHXMC from UF_ZBYGYHXX where GH=?";
      rs.executeQuery(sql, workCode);
      rs.next();
      resp.put("ygxm", Util.null2String(uid));
      resp.put("bm", Util.null2String(departmentID));
      resp.put("ss", Util.null2String(rs.getString("SS")));
      resp.put("yxzh", Util.null2String(rs.getString("YXZH")));
      resp.put("yxzfje", sum.toString());
      resp.put("khx", Util.null2String(rs.getString("KHX")));
      resp.put("lsxx", "");
      resp.put("bz", "");
      resp.put("khxmc", Util.null2String(rs.getString("KHXMC")));
      resp.put("fycdbm", Util.null2String(departmentID));
      resp.put("szjg", "");
      resp.put("yggh", workCode);

      return resp;
    } catch (Exception e) {
      rs.writeLog(e.getMessage());
    }
    return resp;
  }
}
