package com.engine.kq.web;

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;
import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.kq.service.KQScheduleSignImportService;
import com.engine.kq.service.impl.KQScheduleSignImportServiceImpl;

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

	private KQScheduleSignImportService getService(User user) {
		return (KQScheduleSignImportService) ServiceUtil.getService(KQScheduleSignImportServiceImpl.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("HrmResourceEdit: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("/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);
		} 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 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).saveImport(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("/getImportSetForm")
	@Produces(MediaType.TEXT_PLAIN)
	public String getImportSetForm(@Context HttpServletRequest request, @Context HttpServletResponse response) {
		Map<String, Object> apidatas = new HashMap<String, Object>();
		try {
			User user = HrmUserVarify.getUser(request, response);
			apidatas = getService(user).getImportSetForm(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("/saveImportSet")
	@Produces(MediaType.TEXT_PLAIN)
	public String saveImportSet(@Context HttpServletRequest request, @Context HttpServletResponse response) {
		Map<String, Object> apidatas = new HashMap<String, Object>();
		try {
			User user = HrmUserVarify.getUser(request, response);
			apidatas = getService(user).saveImportSet(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("/synData")
	@Produces(MediaType.TEXT_PLAIN)
	public String synData(@Context HttpServletRequest request, @Context HttpServletResponse response) {
		Map<String, Object> apidatas = new HashMap<String, Object>();
		try {
			User user = HrmUserVarify.getUser(request, response);
			apidatas = getService(user).synData(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("/uploadData")
	@Produces(MediaType.APPLICATION_JSON)
	public String uploadData(@Context HttpServletRequest request, @Context HttpServletResponse response) {
		Map<String, Object> apidatas = new HashMap<String, Object>();
		try {
			apidatas = getService(null).uploadData(com.api.hrm.util.ServiceUtil.requestJson2Map(request), null);
		} catch (Exception e) {
			apidatas.put("status", "-1");
			logger.writeLog(e);
		}
		return JSONObject.toJSONString(apidatas);
	}
}
