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.KQBalanceOfLeaveRpService;
import com.engine.kq.service.impl.KQBalanceOfLeaveRpServiceImpl;
import weaver.filter.XssUtil;
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 KQBalanceOfLeaveRpAction {

    private KQBalanceOfLeaveRpService getService(User user) {
        return (KQBalanceOfLeaveRpService) ServiceUtil.getService(KQBalanceOfLeaveRpServiceImpl.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);
            apiDatas.put("hasRight", user != null);
            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);
    }

    /**
     * 获取TAB页签
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getTab")
    @Produces(MediaType.TEXT_PLAIN)
    public String getTab(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apiDatas = getService(user).getTab(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("/getUsageRecordDetail")
    @Produces(MediaType.TEXT_PLAIN)
    public String getUsageRecordDetail(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apiDatas = getService(user).getUsageRecordDetail(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("/getPageInfo")
    @Produces(MediaType.TEXT_PLAIN)
    public String getPageInfo(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apiDatas = getService(user).getPageInfo(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("/getDetailPageInfo")
    @Produces(MediaType.TEXT_PLAIN)
    public String getDetailPageInfo(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apiDatas = getService(user).getDetailPageInfo(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("/getChangeType")
    @Produces(MediaType.TEXT_PLAIN)
    public String getChangeType(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apiDatas = getService(user).getChangeType(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);
    }

    /**
     * 导出Excel
     *
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/exportExcel")
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public String exportExcel(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apiDatas = getService(user).exportExcel(ParamUtil.request2Map(request), user, request, response);
            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);
    }
}
