package com.nbclass.activity.controller.frontapi;

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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.nbclass.activity.model.Content;
import com.nbclass.activity.model.enums.ContentType;
import com.nbclass.activity.service.ElasticSearchService;
import com.nbclass.exception.ParameterException;
import com.nbclass.system.controller.BaseController;
import com.nbclass.vo.base.Result;

import java.util.Map;

/**
 * i前端获取案例查询
 * @author Leon
 * @datetime 2020年5月21日 下午5:32:25
 */
@RestController
@RequestMapping("/front/api/content")
public class FrontContentController extends BaseController {

//	@Resource
//	private ContentService service;

	@Resource
	private ElasticSearchService esService;

	/**
	 * 查询案例列表
	 * @param page
	 * @param o
	 * @param response
	 * @return
	 */
	@RequestMapping("/list")
	public Result getList(String type, HttpServletResponse response,
			@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize,
			String keyWord, String tagIds, String sourceIds){
		response.setHeader("Access-Control-Allow-Origin", "*");
		if(StringUtils.isNotBlank(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]不是有效的数据！");
			}
		}
	//	List<Integer> tagIdList = CommonUtils.toIntList(tagIds);
	//	List<Integer> sourceIdList = CommonUtils.toIntList(sourceIds);
		Map resultObj = esService.getList(pageNum, pageSize, type, keyWord, tagIds, sourceIds);
		return Result.success(resultObj);
	}

	/**
	 * 根据id查找单条记录
	 * @param id
	 * @param response
	 * @return
	 */
	@RequestMapping("/findById")
	public Result findById(Long id, 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]不是有效的数据！");
		}
		if(id == null || id < 1) {
			throw new ParameterException("参数[id]不能为空！");
		}
		// ES里保存的ID是唯一的，所以不需要提交type字段查询
		Content content = esService.findById(id);
		return Result.success(content);
	}

}
