package com.uas.platform.b2b.mobile.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import com.uas.platform.b2b.dao.UserBaseInfoDao;
import com.uas.platform.b2b.mobile.dao.OutPlanDetailDao;
import com.uas.platform.b2b.mobile.dao.OutSignDao;
import com.uas.platform.b2b.mobile.model.OutPlanDetail;
import com.uas.platform.b2b.mobile.model.OutSign;
import com.uas.platform.b2b.mobile.service.OutSignService;

@Service
public class OutSignServiceImpl implements OutSignService {

	@Autowired
	private OutSignDao outSignDao;

	@Autowired
	private OutPlanDetailDao outPlanDetailDao;

	@Autowired
	private UserBaseInfoDao userBaseInfoDao;

	@Override
	public ModelMap saveOutSign(OutSign outSign) {
		ModelMap map = new ModelMap();
		if (outSign.getMo_signtime() == null) {
			outSign.setMo_signtime(new Date());
		}
		outSign.setMo_man(userBaseInfoDao.findByUserUU(outSign.getEmcode()).getUserName());
		outSignDao.save(outSign);
		if (outSign.getMpd_id() != null) { // 更新外勤计划中的签到时间
			OutPlanDetail detail = outPlanDetailDao.findOne(outSign.getMpd_id());
			if (detail != null) {
				if (detail.getMpd_firsttime() == null) {// 更新第一次和最后一次签到时间，如果第一次存在，则只更新最后一次时间
					detail.setMpd_firsttime(new Date());
					detail.setMpd_lasttime(new Date());
				} else {
					detail.setMpd_lasttime(new Date());
				}
				outPlanDetailDao.save(detail);
			}
		}
		map.put("success", true);
		return map;
	}

}
