package com.uas.search.controller;

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

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.uas.search.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.constant.model.BaseOrder;
import com.uas.search.constant.model.PageParams;
import com.uas.search.constant.model.PageParams.FilterField;
import com.uas.search.constant.model.SPage;
import com.uas.search.exception.SearchException;
import com.uas.search.model.OrderInvoice;
import com.uas.search.model.Order;
import com.uas.search.model.PurchaseInvoice;
import com.uas.search.model.Purchase;
import com.uas.search.service.OrderSearchService;
import com.uas.search.service.OrderSearchService.Table_name;

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

	@Autowired
	private OrderSearchService orderSearchService;

	@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,
			HttpServletRequest request) {
		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<FilterField, 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(FilterField.ORDER_STATUS, statusList);
		}
		if (buyeruu != null) {
			filters.put(FilterField.ORDER_BUYER_UU, buyeruu);
		}
		if (buyerenuu != null) {
			filters.put(FilterField.ORDER_BUYER_EN_UU, buyerenuu);
		}
		if (sellerenuu != null) {
			filters.put(FilterField.ORDER_SELLER_EN_UU, sellerenuu);
		}
		if (mintime != null) {
			filters.put(FilterField.ORDER_MIN_TIME, parseTime(mintime));
		}
		if (maxtime != null) {
			filters.put(FilterField.ORDER_MAX_TIME, 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 Order getOrder(@PathVariable Long id, HttpServletRequest request) {
		return orderSearchService.getOrder(id);
	}

	@RequestMapping("/orderInvoice/{id}")
	@ResponseBody
	public OrderInvoice getOrderInvoice(@PathVariable Long id, HttpServletRequest request) {
		return orderSearchService.getOrderInvoice(id);
	}

	@RequestMapping("/purchase/{id}")
	@ResponseBody
	public Purchase getPurchase(@PathVariable Long id, HttpServletRequest request) {
		return orderSearchService.getPurchase(id);
	}

	@RequestMapping("/purchaseInvoice/{id}")
	@ResponseBody
	public PurchaseInvoice getPurchaseInvoice(@PathVariable Long id, HttpServletRequest request) {
		return orderSearchService.getPurchaseInvoice(id);
	}

}
