package com.uas.search.console.b2b.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.uas.search.b2b.model.MultiValue;
import com.uas.search.b2b.model.PageParams;
import com.uas.search.b2b.model.SPage;
import com.uas.search.b2b.model.Sort;
import com.uas.search.b2b.service.SearchService;
import com.uas.search.b2b.service.SearchService.Table_name;
import com.uas.search.b2b.util.SearchConstants;
import com.uas.search.console.b2b.core.util.ContextUtils;
import com.uas.search.console.b2b.service.InnerSearchService;
import com.uas.search.console.b2b.util.ClassAndTableNameUtils;

/**
 * 搜索请求
 * 
 * @author sunyj
 * @since 2016年11月9日 下午4:33:32
 */
// 只用于测试
@Controller
@RequestMapping("/search")
public class SearchController {

	@Autowired
	private SearchService searchService;

	private InnerSearchService innerSearchService = ContextUtils.getApplicationContext().getBean("searchServiceImpl",
			InnerSearchService.class);

	@RequestMapping("")
	@ResponseBody
	public SPage<Long> searchIds(String keyword, String tableName, Integer page, Integer size, String filters,
			String multiValueField, String sort) {
		Table_name tbName = null;
		if (!StringUtils.isEmpty(tableName)) {
			tbName = Table_name.valueOf(tableName.toUpperCase());
		}
		PageParams pageParams = new PageParams();
		if (page != null) {
			pageParams.setPage(page);
		}
		if (size != null) {
			pageParams.setSize(size);
		}
		Map<String, Object> filtersMap = new HashMap<>();
		if (!StringUtils.isEmpty(filters)) {
			filtersMap.putAll(JSONObject.parseObject(filters));
		}
		if (!StringUtils.isEmpty(multiValueField)) {
			JSONArray jsonArray = JSONObject.parseArray(multiValueField);
			for (int i = 0; i < jsonArray.size(); i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				String field = jsonObject.getString("field");
				MultiValue multiValue = jsonObject.getObject("multiValue", MultiValue.class);
				filtersMap.put(field, multiValue);
			}
		}
		if (!StringUtils.isEmpty(sort)) {
			filtersMap.put(SearchConstants.SORT_KEY, JSONObject.parseArray(sort, Sort.class));
		}
		if (!CollectionUtils.isEmpty(filtersMap)) {
			pageParams.setFilters(filtersMap);
		}
		return searchService.searchIds(keyword, tbName, pageParams);
	}

	@RequestMapping("/similar")
	@ResponseBody
	public SPage<String> similar(String keyword, String tableName, Integer size, String... fields) {
		Table_name tbName = null;
		if (!StringUtils.isEmpty(tableName)) {
			tbName = Table_name.valueOf(tableName.toUpperCase());
		}
		return searchService.similar(keyword, tbName, size, fields);
	}

	@RequestMapping("/object")
	@ResponseBody
	public Object getObjectById(Long id, String tableName) {
		Table_name tbName = null;
		if (!StringUtils.isEmpty(tableName)) {
			tbName = Table_name.valueOf(tableName.toUpperCase());
		}
		return innerSearchService.getObjectById(id, ClassAndTableNameUtils.toClass(tbName));
	}

	@RequestMapping("/objects")
	@ResponseBody
	public Object getObjects(String tableName, Integer page, Integer size) {
		Table_name tbName = null;
		if (!StringUtils.isEmpty(tableName)) {
			tbName = Table_name.valueOf(tableName.toUpperCase());
		}
		return innerSearchService.getAllObjects(ClassAndTableNameUtils.toClass(tbName), page == null ? 0 : page,
				size == null ? 0 : size);
	}

}
