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.KQGroupService;
import com.engine.kq.service.impl.KQGroupServiceImpl;
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 KQGroupAction extends BaseBean {

    private KQGroupService getService(User user) {
        return (KQGroupService) ServiceUtil.getService(KQGroupServiceImpl.class, user);
    }

    /**
     * 是否有权限
     *
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/getHasRight")
    @Produces(MediaType.TEXT_PLAIN)
    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", HrmUserVarify.checkUserRight("HrmKQGroup:Add", user));
            apidatas.put("status", "1");
        } catch (Exception e) {
            apidatas.put("status", "-1");
        }
        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);
        } catch (Exception e) {
            apidatas.put("status", "-1");
            writeLog(e);
        }
        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);
        } catch (Exception e) {
            apidatas.put("status", "-1");
            writeLog(e);
        }
        return JSONObject.toJSONString(apidatas);
    }


    /**
     * 获取新建编辑表单
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getForm")
    @Produces(MediaType.TEXT_PLAIN)
    public String getForm(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apidatas = getService(user).getForm(ParamUtil.request2Map(request), user);

        } catch (Exception e) {
            apidatas.put("status", "-1");
            writeLog(e);
        }
        return JSONObject.toJSONString(apidatas);
    }


    /**
     * 保存
     *
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/save")
    @Produces(MediaType.TEXT_PLAIN)
    public String save(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apidatas = getService(user).save(ParamUtil.request2Map(request), user);

        } catch (Exception e) {
            apidatas.put("status", "-1");
            writeLog(e);
        }
        return JSONObject.toJSONString(apidatas);
    }


    /**
     * 删除
     *
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/del")
    @Produces(MediaType.TEXT_PLAIN)
    public String delete(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apidatas = getService(user).del(ParamUtil.request2Map(request), user);
        } catch (Exception e) {
            apidatas.put("status", "-1");
            writeLog(e);
        }
        return JSONObject.toJSONString(apidatas);
    }

    /**
     * 获取Tab
     *
     * @param request
     * @param response
     * @return
     */
    @POST
    @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);
        } catch (Exception e) {
            apidatas.put("status", "-1");
            writeLog(e);
        }
        return JSONObject.toJSONString(apidatas);
    }

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

    /**
     * 导出考勤方式
     *
     * @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), request, response, user);
        } catch (Exception e) {
            apidatas.put("status", "-1");
            writeLog(e);
        }
        return JSONObject.toJSONString(apidatas);
    }

    /**
     * 考勤组树形
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getGroupTree")
    @Produces(MediaType.TEXT_PLAIN)
    public String getGroupTree(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apidatas = getService(user).getGroupTree(ParamUtil.request2Map(request), user);
        } catch (Exception e) {
            apidatas.put("status", "-1");
        }
        return JSONObject.toJSONString(apidatas);
    }

    /**
     * 获取排班周期新建编辑表单
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getShiftCycleForm")
    @Produces(MediaType.TEXT_PLAIN)
    public String getShiftCycleForm(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apidatas = getService(user).getShiftCycleForm(ParamUtil.request2Map(request), user);

        } catch (Exception e) {
            apidatas.put("status", "-1");
            writeLog(e);
        }
        return JSONObject.toJSONString(apidatas);
    }

    /**
     * 获取考勤组tabs
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getMainTabs")
    @Produces(MediaType.TEXT_PLAIN)
    public String getMainTabs(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apidatas = getService(user).getMainTabs(ParamUtil.request2Map(request), user);

        } catch (Exception e) {
            apidatas.put("status", "-1");
            writeLog(e);
        }
        return JSONObject.toJSONString(apidatas);
    }

    /**
     * 获取考勤组人员搜索条件
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getGroupMemeberCondition")
    @Produces(MediaType.TEXT_PLAIN)
    public String getGroupMemeberCondition(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apidatas = getService(user).getGroupMemeberCondition(ParamUtil.request2Map(request), user);

        } catch (Exception e) {
            apidatas.put("status", "-1");
            writeLog(e);
        }
        return JSONObject.toJSONString(apidatas);
    }

    /**
     * 获取添加考勤组成员表单
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getAddMemberCondition")
    @Produces(MediaType.TEXT_PLAIN)
    public String getAddMemberCondition(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apidatas = getService(user).getAddMemberCondition(ParamUtil.request2Map(request), user);

        } catch (Exception e) {
            apidatas.put("status", "-1");
            writeLog(e);
        }
        return JSONObject.toJSONString(apidatas);
    }

    /**
     * 保存考勤组人员信息
     *
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/saveAddMember")
    @Produces(MediaType.TEXT_PLAIN)
    public String saveAddMember(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apidatas = getService(user).saveAddMenber(ParamUtil.request2Map(request), user);

        } catch (Exception e) {
            apidatas.put("status", "-1");
            writeLog(e);
        }
        return JSONObject.toJSONString(apidatas);
    }

    /**
     * 获取考勤组人员信息
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getGroupMemberList")
    @Produces(MediaType.TEXT_PLAIN)
    public String getGroupMemberList(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apidatas = getService(user).getGroupMemberList(ParamUtil.request2Map(request), user);

        } catch (Exception e) {
            apidatas.put("status", "-1");
            writeLog(e);
        }
        return JSONObject.toJSONString(apidatas);
    }
}
