package com.goafanti.activity.service.impl;

import java.text.ParseException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.goafanti.activity.bo.ActivityListBo;
import com.goafanti.activity.service.ActivityService;
import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.dao.ActivityMapper;
import com.goafanti.common.model.Activity;
import com.goafanti.common.utils.DateUtils;
import com.goafanti.common.utils.LoggerUtils;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.mybatis.JDBCIdGenerator;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.news.service.NewsService;

@Service
public class ActivityServiceImpl extends BaseMybatisDao<ActivityMapper> implements ActivityService {
	@Autowired
	private ActivityMapper	activityMapper;
	@Autowired
	private JDBCIdGenerator	idGenerator;
	
	private static final Logger	logger	= LoggerFactory.getLogger(NewsService.class);

	@SuppressWarnings("unchecked")
	@Override
	public Pagination<ActivityListBo> listActivity(String sTime, String eTime, String name, String host, Integer type,
			Integer form, Integer pNo, Integer pSize) {

		Map<String, Object> params = new HashMap<>();

		try {
			params.put("sTime", StringUtils.isBlank(sTime) ? null : DateUtils.parseDate(sTime, AFTConstants.YYYYMMDDHHMMSS));
			params.put("eTime", StringUtils.isBlank(eTime) ? null
					: DateUtils.addDays(DateUtils.parseDate(eTime, AFTConstants.YYYYMMDDHHMMSS), 1));
		} catch (ParseException e) {
		}

		if (StringUtils.isNotBlank(name)) {
			params.put("name", name);
		}

		if (StringUtils.isNotBlank(host)) {
			params.put("host", host);
		}

		if (null != type) {
			params.put("type", type);
		}

		if (null != form) {
			params.put("form", form);
		}

		if (pNo == null || pNo < 0) {
			pNo = 1;
		}

		if (pSize == null || pSize < 0 || pSize > 10) {
			pSize = 10;
		}
		return (Pagination<ActivityListBo>) findPage("findActivityListByPage", "findActivityCount", params, pNo, pSize);
	}

	@Override
	public void save(Activity a, String startTimeFormattedDate, String endTimeFormattedDate,
			String enrollDeadlineFormattedDate) {
		Calendar now = Calendar.getInstance();
		now.set(Calendar.MILLISECOND, 0);
		a.setCreateTime(now.getTime());
		a.setId(idGenerator.generateId());
		activityMapper
				.insert(disposeActivity(a, startTimeFormattedDate, endTimeFormattedDate, enrollDeadlineFormattedDate));
	}

	@Override
	public int update(Activity a, String startTimeFormattedDate, String endTimeFormattedDate,
			String enrollDeadlineFormattedDate, Long id) {
		a.setId(id);
		return activityMapper.updateByPrimaryKeyWithBLOBs(
				disposeActivity(a, startTimeFormattedDate, endTimeFormattedDate, enrollDeadlineFormattedDate));
	}

	private Activity disposeActivity(Activity a, String startTimeFormattedDate, String endTimeFormattedDate,
			String enrollDeadlineFormattedDate) {
		try {
			if (StringUtils.isNotBlank(startTimeFormattedDate)) {
				a.setStartTime(DateUtils.parseDate(startTimeFormattedDate, AFTConstants.YYYYMMDDHHMMSS));
			}
			if (StringUtils.isNotBlank(endTimeFormattedDate)) {
				a.setEndTime(DateUtils.parseDate(endTimeFormattedDate, AFTConstants.YYYYMMDDHHMMSS));
			}
			if (StringUtils.isNotBlank(enrollDeadlineFormattedDate)) {
				a.setEnrollDeadline(DateUtils.parseDate(enrollDeadlineFormattedDate, AFTConstants.YYYYMMDDHHMMSS));
			}
		} catch (ParseException e) {
		}
		return a;
	}

	@Override
	public int batchDeleteByPrimaryKey(List<String> id) {
		return activityMapper.batchDeleteByPrimaryKey(id);
	}

	@Override
	public Activity findActivityDetail(Long id) {
		return activityMapper.selectByPrimaryKey(id);
	}

	@Cacheable(value = "ActivityPortalListCache", key = "'ActivityPortalList:Page:'+#pageNo+'Size:'+#pageSize+'Form:'+#form")
	public List<Activity> findPortalList(Integer pageNo, Integer pageSize, Integer form) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("pageSize", pageSize);
		params.put("pageNo", pageNo);
		params.put("form", form);
		return activityMapper.findPortalList(params);
	}

	@CacheEvict(value = "ActivityPortalListCache", allEntries = true)
	public void cleanActivityPortalList() {
		LoggerUtils.debug(logger, "清除门户端科技活动列表缓存:[%s]");
		
	}

}
