package com.engine.kq.web;

import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.kq.service.KQLeaveTypesService;
import com.engine.kq.service.impl.KQLeaveTypesServiceImpl;
import weaver.general.BaseBean;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: Administrator
 * @Date: 2019-03-12 11:17
 * @Description:
 */
public class KQLeaveTypesAction extends BaseBean {

    private KQLeaveTypesService getService(User user){
        return (KQLeaveTypesService) ServiceUtil.getService(KQLeaveTypesServiceImpl.class, user);
    }

    /**
     * 判断是否有权限
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getHasRight")
    public String getHasRight(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();//传给前台的数据集合
        try {
            User user = HrmUserVarify.getUser(request, response);
            boolean hasRight = HrmUserVarify.checkUserRight("KQLeaveRulesEdit:Edit", user);
            apiDatas.put("hasRight", hasRight);
            apiDatas.put("api_status", true);
        } catch (Exception e) {
            writeLog(e);
            apiDatas.put("api_status", true);
            apiDatas.put("api_errormsg", e.getMessage());
        }
        return JSONObject.toJSONString(apiDatas);
    }

    /**
     * 获取右键菜单
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getRightMenu")
    @Produces(MediaType.TEXT_PLAIN)
    public String getRightMenu(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apiDatas = getService(user).getRightMenu(ParamUtil.request2Map(request), user);
            apiDatas.put("api_status", true);
        } catch (Exception e) {
            writeLog(e);
            apiDatas.put("api_status", false);
            apiDatas.put("api_errormsg", e.getMessage());
        }
        return JSONObject.toJSONString(apiDatas);
    }

    @GET
    @Path("/getSearchCondition")
    @Produces(MediaType.TEXT_PLAIN)
    public String getSearchCondition(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apiDatas = getService(user).getSearchCondition(ParamUtil.request2Map(request), user);
            apiDatas.put("api_status", true);
        } catch (Exception e) {
            writeLog(e);
            apiDatas.put("api_status", false);
            apiDatas.put("api_errormsg", e.getMessage());
        }
        return JSONObject.toJSONString(apiDatas);
    }

    /**
     * 获取查询列表
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getSearchList")
    @Produces(MediaType.TEXT_PLAIN)
    public String getSearchList(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apiDatas = getService(user).getSearchList(ParamUtil.request2Map(request), user);
            apiDatas.put("api_status", true);
        } catch (Exception e) {
            writeLog(e);
            apiDatas.put("api_status", false);
            apiDatas.put("api_errormsg", e.getMessage());
        }
        return JSONObject.toJSONString(apiDatas);
    }

    /**
     * 获取新建或者编辑的表单
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getLeaveTypesForm")
    @Produces(MediaType.TEXT_PLAIN)
    public String getLeaveTypesForm(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apiDatas = getService(user).getLeaveTypesForm(ParamUtil.request2Map(request), user);
            apiDatas.put("api_status", true);
        } catch (Exception e) {
            writeLog(e);
            apiDatas.put("api_status", false);
            apiDatas.put("api_errormsg", e.getMessage());
        }
        return JSONObject.toJSONString(apiDatas);
    }

    /**
     * 新建
     *
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/addLeaveTypes")
    @Produces(MediaType.TEXT_PLAIN)
    public String addLeaveTypes(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apiDatas = getService(user).addLeaveTypes(ParamUtil.request2Map(request), user);
            apiDatas.put("api_status", true);
        } catch (Exception e) {
            writeLog(e);
            apiDatas.put("api_status", false);
            apiDatas.put("api_errormsg", e.getMessage());
        }
        return JSONObject.toJSONString(apiDatas);
    }

    /**
     * 编辑
     *
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/editLeaveTypes")
    @Produces(MediaType.TEXT_PLAIN)
    public String editLeaveTypes(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apiDatas = getService(user).editLeaveTypes(ParamUtil.request2Map(request), user);
            apiDatas.put("api_status", true);
        } catch (Exception e) {
            writeLog(e);
            apiDatas.put("api_status", false);
            apiDatas.put("api_errormsg", e.getMessage());
        }
        return JSONObject.toJSONString(apiDatas);
    }

    /**
     * 删除
     *
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/deleteLeaveTypes")
    @Produces(MediaType.TEXT_PLAIN)
    public String deleteLeaveTypes(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apiDatas = getService(user).deleteLeaveTypes(ParamUtil.request2Map(request), user);
            apiDatas.put("api_status", true);
        } catch (Exception e) {
            writeLog(e);
            apiDatas.put("api_status", false);
            apiDatas.put("api_errormsg", e.getMessage());
        }
        return JSONObject.toJSONString(apiDatas);
    }

    /**
     * 判断名称是否重复
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/isNameRepeat")
    @Produces(MediaType.TEXT_PLAIN)
    public String isNameRepeat(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apiDatas = getService(user).isNameRepeat(ParamUtil.request2Map(request), user);
            apiDatas.put("api_status", true);
        } catch (Exception e) {
            writeLog(e);
            apiDatas.put("api_status", false);
            apiDatas.put("api_errormsg", e.getMessage());
        }
        return JSONObject.toJSONString(apiDatas);
    }

    /**
     * 是否启用
     *
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/saveDisableLeaveTypes")
    @Produces(MediaType.TEXT_PLAIN)
    public String saveDisableLeaveRules(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apiDatas = getService(user).saveDisableLeaveTypes(ParamUtil.request2Map(request), user);
            apiDatas.put("api_status", true);
        } catch (Exception e) {
            writeLog(e);
            apiDatas.put("api_status", false);
            apiDatas.put("api_errormsg", e.getMessage());
        }
        return JSONObject.toJSONString(apiDatas);
    }

  /**
   * 时间选择方式表单
   *
   * @param request
   * @param response
   * @return
   */
  @POST
  @Path("/getTimeSelectionForm")
  @Produces(MediaType.TEXT_PLAIN)
  public String getTimeSelectionForm(@Context HttpServletRequest request, @Context HttpServletResponse response) {
    Map<String, Object> apidatas = new HashMap<String, Object>();
    try {
      User user = HrmUserVarify.getUser(request, response);
      apidatas = getService(user).getTimeSelectionForm(ParamUtil.request2Map(request), user);
    } catch (Exception e) {
      apidatas.put("status", "-1");
    }
    return JSONObject.toJSONString(apidatas);
  }

  /**
   * 保存时间选择方式表单
   *
   * @param request
   * @param response
   * @return
   */
  @POST
  @Path("/saveTimeSelectionForm")
  @Produces(MediaType.TEXT_PLAIN)
  public String saveTimeSelectionForm(@Context HttpServletRequest request, @Context HttpServletResponse response) {
    Map<String, Object> apidatas = new HashMap<String, Object>();
    try {
      User user = HrmUserVarify.getUser(request, response);
      apidatas = getService(user).saveTimeSelectionForm(ParamUtil.request2Map(request), user);
    } catch (Exception e) {
      apidatas.put("status", "-1");
    }
    return JSONObject.toJSONString(apidatas);
  }
}
