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.KQSetupWizardService;
import com.engine.kq.service.impl.KQSetupWizardServiceImpl;
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.Path;
import javax.ws.rs.core.Context;
import java.util.HashMap;
import java.util.Map;

/**
 * 一键开启考勤
 */
public class KQSetupWizardAction {

    private KQSetupWizardService getService(User user) {
        return (KQSetupWizardService) ServiceUtil.getService(KQSetupWizardServiceImpl.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("KQ:SetupWizard", user);
            apiDatas.put("hasRight", hasRight);
            apiDatas.put("status", "1");
        } catch (Exception e) {
            e.printStackTrace();
            apiDatas.put("status", "-1");
        }
        return JSONObject.toJSONString(apiDatas);
    }

    /**
     * 获取右键菜单
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getRightMenu")
    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", true);
            apiDatas.put("api_errormsg", e.getMessage());
        }
        return JSONObject.toJSONString(apiDatas);
    }

    /**
     * 获取表单
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getSetupForm")
    public String getSetupForm(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();//传给前台的数据集合
        try {
            User user = HrmUserVarify.getUser(request, response);
            apiDatas = getService(user).getSetupForm(ParamUtil.request2Map(request), user);
            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("/getSetupSteps")
    public String getSetupSteps(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();//传给前台的数据集合
        try {
            User user = HrmUserVarify.getUser(request, response);
            apiDatas = getService(user).getSetupSteps(ParamUtil.request2Map(request), user);
            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("/performInitialization")
    public String performInitialization(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apiDatas = new HashMap<String, Object>();//传给前台的数据集合
        try {
            User user = HrmUserVarify.getUser(request, response);
            apiDatas = getService(user).performInitialization(ParamUtil.request2Map(request), user);
            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("/checkNeedInit")
    public String checkNeedInit(@Context HttpServletRequest request, @Context HttpServletResponse response) {
      Map<String, Object> apiDatas = new HashMap<String, Object>();//传给前台的数据集合
      try {
        User user = HrmUserVarify.getUser(request, response);
        apiDatas = getService(user).checkNeedInit(ParamUtil.request2Map(request), user);
        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);
    }
}
