package com.engine.htsc.payment.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.engine.htsc.payment.dao.BudgetDao;
import com.engine.htsc.payment.dao.PaymentDao;
import com.engine.htsc.payment.pojo.OaPaymentBizRequest;
import com.engine.htsc.trip.util.WeaverUtil;

import java.text.DecimalFormat;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetDataSource;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.htsc.cus.DepartmentUtils;
import weaver.interfaces.htsc.cus.HTUtil;
import weaver.interfaces.workflow.action.Action;

import java.lang.reflect.Field;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;


/**
 * 付款流程通用工具类
 */
public class PayUtil {

  private static final Log log = LogFactory.getLog(PayUtil.class.getName());
  private static final RecordSet rs = new RecordSet();

  public static String getTbNameByRid(String requestId) throws Exception {
    String sql = "select t1.tablename from workflow_bill t1,workflow_base t2,workflow_requestbase t3 \n" +
            " where t1.id =t2.formid and t2.id = t3.workflowid and t3.requestid =?";
    RecordSet rs = new RecordSet();
    rs.executeQuery(sql, requestId);
    if (rs.next()) {
      return Util.null2String(rs.getString("tablename"));
    } else {
      throw new Exception(String.format("requestId:%s; 没有对应的流程表名", requestId));
    }
  }

  public static String getDeptTypeCode(String deptId) {
    String code = "";
    String sql = "select depttypecode from hrmdepartmentdefined where deptid=?";
    RecordSet rs = new RecordSet();
    rs.executeQuery(sql, deptId);
    while (rs.next()) {
      code = rs.getString("depttypecode");
      break;
    }
    return code;
  }

  /**
   * 费用承担部门负责人 逗号分隔后获取loginId再拼接
   *
   * @return 费用承担部门负责人loginId
   */
  public static String splitBmfzrThenJoin(String fzrWithDelimiter, ResourceComInfo rc) {
    List<String> fzrIdList = Arrays.asList(fzrWithDelimiter.split(","));
    List<String> fzrLoginIdList = fzrIdList.stream().map(rc::getLoginID).collect(Collectors.toList());
    return StringUtils.join(fzrLoginIdList, ",");
  }

  /**
   * 获取预算科目上级预算科目
   *
   * @return 上级预算科目code
   */
  public static String superBudgetSubject(String subBudgetCode) {
    RecordSet rs = new RecordSet();
    BudgetDao budgetDao = new BudgetDao();
    String sql = budgetDao.superiorBudgetSubject(subBudgetCode);
    rs.executeQuery(sql);
    String superBudgetCode = "";
    if (rs.next()) {
      superBudgetCode = rs.getString(Util.null2String("sjyskm"));
    }
    return superBudgetCode;
  }

  /**
   * 三级预算科目取上级预算科目名称 二级预算科目取当前值
   *
   * @param subBudgetCode
   * @return
   */
  public static String getSuperBudgetCode(String subBudgetCode) {
    RecordSet rs = new RecordSet();
    String sql = "select sjyskm,kmjb from uf_kmpz where fc= 0 and yskmbm = ?";
    rs.executeQuery(sql, subBudgetCode);
    String superBudgetCode = "";
    String kmjb = "";
    if (rs.next()) {
      kmjb = Util.null2String(rs.getString("kmjb"));
      superBudgetCode = Util.null2String(rs.getString("sjyskm"));
    }
    if (!"3".equals(kmjb)) {
      superBudgetCode = subBudgetCode;
    }
    if ("".equals(superBudgetCode)) {
      //为空就返回入参
      superBudgetCode = subBudgetCode;
    }
    return superBudgetCode;
  }

  /**
   * 三级预算科目取上级预算科目名称 二级预算科目取当前值
   *
   * @param subBudgetCode 预算科目
   * @return 上级预算
   */
  public static String getSuperBudgetCodeLh(String subBudgetCode) {
    RecordSet rs = new RecordSet();
    String sql = "select sjyskm,kmjb from uf_lhkmpz where fc= 0 and yskmbm = ?";
    rs.executeQuery(sql, subBudgetCode);
    String superBudgetCode = "";
    String kmjb = "";
    if (rs.next()) {
      kmjb = Util.null2String(rs.getString("kmjb"));
      superBudgetCode = Util.null2String(rs.getString("sjyskm"));
    }
    if (!"3".equals(kmjb)) {
      superBudgetCode = subBudgetCode;
    }
    if ("".equals(superBudgetCode)) {
      //为空就返回入参
      superBudgetCode = subBudgetCode;
    }
    return superBudgetCode;
  }

  /**
   * 超预算id转字符串
   *
   * @param cys 超预算id
   * @return 每次
   */
  public static String overWeight(String cys) {
    String c = "";
    switch (cys) {
      case "0":
        c = "否";
        break;
      case "1":
        c = "是";
        break;
      default:
        c = "预算数据未维护";
    }
    return c;
  }

  /**
   * 根据unNumber获取表明
   *
   * @param unNumber 唯一标识
   * @return sql
   */
  public static String getTableNameByUnNumber(String unNumber) {
    rs.executeQuery("select t1.TABLENAME from workflow_bill t1\n" +
            "LEFT JOIN workflow_base t2\n" +
            "on t1.ID=t2.FORMID\n" +
            "where t2.ISVALID=1 and UNNUMBER='" + unNumber + "'");
    rs.next();
    return rs.getString("TABLENAME");
  }

  /**
   * 根据建模表名获取formModeId
   *
   * @param cubeName 建模表明
   * @return int formModeId
   */
  public static int getFormModeIdByCubeName(String cubeName) {
    rs.executeQuery("select t1.id from modeinfo t1\n" +
            " left join workflow_bill t2\n" +
            " on t1.formid=t2.id\n" +
            " where t2.tablename='" + cubeName + "'");
    rs.next();
    return rs.getInt("id");
  }

  /**
   * 获取省市code对应的省市名称
   *
   * @param code 省市code
   * @return 省市名
   */
  public static String getProvinceAndCityNameByCode(String code) {
    RecordSetDataSource rsd = new RecordSetDataSource("Travel"); //城市
    if (StringUtils.isBlank(code)) {
      return "";
    }
    String[] desInfo = code.split(","); //省市区
    String ids = Arrays.stream(desInfo).map(s -> "\'" + s + "\'").collect(Collectors.joining(","));
    rsd.executeSql("select name from travel_area where code in (" + ids + ") order by instr('" + code + "',code)");
    List<String> names = new ArrayList<>();
    while (rsd.next()) {
      names.add(rsd.getString("name"));
    }
    return String.join(",", names);

  }

  /**
   * 根据uuid获取建模的数据id，常用于建模的权限重构
   *
   * @param uuid modeUuid
   * @return dataId
   */
  public static int getDataIdByUUID(String uuid) {
    int dataid = -1;
    String sql = "select id from uf_zfmx where modeuuid = ?";
    if (rs.executeQuery(sql, uuid) && rs.next()) {
      dataid = Util.getIntValue(rs.getString(1));
    }
    return dataid;
  }

  /**
   * 获取UnNumber by 流程id
   *
   * @param workFlowId 流程id
   * @return string
   */
  public static String getUnNumberByWorkFlowId(int workFlowId) {
    String sql = "select workflowname,id,unnumber from workflow_base where isvalid=1 and id=?";
    if (rs.executeQuery(sql, workFlowId)) {
      rs.next();
      return Util.null2String(rs.getString("unnumber"));
    } else {
      return "";
    }
  }

  /**
   * 获取部门负责人
   *
   * @param deptIds 部门id
   * @return 人员id
   */
  public static String getDivisionHead(String deptIds) {
    if (StringUtils.isBlank(deptIds)) {
      return "";
    }
    PaymentDao paymentDao = new PaymentDao();
    String sql = paymentDao.getDivisonHead(deptIds);
    rs.executeQuery(sql);
    log.info(sql + "==" + deptIds);
    List<String> heads = new ArrayList<>();
    while (rs.next()) {
      heads.add(Util.null2String(rs.getString("deptleaders")));
    }
    return heads.stream().filter(StringUtils::isNotBlank).distinct()
            .collect(Collectors.joining(","));
  }

  /**
   * 根据流程表单获取投行项目
   *
   * @param reqId 流程id
   * @return map
   */
  public static Map<String, Object> getItItemByReqId(String reqId) {
    Map<String, Object> item = new HashMap<>();
    RecordSet recordSet = new RecordSet();
    String ccsqTable = WeaverUtil.getTableNameByRequestId(reqId, recordSet);
    if ("".equals(ccsqTable)) {
      return item;
    }
    String thxm_sql = "select xmjc from " + ccsqTable;
    recordSet.executeQuery(thxm_sql);
    recordSet.next();
    String xmjc = Util.null2String(recordSet.getString("xmjc"));
    if ("".equals(xmjc)) {
      Map<String, Object> paras = new HashMap<>();
      paras.put("projectId", xmjc);
      String projectById = new HTUtil().getProjectById(paras);
      if ("".equals(projectById)) {
        return item;
      }
      JSONObject jsonObject = JSONObject.parseObject(projectById);
      String shortName = jsonObject.getString("shortName");
      item.put("id", xmjc);
      item.put("shortName", shortName);
      item.put("isItProj", shortName);
    } else {
      item.put("id", "");
      item.put("shortName", "");
      item.put("isItProj", "否");
    }
    return item;
  }

  /**
   * 将Object对象里面的属性和值转化成Map对象
   *
   * @param obj
   * @return
   * @throws IllegalAccessException
   */
  public static Map<String, Object> objectToMap(Object obj) throws IllegalAccessException {
    Map<String, Object> map = new HashMap<String, Object>();
    Class<?> clazz = obj.getClass();
    for (Field field : clazz.getDeclaredFields()) {
      field.setAccessible(true);
      String fieldName = field.getName();
      Object value = nvl(field.get(obj), "");
      map.put(fieldName, value);
    }
    return map;
  }

  /**
   * 获取参数不为空值
   *
   * @param value defaultValue 要判断的value
   * @return value 返回值
   */
  public static <T> T nvl(T value, T defaultValue) {
    return value != null ? value : defaultValue;
  }

  /**
   * 预算明细表8或9 取科目对应数据
   * 金额,是否超标,实际金额,标准金额,不含税价金额,税金
   *
   * @param budgetKmTable 预算明细表
   * @param mainId        流程mainId
   * @param isIt          是否it付款
   * @return map
   */
  public static JSONObject getKjKmCharges(String budgetKmTable, String mainId, boolean isIt) {
    RecordSet rs = new RecordSet();
    JSONObject subParam = new JSONObject();
    BudgetDao budgetDao = new BudgetDao();
    String yskmAmountParam = budgetDao.budgetKmTableInfo(budgetKmTable, mainId, isIt);
    rs.executeQuery(yskmAmountParam);
    if (rs.next()) {
      if (budgetKmTable.toLowerCase().contains("dt8")) {
        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));
        // 金额
        subParam.put("amount", JSHJJE);
        // 不含价税金额
        subParam.put("notaxAmount", BHSJE);
        //税金
        subParam.put("taxAmount", SJ);
        // 非IT付款的情况下,价税合计不为0 不含税为0的情况下 不含税金额改成和价税合计一样的金额
        if (!isIt) {
          if (StringUtils.isNotBlank(JSHJJE) && !"0".equals(JSHJJE) && !"0.00".equals(JSHJJE) && (
                  StringUtils.isBlank(BHSJE) || "0".equals(BHSJE) || "0.00".equals(BHSJE))) {
            // 不含价税金额
            subParam.put("notaxAmount", JSHJJE);
          }
        }
      } else {
        // 人民币金额
        String JSHJJE = Util.null2String(rs.getString("rmbje"));
        //格式化金额
        JSHJJE = new DecimalFormat("0.00").format(NumberUtils.toDouble(JSHJJE));
        // 金额
        subParam.put("amount", JSHJJE);
        // 不含价税金额
        subParam.put("notaxAmount", "0.00");
        //税金
        subParam.put("taxAmount", "0.00");
      }
    }
    return subParam;
  }

  /**
   * 更新合同付款信息到华泰
   *
   * @param requestId 流程id
   * @return 是否成功
   */
  public static boolean updatePaymentInfoToHuaTai(String requestId) {
    log.info("====更新合同付款信息到华泰");
    log.info("====通用付款流程id@" + requestId + "@");
    if ("".equals(requestId)) {
      log.info("====流程requestId为空,更新合同信息终止");
      return false;
    }
    PropBean propBean = new PropBean();
    String updateProjectsUrl = propBean.getPropName("updatePaymentInfo");
    //合同跳转到oa的url前缀
    String contractForwardOaUrlPre = propBean.getPropName("contractForwardOaUrlPre");
    OaPaymentBizRequest oaPaymentBizRequest = new OaPaymentBizRequest();
    RecordSet rs = new RecordSet();
    String table = WeaverUtil.getTableNameByRequestId(requestId, rs);
    if ("".equals(table)) {
      log.info("====根据流程requestId无法获取表名,更新合同信息终止");
      return false;
    }
    try {
      rs.executeQuery("select t1.LCBH,t2.CKHTXX,t1.hjxxjezfmx,t2.HTBH from " + table + " t1\n" +
              " left join " + table + "_DT4 t2\n" +
              " on t1.ID=t2.MAINID\n" +
              " where REQUESTID=" + requestId);
      if (rs.next()) {
        oaPaymentBizRequest.setOaPayUrl(contractForwardOaUrlPre + requestId);
        oaPaymentBizRequest.setOaCode(Util.null2String(rs.getString("LCBH")));
        oaPaymentBizRequest.setCtrctCode(Util.null2String(rs.getString("HTBH")));
        oaPaymentBizRequest.setPaymentMoney(Util.null2String(rs.getString("hjxxjezfmx")));
        oaPaymentBizRequest.setPaymentDate(LocalDateTime.now().toString());
        oaPaymentBizRequest.setProcessCode(Util.null2String(rs.getString("CKHTXX")));
        oaPaymentBizRequest.setSysCode("OA");
        oaPaymentBizRequest.setModuleCode("CTRCT");
      }
    } catch (Exception e) {
      log.info("明细表4不存在，直接返回");
      return true;
    }

    try {
      //判断是否有合同数据
      if (AssertUtil.isEmpty(oaPaymentBizRequest.getCtrctCode())) {
        log.info("当前流程无合同信息,直接返回; requestid:" + requestId);
        return true;
      }
      log.info("====更新合同付款信息url@" + updateProjectsUrl + "@");
      log.info("====更新合同付款信息请求参数@" + oaPaymentBizRequest + "@");
      HTUtil htUtil = new HTUtil();
      Map<String, String> appHeader = htUtil.getAppHeader();
      log.info("getAppHeader@" + appHeader + "@");
      String response = HttpUtil.postApplicationJson(updateProjectsUrl, JSON.toJSONString(oaPaymentBizRequest), appHeader);
      log.info("====更新合同付款信息返回参数@" + response + "@");
      JSONObject jsonObject = JSONObject.parseObject(response);
      return (boolean) jsonObject.get("isSuccess");
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }

  /**
   * 更新合同付款信息到华泰,关联多个合同的情况
   *
   * @param requestId 流程id
   * @return 是否成功
   */
  public static boolean updatePaymentInfoListToHuaTai(String requestId) {
    log.info("====更新合同付款信息到华泰");
    log.info("====通用付款流程id@" + requestId + "@");
    if ("".equals(requestId)) {
      log.info("====流程requestId为空,更新合同信息终止");
      return false;
    }
    PropBean propBean = new PropBean();
    String updateProjectsUrl = propBean.getPropName("updatePaymentInfo");
    //合同跳转到oa的url前缀
    String contractForwardOaUrlPre = propBean.getPropName("contractForwardOaUrlPre");
    RecordSet rs = new RecordSet();
    String table = WeaverUtil.getTableNameByRequestId(requestId, rs);
    if ("".equals(table)) {
      log.info("====根据流程requestId无法获取表名,更新合同信息终止");
      return false;
    }
    List<OaPaymentBizRequest> oaPaymentBizRequestList = new ArrayList<>();
    rs.executeQuery("select t1.LCBH,t2.CKHTXX,t1.hjxxjezfmx,t2.HTBH from " + table + " t1\n" +
            " left join " + table + "_DT4 t2\n" +
            " on t1.ID=t2.MAINID\n" +
            " where REQUESTID=" + requestId);
    while (rs.next()) {
      OaPaymentBizRequest oaPaymentBizRequest = new OaPaymentBizRequest();
      oaPaymentBizRequest.setOaPayUrl(contractForwardOaUrlPre + requestId);
      oaPaymentBizRequest.setOaCode(Util.null2String(rs.getString("LCBH")));
      oaPaymentBizRequest.setCtrctCode(Util.null2String(rs.getString("HTBH")));
      oaPaymentBizRequest.setPaymentMoney(Util.null2String(rs.getString("hjxxjezfmx")));
      oaPaymentBizRequest.setPaymentDate(LocalDateTime.now().toString());
      oaPaymentBizRequest.setProcessCode(Util.null2String(rs.getString("CKHTXX")));
      oaPaymentBizRequest.setSysCode("OA");
      oaPaymentBizRequest.setModuleCode("CTRCT");
      //判断是否有合同数据
      if (!"".equals(oaPaymentBizRequest.getCtrctCode())) {
        oaPaymentBizRequestList.add(oaPaymentBizRequest);
      }
    }
    try {
      //判断是否有合同数据
      if (oaPaymentBizRequestList.size()==0) {
        log.info("当前流程无合同信息,直接返回; requestid:" + requestId);
        return true;
      }
      log.info("====更新合同付款信息url@" + updateProjectsUrl + "@");
      log.info("====更新合同付款信息请求参数@" + JSON.toJSONString(oaPaymentBizRequestList) + "@");
      JSONObject jsonObject = new JSONObject();
      boolean flag = true;
      for (OaPaymentBizRequest oaPaymentBizRequest:oaPaymentBizRequestList){
        HTUtil htUtil = new HTUtil();
        Map<String, String> appHeader = htUtil.getAppHeader();
        log.info("getAppHeader@" + appHeader + "@");
        String response = HttpUtil.postApplicationJson(updateProjectsUrl, JSON.toJSONString(oaPaymentBizRequest), appHeader);
        log.info("====更新合同付款信息返回参数@" + response + "@");
        jsonObject = JSONObject.parseObject(response);
        if (!(boolean) jsonObject.get("isSuccess")){
          flag = false;
        }
      }
      return flag;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }

  /**
   * 更新it合同信息信息
   *
   * @param requestId 流程id
   * @return 是否成功
   */
  public static boolean updateItPaymentInfoToHT(String requestId, String workflowTable) throws Exception {
    log.info("====更新it合同付款信息到华泰@requestId" + requestId + "@workflowTable:" + workflowTable);
    //根据主表字段fkxx判断当前流程是否为付款付款
    try {
      RecordSet rs = new RecordSet();
      RecordSet rs2 = new RecordSet();
      Map<String, Object> dataMap = new HashMap<>();
      ResourceComInfo resourceComInfo = new ResourceComInfo();
      List<Object> costDetails = new ArrayList<>();
      rs.executeQuery(" select id,ngr,lcbh,xmfzr from " + workflowTable + " where requestid=? ", new Object[]{requestId});
      boolean flag = false;
      if (rs.next()) {
        int mainid = rs.getInt("id");
        String lcbh = Util.null2String(rs.getString("lcbh"));
        String ngr = resourceComInfo.getWorkcode(Util.null2String(rs.getString("ngr")));
        String xmfzr = resourceComInfo.getWorkcode(Util.null2String(rs.getString("xmfzr")));

        rs.executeQuery(" select htbh,htmc from " + workflowTable + "_dt4 where mainid=? and rownum=1 order by id ", new Object[]{mainid});
        rs.next();
        String htbh = Util.null2String(rs.getString("htbh"));
        String htmc = Util.null2String(rs.getString("htmc"));


        rs.executeQuery(" select skdw from " + workflowTable + "_dt11 where mainid=? and rownum=1 order by id ", new Object[]{mainid});
        rs.next();
        String skdw = Util.null2String(rs.getString("skdw"));


        rs.executeQuery(" select ID,projectcode from " + workflowTable + "_dt8 where mainid=? ", new Object[]{mainid});
        while (rs.next()) {
          String xmjdid = Util.null2String(rs.getString("projectcode"));
          String id = Util.null2String(rs.getString("ID"));
          List<Object> itemList = new ArrayList<>();

          String dataSql = " select bhsje,jshjje,fysjcdbmbm,sy,ghxm,yskm,fysjcdbm from " + workflowTable + "_dt8 where mainid=? and ID=? ";
          rs2.executeQuery(dataSql, new Object[]{mainid, id});
          while (rs2.next()) {
            String bhsje = Util.null2String(rs2.getString("bhsje"));
            String jshjje = Util.null2String(rs2.getString("jshjje"));
            String fysjcdbmbm = Util.null2String(rs2.getString("fysjcdbmbm"));//费用承担部门编码
            String fysjcdbm = Util.null2String(rs2.getString("fysjcdbm"));//费用实际承担部门id
            String sy = Util.null2String(rs2.getString("sy"));
            String ghxm = Util.null2String(rs2.getString("ghxm"));
            String yskm = getBudgetItemName(Util.null2String(rs2.getString("yskm")));

            if(StringUtils.isEmpty(fysjcdbmbm)) {//非空判断
              fysjcdbmbm = DepartmentUtils.getDeptCode(Integer.valueOf(fysjcdbm));
            }

            Map<String, String> itemMap = new HashMap<String, String>();
            itemMap.put("budgetAcct", yskm);
            itemMap.put("contractAmtNoTax", bhsje);
            itemMap.put("contractAmtWithTax", jshjje);
            itemMap.put("contractName", htmc);
            itemMap.put("contractSN", htbh);
            itemMap.put("isPlanNing", ghxm);
            itemMap.put("payee", skdw);
            itemMap.put("paymentDep", fysjcdbmbm);
            itemMap.put("paymentNote", sy);

            itemList.add(itemMap);

          }

          Map<String, Object> costDetailsMap = new HashMap<>();
          costDetailsMap.put("costDetails", itemList);
          costDetailsMap.put("projectCode", xmjdid);

          costDetails.add(costDetailsMap);

        }


        dataMap.put("costDetails", costDetails);
        dataMap.put("drafter", ngr);
        dataMap.put("projectDirectorId", xmfzr);
        dataMap.put("workFlowNum", lcbh);
        rs.writeLog("UpdatePaymentDataAction====params====, " + JSON.toJSONString(dataMap));

        HTUtil htUtil = new HTUtil();
        Map<String, String> appHeader = htUtil.getAppHeader();
        rs.writeLog("getAppHeader@{}@", appHeader);
        String requestUrl = new PropBean().getPropName("UpdatePaymentDataUrl");
        //添加app header
        String responseData = HttpUtil.postApplicationJson(requestUrl, JSON.toJSONString(dataMap), appHeader);
        rs.writeLog("UpdatePaymentDataAction====result==== " + responseData);
        JSONObject resJson = JSON.parseObject(responseData);
        if ("0000".equals(resJson.getString("retCode"))) {
          rs.writeLog("====更新it合同信息成功@" + requestId);
          flag = true;
        } else {
          rs.writeLog("====更新it合同信息失败@" + requestId);
          flag = false;
        }

      }
      return flag;
    } catch (Exception e) {
      rs.writeLog("====更新it合同信息方法执行异常@" + requestId);
      return false;
    }
  }

  /**
   * @param code 例如: code=32,3201
   * @return name=江苏省,江苏南京市
   */
  public static String cityCodeToName(String code) {
    RecordSet rs = new RecordSet();
    if (StringUtils.isBlank(code)) {
      return "";
    }
    String[] desInfo = code.split(","); //省市
    String ids = Arrays.stream(desInfo).map(s -> "\'" + s + "\'").collect(Collectors.joining(","));
    rs.executeQuery("select name from uf_city where code in (" + ids + ") order by instr('" + code + "',code)");
    List<String> names = new ArrayList<>();
    while (rs.next()) {
      names.add(rs.getString("name"));
    }
    return String.join(",", names);
  }

  /**
   * 根据流程编号判断调用核算接口的入参
   * lb：费用核算类别 用于区分不同类别的费用核算，可选参数如下：
   * <p>
   * htcl：总部差旅报销费用核算
   * htpt：总部普通报销费用核算
   * htfk：总部付款费用核算
   *
   * @param requestMark
   * @return
   */
  public static String switchBudgetParamByRequestMark(String requestMark) {
    if (StringUtils.isBlank(requestMark)) {
      return "";
    }
    if (requestMark.startsWith("总部IT付款") || requestMark.startsWith("总部通用付款") || requestMark.startsWith("总部工会付款")) {
      return "htfk";
    } else {
      return "";
    }


  }

  /**
   * 查询预算科目名称
   *
   * @param code
   * @return
   */
  private static String getBudgetItemName(String code) {
    String budgetItemName = "";
    RecordSet rs = new RecordSet();
    rs.executeQuery(" select yskmmc from uf_kmpz where yskmbm=? ", new Object[]{code});
    if (rs.next()) {
      budgetItemName = Util.null2String(rs.getString("yskmmc"));
    }
    return budgetItemName;

  }

}
