package com.goafanti.activityUser.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.goafanti.activityUser.service.ActivityUserService;
import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.BaseApiController;
import com.goafanti.common.model.ActivityUser;
import com.goafanti.core.shiro.token.TokenManager;

@RestController
@RequestMapping(value = "/api/user/activity")
public class UserActivityUserApiController extends BaseApiController {
	@Resource
	private ActivityUserService activityUserService;

	/**
	 * 报名
	 */
	@RequestMapping(value = "/signUp", method = RequestMethod.POST)
	public Result signUp(Long aid) {
		Result res = new Result();
		if (null == aid) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "活动ID"));
			return res;
		}
		ActivityUser au = activityUserService.selectByAidAndUid(aid, TokenManager.getUserId());
		if (null == au) {
			res.setData(activityUserService.save(aid));
		} else {
			res.setData(activityUserService.update(au));
		}
		return res;
	}

	/**
	 * 取消报名
	 */
	@RequestMapping(value = "/signOff", method = RequestMethod.POST)
	public Result signOff(Long aid) {
		Result res = new Result();
		if (null == aid) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "活动ID"));
			return res;
		}
		return res.data(activityUserService.batchDeleteByPrimaryKey(aid, TokenManager.getUserId()));
	}

	/**
	 * 取消报名
	 */
	@RequestMapping(value = "/isSigned", method = RequestMethod.GET)
	public Result isSigned(Long aid) {
		Result res = new Result();
		if (null == aid) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "活动ID"));
			return res;
		}
		return res.data(activityUserService.selectByAidAndUid(aid, TokenManager.getUserId()) != null);
	}
}
