package com.goafanti.star.controller;

import java.util.List;

import javax.annotation.Resource;

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

import com.alibaba.fastjson.JSON;
import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.BaseApiController;
import com.goafanti.common.enums.StarCacheKey;
import com.goafanti.common.enums.StarType;
import com.goafanti.common.model.Star;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.star.service.StarService;
import com.goafanti.user.service.UserService;

/**
 * 科技明星
 */
@RestController
@RequestMapping(value = "/api/admin/star")
public class AdminStarApiController extends BaseApiController {
	@Resource
	private UserService				userService;
	@Resource
	private StarService				starService;

	private static final Integer	STAR_MAX_NUM	= 6;

	private static final Integer	HOT_MAX_NUM		= 4;

	/**
	 * 科技明星列表
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public Result starList(Integer number, String engagedField, String username, String professionalTitle,
			String workUnit, String pageNo, String pageSize) {
		Result res = new Result();
		Integer pNo = 1;
		Integer pSize = 10;
		if (StringUtils.isNumeric(pageSize)) {
			pSize = Integer.parseInt(pageSize);
		}

		if (StringUtils.isNumeric(pageNo)) {
			pNo = Integer.parseInt(pageNo);
		}
		res.setData(userService.listStar(number, engagedField, username, professionalTitle, workUnit, pNo, pSize));
		return res;
	}

	/**
	 * 大咖说首页展示明星列表
	 */
	@RequestMapping(value = "/hotList", method = RequestMethod.GET)
	public Result hotList() {
		Result res = new Result();
		res.setData(starService.listHotStar());
		return res;
	}

	/**
	 * 科技明星页面展示列表
	 */
	@RequestMapping(value = "/starList", method = RequestMethod.GET)
	public Result starList() {
		Result res = new Result();
		res.setData(starService.listStarList(null));
		return res;
	}

	/**
	 * 保存展示科技明星
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public Result save(@RequestParam(name = "data", required = false) String d,
			@RequestParam(name = "hot[]", required = false) String[] hot) {
		Result res = new Result();
		List<Star> data = JSON.parseArray(d, Star.class);
		if (null == data || data.size() < 1) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "科技明星列表"));
			return res;
		}

		if (null == hot || hot.length < 1) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "大咖说科技明星列表"));
			return res;
		}

		if (data.size() > STAR_MAX_NUM) {
			res.getError().add(buildError("", "科技明星数量过多！"));
			return res;
		}

		if (hot.length > HOT_MAX_NUM) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "大咖说科技明星数量过多！"));
			return res;
		}

		for (Star s : data) {
			if (StringUtils.isBlank(s.getUid())) {
				res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "科技明星ID"));
				return res;
			}
			if (null == s.getStar()){
				res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "star"));
				return res;
			}
			
			if (!StarType.STAR.getCode().equals(s.getStar())){
				res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "star"));
				return res;
			}
		}
		starService.save(data, hot);
		starService.clearBigShotStarList(AFTConstants.BIG_SHOT_STAR_CACHE_KEY);
		return res;
	}
	
	/**
	 * 清除缓存
	 */
	@RequestMapping(value = "/cleanCache", method = RequestMethod.POST)
	public Result cleanCache(String cacheKey) {
		Result res = new Result();
		if (StringUtils.isBlank(cacheKey)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "缓存标记"));
			return res;
		}
		if (StarCacheKey.BIG_SHOT_STAR_CACHE_KEY.getCode().equals(cacheKey)) {
			starService.clearBigShotStarList(AFTConstants.BIG_SHOT_STAR_CACHE_KEY); // 清除大咖说科技明星列表缓存
		} else {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "缓存标记"));
		}
		return res;
	}
	
}
