package org.fouram.service;

import org.fouram.core.util.DateUtil;
import org.fouram.core.util.LoggerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CheckTaskService {

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

	public void taskBiz() {
		LoggerUtil.info(DateUtil.getTime());
		try {
			if (DateUtil.getHour().equals("10") || DateUtil.getHour().equals("20")) {
				LoggerUtil.info("执行开始:" + DateUtil.getTime());
				executeBiz(DateUtil.getDay());
				LoggerUtil.info("执行结束:"  + DateUtil.getTime());
			}
		} catch (Exception e) {
			LoggerUtil.error(e);
		}
	}

	public void executeBiz(String checkinDay) throws Exception {
		String startDay = checkinDay;
		String endDay = checkinDay;
		wxDepartService.saveDeparts();
		LoggerUtil.info("saveDeparts:" + DateUtil.getTime());
		wxUserService.saveUsers();
		LoggerUtil.info("saveUsers:" + DateUtil.getTime());
		sapCheckService.saveCheckInOffOuts(startDay, endDay);
		LoggerUtil.info("saveCheckInOffOuts:" + DateUtil.getTime());
		// 判断打卡记录小于10条记录认为是假期
		if (sapCheckService.queryCheckInTotal() < 10) {
			return;
		}
		sapCheckdateService.saveCheckout(startDay);
		LoggerUtil.info("saveCheckout:" + DateUtil.getTime());
		employeeTimeService.saveEmployeeTimes();
		LoggerUtil.info("saveEmployeeTimes:" + DateUtil.getTime());
		sapCheckOptionService.saveOptions(endDay);
		LoggerUtil.info("saveOptions:" + DateUtil.getTime());
		sapApprovalInfoService.saveApprovalInfos(endDay.substring(0, 7));
		LoggerUtil.info("saveApprovalInfos:" + DateUtil.getTime());
		sapCheckService.saveReportResultData(startDay, endDay);
		LoggerUtil.info("saveReportResultData:" + DateUtil.getTime());
		sapCheckService.updateReportResultData(startDay, endDay);
		LoggerUtil.info("updateReportResultData:" + DateUtil.getTime());
	}
}
