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

import com.engine.core.impl.Service;
import com.engine.htsc.payment.service.UnionPaymentService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import weaver.conn.RecordSet;
import weaver.general.Util;

import java.util.HashMap;
import java.util.Map;

public class UnionPaymentServiceImpl extends Service implements UnionPaymentService {

  @Override
  public Map<String, Object> getApprovalLinkByDept(Map<String, Object> params) throws Exception {
    String department = Util.null2String(params.get("department"));
    Validate.notEmpty(department, "department为空");
    Map<String, Object> resp = new HashMap<>();
    String sql = "SELECT t1.spr,t2.LASTNAME FROM uf_htlhbmxgld t1\n" +
        "LEFT JOIN HRMRESOURCE t2\n" +
        "ON t1.spr = t2 .id\n" +
        "WHERE bm=?";
    RecordSet rs = new RecordSet();
    rs.executeQuery(sql, department);
    String id = "";
    String name = "";
    if (rs.next()) {
      id = Util.null2String(rs.getString("spr"));
      name = Util.null2String(rs.getString("LASTNAME"));
      Map<String, String> body = new HashMap<>();
      body.put("id", id);
      body.put("name", name);
      resp.put("needFilled", "xgldsp");
      resp.put("detail", body);
      return resp;
    }
    if (StringUtils.isEmpty(id)) {
      sql = "select d.DEPTLEADERS,h.LASTNAME from hrmdepartmentdefined d\n" +
          "LEFT JOIN HRMRESOURCE h\n" +
          "ON d.DEPTLEADERS = h.ID \n" +
          "where d.deptid =?";
      rs.executeQuery(sql, department);
      if (rs.next()) {
        id = Util.null2String(rs.getString("DEPTLEADERS"));
        name = Util.null2String(rs.getString("LASTNAME"));
        Map<String, String> body = new HashMap<>();
        body.put("id", id);
        body.put("name", name);
        resp.put("needFilled", "fgldsp");
        resp.put("detail", body);
        return resp;
      }
    }
    if (MapUtils.isEmpty(resp)) {
      throw new Exception("该部门没有对应的审批连 department code:" + department);
    }
    return resp;
  }
}
