package org.fouram.service;

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

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.SapCheckOption;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;

import me.chanjar.weixin.cp.bean.oa.WxCpCheckinOption;
import me.chanjar.weixin.cp.bean.oa.WxCpCheckinOption.CheckinTime;

@Service
public class SapCheckOptionService extends BaseService {
	
	@Autowired
	private WxUserService wxUserService;

	public void saveOptions(String endDay) throws Exception {
		List<String> userIds = wxUserService.selectAllUserIds();
		Map<String, List<String>> map = groupList(userIds);
		List<Date> datetimes = Lists.newArrayList();
		datetimes.add(DateUtil.sdfTime.parse(endDay + " 00:00:00"));
		for (String key : map.keySet()) {
			List<WxCpCheckinOption> options = WXCpOaUtil.getCheckinOption(datetimes, map.get(key));
			for (WxCpCheckinOption option : options) {
				if (option.getGroup() != null && !option.getGroup().getCheckinDate().isEmpty()) {
					SapCheckOption check = new SapCheckOption();
					CheckinTime checkinTime = option.getGroup().getCheckinDate().get(0).getCheckinTime()[0];
					check.setUserId(option.getUserId());
					check.setWorkSec(checkinTime.getWorkSec());
					check.setOffWorkSec(checkinTime.getOffWorkSec());
					save(check);
				}
			}
		}
	}
	
	public void save(SapCheckOption check) throws Exception {
		save("SapCheckOptionMapper.save", check);
	}
}
