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

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.erp.support.ErpBufferedLogger;
import com.uas.platform.b2b.model.SaleQuotationItem;
import com.uas.platform.b2b.service.SaleQuotationService;
import com.uas.platform.core.logging.BufferedLoggerManager;
import com.uas.platform.core.util.serializer.FlexJsonUtils;
import org.apache.commons.collections.CollectionUtils;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

	@Autowired
	private QuotationService quotationService;

	@Autowired
	private SaleQuotationService saleQuotationService;
	
	private final static ErpBufferedLogger logger = BufferedLoggerManager.getLogger(ErpBufferedLogger.class);

	/**
	 * 将卖家ERP的主动报价写到平台
	 * 
	 * @param data
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public List<Map<String, Object>> saveQuotations(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<Quotation> quotations = FlexJsonUtils.fromJsonArray(jsonStr, Quotation.class);
		Map<String, List<SaleQuotationItem>> convertQuotation = quotationService.convertQuotation(quotations);
		List<SaleQuotationItem> save = convertQuotation.get("save");
		if(!CollectionUtils.isEmpty(save)) {
			saleQuotationService.save(save);
		}
		List<SaleQuotationItem> res = convertQuotation.get("res");
		List<Map<String, Object>> response = new ArrayList<Map<String,Object>>();
		if(!CollectionUtils.isEmpty(res)) {
			for (SaleQuotationItem item : res) {
				Map<String, Object> error= new HashMap<String, Object>();
				error.put("code", item.getQuotation().getCode());//获得主动报价单的单号
				error.put("detno", item.getNumber());//获取报价单明细表哪一行出错。
				response.add(error);
			}
		}
		logger.log("主动报价单", "上传主动报价", save.size());
		return response;
	}

	/**
	 * 卖家ERP从平台获取报价信息的采纳结果
	 * 
	 * @return
	 */
	@RequestMapping(value = "/decide", method = RequestMethod.GET)
	@ResponseBody
	public List<QuotationDecide> getQuotationDecide() {
		List<QuotationDecide> decides = quotationService.convertQuotationDecide(saleQuotationService.findNotUploadQuotationDecide());
		logger.log("主动报价单", "下载客户对主动报价的采纳结果", decides.size());
		return decides;
	}

	/**
	 * 平台的报价信息采纳结果传到供应商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(","));
	}
	
	/**
	 * 卖家ERP从平台获取主动报价
	 * 
	 * @return
	 */
	@RequestMapping(value = "back", method = RequestMethod.GET)
	@ResponseBody
	public List<Quotation> getNotBackQuotation() {
		List<Quotation> quotations = quotationService.convertSaleQuotation(saleQuotationService.findNotBackQuotation());
		logger.log("主动报价单", "下载在平台上新增的的主动报价", quotations.size());
		return quotations;
	}

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

	/**
	 * 卖家ERP从平台获取报价信息的作废
	 *
	 * @return
	 */
	@RequestMapping(value = "/reply/invalid", method = RequestMethod.GET)
	@ResponseBody
	public List<Quotation> getReplyInvalid() {
		List<Quotation> quotations = quotationService.convertSaleQuotation(saleQuotationService.findNotUploadQuotationReplyInvalid());
		logger.log("主动报价单", "下载客户对主动报价单报价信息的作废处理", quotations.size());
		return quotations;
	}

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

}
