package org.fouram.controller;

import org.apache.commons.lang3.StringUtils;
import org.fouram.core.base.controller.BaseController;
import org.fouram.core.util.AppUtil;
import org.fouram.core.util.AppUtil.ResultConstant;
import org.fouram.core.util.LoggerUtil;
import org.fouram.service.SapApprovalInfoService;
import org.fouram.service.SapCheckOptionService;
import org.fouram.service.SapCheckService;
import org.fouram.service.SapEmployeeTimeService;
import org.fouram.service.WxDepartService;
import org.fouram.service.WxUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/report")
public class ReportController extends BaseController {

	@Autowired
	private WxUserService wxUserService;
	@Autowired
	private WxDepartService wxDepartService;
	@Autowired
	private SapCheckService sapCheckService;
	@Autowired
	private SapCheckOptionService sapCheckOptionService;
	@Autowired
	private SapEmployeeTimeService employeeTimeService;
	@Autowired
	private SapApprovalInfoService sapApprovalInfoService;

	/**
	 * http://localhost:8080/sapcms/report/initReport?startDay=2020-05-11&endDay=2020-05-15
	 * 
	 * @return
	 */
	@RequestMapping(value = "/initReport", produces = "application/json;charset=utf-8")
	@ResponseBody
	public Object initReport() {
		try {
			String startDay = getPageData().getString("startDay");
			String endDay = getPageData().getString("endDay");
			if(StringUtils.isBlank(startDay)) {
				return AppUtil.error("参数startDay为空");
			}
			if(StringUtils.isBlank(endDay)) {
				return AppUtil.error("参数endDay为空");
			}
			System.out.println("=====================================================");
			wxDepartService.saveDeparts();
			System.out.println("=====================================================");
			wxUserService.saveUsers();
			System.out.println("=====================================================");
			sapCheckService.saveCheckInOffOuts(startDay, endDay);
			System.out.println("=====================================================");
			employeeTimeService.saveEmployeeTimes();
			System.out.println("=====================================================");
			sapCheckOptionService.saveOptions(endDay);
			System.out.println("=====================================================");
			sapApprovalInfoService.saveApprovalInfos(endDay.substring(0, 7));
			System.out.println("=====================================================");
			return AppUtil.success();
		} catch (Exception e) {
			LoggerUtil.error(e);
			return AppUtil.error(ResultConstant.WEB_ERR_MSG);
		}
	}
	
	/**
	 * http://localhost:8080/sapcms/report/saveReport?startDay=2020-05-11&endDay=2020-05-15
	 * 
	 * @return
	 */
	@RequestMapping(value = "/saveReport", produces = "application/json;charset=utf-8")
	@ResponseBody
	public Object saveReport() {
		try {
			String startDay = getPageData().getString("startDay");
			String endDay = getPageData().getString("endDay");
			if(StringUtils.isBlank(startDay)) {
				return AppUtil.error("参数startDay为空");
			}
			if(StringUtils.isBlank(endDay)) {
				return AppUtil.error("参数endDay为空");
			}
			sapCheckService.saveReportResultData(startDay, endDay);
			return AppUtil.success();
		} catch (Exception e) {
			LoggerUtil.error(e);
			return AppUtil.error(ResultConstant.WEB_ERR_MSG);
		}
	}
}