package cn.feast.coding.controller.stock;

import cn.feast.coding.controller.BaseController;
import cn.feast.coding.model.stock.StockAnalyse;
import cn.feast.coding.service.stock.StockAnalyseService;
import cn.feast.coding.tools.DateUtils;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.HashMap;


@Controller
@RequestMapping("/stockAnalyse")
public class StockAnalyseController extends BaseController {
	@Autowired
	private StockAnalyseService stockAnalyseService;

	/**
	* 分页列表
	*/
	@RequestMapping(value = "/list")
	public String list(StockAnalyse stockAnalyse, Integer curPage, Integer pageSize, ModelMap model, HttpServletRequest request){
		PageInfo<StockAnalyse> pageInfo = stockAnalyseService.queryPage(stockAnalyse, pageNum(curPage), pageSize(request, pageSize));
    	model.put("pages", pageInfo);
		model.put("stockAnalyse", stockAnalyse);
		return "stockAnalyse/stockAnalyse_list";
	}

	/**
	 * 查询
	 */
	@RequestMapping(value = "/query")
	public String query(StockAnalyse stockAnalyse, Integer curPage, Integer pageSize, ModelMap model, HttpServletRequest request){
		if(StringUtils.isEmpty(stockAnalyse.getDate())){
			stockAnalyse.setDate(DateUtils.getDate(new Date()));
		}
		PageInfo<Map<String, Object>> pageInfo = stockAnalyseService.queryAnalyse(stockAnalyse, pageNum(curPage), pageSize(request, pageSize));
		model.put("pages", pageInfo);
		model.put("stockAnalyse", stockAnalyse);
		return "stockAnalyse/stockAnalyse_list";
	}

	/**
	 * 查询
	 */
	@RequestMapping(value = "/query1")
	public String query1(StockAnalyse stockAnalyse, Integer curPage, Integer pageSize, ModelMap model, HttpServletRequest request){
		PageInfo<Map<String, Object>> pageInfo = stockAnalyseService.queryAnalyse1(stockAnalyse, pageNum(curPage), pageSize(request, pageSize));
		model.put("pages", pageInfo);
		model.put("stockAnalyse", stockAnalyse);
		return "stockAnalyse/stockAnalyse_list1";
	}

	/**
	 * 查询
	 */
	@RequestMapping(value = "/query2")
	public String query2(StockAnalyse stockAnalyse, Integer curPage, Integer pageSize, ModelMap model, HttpServletRequest request){
		if(StringUtils.isEmpty(stockAnalyse.getDate())){
			stockAnalyse.setDate(DateUtils.getDate(new Date()));
		}
		PageInfo<Map<String, Object>> pageInfo = stockAnalyseService.queryAnalyse2(stockAnalyse, pageNum(curPage), pageSize(request, pageSize));
		model.put("pages", pageInfo);
		model.put("stockAnalyse", stockAnalyse);
		return "stockAnalyse/stockAnalyse_list2";
	}

	/**
	 * 股票按成交额排序
	 */
	@RequestMapping(value = "/sortByTurn")
	public String sortByTurn(StockAnalyse stockAnalyse, Integer curPage, Integer pageSize, ModelMap model, HttpServletRequest request){
		PageInfo<Map<String, Object>> pageInfo = stockAnalyseService.sortByTurn(stockAnalyse, pageNum(curPage), pageSize(request, pageSize));
		model.put("pages", pageInfo);
		model.put("stockAnalyse", stockAnalyse);
		return "stockAnalyse/sortByTurn";
	}

	/**
	 * 股票按涨跌幅排序
	 */
	@RequestMapping(value = "/sortByPrice")
	public String sortByPrice(StockAnalyse stockAnalyse, Integer curPage, Integer pageSize, ModelMap model, HttpServletRequest request){
		PageInfo<Map<String, Object>> pageInfo = stockAnalyseService.sortByPrice(stockAnalyse, pageNum(curPage), pageSize(request, pageSize));
		model.put("pages", pageInfo);
		model.put("stockAnalyse", stockAnalyse);
		return "stockAnalyse/sortByPrice";
	}

	/**
	 * 股吧人气排行查询
	 */
	@RequestMapping(value = "/querySentiment")
	public String querySentiment(StockAnalyse stockAnalyse, Integer curPage, Integer pageSize, ModelMap model, HttpServletRequest request){
		PageInfo<Map<String, Object>> pageInfo = stockAnalyseService.querySentiment(stockAnalyse, pageNum(curPage), pageSize(request, pageSize));
		model.put("pages", pageInfo);
		model.put("stockAnalyse", stockAnalyse);
		return "stockAnalyse/stockAnalyse_sentiment";
	}

	/**
	 * 热门概念查询
	 */
	@RequestMapping(value = "/queryConcept")
	public String queryConcept(StockAnalyse stockAnalyse, Integer curPage, Integer pageSize, ModelMap model, HttpServletRequest request){
		PageInfo<Map<String, Object>> pageInfo = stockAnalyseService.queryConcept(stockAnalyse, pageNum(curPage), pageSize(request, pageSize));
		model.put("pages", pageInfo);
		model.put("stockAnalyse", stockAnalyse);
		return "stockAnalyse/stockAnalyse_concept";
	}

	/**
	 * 查询股票相关概念
	 */
	@RequestMapping(value = "/queryStockConcept")
	public String queryStockConcept(String stockCode, Integer curPage, Integer pageSize, ModelMap model, HttpServletRequest request) {
		PageInfo<Map<String, Object>> pageInfo = stockAnalyseService.queryStockConcept(stockCode, pageNum(curPage), pageSize(request, pageSize));
		model.put("pages", pageInfo);
		model.put("stockCode", stockCode);
		return "stockAnalyse/stockAnalyse_concept_detail";
	}

	/**
	 * 查询概念包含的股票
	 */
	@RequestMapping(value = "/queryConceptStocks", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> queryConceptStocks(String conceptName) {
		Map<String, Object> result = new HashMap<>();
		try {
			if (StringUtils.isEmpty(conceptName)) {
				result.put("code", 1);
				result.put("msg", "概念名称不能为空");
				return result;
			}
			List<Map<String, Object>> data = stockAnalyseService.queryConceptStocks(conceptName);
			result.put("code", 0);
			result.put("msg", "success");
			result.put("data", data);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", 1);
			result.put("msg", "查询失败：" + e.getMessage());
		}
		return result;
	}

	/**
	 * 获取股票K线数据
	 */
	@RequestMapping(value = "/getKLineData", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getKLineData(String stockCode) {
		Map<String, Object> result = new HashMap<>();
		try {
			if (StringUtils.isEmpty(stockCode)) {
				result.put("code", 1);
				result.put("msg", "股票代码不能为空");
				return result;
			}
			List<Map<String, Object>> data = stockAnalyseService.getKLineData(stockCode);
			result.put("code", 0);
			result.put("msg", "success");
			result.put("data", data);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", 1);
			result.put("msg", "查询失败：" + e.getMessage());
		}
		return result;
	}

	/**
	* 新增页面
	*/
	@RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap model){
    	return "stockAnalyse/stockAnalyse_add";
    }

	/**
	* 新增
	*/
    @RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(StockAnalyse stockAnalyse, HttpServletRequest request, ModelMap model){
		stockAnalyseService.insert(stockAnalyse);
		model.put("flag", true);
		return "stockAnalyse/stockAnalyse_add";
	}

	/**
	* 修改页面
	*/
	@RequestMapping(value = "/update", method = RequestMethod.GET)
	public String update(StockAnalyse stockAnalyse, ModelMap model){
		stockAnalyse = stockAnalyseService.selectByPrimaryKey(stockAnalyse.getId());
		model.put("stockAnalyse", stockAnalyse);
		return "stockAnalyse/stockAnalyse_edit";
	}

	/**
	* 修改
	*/
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(StockAnalyse stockAnalyse, HttpServletRequest request, ModelMap model){
		stockAnalyseService.update(stockAnalyse);
		model.put("flag", true);
		return "stockAnalyse/stockAnalyse_edit";
	}

	/**
	* 明细页面
	*/
	@RequestMapping(value = "/detail", method = RequestMethod.GET)
	public String detail(StockAnalyse stockAnalyse, ModelMap model){
		stockAnalyse = stockAnalyseService.selectByPrimaryKey(stockAnalyse.getId());
		model.put("stockAnalyse", stockAnalyse);
		return "stockAnalyse/stockAnalyse_detail";
	}

	/**
	* 删除
	*/
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	@ResponseBody
	public void delete(StockAnalyse stockAnalyse){
		stockAnalyseService.deleteByPrimaryKey(stockAnalyse.getId());
	}



}
