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.SaleNotifyDown;
import com.uas.platform.b2b.erp.model.SaleNotifyDownEnd;
import com.uas.platform.b2b.erp.model.SaleOut;
import com.uas.platform.b2b.erp.service.SaleNotifyDownService;
import com.uas.platform.b2b.service.PurchaseNoticeService;
import com.uas.platform.core.util.serializer.FlexJsonUtils;

/**
 * 对卖家ERP的数据接口<br>
 * 客户送货提醒
 * 
 * @author yingp
 *
 */
@Controller
@RequestMapping("/erp/sale/notice")
public class SaleNotifyDownController {

	@Autowired
	private SaleNotifyDownService saleNotifyDownService;

	@Autowired
	private PurchaseNoticeService purchaseNoticeService;

	/**
	 * 卖家ERP从平台获取客户送货提醒
	 * 
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public List<SaleNotifyDown> getNotices() {
		return saleNotifyDownService.convertPurchaseNotice(purchaseNoticeService.findNotUploadNotice());
	}

	/**
	 * 平台的送货提醒传到供应商ERP之后，修改平台里面的送货提醒的上传状态
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public void onSaleNotifyDownSuccess(@RequestParam("data") String data) throws UnsupportedEncodingException {
		purchaseNoticeService.onNoticeDownSuccess(URLDecoder.decode(data, "UTF-8").split(","));
	}

	/**
	 * 将卖家ERP的发货单写到平台
	 * 
	 * @param data
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/send", method = RequestMethod.POST)
	@ResponseBody
	public void saveReplies(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<SaleOut> outs = FlexJsonUtils.fromJsonArray(jsonStr, SaleOut.class);
		purchaseNoticeService.send(saleNotifyDownService.convertSaleOut(outs));
	}

	/**
	 * 卖家ERP从平台获取在平台维护的发货单
	 * 
	 * @return
	 */
	@RequestMapping(value = "/send", method = RequestMethod.GET)
	@ResponseBody
	public List<SaleOut> getSaleOut() {
		return saleNotifyDownService.convertSaleSend(purchaseNoticeService.findNotSendSend());
	}

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

	/**
	 * 卖家ERP从平台获取结案、反结案客户送货提醒
	 * 
	 * @return
	 */
	@RequestMapping(value = "/end", method = RequestMethod.GET)
	@ResponseBody
	public List<SaleNotifyDownEnd> getSaleNotifyEnd() {
		return saleNotifyDownService.convertPurchaseNoticeEnd(purchaseNoticeService.findNotSendEnd());
	}

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

}
