package org.fouram.service;

import java.util.Date;
import java.util.List;

import org.fouram.core.base.service.BaseService;
import org.fouram.core.plugin.weixin.cp.util.WXCpOaUtil;
import org.fouram.core.util.DateUtil;
import org.fouram.entity.SapApprovalInfo;
import org.springframework.stereotype.Service;

@Service
public class SapApprovalInfoService extends BaseService {

	/**
	 * 保存补卡记录
	 * 
	 * @param yearMonth
	 * @throws Exception
	 */
	public void saveApprovalInfos(String yearMonth) throws Exception {
		Date startDate = DateUtil.sdfTime.parse(yearMonth + "-01 00:00:00");
		Date endDate = DateUtil.sdfTime.parse(DateUtil.getLastDay(yearMonth) + " 23:59:59");
		List<SapApprovalInfo> approvalInfos = WXCpOaUtil.getApprovalDetailList(startDate, endDate);
		for (SapApprovalInfo approvalInfo : approvalInfos) {
			if (selectListByUserIdAndDay(approvalInfo).isEmpty()) {
				save("SapApprovalInfoMapper.save", approvalInfo);
			}
		}
	}

	@SuppressWarnings("unchecked")
	public List<SapApprovalInfo> selectListByUserIdAndDay(SapApprovalInfo approvalInfo) throws Exception {
		return (List<SapApprovalInfo>) findList("SapApprovalInfoMapper.selectListByUserIdAndDay", approvalInfo);
	}
}
