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.BarAcceptNotifyDown;
import com.uas.platform.b2b.erp.model.BarPackageDown;
import com.uas.platform.b2b.erp.service.BarAcceptNotifyDownService;
import com.uas.platform.b2b.erp.service.BarPackageDownService;
import com.uas.platform.b2b.erp.support.ErpBufferedLogger;
import com.uas.platform.b2b.service.BarPackageService;
import com.uas.platform.b2b.service.BarSendNotifyService;
import com.uas.platform.core.logging.BufferedLoggerManager;

/**
 * 对卖家ERP的数据接口,下载barAcceptNotify
 * @author aof
 * @date 2015年10月30日
 */
@Controller
@RequestMapping("/erp/bar")
public class BarAcceptNotifyDownController {
	
	@Autowired
	private BarSendNotifyService barSendNotifyService;
	
	@Autowired
	private BarAcceptNotifyDownService barAcceptNotifyDownService;
	
	@Autowired
	private BarPackageService barPackageService;
	
	@Autowired
	private BarPackageDownService barPackageDownService;
	
	private final static ErpBufferedLogger logger = BufferedLoggerManager.getLogger(ErpBufferedLogger.class);
	
	/**
	 * 卖家ERP从平台获取未下载的sendnotify
	 * 
	 * @return
	 */
	@RequestMapping(value="/sendnotify",method = RequestMethod.GET)
	@ResponseBody
	public List<BarAcceptNotifyDown> getBarSendnotifys() {
		List<BarAcceptNotifyDown> downs = barAcceptNotifyDownService.convertBarAcceptNotifys(barSendNotifyService.findNotUpload());
		logger.log("客户发货条码通知", "下载发货条码通知", downs.size());
		return downs;
	}
	
	/**
	 * 上传成功后修改表的上传状态
	 * @param data
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value="/sendnotify",method = RequestMethod.POST)
	@ResponseBody
	public void downBANotifySuccess(@RequestParam("data") String data) throws UnsupportedEncodingException {
		barSendNotifyService.onUploadSuccess(URLDecoder.decode(data, "UTF-8").split(","));
	}
	
	/**
	 * 卖家ERP从平台获取未下载的发货条码包装
	 * 
	 * @return
	 */
	@RequestMapping(value="/package",method = RequestMethod.GET)
	@ResponseBody
	public List<BarPackageDown> getBarPackages() {
		List<BarPackageDown> downs = barPackageDownService.convertBarPackageDowns(barPackageService.findNotUpload());
		logger.log("客户发货条码包装通知", "下载发货条码包装", downs.size());
		return downs;
	}
	
	/**
	 * 上传成功后修改表的上传状态
	 * @param data
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value="/packagesuccess",method = RequestMethod.POST)
	@ResponseBody
	public void downBarPackageDownSuccess(@RequestParam("data") String data) throws UnsupportedEncodingException {
		barPackageService.onUploadSuccess(URLDecoder.decode(data, "UTF-8").split(","));
	}
}
