package com.uas.platform.b2b.history.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.uas.platform.b2b.history.model.PurchaseOrder;
import com.uas.platform.b2b.history.model.PurchaseOrderAll;
import com.uas.platform.b2b.history.model.PurchaseOrderEnd;
import com.uas.platform.b2b.history.model.PurchaseOrderReply;
import com.uas.platform.b2b.history.model.PurchaseOrderTodo;
import com.uas.platform.b2b.history.service.PurchaseOrderService;
import com.uas.platform.b2b.support.SystemSession;
import com.uas.platform.core.model.PageInfo;
import com.uas.platform.core.model.PageParams;
import com.uas.platform.core.web.bind.RequestState;

/**
 * 卖家对订单的操作（历史数据）
 * 
 * @author yingp
 * 
 */
@Controller("history.SaleOrderController")
@RequestMapping("/sale/orders/history")
public class SaleOrderController {

	@Autowired
	private PurchaseOrderService purchaseOrderService;

	/**
	 * 作为卖家，收到的采购订单
	 * 
	 * @param json
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public Page<PurchaseOrder> getReceivedPurchaseOrders(@RequestBody String json) {
		PageInfo info = new PageInfo(json);
		// 我作为卖家，把我的企业ID作为供应商ID传入
		info.filter("vendUU", SystemSession.getUser().getEnterprise().getUu());
		return purchaseOrderService.findAllByPageInfo(info);
	}

	/**
	 * 作为卖家，根据采购单据ID查找采购订单(含明细)
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	@ResponseBody
	public PurchaseOrderAll getReceivedPurchaseOrderItemById(@PathVariable("id") Long id) {
		return purchaseOrderService.findById(id);
	}

	/**
	 * 作为卖家，收到的采购订单(含明细)
	 * 
	 * @param params
	 * @return
	 */
	@RequestMapping(value = "/items", method = RequestMethod.GET)
	@ResponseBody
	public Page<PurchaseOrderAll> getReceivedPurchaseOrderItems(PageParams params) {
		PageInfo info = new PageInfo(params);
		// 我作为卖家，把我的企业ID作为供应商ID传入
		info.filter("vendUU", SystemSession.getUser().getEnterprise().getUu());
		return purchaseOrderService.findAllDetailByPageInfo(info);
	}

	/**
	 * 作为卖家，收到的采购订单(含明细)(待回复)
	 * 
	 * @param params
	 * @return
	 */
	@RequestMapping(value = "/items", params = RequestState.TODO, method = RequestMethod.GET)
	@ResponseBody
	public Page<PurchaseOrderTodo> getTodoPurchaseOrderItems(PageParams params) {
		PageInfo info = new PageInfo(params);
		// 我作为卖家，把我的企业ID作为供应商ID传入
		info.filter("vendUU", SystemSession.getUser().getEnterprise().getUu());
		return purchaseOrderService.findTodoByPageInfo(info);
	}

	/**
	 * 作为卖家，收到的采购订单(含明细)(已回复)
	 * 
	 * @param params
	 * @return
	 */
	@RequestMapping(value = "/items", params = RequestState.END, method = RequestMethod.GET)
	@ResponseBody
	public Page<PurchaseOrderEnd> getEndPurchaseOrderItems(PageParams params) {
		PageInfo info = new PageInfo(params);
		// 我作为卖家，把我的企业ID作为供应商ID传入
		info.filter("vendUU", SystemSession.getUser().getEnterprise().getUu());
		return purchaseOrderService.findEndByPageInfo(info);
	}

	/**
	 * 作为卖家，查看我的回复
	 * 
	 * @param orderItemId
	 *            订单明细行ID
	 * @return
	 */
	@RequestMapping(value = "/items/{orderItemId}/reply", method = RequestMethod.GET)
	@ResponseBody
	public List<PurchaseOrderReply> getPurchaseReply(@PathVariable("orderItemId") Long orderItemId) {
		return purchaseOrderService.findReplyByOrderItem(orderItemId);
	}

}