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.KQReportService;
import com.engine.kq.service.impl.KQReportServiceImpl;
import weaver.general.BaseBean;
import weaver.general.Util;
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 KQReportAction extends BaseBean {

	private KQReportService getService(User user) {
		return (KQReportService) ServiceUtil.getService(KQReportServiceImpl.class, user);
	}

	/**
	 * 获取右键菜单
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@GET
	@Path("/getRightMenu")
	@Produces(MediaType.TEXT_PLAIN)
	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);
		} catch (Exception e) {
			apidatas.put("status", "-1");
			writeLog(e);
		}
		return JSONObject.toJSONString(apidatas);
	}

	/**
	 * 查询条件
	 * @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);
	}

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


	/**
	 * 获取考勤报表
	 * @param request
	 * @param response
	 * @return
	 */
	@POST
	@Path("/getKQReport")
	@Produces(MediaType.TEXT_PLAIN)
	public String getKQReport(@Context HttpServletRequest request, @Context HttpServletResponse response) {
		Map<String, Object> apidatas = new HashMap<String, Object>();
		try {
			User user = HrmUserVarify.getUser(request, response);
			String reportType = Util.null2String(request.getParameter("reportType"));
			if(reportType.equals("month")){
				apidatas = getService(user).getKQReport(ParamUtil.request2Map(request), user);
			}else if(reportType.equals("daily")){
				apidatas = getService(user).getKQDailyReport(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);
			String reportType = Util.null2String(request.getParameter("reportType"));
			if(reportType.equals("month")) {
				apidatas = getService(user).exportExcel(ParamUtil.request2Map(request), request, response, user);
			}else if(reportType.equals("daily")){
				apidatas = getService(user).exportDailyExcel(ParamUtil.request2Map(request), request, response, user);
			}
		} catch (Exception e) {
			apidatas.put("status", "-1");
			writeLog(e);
		}
		return JSONObject.toJSONString(apidatas);
	}
}
