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.KQShiftScheduleService;
import com.engine.kq.service.impl.KQShiftScheduleServiceImpl;
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 KQShiftScheduleAction extends BaseBean {

	private KQShiftScheduleService getService(User user) {
		return (KQShiftScheduleService) ServiceUtil.getService(KQShiftScheduleServiceImpl.class, user);
	}

	/**
	 * 查询条件
	 * @param request
	 * @param response
	 * @return
	 */
	@POST
	@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, SerializerFeature.DisableCircularReferenceDetect);
	}
	/**
	 * 获取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("/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
	 */
	@POST
	@Path("/getShiftSchedule")
	@Produces(MediaType.TEXT_PLAIN)
	public String getShiftSchedule(@Context HttpServletRequest request, @Context HttpServletResponse response) {
		Map<String, Object> apidatas = new HashMap<String, Object>();
		try {
			User user = HrmUserVarify.getUser(request, response);
			apidatas = getService(user).getShiftSchedule(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("/delete")
	@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);
	}

	/**
	 * 获取批量排班Form
	 * @param request
	 * @param response
	 * @return
	 */
	@POST
	@Path("/getBatchShiftScheduleFrom")
	@Produces(MediaType.TEXT_PLAIN)
	public String getBatchShiftScheduleFrom(@Context HttpServletRequest request, @Context HttpServletResponse response) {
		Map<String, Object> apidatas = new HashMap<String, Object>();
		try {
			User user = HrmUserVarify.getUser(request, response);
			apidatas = getService(user).getBatchShiftScheduleFrom(ParamUtil.request2Map(request), user);
		} catch (Exception e) {
			apidatas.put("status", "-1");
			writeLog(e);
		}
		return JSONObject.toJSONString(apidatas, SerializerFeature.DisableCircularReferenceDetect);
	}

	/**
	 *批量保存
	 * @param request
	 * @param response
	 * @return
	 */
	@POST
	@Path("/batchSave")
	@Produces(MediaType.TEXT_PLAIN)
	public String batchSave(@Context HttpServletRequest request, @Context HttpServletResponse response) {
		Map<String, Object> apidatas = new HashMap<String, Object>();
		try {
			User user = HrmUserVarify.getUser(request, response);
			apidatas = getService(user).batchSave(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.TEXT_PLAIN)
	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
	 */
	@POST
	@Path("/getShiftScheduleTotal")
	@Produces(MediaType.TEXT_PLAIN)
	public String getShiftScheduleTotal(@Context HttpServletRequest request, @Context HttpServletResponse response) {
		Map<String, Object> apidatas = new HashMap<String, Object>();
		try {
			User user = HrmUserVarify.getUser(request, response);
			apidatas = getService(user).getShiftScheduleTotal(ParamUtil.request2Map(request), user);
		} catch (Exception e) {
			apidatas.put("status", "-1");
			writeLog(e);
		}
		return JSONObject.toJSONString(apidatas, SerializerFeature.DisableCircularReferenceDetect);
	}

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