package com.uas.platform.b2b.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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.model.PurchaseOrder;
import com.uas.platform.b2b.model.PurchaseOrderAll;
import com.uas.platform.b2b.model.PurchaseOrderItem;
import com.uas.platform.b2b.model.PurchaseOrderReply;
import com.uas.platform.b2b.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.util.serializer.FlexJsonUtils;

/**
 * 卖家对订单的操作
 * 
 * @author yingp
 * 
 */
@Controller
@RequestMapping("/sale/orders")
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 json
	 * @param orderItemId
	 * @return
	 */
	@RequestMapping(value = "/items/{orderItemId}/reply", method = RequestMethod.POST)
	public ResponseEntity<String> replyOrderItem(@RequestBody String json, @PathVariable("orderItemId") Long orderItemId) {
		PurchaseOrderReply reply = FlexJsonUtils.fromJson(json, PurchaseOrderReply.class);
		PurchaseOrderItem orderItem = new PurchaseOrderItem();
		orderItem.setId(orderItemId);
		reply.setOrderItem(orderItem);
		purchaseOrderService.reply(reply);
		return new ResponseEntity<String>(HttpStatus.OK);
	}

	/**
	 * 作为卖家，查看我的回复
	 * 
	 * @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);
	}

	/**
	 * 作为卖家，批量回复采购订单
	 * 
	 * @param idString
	 * @return
	 */
	@RequestMapping(value = "/items/reply", method = RequestMethod.POST)
	public ResponseEntity<String> replyOrderItems(@RequestBody String idString) {
		purchaseOrderService.reply(idString.split(","));
		return new ResponseEntity<String>(HttpStatus.OK);
	}
}
