package com.uas.platform.b2b.controller;

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.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.PurchaseReply;
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;

@Controller
@RequestMapping("/sale")
public class SaleOrderController {

	@Autowired
	private PurchaseOrderService purchaseOrderService;

	/**
	 * 作为卖家，收到的采购订单
	 * 
	 * @param json
	 * @return
	 */
	@RequestMapping(value = "/orders", 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);
	}

	/**
	 * 作为卖家，收到的采购订单(含明细)
	 * 
	 * @param json
	 * @return
	 */
	@RequestMapping(value = "/orders/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
	 * @return
	 */
	@RequestMapping(value = "/orders/items/reply", method = RequestMethod.POST)
	public ResponseEntity<String> replyOrderItem(@RequestBody String json) {
		PurchaseReply reply = FlexJsonUtils.fromJson(json, PurchaseReply.class);
		purchaseOrderService.reply(reply);
		return new ResponseEntity<String>(HttpStatus.OK);
	}
}
