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.KQBalanceOfLeaveService;
import com.engine.kq.service.impl.KQBalanceOfLeaveServiceImpl;
import com.engine.kq.util.ImportProcessUtil;
import weaver.general.BaseBean;
import weaver.general.Util;
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 KQBalanceOfLeaveAction extends BaseBean {

    private KQBalanceOfLeaveService getService(User user) {
        return (KQBalanceOfLeaveService) ServiceUtil.getService(KQBalanceOfLeaveServiceImpl.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);
    }

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

    /**
     * 获取查询列表
     *
     * @param request
     * @param response
     * @return
     */
    @POST
    @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("/getTabs")
    @Produces(MediaType.TEXT_PLAIN)
    public String getTabs(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apiDatas = getService(user).getTabs(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("/batchProcessing")
    @Produces(MediaType.TEXT_PLAIN)
    public String batchProcessing(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apiDatas = getService(user).batchProcessing(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("/saveBalanceOfLeave")
    @Produces(MediaType.TEXT_PLAIN)
    public String saveBalanceOfLeave(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apiDatas = getService(user).saveBalanceOfLeave(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("/getImportForm")
    @Produces(MediaType.TEXT_PLAIN)
    public String getImportForm(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apiDatas = getService(user).getImportForm(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("/saveImport")
    @Produces(MediaType.TEXT_PLAIN)
    public String saveImport(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apiDatas = getService(user).saveImport(ParamUtil.request2Map(request), user,request);
            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);
    }
}
