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.KQScheduleCodeService;
import com.engine.kq.service.impl.KQScheduleCodeServiceImpl;
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;

/**
 * 外部集成考勤
 * 
 * @author lvyi
 * 
 */
public class KQScheduleCodeAction {
	private BaseBean logger = new BaseBean();

	private KQScheduleCodeService getService(User user) {
		return (KQScheduleCodeService) ServiceUtil.getService(KQScheduleCodeServiceImpl.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("ScheduleCode:Edit", user));
			apidatas.put("status", "1");
		} catch (Exception e) {
			apidatas.put("status", "-1");
			logger.writeLog(e);
		}
		return JSONObject.toJSONString(apidatas);
	}
	
	/**
	 * 获取考勤编号管理查询条件
	 * @param request
	 * @param response
	 * @return
	 */
	@GET
	@Path("/getScheduleCodeCondition")
	@Produces(MediaType.TEXT_PLAIN)
	public String getScheduleCodeCondition(@Context HttpServletRequest request, @Context HttpServletResponse response) {
		Map<String, Object> apidatas = new HashMap<String, Object>();
		try {
			User user = HrmUserVarify.getUser(request, response);
			apidatas = getService(user).getScheduleCodeCondition(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("/getScheduleCodeList")
	@Produces(MediaType.TEXT_PLAIN)
	public String getScheduleCodeList(@Context HttpServletRequest request, @Context HttpServletResponse response) {
		Map<String, Object> apidatas = new HashMap<String, Object>();
		try {
			User user = HrmUserVarify.getUser(request, response);
			apidatas = getService(user).getScheduleCodeList(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("/getScheduleCodeForm")
	@Produces(MediaType.TEXT_PLAIN)
	public String getScheduleCodeForm(@Context HttpServletRequest request, @Context HttpServletResponse response) {
		Map<String, Object> apidatas = new HashMap<String, Object>();
		try {
			User user = HrmUserVarify.getUser(request, response);
			apidatas = getService(user).getScheduleCodeForm(ParamUtil.request2Map(request), user);
		} catch (Exception e) {
			apidatas.put("status", "-1");
			logger.writeLog(e);
		}
		return JSONObject.toJSONString(apidatas, SerializerFeature.DisableCircularReferenceDetect);
	}
	
	/**
	 * 保存考勤编号管理
	 * @param request
	 * @param response
	 * @return
	 */
	@POST
	@Path("/saveScheduleCode")
	@Produces(MediaType.TEXT_PLAIN)
	public String saveScheduleCode(@Context HttpServletRequest request, @Context HttpServletResponse response) {
		Map<String, Object> apidatas = new HashMap<String, Object>();
		try {
			User user = HrmUserVarify.getUser(request, response);
			apidatas = getService(user).saveScheduleCode(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("/getScheduleDeviceCondition")
	@Produces(MediaType.TEXT_PLAIN)
	public String getScheduleDeviceCondition(@Context HttpServletRequest request, @Context HttpServletResponse response) {
		Map<String, Object> apidatas = new HashMap<String, Object>();
		try {
			User user = HrmUserVarify.getUser(request, response);
			apidatas = getService(user).getScheduleDeviceCondition(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("/getScheduleDeviceList")
	@Produces(MediaType.TEXT_PLAIN)
	public String getScheduleDeviceList(@Context HttpServletRequest request, @Context HttpServletResponse response) {
		Map<String, Object> apidatas = new HashMap<String, Object>();
		try {
			User user = HrmUserVarify.getUser(request, response);
			apidatas = getService(user).getScheduleDeviceList(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("/getScheduleDeviceForm")
	@Produces(MediaType.TEXT_PLAIN)
	public String getScheduleDeviceForm(@Context HttpServletRequest request, @Context HttpServletResponse response) {
		Map<String, Object> apidatas = new HashMap<String, Object>();
		try {
			User user = HrmUserVarify.getUser(request, response);
			apidatas = getService(user).getScheduleDeviceForm(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("/saveScheduleDevice")
	@Produces(MediaType.TEXT_PLAIN)
	public String saveScheduleDevice(@Context HttpServletRequest request, @Context HttpServletResponse response) {
		Map<String, Object> apidatas = new HashMap<String, Object>();
		try {
			User user = HrmUserVarify.getUser(request, response);
			apidatas = getService(user).saveScheduleDevice(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("/delScheduleDevice")
	@Produces(MediaType.TEXT_PLAIN)
	public String delScheduleDevice(@Context HttpServletRequest request, @Context HttpServletResponse response) {
		Map<String, Object> apidatas = new HashMap<String, Object>();
		try {
			User user = HrmUserVarify.getUser(request, response);
			apidatas = getService(user).delScheduleDevice(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("/getImportForm")
	@Produces(MediaType.TEXT_PLAIN)
	public String getScheduleCodeImportForm(@Context HttpServletRequest request, @Context HttpServletResponse response) {
		Map<String, Object> apidatas = new HashMap<String, Object>();
		try {
			User user = HrmUserVarify.getUser(request, response);
			apidatas = getService(user).getScheduleCodeImportForm(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("/saveImport")
	@Produces(MediaType.TEXT_PLAIN)
	public String saveScheduleCodeImport(@Context HttpServletRequest request, @Context HttpServletResponse response) {
		Map<String, Object> apidatas = new HashMap<String, Object>();
		try {
			User user = HrmUserVarify.getUser(request, response);
			apidatas = getService(user).saveScheduleCodeImport(ParamUtil.request2Map(request), user, request);
		} catch (Exception e) {
			apidatas.put("status", "-1");
			logger.writeLog(e);
		}
		return JSONObject.toJSONString(apidatas);
	}
}
