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.SaleDown;
import com.uas.platform.b2b.erp.model.SaleDownDetailEnd;
import com.uas.platform.b2b.erp.model.SaleReply;
import com.uas.platform.b2b.erp.service.SaleDownService;
import com.uas.platform.b2b.service.PurchaseOrderService;
import com.uas.platform.core.util.serializer.FlexJsonUtils;

/**
 * 对卖家ERP的数据接口
 * 
 * @author yingp
 *
 */
@Controller
@RequestMapping("/erp/sale")
public class SaleDownController {

	@Autowired
	private SaleDownService saleDownService;

	@Autowired
	private PurchaseOrderService purchaseOrderService;

	/**
	 * 卖家ERP从平台获取未回复的订单
	 * 
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public List<SaleDown> getOrders() {
		return saleDownService.convertPurchaseOrder(purchaseOrderService.findNotUploadOrder());
	}

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

	/**
	 * 将卖家ERP的订单回复写到平台
	 * 
	 * @param data
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/reply", method = RequestMethod.POST)
	@ResponseBody
	public void saveReplies(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<SaleReply> replies = FlexJsonUtils.fromJsonArray(jsonStr, SaleReply.class);
		purchaseOrderService.reply(saleDownService.convertSaleReply(replies));
	}

	/**
	 * 卖家ERP从平台获取在平台回复的记录
	 * 
	 * @return
	 */
	@RequestMapping(value = "/reply", method = RequestMethod.GET)
	@ResponseBody
	public List<SaleReply> getReply() {
		return saleDownService.convertOrderReply(purchaseOrderService.findNotSendReply());
	}

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

	/**
	 * 卖家ERP从平台获取结案、反结案客户采购单
	 * 
	 * @return
	 */
	@RequestMapping(value = "/end", method = RequestMethod.GET)
	@ResponseBody
	public List<SaleDownDetailEnd> getSaleEnd() {
		return saleDownService.convertOrderItemEnd(purchaseOrderService.findNotSendEnd());
	}

	/**
	 * 平台的结案、反结案客户采购单传到供应商ERP之后，修改平台里面的上传状态
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/end/back", method = RequestMethod.POST)
	@ResponseBody
	public void onEndDownSuccess(@RequestParam("data") String data) throws UnsupportedEncodingException {
		purchaseOrderService.onEndDownSuccess(URLDecoder.decode(data, "UTF-8").split(","));
	}

}
