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.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.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.model.Status;
import com.uas.platform.core.util.serializer.FlexJsonUtils;
import com.uas.platform.core.web.bind.RequestState;

/**
 * 卖家对询价单的操作
 * 
 * @author yingp
 * 
 */
@Controller
@RequestMapping("/sale/inquiry")
public class SaleInquiryController {

	@Autowired
	private PurchaseInquiryService purchaseInquiryService;

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

	/**
	 * 作为卖家，收到的采购询价（全部）
	 * 
	 * @param params
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public Page<PurchaseInquiryItem> getReceivedPurchaseInquiries(PageParams params) {
		logger.log("客户询价单", "查看收到的客户询价单列表（全部）");
		PageInfo info = new PageInfo(params);
		// 我作为卖家，把我的企业ID作为供应商ID传入
		info.filter("vendUU", SystemSession.getUser().getEnterprise().getUu());
		return purchaseInquiryService.findAllByPageInfo(info);
	}

	/**
	 * 作为卖家，收到的采购询价（待回复）
	 * 
	 * @param params
	 * @return
	 */
	@RequestMapping(params = RequestState.TODO, method = RequestMethod.GET)
	@ResponseBody
	public Page<PurchaseInquiryItem> getTodoInquiries(PageParams params) {
		logger.log("客户询价单", "查看收到的客户询价单列表（待回复）");
		PageInfo info = new PageInfo(params);
		// 询价单状态为未回复
		info.filter("status", Status.NOT_REPLY.value());
		// 我作为卖家，把我的企业ID作为供应商ID传入
		info.filter("vendUU", SystemSession.getUser().getEnterprise().getUu());
		return purchaseInquiryService.findAllByPageInfo(info);
	}

	/**
	 * 作为卖家，收到的采购询价（已回复）
	 * 
	 * @param params
	 * @return
	 */
	@RequestMapping(params = RequestState.DONE, method = RequestMethod.GET)
	@ResponseBody
	public Page<PurchaseInquiryItem> getDoneInquiries(PageParams params) {
		logger.log("客户询价单", "查看收到的客户询价单列表（已回复）");
		PageInfo info = new PageInfo(params);
		// 询价单状态为已回复
		info.filter("status", Status.REPLIED.value());
		// 我作为卖家，把我的企业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) {
		PurchaseInquiryItem item = purchaseInquiryService.findItemByItemId(inquiryItemId);
		logger.log("客户询价单", "查看收到的客户询价单", null, item.getInquiry().getCode(), inquiryItemId);
		return item;
	}

	/**
	 * 作为卖家，根据询价单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 inquiryItemId
	 * @return
	 */
	@RequestMapping(value = "/items/{inquiryItemId}/reply", method = RequestMethod.POST)
	public ResponseEntity<String> replyInquiryItem(@RequestBody String json, @PathVariable("inquiryItemId") Long inquiryItemId) {
		PurchaseInquiryItem item = FlexJsonUtils.fromJson(json, PurchaseInquiryItem.class);
		item = purchaseInquiryService.reply(item);
		logger.log("客户询价单", "给单个客户询价明细填写价格并确认报价", item.replyDescription(), item.getInquiry().getCode(), inquiryItemId);
		return new ResponseEntity<String>(HttpStatus.OK);
	}

	/**
	 * 作为卖家，保存更新询价单
	 * 
	 * @param json
	 * @param inquiryItemId
	 * @return
	 */
	@RequestMapping(value = "/items/{inquiryItemId}", method = RequestMethod.POST)
	public ResponseEntity<String> saveInquiryItem(@RequestBody String json, @PathVariable("inquiryItemId") Long inquiryItemId) {
		PurchaseInquiryItem item = FlexJsonUtils.fromJson(json, PurchaseInquiryItem.class);
		item = purchaseInquiryService.save(item);
		logger.log("客户询价单", "给单个客户询价明细填写价格", item.replyDescription(), item.getInquiry().getCode(), inquiryItemId);
		return new ResponseEntity<String>(HttpStatus.OK);
	}

	/**
	 * 作为卖家，查看同产品的报价历史
	 * 
	 */
	@RequestMapping(value = "/items/{inquiryItemId}/history", method = RequestMethod.GET)
	@ResponseBody
	public List<PurchaseInquiryItem> getInquiryHistoryById(@PathVariable("inquiryItemId") Long inquiryItemId) {
		return purchaseInquiryService.findHistoryByItemId(inquiryItemId);
	}

}
