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

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.uas.search.b2b.model.SPage;
import com.uas.search.b2b.service.SearchService.Table_name;
import com.uas.search.console.b2b.jms.AQListener;
import com.uas.search.console.b2b.model.LuceneQueueMessage;
import com.uas.search.console.b2b.service.IndexService;

/**
 * 索引创建相关请求
 * 
 * @author sunyj
 * @since 2016年8月5日 上午11:42:54
 */
@Controller
@RequestMapping("/index")
public class IndexController {

	@Autowired
	private IndexService indexService;

	@Autowired
	private AQListener aqListener;

	@RequestMapping("/create")
	@ResponseBody
	public String createIndexs(String tableNames, Boolean fromFiles) {
		List<Table_name> tableNameList = null;
		if (!StringUtils.isEmpty(tableNames)) {
			String[] strs = tableNames.split(",");
			tableNameList = new ArrayList<>();
			for (String str : strs) {
				tableNameList.add(Table_name.valueOf(str.toUpperCase()));
			}
		}
		return String.format("Indexes created success in %.2fs",
				indexService.createIndexes(tableNameList, fromFiles) / 1000.0);
	}

	@RequestMapping("/downloadData")
	@ResponseBody
	public String downloadData(String tableNames, Integer startFileIndex) {
		List<Table_name> tableNameList = null;
		if (!StringUtils.isEmpty(tableNames)) {
			String[] strs = tableNames.split(",");
			tableNameList = new ArrayList<>();
			for (String str : strs) {
				tableNameList.add(Table_name.valueOf(str.toUpperCase()));
			}
		}
		return "Data downloaded success in " + indexService.downloadDataFromDatabase(tableNameList, startFileIndex)
				+ " ms.";
	}

	@RequestMapping("/listen/start")
	@ResponseBody
	public String startListen(Long waitInterval) {
		return aqListener.start(waitInterval);
	}

	@RequestMapping("/listen/stop")
	@ResponseBody
	public String stopListen() {
		return aqListener.stop();
	}

	@RequestMapping("/listen/restart")
	@ResponseBody
	public String restartListen(Long waitInterval) {
		if (aqListener.isRunning()) {
			aqListener.stop();
		}
		return aqListener.start(waitInterval);
	}

	@RequestMapping("/listen/details")
	@ResponseBody
	public SPage<LuceneQueueMessage> listenDetails(Integer page, Integer size, String searchContent) {
		return indexService.getListenDetails(page, size, searchContent);
	}

	@RequestMapping("/maintain")
	@ResponseBody
	public String maintainIndexes(@RequestParam(required = true) String tableName,
			@RequestParam(required = true) String ids, @RequestParam(required = true) String method) {
		return "已维护：" + indexService.maintainIndexes(tableName, ids, method);
	}

	@RequestMapping("/dequeue")
	@ResponseBody
	public boolean dequeueLuceneQueueMessage(String messageId) {
		return indexService.dequeueLuceneQueueMessage(messageId);
	}
}
