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.KQAttendanceButtonService;
import com.engine.kq.service.impl.KQAttendanceButtonServiceImpl;
import java.util.HashMap;
import java.util.Map;
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 weaver.general.BaseBean;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;


/**
 * 考勤按钮控制
 */
public class KQAttendanceButtonAction {
	private BaseBean logger = new BaseBean();

	private KQAttendanceButtonService getService(User user) {
		return (KQAttendanceButtonServiceImpl) ServiceUtil.getService(KQAttendanceButtonServiceImpl.class, user);
	}

  /**
   * 获取考勤组的基本信息的
   * @param request
   * @param response
   * @return
   */
  @POST
  @Path("/getButtonBaseInfo")
  @Produces(MediaType.TEXT_PLAIN)
  public String getButtonBaseInfo(@Context HttpServletRequest request, @Context HttpServletResponse response) {
    Map<String, Object> apidatas = new HashMap<String, Object>();
    try {
      User user = HrmUserVarify.getUser (request , response) ;
      apidatas = getService(user).getButtonBaseInfo(ParamUtil.request2Map(request), user);
    } catch (Exception e) {
      apidatas.put("status", "-1");
      logger.writeLog(e);
    }
    return JSONObject.toJSONString(apidatas);
  }

	/**
	 * 获取签到签退按钮
   * 以及考勤流程数据
   * 以及上次签到时间
	 * @param request
	 * @param response
	 * @return
	 */
	@POST
	@Path("/getButtons")
	@Produces(MediaType.TEXT_PLAIN)
	public String getButtons(@Context HttpServletRequest request, @Context HttpServletResponse response) {
		Map<String, Object> apidatas = new HashMap<String, Object>();
		try {
			User user = HrmUserVarify.getUser (request , response) ;
      apidatas = getService(user).getButtons(ParamUtil.request2Map(request), user);
		} catch (Exception e) {
			apidatas.put("status", "-1");
			logger.writeLog(e);
		}
		return JSONObject.toJSONString(apidatas);
	}

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

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

  /**
   * 外勤签到签退 关联考勤的时候，打卡范围
   * @param request
   * @param response
   * @return
   */
  @POST
  @Path("/getOutButtonRange")
  @Produces(MediaType.TEXT_PLAIN)
  public String getOutButtonRange(@Context HttpServletRequest request, @Context HttpServletResponse response) {
    Map<String, Object> apidatas = new HashMap<String, Object>();
    try {
      User user = HrmUserVarify.getUser (request , response) ;
      apidatas = getService(user).getOutButtonRange(ParamUtil.request2Map(request), user);
    } catch (Exception e) {
      apidatas.put("status", "-1");
      logger.writeLog(e);
    }
    return JSONObject.toJSONString(apidatas);
  }

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

  /**
   * 外勤记录 查询条件
   * @param request
   * @param response
   * @return
   */
  @GET
  @Path("/getOutButtonCondition")
  @Produces(MediaType.TEXT_PLAIN)
  public String getOutButtonCondition(@Context HttpServletRequest request, @Context HttpServletResponse response) {
    Map<String, Object> apidatas = new HashMap<String, Object>();
    try {
      User user = HrmUserVarify.getUser (request , response) ;
      apidatas = getService(user).getOutButtonCondition(request,ParamUtil.request2Map(request), user);
    } catch (Exception e) {
      apidatas.put("status", "-1");
      logger.writeLog(e);
    }
    return JSONObject.toJSONString(apidatas);
  }

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

  /**
   * 获取外勤签到配置
   * @param request
   * @param response
   * @return
   */
  @GET
  @Path("/getOutSignSetting")
  @Produces(MediaType.TEXT_PLAIN)
  public String getOutSignSetting(@Context HttpServletRequest request, @Context HttpServletResponse response) {
    Map<String, Object> apidatas = new HashMap<String, Object>();
    try {
      User user = HrmUserVarify.getUser (request , response) ;
      apidatas = getService(user).getOutSignSetting(ParamUtil.request2Map(request), user);
    } catch (Exception e) {
      apidatas.put("status", "-1");
      logger.writeLog(e);
    }
    return JSONObject.toJSONString(apidatas);
  }

  /**
   * 保存外勤签到配置
   * @param request
   * @param response
   * @return
   */
  @POST
  @Path("/saveOutSignSetting")
  @Produces(MediaType.TEXT_PLAIN)
  public String saveOutSignSetting(@Context HttpServletRequest request, @Context HttpServletResponse response) {
    Map<String, Object> apidatas = new HashMap<String, Object>();
    try {
      User user = HrmUserVarify.getUser (request , response) ;
      apidatas = getService(user).saveOutSignSetting(ParamUtil.request2Map(request), user);
    } catch (Exception e) {
      apidatas.put("status", "-1");
      logger.writeLog(e);
    }
    return JSONObject.toJSONString(apidatas);
  }

  /**
   * EM端自动打卡设置界面
   * @param request
   * @param response
   * @return
   */
  @POST
  @Path("/getAutoEMSet")
  @Produces(MediaType.TEXT_PLAIN)
  public String getAutoEMSet(@Context HttpServletRequest request, @Context HttpServletResponse response) {
    Map<String, Object> apidatas = new HashMap<String, Object>();
    try {
      User user = HrmUserVarify.getUser (request , response) ;
      apidatas = getService(user).getAutoEMSet(ParamUtil.request2Map(request), user);
    } catch (Exception e) {
      apidatas.put("status", "-1");
      logger.writeLog(e);
    }
    return JSONObject.toJSONString(apidatas);
  }

  /**
   * 保存EM端自动打卡设置界面
   * @param request
   * @param response
   * @return
   */
  @POST
  @Path("/saveAutoEMSet")
  @Produces(MediaType.TEXT_PLAIN)
  public String saveAutoEMSet(@Context HttpServletRequest request, @Context HttpServletResponse response) {
    Map<String, Object> apidatas = new HashMap<String, Object>();
    try {
      User user = HrmUserVarify.getUser (request , response) ;
      apidatas = getService(user).saveAutoEMSet(ParamUtil.request2Map(request), user);
    } catch (Exception e) {
      apidatas.put("status", "-1");
      logger.writeLog(e);
    }
    return JSONObject.toJSONString(apidatas);
  }

  /**
   * 移动端切换地图，保存最新的当前用户选择的地图
   * @param request
   * @param response
   * @return
   */
  @POST
  @Path("/resetUserMap")
  @Produces(MediaType.TEXT_PLAIN)
  public String resetUserMap(@Context HttpServletRequest request, @Context HttpServletResponse response) {
    Map<String, Object> apidatas = new HashMap<String, Object>();
    try {
      User user = HrmUserVarify.getUser (request , response) ;
      apidatas = getService(user).resetUserMap(ParamUtil.request2Map(request), user);
    } catch (Exception e) {
      apidatas.put("status", "-1");
      logger.writeLog(e);
    }
    return JSONObject.toJSONString(apidatas);
  }

  /**
   * 获取用户最后一次选择的地图
   * @param request
   * @param response
   * @return
   */
  @POST
  @Path("/getUserLastMap")
  @Produces(MediaType.TEXT_PLAIN)
  public String getUserLastMap(@Context HttpServletRequest request, @Context HttpServletResponse response) {
    Map<String, Object> apidatas = new HashMap<String, Object>();
    try {
      User user = HrmUserVarify.getUser (request , response) ;
      apidatas = getService(user).getUserLastMap(ParamUtil.request2Map(request), user);
    } catch (Exception e) {
      apidatas.put("status", "-1");
      logger.writeLog(e);
    }
    return JSONObject.toJSONString(apidatas);
  }
}
