package com.engine.kq.web;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.kq.service.KQHolidaySetService;
import com.engine.kq.service.impl.KQHolidaySetServiceImpl;
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;

/**
 * 节假日设置
 */
public class KQHolidaySetAction extends BaseBean {

    private KQHolidaySetService getService(User user) {
        return (KQHolidaySetService) ServiceUtil.getService(KQHolidaySetServiceImpl.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("KQHolidaySetEdit:Edit", user);
            apiDatas.put("hasRight", hasRight);
            apiDatas.put("status", "1");
        } catch (Exception e) {
            writeLog(e);
            apiDatas.put("status", "-1");
        }
        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, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 清单
     *
     * @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("/getHolidaySetCalendar")
    @Produces(MediaType.TEXT_PLAIN)
    public String getHolidaySetCalendar(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();//传递给前台的数据集合
        try {
            User user = HrmUserVarify.getUser(request, response);
            apiDatas = getService(user).getHolidaySetCalendar(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("/getHolidaySetForm")
    @Produces(MediaType.TEXT_PLAIN)
    public String getHolidaySetForm(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();//传递给前台的数据集合
        try {
            User user = HrmUserVarify.getUser(request, response);
            apiDatas = getService(user).getHolidaySetForm(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, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 新增
     *
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/addHolidaySet")
    @Produces(MediaType.TEXT_PLAIN)
    public String addHolidaySet(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            Map<String, Object> params = new HashMap<String, Object>();
            apiDatas = getService(user).addHolidaySet(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("/editHolidaySet")
    @Produces(MediaType.TEXT_PLAIN)
    public String editHolidaySet(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            Map<String, Object> params = new HashMap<String, Object>();
            apiDatas = getService(user).editHolidaySet(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("/deleteHolidaySet")
    @Produces(MediaType.TEXT_PLAIN)
    public String deleteHolidaySet(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            Map<String, Object> params = new HashMap<String, Object>();
            apiDatas = getService(user).deleteHolidaySet(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("/getHolidayCount")
    @Produces(MediaType.TEXT_PLAIN)
    public String getHolidayCount(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();//传递给前台的数据集合
        try {
            User user = HrmUserVarify.getUser(request, response);
            apiDatas = getService(user).getHolidayCount(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("/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) {
            e.printStackTrace();
            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);
            Map<String, Object> params = new HashMap<String, Object>();
            apiDatas = getService(user).saveImport(ParamUtil.request2Map(request), 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("/initHolidaySet")
    @Produces(MediaType.TEXT_PLAIN)
    public String initHolidaySet(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();//传递给前台的数据集合
        try {
            User user = HrmUserVarify.getUser(request, response);
            apiDatas = getService(user).initHolidaySet(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("/hasRepeactData")
    @Produces(MediaType.TEXT_PLAIN)
    public String hasRepeactData(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();//传递给前台的数据集合
        try {
            User user = HrmUserVarify.getUser(request, response);
            apiDatas = getService(user).hasRepeactData(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("/getSyncForm")
    @Produces(MediaType.TEXT_PLAIN)
    public String getSyncForm(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();//传递给前台的数据集合
        try {
            User user = HrmUserVarify.getUser(request, response);
            apiDatas = getService(user).getSyncForm(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("/syncHolidaySet")
    @Produces(MediaType.TEXT_PLAIN)
    public String syncHolidaySet(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();//传递给前台的数据集合
        try {
            User user = HrmUserVarify.getUser(request, response);
            apiDatas = getService(user).syncHolidaySet(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);
    }
}
