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

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.erp.support.ErpBufferedLogger;
import com.uas.platform.b2b.service.SaleQuotationService;
import com.uas.platform.core.logging.BufferedLoggerManager;
import com.uas.platform.core.util.ArrayUtils;
import com.uas.platform.core.util.serializer.FlexJsonUtils;
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 java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

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

	@Autowired
	private InquiryService inquiryService;

	@Autowired
	private SaleQuotationService saleQuotationService;

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

	/**
	 * 买家ERP从平台获取主动报价
	 *
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public List<Inquiry> getInquiries() {
		List<Inquiry> inquiries = inquiryService.convertSaleQuotation(saleQuotationService.findNotUploadQuotation());
		logger.log("主动报价单", "下载来自供应商的主动报价", inquiries.size());
		return inquiries;
	}

	/**
	 * 平台的主动报价传到买家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 = "/invalid", method = RequestMethod.POST)
	@ResponseBody
	public void onReplyInvalid(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<Inquiry> inquiries = FlexJsonUtils.fromJsonArray(jsonStr, Inquiry.class);
		saleQuotationService.onReplyInvalid(inquiries);
		logger.log("主动报价单", "作废主动报价询价单", inquiries.size());
	}

	/**
	 * 买家ERP（不）采纳了价格之后，修改平台里面的主动报价的状态
	 *
	 * @param data
	 * @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));
		logger.log("主动报价单", "修改主动报价单的状态", decides.size());
	}

	/**
	 * 买家在收到报价信息后，点击提交按钮后，状态传至平台
	 *
	 * @param data
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/checking", method = RequestMethod.POST)
	@ResponseBody
	public void checkInquiry(@RequestParam("data") String data) throws UnsupportedEncodingException {
		saleQuotationService.check(ArrayUtils.convertStringToNumberArray(URLDecoder.decode(data, "UTF-8"), Long.class));
	}

	/**
	 * 买家在收到报价信息后，点击提交按钮后，状态传至平台
	 *
	 * @param data
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/rechecking", method = RequestMethod.POST)
	@ResponseBody
	public void reCheckInquiry(@RequestParam("data") String data) throws UnsupportedEncodingException {
		saleQuotationService
				.recheck(ArrayUtils.convertStringToNumberArray(URLDecoder.decode(data, "UTF-8"), Long.class));
	}
}
