package com.goafanti.activityUser.service.impl;

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

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

import com.goafanti.activityUser.bo.ActivityUserListBo;
import com.goafanti.activityUser.service.ActivityUserService;
import com.goafanti.common.dao.ActivityUserMapper;
import com.goafanti.common.model.ActivityUser;
import com.goafanti.common.model.ActivityUserKey;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.core.shiro.token.TokenManager;

@Service
public class ActivityUserServiceImpl extends BaseMybatisDao<ActivityUserMapper> implements ActivityUserService {

	@Autowired
	private ActivityUserMapper activityUserMapper;

	@Override
	public ActivityUser selectByUid(String uid) {
		return activityUserMapper.selectByUid(uid);
	}

	@Override
	public int save(Long aid) {
		ActivityUser au = new ActivityUser();
		au.setAid(aid);
		au.setUid(TokenManager.getUserId());
		Calendar now = Calendar.getInstance();
		now.set(Calendar.MILLISECOND, 0);
		au.setCreateTime(now.getTime());
		au.setLastUpdateTime(au.getCreateTime());
		return activityUserMapper.insert(au);
	}

	@Override
	public int update(ActivityUser au) {
		Calendar now = Calendar.getInstance();
		now.set(Calendar.MILLISECOND, 0);
		au.setLastUpdateTime(now.getTime());
		return activityUserMapper.updateByPrimaryKey(au);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pagination<ActivityUserListBo> listActivityUser(String activityName, String mobile, String username,
			Integer number, Integer pSize, Integer pNo) {

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

		if (pSize == null || pSize < 0 || pSize > 10) {
			pSize = 10;
		}

		return (Pagination<ActivityUserListBo>) findPage("findActivityUserListByPage", "findActivityUserCount",
				disposeActivityList(activityName, mobile, number, username, null), pNo, pSize);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pagination<ActivityUserListBo> listActivityOrg(String activityName, String mobile, String unitName,
			Integer number, Integer pSize, Integer pNo) {
		if (pNo == null || pNo < 0) {
			pNo = 1;
		}

		if (pSize == null || pSize < 0 || pSize > 10) {
			pSize = 10;
		}
		return (Pagination<ActivityUserListBo>) findPage("findActivityOrgListByPage", "findActivityOrgCount",
				disposeActivityList(activityName, mobile, number, null, unitName), pNo, pSize);
	}

	private Map<String, Object> disposeActivityList(String activityName, String mobile, Integer number, String username,
			String unitName) {
		Map<String, Object> params = new HashMap<>();

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

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

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

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

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

	@Override
	public int batchDeleteByPrimaryKey(Long aid, String uid) {
		return activityUserMapper.batchDeleteByPrimaryKey(aid, uid);
	}

	@Override
	public ActivityUser selectByPrimaryKey(ActivityUserKey key) {
		return activityUserMapper.selectByPrimaryKey(key);
	}

	@Override
	public ActivityUser selectByAidAndUid(Long aid, String uid) {
		return activityUserMapper.selectByAidAndUid(aid, uid);
	}

	@Override
	public List<ModelMap> selectUserActivities() {
		return activityUserMapper.selectUserActivities(TokenManager.getUserId());
	}

}
