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.KQLeaveRulesService;
import com.engine.kq.service.impl.KQLeaveRulesServiceImpl;
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;

/**
 * 假期规则
 */
public class KQLeaveRulesAction {

    private KQLeaveRulesService getService(User user){
        return (KQLeaveRulesService) ServiceUtil.getService(KQLeaveRulesServiceImpl.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) {
            e.printStackTrace();
            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) {
            e.printStackTrace();
            apiDatas.put("api_status", false);
            apiDatas.put("api_errormsg", e.getMessage());
        }
        return JSONObject.toJSONString(apiDatas);
    }

    /**
     * 获取搜索条件
     *
     * @param request
     * @param response
     * @return
     */
    @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) {
            e.printStackTrace();
            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) {
            e.printStackTrace();
            apiDatas.put("api_status", false);
            apiDatas.put("api_errormsg", e.getMessage());
        }
        return JSONObject.toJSONString(apiDatas);
    }

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

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

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

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

    /**
     * 判断当前选择的请假类型的请假单位是天还是小时
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getUnitName")
    @Produces(MediaType.TEXT_PLAIN)
    public String getUnitName(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apiDatas = getService(user).getUnitName(ParamUtil.request2Map(request), user);
            apiDatas.put("api_status", true);
        } catch (Exception e) {
            e.printStackTrace();
            apiDatas.put("api_status", false);
            apiDatas.put("api_errormsg", e.getMessage());
        }
        return JSONObject.toJSONString(apiDatas);
    }
}
