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.AcceptNotify;
import com.uas.platform.b2b.erp.model.PurchaseNotify;
import com.uas.platform.b2b.erp.service.PurchaseNotifyService;
import com.uas.platform.b2b.service.PurchaseNoticeService;
import com.uas.platform.core.util.serializer.FlexJsonUtils;

/**
 * 对买家ERP的数据接口<br>
 * 买家送货提醒
 * 
 * @author yingp
 * 
 */
@Controller
@RequestMapping("/erp/purchase/notice")
public class PurchaseNotifyController {

	@Autowired
	private PurchaseNotifyService purchaseNotifyService;

	@Autowired
	private PurchaseNoticeService purchaseNoticeService;

	/**
	 * 将ERP的送货提醒写到平台
	 * 
	 * @param data
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public void saveNotifies(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<PurchaseNotify> notifies = FlexJsonUtils.fromJsonArray(jsonStr, PurchaseNotify.class);
		purchaseNoticeService.save(purchaseNotifyService.convertPurchaseNotify(notifies));
	}

	/**
	 * 从买家ERP获取平台的发货单
	 * 
	 * @return
	 */
	@RequestMapping(value = "/accept", method = RequestMethod.GET)
	@ResponseBody
	public List<AcceptNotify> getAcceptNotify() {
		return purchaseNotifyService.convertSaleSend(purchaseNoticeService.findNotUploadSend());
	}

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

	/**
	 * 买家ERP主动收料的记录上传到平台 <br>
	 * 买卖双方直接电话沟通之后，结果由买家填写到ERP系统的情况
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/accept", method = RequestMethod.POST)
	@ResponseBody
	public void send(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<AcceptNotify> accepts = FlexJsonUtils.fromJsonArray(jsonStr, AcceptNotify.class);
		purchaseNoticeService.send(purchaseNotifyService.convertAcceptNotify(accepts));
	}

	/**
	 * 买家结案送货提醒后，传到平台
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/end", method = RequestMethod.POST)
	@ResponseBody
	public void end(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<PurchaseNotify> ends = FlexJsonUtils.fromJsonArray(jsonStr, PurchaseNotify.class);
		purchaseNoticeService.end(ends);
	}

}
