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.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.PurchaseInquiry;
import com.uas.platform.b2b.model.PurchaseInquiryItem;
import com.uas.platform.b2b.service.PurchaseInquiryService;
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/inquiry")
public class SaleInquiryController {

	@Autowired
	private PurchaseInquiryService purchaseInquiryService;

	/**
	 * 作为卖家，收到的采购询价
	 * 
	 * @param json
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public Page<PurchaseInquiryItem> getReceivedPurchaseOrders(PageParams params) {
		PageInfo info = new PageInfo(params);
		// 我作为卖家，把我的企业ID作为供应商ID传入
		info.filter("vendUU", SystemSession.getUser().getEnterprise().getUu());
		return purchaseInquiryService.findAllByPageInfo(info);
	}
	
	/**
	 * 作为卖家，收到的采购询价明细
	 * 
	 */
	@RequestMapping(value = "/items/{inquiryItemId}", method = RequestMethod.GET)
	@ResponseBody
	public PurchaseInquiryItem getInquiryItemById(@PathVariable("inquiryItemId") Long inquiryItemId) {
		return purchaseInquiryService.findItemByItemId(inquiryItemId);
	}

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

	/**
	 * 作为卖家，给询价单报价
	 * 
	 * @param json
	 * @param orderItemId
	 * @return
	 */
	@RequestMapping(value = "/items/{inquiryItemId}/reply", method = RequestMethod.POST)
	public ResponseEntity<String> replyOrderItem(@RequestBody String json, @PathVariable("inquiryItemId") Long inquiryItemId) {
		PurchaseInquiryItem item = FlexJsonUtils.fromJson(json, PurchaseInquiryItem.class);
		purchaseInquiryService.reply(item);
		return new ResponseEntity<String>(HttpStatus.OK);
	}
	
	/**
	 * 作为卖家，保存更新询价单
	 * 
	 * @param json
	 * @param orderItemId
	 * @return
	 */
	@RequestMapping(value = "/items/{inquiryItemId}/save", method = RequestMethod.POST)
	public ResponseEntity<String> saveOrderItem(@RequestBody String json, @PathVariable("inquiryItemId") Long inquiryItemId) {
		PurchaseInquiryItem item = FlexJsonUtils.fromJson(json, PurchaseInquiryItem.class);
		purchaseInquiryService.save(item);
		return new ResponseEntity<String>(HttpStatus.OK);
	}

}
