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.SaleDownChange;
import com.uas.platform.b2b.erp.service.OutsourceChangeService;
import com.uas.platform.b2b.erp.support.ErpBufferedLogger;
import com.uas.platform.b2b.service.MakeOrderChangeService;
import com.uas.platform.core.logging.BufferedLoggerManager;

/**
 * 委外变更单，对卖方ERP接口
 * @author suntg
 * @since 2015年8月11日上午10:54:00
 */
@Controller
@RequestMapping("/erp/outsource/change")
public class OutsourceChangeController {

	@Autowired
	private MakeOrderChangeService makeOrderChangeService;

	@Autowired
	private OutsourceChangeService outsourceChangeService;
	
	private final static ErpBufferedLogger logger = BufferedLoggerManager.getLogger(ErpBufferedLogger.class);

	/**
	 * 卖家ERP从平台获取委外变更单
	 * 
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public List<SaleDownChange> getOrderChanges() {
		List<SaleDownChange> changes = outsourceChangeService.convertMakeOrderChange(makeOrderChangeService.findNotUpload());
		logger.log("客户委外变更单", "下载客户委外变更单", changes.size());
		return changes;
	}
 
	/**
	 * 平台的委外变更单传到供应商ERP之后，修改平台里面的委外变更单的上传状态
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public void onSaleDownChangeSuccess(@RequestParam("data") String data) throws UnsupportedEncodingException {
		makeOrderChangeService.onUploadChangeSuccess(URLDecoder.decode(data, "UTF-8").split(","));
	}

}