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.utils.StringUtils;

@RestController
@RequestMapping(value = "/api/admin/activityUser")
public class AdminActivityUserApiController extends BaseApiController {
	@Resource
	private ActivityUserService activityUserService;

	/**
	 * 活动圈个人用户报名列表
	 */
	@RequestMapping(value = "/userList", method = RequestMethod.GET)
	public Result activityUserList(String activityName, String mobile, String username, Integer number, String pageSize,
			String pageNo) {
		Result res = new Result();
		res.setData(activityUserService.listActivityUser(activityName, mobile, username, number, getPSize(pageSize),
				getPNo(pageNo)));
		return res;
	}

	/**
	 * 活动圈组织用户报名列表
	 */
	@RequestMapping(value = "/orgList", method = RequestMethod.GET)
	public Result activityOrgList(String activityName, String mobile, String unitName, Integer number, String pageSize,
			String pageNo) {
		Result res = new Result();
		res.setData(activityUserService.listActivityOrg(activityName, mobile, unitName, number, getPSize(pageSize),
				getPNo(pageNo)));
		return res;
	}

	/**
	 * 批量删除活动报名记录
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public Result delete(String aid, String uid) {
		Result res = new Result();
		if (StringUtils.isBlank(aid) || StringUtils.isBlank(uid) || !StringUtils.isNumeric(aid)) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", ""));
		} else {
			res.setData(activityUserService.batchDeleteByPrimaryKey(Long.parseLong(aid), uid));
		}
		return res;
	}

	private Integer getPNo(String pageNo) {
		Integer pNo = 1;
		if (StringUtils.isNumeric(pageNo)) {
			pNo = Integer.parseInt(pageNo);
		}
		return pNo;
	}

	private Integer getPSize(String pageSize) {
		Integer pSize = 10;
		if (StringUtils.isNumeric(pageSize)) {
			pSize = Integer.parseInt(pageSize);
		}
		return pSize;
	}
}
