package com.uas.search.console.controller;

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

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

import com.uas.search.console.core.util.ContextUtils;
import com.uas.search.console.model.OrderInvoiceSimpleInfo;
import com.uas.search.console.model.OrderSimpleInfo;
import com.uas.search.console.model.PurchaseInvoiceSimpleInfo;
import com.uas.search.console.model.PurchaseSimpleInfo;
import com.uas.search.console.service.InnerOrderSearchService;
import com.uas.search.console.service.impl.OrderSearchServiceImpl;
import com.uas.search.console.util.SearchConstants;
import com.uas.search.exception.SearchException;
import com.uas.search.model.BaseOrder;
import com.uas.search.model.PageParams;
import com.uas.search.model.SPage;
import com.uas.search.service.OrderSearchService.Table_name;
import com.uas.search.utils.OrderSearchConstants;

/**
 * 订单搜索请求
 * 
 * @author sunyj
 * @since 2016年10月14日 上午11:59:45
 */
@Controller
@RequestMapping("/search")
public class OrderSearchController {

	@Autowired
	private OrderSearchServiceImpl orderSearchService;

	private InnerOrderSearchService innerOrderSearchService = ContextUtils.getApplicationContext()
			.getBean("orderSearchServiceImpl", InnerOrderSearchService.class);

	@RequestMapping("orderIds")
	@ResponseBody
	public SPage<BaseOrder> getOrderIdsWithDetail(String keyword, String tableName, Integer page, Integer size,
			String status, Long buyeruu, Long buyerenuu, Long sellerenuu, String mintime, String maxtime) {
		Table_name tbName = null;
		if (!StringUtils.isEmpty(tableName)) {
			tbName = Table_name.valueOf(tableName);
		}

		PageParams params = new PageParams();
		if (page != null) {
			params.setPage(page);
		}
		if (size != null) {
			params.setSize(size);
		}
		Map<String, Object> filters = new HashMap<>();
		if (!StringUtils.isEmpty(status)) {
			String[] strs = status.split(",");
			List<Integer> statusList = new ArrayList<>();
			for (String str : strs) {
				statusList.add(Integer.valueOf(str));
			}
			filters.put(OrderSearchConstants.STATUS_KEY, statusList);
		}
		if (buyeruu != null) {
			filters.put(OrderSearchConstants.BUYER_UU_KEY, buyeruu);
		}
		if (buyerenuu != null) {
			filters.put(OrderSearchConstants.BUYER_EN_UU_KEY, buyerenuu);
		}
		if (sellerenuu != null) {
			filters.put(OrderSearchConstants.SELLER_EN_UU_KEY, sellerenuu);
		}
		if (mintime != null) {
			filters.put(SearchConstants.MIN_TIME_KEY, parseTime(mintime));
		}
		if (maxtime != null) {
			filters.put(SearchConstants.MAX_TIME_KEY, parseTime(maxtime));
		}
		params.setFilters(filters);
		return orderSearchService.getOrderIdsWithDetail(keyword, tbName, params);
	}

	/**
	 * 将时间字符串转为long eg. 2016-10-10 1:12:20
	 * 
	 * @param str
	 *            时间，eg. 2016-10-10 1:12:20
	 * @return long值
	 */
	private Long parseTime(String str) {
		try {
			String[] strs = str.split("-");
			int year = Integer.parseInt(strs[0]);
			int month = Integer.parseInt(strs[1]) - 1;
			String[] strs2 = strs[2].split(" ");
			int date = Integer.parseInt(strs2[0]);
			String[] strs3 = strs2[1].split(":");
			int hour = Integer.parseInt(strs3[0]);
			int minute = Integer.parseInt(strs3[1]);
			int second = Integer.parseInt(strs3[2]);
			Calendar calendar = Calendar.getInstance();
			// 月份减1
			calendar.set(year, month, date, hour, minute, second);
			return calendar.getTimeInMillis();
		} catch (Exception e) {
			throw new SearchException("时间格式错误，示例：'2016-10-10 1:12:20'，" + e);
		}
	}

	@RequestMapping("/order/{id}")
	@ResponseBody
	public OrderSimpleInfo getOrder(@PathVariable Long id) {
		return innerOrderSearchService.getOrder(id);
	}

	@RequestMapping("/orderInvoice/{id}")
	@ResponseBody
	public OrderInvoiceSimpleInfo getOrderInvoice(@PathVariable Long id) {
		return innerOrderSearchService.getOrderInvoice(id);
	}

	@RequestMapping("/purchase/{id}")
	@ResponseBody
	public PurchaseSimpleInfo getPurchase(@PathVariable Long id) {
		return innerOrderSearchService.getPurchase(id);
	}

	@RequestMapping("/purchaseInvoice/{id}")
	@ResponseBody
	public PurchaseInvoiceSimpleInfo getPurchaseInvoice(@PathVariable Long id) {
		return innerOrderSearchService.getPurchaseInvoice(id);
	}

}
