package com.ym.mec.biz.service.impl;

import java.util.Date;

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

import com.ym.mec.biz.dal.dao.TenantEntryActivitiesDao;
import com.ym.mec.biz.dal.entity.TenantEntryActivities;
import com.ym.mec.biz.service.TenantEntryActivitiesService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.common.service.impl.BaseServiceImpl;

@Service
public class TenantEntryActivitiesServiceImpl extends BaseServiceImpl<Integer, TenantEntryActivities> implements TenantEntryActivitiesService {

	@Autowired
	private TenantEntryActivitiesDao tenantEntryActivitiesDao;

	@Override
	public BaseDAO<Integer, TenantEntryActivities> getDAO() {
		return tenantEntryActivitiesDao;
	}

	@Override
	public boolean merge(TenantEntryActivities tenantEntryActivites) {

		if (!tenantEntryActivites.getStartDate().before(tenantEntryActivites.getEndDate())) {
			throw new BizException("活动开始时间必须在结束时间之前");
		}

		Date date = new Date();
		if (tenantEntryActivites.getId() != null) {
			TenantEntryActivities orginTenantEntryActivites = tenantEntryActivitiesDao.get(tenantEntryActivites.getId());

			if (orginTenantEntryActivites == null) {
				throw new BizException("操作失败，请检查输入参数");
			}
			tenantEntryActivites.setCreateTime(orginTenantEntryActivites.getCreateTime());
			tenantEntryActivites.setUpdateTime(date);
			tenantEntryActivitiesDao.update(tenantEntryActivites);
		} else {
			tenantEntryActivites.setCreateTime(date);
			tenantEntryActivites.setUpdateTime(date);
			tenantEntryActivitiesDao.insert(tenantEntryActivites);
		}
		return true;
	}

}