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.Quotation;
import com.uas.platform.b2b.erp.model.QuotationDecide;
import com.uas.platform.b2b.erp.service.QuotationService;
import com.uas.platform.b2b.service.SaleQuotationService;
import com.uas.platform.core.util.serializer.FlexJsonUtils;

/**
 * 对卖家ERP的数据接口<br>
 * 上传主动报价
 * 
 * @author yingp
 * 
 */
@Controller
@RequestMapping("/erp/sale/quotation")
public class QuotationController {

	@Autowired
	private QuotationService quotationService;

	@Autowired
	private SaleQuotationService saleQuotationService;

	/**
	 * 将卖家ERP的主动报价写到平台
	 * 
	 * @param data
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public void saveQuotations(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<Quotation> quotations = FlexJsonUtils.fromJsonArray(jsonStr, Quotation.class);
		saleQuotationService.save(quotationService.convertQuotation(quotations));
	}

	/**
	 * 卖家ERP从平台获取报价信息的采纳结果
	 * 
	 * @return
	 */
	@RequestMapping(value = "/decide", method = RequestMethod.GET)
	@ResponseBody
	public List<QuotationDecide> getQuotationDecide() {
		return quotationService.convertQuotationDecide(saleQuotationService.findNotUploadQuotationDecide());
	}

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

}
