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

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
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.OutSourceProdInOutDown;
import com.uas.platform.b2b.erp.service.OutSourceProdInOutDownService;
import com.uas.platform.b2b.erp.support.ErpBufferedLogger;
import com.uas.platform.b2b.model.MakeReturn;
import com.uas.platform.b2b.service.MakeReturnService;
import com.uas.platform.core.logging.BufferedLoggerManager;

/**
 * 对买家ERP的数据接口<br>
 * 买家委外验退单接口
 * 
 * @author aof
 * @date 2015年8月31日
 */
@Controller
@RequestMapping("/erp/outSource/prodReturn")
public class OutSourceProdReturnDownController {

	@Autowired
	private MakeReturnService makeReturnService;

	@Autowired
	private OutSourceProdInOutDownService outSourceProdInOutDownService;
	
	private final static ErpBufferedLogger logger = BufferedLoggerManager.getLogger(ErpBufferedLogger.class);
	/**
	 * 卖家ERP从平台获取未下载的客户委外验退单
	 * 
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public List<OutSourceProdInOutDown> getOrderChanges() {
		List<OutSourceProdInOutDown> downs = outSourceProdInOutDownService.convertMakeReturns(makeReturnService.findNotUpload());
		logger.log("客户委外验退单", "下载已过账委外验退单", downs.size());
		return downs;
	}

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

	/**
	 * 卖家ERP从平台获取未下载客户已反过帐的的客户委外验退单
	 * 
	 * @return
	 */
	@RequestMapping(value = "/nonPosting", method = RequestMethod.GET)
	@ResponseBody
	public List<Long> getNonPosting() {
		List<Long> ids = new ArrayList<Long>();
		List<MakeReturn> makeReturns = makeReturnService.getNonPosting();
		if (!CollectionUtils.isEmpty(makeReturns)) {
			for (MakeReturn makeReturn : makeReturns) {
				ids.add(makeReturn.getId());
			}
		}
		logger.log("客户委外验退单", "下载反过帐的客户委外验退单", ids.size());
		return ids;
	}

	/**
	 * 平台的客户委外验收单传到供应商ERP之后，修改平台里面的客户委外验退单的上传状态
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/nonPosting", method = RequestMethod.POST)
	@ResponseBody
	public void onNonPostingDownSuccess(@RequestParam("data") String data) throws UnsupportedEncodingException {
		makeReturnService.onNonPostingUploadSuccess(URLDecoder.decode(data, "UTF-8").split(","));
	}

}
