package com.nbclass.activity.controller.frontapi;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nbclass.activity.model.DataDictItem;
import com.nbclass.activity.model.enums.ContentType;
import com.nbclass.activity.service.DataDictService;
import com.nbclass.exception.ParameterException;
import com.nbclass.system.controller.BaseController;
import com.nbclass.vo.base.Result;

/**
 * i前端获取案例标签或来源列表
 * @author Leon
 * @datetime 2020年5月21日 下午5:32:25
 */
@RestController
@RequestMapping("/front/api/dict")
public class FrontDataDictController extends BaseController {
	
	@Resource
	public DataDictService service;

	/**
	 * 根据类型查询案例标签列表
	 * @param type
	 * @param response
	 * @return
	 */
	@RequestMapping("/getTags")
	public Result getTags(String type, HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		if(StringUtils.isBlank(type)) {
			throw new ParameterException("参数type不能为空");
		}
		type=type.trim().toLowerCase();
		if(!ContentType.ppqa.key().equals(type) && !ContentType.ggqa.key().equals(type) && 
			!ContentType.design.key().equals(type) && !ContentType.video.key().equals(type) && 
			!ContentType.h5.key().equals(type) && !ContentType.ldhd.key().equals(type)
			&& !ContentType.others.key().equals(type)){
			throw new ParameterException("参数[type]不是有效的数据！");
		}
		type = "tag_"+type;
		List<DataDictItem> list = service.getItemsByKey(type);
		return Result.success(list);
	}
	
	/**
	 * 根据类型查询案例来源列表
	 * @param type
	 * @param response
	 * @return
	 */
	@RequestMapping("/getSources")
	public Result getSources(String type, HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		if(StringUtils.isBlank(type)) {
			throw new ParameterException("参数type不能为空");
		}
		type=type.trim().toLowerCase();
		if(!ContentType.ppqa.key().equals(type) && !ContentType.ggqa.key().equals(type) && 
			!ContentType.design.key().equals(type) && !ContentType.video.key().equals(type) && 
			!ContentType.h5.key().equals(type) && !ContentType.ldhd.key().equals(type)
			&& !ContentType.others.key().equals(type)){
			throw new ParameterException("参数[type]不是有效的数据！");
		}
		type = "from_"+type;
		List<DataDictItem> list = service.getItemsByKey(type);
		return Result.success(list);
	}
	
}
