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

import com.uas.platform.b2b.erp.model.MakeProdInOut;
import com.uas.platform.b2b.erp.service.MakeProdInOutService;
import com.uas.platform.b2b.erp.support.ErpBufferedLogger;
import com.uas.platform.b2b.service.MakeReturnService;
import com.uas.platform.core.logging.BufferedLoggerManager;
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的数据接口 买家委外验退单接口
 * 
 * @author aof
 * @date 2015年8月26日
 */
@Controller
@RequestMapping("/erp/make/prodReturn")
public class MakeProdReturnController {

	@Autowired
	private MakeReturnService makeReturnService;

	@Autowired
	private MakeProdInOutService makeProdInOutService;

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

	/**
	 * 将ERP的委外验退单写到平台
	 * 
	 * @param data
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public List<MakeProdInOut> saveMakeProdReturn(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<MakeProdInOut> prodInOuts = FlexJsonUtils.fromJsonArray(jsonStr, MakeProdInOut.class);
		makeReturnService.save(makeProdInOutService.convertReturns(prodInOuts));
		logger.log("委外验退单", "上传委外验退单", prodInOuts.size());
		return makeProdInOutService.getMakeReturnB2bId(prodInOuts);
	}

	/**
	 * 将ERP的反过账的委外验退单写到平台
	 * 
	 * @param data
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/nonPosting", method = RequestMethod.POST)
	@ResponseBody
	public void nonPostingProdInOuts(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<MakeProdInOut> prodInOuts = FlexJsonUtils.fromJsonArray(jsonStr, MakeProdInOut.class);
		makeReturnService.nonPosting(makeProdInOutService.convertNonPostingReturns(prodInOuts));
		logger.log("反过账的委外验退单", "上传反过账的委外验退单", prodInOuts.size());
	}
}
