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.Inquiry;
import com.uas.platform.b2b.erp.model.InquiryDecide;
import com.uas.platform.b2b.erp.service.InquiryService;
import com.uas.platform.b2b.service.SaleQuotationService;
import com.uas.platform.core.util.serializer.FlexJsonUtils;

/**
 * 对买家ERP的数据接口<br>
 * 买家接收供应商的主动报价
 * 
 * @author yingp
 * 
 */
@Controller
@RequestMapping("/erp/purchase/quotation")
public class InquiryDownController {

	@Autowired
	private InquiryService inquiryService;

	@Autowired
	private SaleQuotationService saleQuotationService;

	/**
	 * 买家ERP从平台获取主动报价
	 * 
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public List<Inquiry> getInquiries() {
		return inquiryService.convertSaleQuotation(saleQuotationService.findNotUploadQuotation());
	}

	/**
	 * 平台的主动报价传到买家ERP之后，修改平台里面的主动报价的上传状态
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/back", method = RequestMethod.POST)
	@ResponseBody
	public void onReplySuccess(@RequestParam("data") String data) throws UnsupportedEncodingException {
		saleQuotationService.onQuotationUploadSuccess(URLDecoder.decode(data, "UTF-8").split(","));
	}

	/**
	 * 买家ERP（不）采纳了价格之后，修改平台里面的主动报价的状态
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/decide", method = RequestMethod.POST)
	@ResponseBody
	public void onReplyDecide(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<InquiryDecide> decides = FlexJsonUtils.fromJsonArray(jsonStr, InquiryDecide.class);
		saleQuotationService.onReplyDecide(inquiryService.convertQuotationDecide(decides));
	}

}
