package com.engine.htsc.trip.web;

import com.alibaba.fastjson.JSONObject;
import com.engine.htsc.payment.enums.Sequences;
import com.engine.htsc.payment.util.CoWfUtil;
import com.engine.htsc.trip.service.BtService;
import com.engine.htsc.trip.service.impl.BtServiceImpl;
import com.engine.htsc.trip.util.AjaxResult;
import com.engine.common.util.ServiceUtil;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.interfaces.htsc.cus.HTUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by qlk
 */
public class BtAction {
  public BtAction() {
  }

  private BtService getServices(HttpServletRequest request, HttpServletResponse response) {
    User user = HrmUserVarify.getUser(request, response);
    return ServiceUtil.getService(BtServiceImpl.class, user);
  }

  @GET
  @Path("/update/bmldyjcc")
  @Produces(MediaType.APPLICATION_JSON)
  public AjaxResult<String> updateBmldyjcc(@QueryParam("xm") String dt2xm) {
    try {
      RecordSet rs = new RecordSet();
      rs.executeQuery("SELECT sfsp FROM uf_LedSP WHERE ccr=?", dt2xm);
      rs.writeLog("联合出差申请,更新主表 bmldyjcc , dt2 xm(人员id):" + dt2xm);
      String sfsp = "";
      if (rs.next()) {
        sfsp = Util.null2String(rs.getString("sfsp"));
      }
      rs.writeLog("联合出差申请,更新主表 bmldyjcc , " +
              "sql:\"SELECT sfsp FROM uf_LedSP WHERE ccr='" + dt2xm + "''");
      return AjaxResult.ok(sfsp);
    } catch (Exception e) {
      return AjaxResult.error("生成流程编号失败,发生异常:" + e.getMessage());
    }
  }


  @GET
  @Path("/standard/{travelers}/{destinations}")
  @Produces({MediaType.APPLICATION_JSON})
  public AjaxResult<List<String>> standard(
          @PathParam("travelers") String travelers,
          @PathParam("destinations") String destinations,
          @Context HttpServletRequest request,
          @Context HttpServletResponse response) {

    List<String> travelStandard = this.getServices(request, response).getTravelStandard(travelers, destinations);
    return AjaxResult.ok(travelStandard, "人员和部门组合生成差旅信息标准");
  }

  @GET
  @Path("/check/names/{names}")
  @Produces({MediaType.APPLICATION_JSON})
  public AjaxResult<Boolean> checkNames(
          @PathParam("names") String names,
          @Context HttpServletRequest request,
          @Context HttpServletResponse response) {

    Boolean isContain = this.getServices(request, response).checkNames(names);
    return AjaxResult.ok(isContain, "校验明细字段姓名是否存在范围下");
  }

  @GET
  @Path("/check/dept/name/{dept}/{name}")
  @Produces({MediaType.APPLICATION_JSON})
  public AjaxResult<Boolean> checkDept(
          @PathParam("dept") String dept,
          @PathParam("name") String name,
          @Context HttpServletRequest request,
          @Context HttpServletResponse response) {

    Boolean isContain = this.getServices(request, response).checkDeptAndName(dept, name);
    return AjaxResult.ok(isContain, "网约车字段是否显示");
  }

  @GET
  @Path("/get/dept/directors/{dept}/{expDept}")
  @Produces({MediaType.APPLICATION_JSON})
  public AjaxResult<String> getDeptDirectors(
          @PathParam("dept") String dept,
          @PathParam("expDept") String expDept,
          @Context HttpServletRequest request,
          @Context HttpServletResponse response) {

    String directors = this.getServices(request, response).getDirectors(dept, expDept);
    return AjaxResult.ok(directors, "获取部门和费用承担部门的部门负责人");
  }

  /**
   * 根据项目id获取投行项目信息 code name
   *
   * @param projectId 项目id
   * @return map => projectId projectCode projectName
   */
  @GET
  @Path("/getProjectInfo")
  @Produces(MediaType.APPLICATION_JSON)
  public AjaxResult<Map<String, Object>> getProjectById(@QueryParam("projectId") String projectId) {
    Map<String, Object> projectParam = new HashMap<>();
    Map<String, Object> data = new HashMap<>();
    try {
      if (StringUtils.isBlank(projectId)) {
        return AjaxResult.failed("projectId is blank");
      }
      projectParam.put("projectId", projectId);
      String res = new HTUtil().getProjectById(projectParam);
      if (StringUtils.isBlank(res)) {
        return AjaxResult.failed("getProjectById api return blank");
      }
      JSONObject jsonObject = JSONObject.parseObject(res);
      data.put("projectId", projectId);
      data.put("projectCode", Util.null2String(jsonObject.get("code")));
      data.put("projectName", Util.null2String(jsonObject.get("shortName")));
      return AjaxResult.ok(data);
    } catch (Exception e) {
      e.printStackTrace();
      return AjaxResult.error("error: " + e);
    }

  }


}
