package com.uas.platform.b2b.erp.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.uas.platform.b2b.erp.model.QuotationDown;
import com.uas.platform.b2b.erp.model.QuotationDownDecide;
import com.uas.platform.b2b.erp.service.QuotationDownService;
import com.uas.platform.b2b.service.PurchaseInquiryService;
import com.uas.platform.core.util.serializer.FlexJsonUtils;

/**
 * 对卖家ERP的数据接口<br>
 * 询价
 * 
 * @author yingp
 * 
 */
@Controller
@RequestMapping("/erp/sale/inquiry")
public class QuotationDownController {

	@Autowired
	private QuotationDownService quotationDownService;

	@Autowired
	private PurchaseInquiryService purchaseInquiryService;

	/**
	 * 卖家ERP从平台获取未报价的询价明细
	 * 
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public List<QuotationDown> getQuotations() {
		return quotationDownService.convertPurchaseInquiry(purchaseInquiryService.findNotUploadInquiry());
	}

	/**
	 * 平台的客户询价单传到供应商ERP之后，修改平台里面的询价单的上传状态
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public void onQuotationDownSuccess(@RequestParam("data") String data) throws UnsupportedEncodingException {
		purchaseInquiryService.onQuotationDownSuccess(URLDecoder.decode(data, "UTF-8").split(","));
	}

	/**
	 * 将卖家ERP的报价信息写到平台
	 * 
	 * @param data
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/reply", method = RequestMethod.POST)
	@ResponseBody
	public void saveQuotationReplies(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<QuotationDown> downs = FlexJsonUtils.fromJsonArray(jsonStr, QuotationDown.class);
		purchaseInquiryService.reply(downs);
	}

	/**
	 * 卖家ERP从平台获取在平台报价的记录
	 * 
	 * @return
	 */
	@RequestMapping(value = "/reply", method = RequestMethod.GET)
	@ResponseBody
	public List<QuotationDown> getReply() {
		return quotationDownService.convertInquiryReply(purchaseInquiryService.findNotUploadInquiryReply());
	}

	/**
	 * 平台的报价记录传到供应商ERP之后，修改平台里面的询价明细的报价记录上传状态
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/reply/back", method = RequestMethod.POST)
	@ResponseBody
	public void onReplyDownSuccess(@RequestParam("data") String data) throws UnsupportedEncodingException {
		purchaseInquiryService.onReplyDownSuccess(URLDecoder.decode(data, "UTF-8").split(","));
	}

	/**
	 * 卖家ERP从平台获取报价信息的采纳结果
	 * 
	 * @return
	 */
	@RequestMapping(value = "/reply/decide", method = RequestMethod.GET)
	@ResponseBody
	public List<QuotationDownDecide> getReplyDecide() {
		return quotationDownService.convertInquiryReplyDecide(purchaseInquiryService.findNotUploadInquiryReplyDecide());
	}

	/**
	 * 平台的报价信息采纳结果传到供应商ERP之后，修改平台里面的采纳结果上传状态
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/reply/decide/back", method = RequestMethod.POST)
	@ResponseBody
	public void onReplyDecideDownSuccess(@RequestParam("data") String data) throws UnsupportedEncodingException {
		purchaseInquiryService.onReplyDecideDownSuccess(URLDecoder.decode(data, "UTF-8").split(","));
	}

}
