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.OutsourceService;
import com.uas.platform.b2b.erp.support.ErpBufferedLogger;
import com.uas.platform.b2b.service.MakeOrderService;
import com.uas.platform.core.logging.BufferedLoggerManager;
import com.uas.platform.core.util.serializer.FlexJsonUtils;

/**
 * 对卖家ERP的委外加工单数据接口
 * 
 * @author suntg
 *
 */
@Controller
@RequestMapping("/erp/outsource")
public class OutsourceController {

	@Autowired
	private OutsourceService outsourceService;

	@Autowired
	private MakeOrderService makeOrderService;
	
	private final static ErpBufferedLogger logger = BufferedLoggerManager.getLogger(ErpBufferedLogger.class);

	/**
	 * 卖家ERP从平台获取未下载的委外订单
	 * 
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public List<SaleDown> getOrders() {
		List<SaleDown> downs = outsourceService.convertMakeOrder(makeOrderService.findNotUploadOrder());
		logger.log("客户委外加工单", "下载客户委外加工单", downs.size());
		return downs;
	}

	/**
	 * 平台的委外订单传到供应商ERP之后，修改平台里面的订单的上传状态
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public void onSaleDownSuccess(@RequestParam("data") String data) throws UnsupportedEncodingException {
		makeOrderService.onMakeDownSuccess(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);
		makeOrderService.reply(outsourceService.convertSaleReply(replies));
		logger.log("客户委外加工单", "上传客户委外加工单回复信息", replies.size());
	}

	/**
	 * 卖家ERP从平台获取在平台回复的记录
	 * 
	 * @return
	 */
	@RequestMapping(value = "/reply", method = RequestMethod.GET)
	@ResponseBody
	public List<SaleReply> getReply() {
		List<SaleReply> replies = outsourceService.convertMakeOrderReply(makeOrderService.findNotBackReply());
		logger.log("客户委外加工单", "下载直接在平台回复的客户委外加工单回复信息", replies.size());
		return replies;
	}

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

	/**
	 * 卖家ERP从平台获取结案、反结案客户采购单
	 * 
	 * @return
	 */
	@RequestMapping(value = "/end", method = RequestMethod.GET)
	@ResponseBody
	public List<SaleDownDetailEnd> getSaleEnd() {
		List<SaleDownDetailEnd> ends = outsourceService.convertMakeOrderEnd(makeOrderService.findNotSendEnd());
		logger.log("客户委外加工单", "下载结案、反结案的客户委外加工单", ends.size());
		return ends;
	}

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


}
