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

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.service.InnerSearchService;
import com.uas.search.console.b2b.util.ClassAndTableNameUtils;
import com.uas.search.console.b2b.util.ContextUtils;
import com.uas.search.util.CollectionUtils;
import com.uas.search.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 搜索请求
 * 
 * @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 keywordSearchColumns, String filters, String multiValueField, String sort, String notEqualFilters,
			String multiValueNotField) {
		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);
		}

		List<String> keywordSearchColumnsList = null;
		if (!StringUtils.isEmpty(keywordSearchColumns)) {
			String[] strs = keywordSearchColumns.split(",");
			keywordSearchColumnsList = new ArrayList<>();
			for (String str : strs) {
				keywordSearchColumnsList.add(str);
			}
		}
		pageParams.setKeywordSearchColumns(keywordSearchColumnsList);

		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);
		}

		Map<String, Object> notEqualFiltersMap = new HashMap<>();
		if (!StringUtils.isEmpty(notEqualFilters)) {
			notEqualFiltersMap.putAll(JSONObject.parseObject(notEqualFilters));
		}
		if (!StringUtils.isEmpty(multiValueNotField)) {
			JSONArray jsonArray = JSONObject.parseArray(multiValueNotField);
			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);
				notEqualFiltersMap.put(field, multiValue);
			}
		}
		if (!CollectionUtils.isEmpty(notEqualFiltersMap)) {
			pageParams.setNotEqualFilters(notEqualFiltersMap);
		}

		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);
	}

	@RequestMapping("/objectsToFiles")
	@ResponseBody
	public String writeObjectsToFiles(String tableName) {
		Table_name tbName = null;
		if (!StringUtils.isEmpty(tableName)) {
			tbName = Table_name.valueOf(tableName.toUpperCase());
		}
		return innerSearchService.writeObjectsToFiles(ClassAndTableNameUtils.toClass(tbName));
	}

}
