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.PurchaseOrderDone;
import com.uas.platform.b2b.model.PurchaseOrderEnd;
import com.uas.platform.b2b.model.PurchaseOrderItem;
import com.uas.platform.b2b.model.PurchaseOrderReply;
import com.uas.platform.b2b.model.PurchaseOrderTodo;
import com.uas.platform.b2b.service.PurchaseOrderService;
import com.uas.platform.b2b.support.SystemSession;
import com.uas.platform.b2b.support.UsageBufferedLogger;
import com.uas.platform.core.logging.BufferedLoggerManager;
import com.uas.platform.core.model.PageInfo;
import com.uas.platform.core.model.PageParams;
import com.uas.platform.core.util.serializer.FlexJsonUtils;
import com.uas.platform.core.web.bind.RequestState;

/**
 * 卖家对订单的操作
 * 
 * @author yingp
 * 
 */
@Controller
@RequestMapping("/sale/orders")
public class SaleOrderController {

	@Autowired
	private PurchaseOrderService purchaseOrderService;

	private final static UsageBufferedLogger logger = BufferedLoggerManager.getLogger(UsageBufferedLogger.class);

	/**
	 * 作为卖家，收到的采购订单
	 * 
	 * @param json
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public Page<PurchaseOrder> getReceivedPurchaseOrders(@RequestBody String json) {
		logger.log("客户采购单", "查看收到的客户采购订单列表");
		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) {
		logger.log("客户采购单", "查看收到的客户采购订单列表（包括采购明细）（全部）");
		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) {
		logger.log("客户采购单", "查看收到的客户采购订单列表（包括采购明细）（待回复）");
		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.DONE, method = RequestMethod.GET)
	@ResponseBody
	public Page<PurchaseOrderDone> getDonePurchaseOrderItems(PageParams params) {
		logger.log("客户采购单", "查看收到的客户采购订单列表（包括采购明细）（已回复）");
		PageInfo info = new PageInfo(params);
		// 我作为卖家，把我的企业ID作为供应商ID传入
		info.filter("vendUU", SystemSession.getUser().getEnterprise().getUu());
		return purchaseOrderService.findDoneByPageInfo(info);
	}

	/**
	 * 作为卖家，收到的采购订单(含明细)(已结案)
	 * 
	 * @param params
	 * @return
	 */
	@RequestMapping(value = "/items", params = RequestState.END, method = RequestMethod.GET)
	@ResponseBody
	public Page<PurchaseOrderEnd> getEndPurchaseOrderItems(PageParams params) {
		logger.log("客户采购单", "查看收到的客户采购订单列表（包括采购明细）（已结案）");
		PageInfo info = new PageInfo(params);
		// 我作为卖家，把我的企业ID作为供应商ID传入
		info.filter("vendUU", SystemSession.getUser().getEnterprise().getUu());
		return purchaseOrderService.findEndByPageInfo(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);
		reply = purchaseOrderService.reply(reply);
		logger.log("客户采购单", "单个回复客户采购明细", reply.replyDescription(), reply.getOrderItem().getOrder().getCode(), orderItemId);
		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) {
		List<PurchaseOrderReply> replies = purchaseOrderService.reply(idString.split(","));
		for (PurchaseOrderReply reply : replies)
			logger.log("客户采购单", "批量回复客户采购明细", reply.replyDescription(), reply.getOrderItem().getOrder().getCode(), reply.getOrderItem()
					.getId());
		return new ResponseEntity<String>(HttpStatus.OK);
	}
}
