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.Purchase;
import com.uas.platform.b2b.erp.model.PurchaseDetailEnd;
import com.uas.platform.b2b.erp.model.PurchaseReply;
import com.uas.platform.b2b.erp.service.PurchaseService;
import com.uas.platform.b2b.service.PurchaseOrderService;
import com.uas.platform.core.util.serializer.FlexJsonUtils;

/**
 * 对买家ERP的数据接口<br>
 * 买家订单处理
 * 
 * @author yingp
 * 
 */
@Controller
@RequestMapping("/erp/purchase")
public class PurchaseController {

	@Autowired
	private PurchaseService purchaseService;

	@Autowired
	private PurchaseOrderService purchaseOrderService;

	/**
	 * 将ERP的采购订单写到平台
	 * 
	 * @param data
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public void savePurchases(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<Purchase> purchases = FlexJsonUtils.fromJsonArray(jsonStr, Purchase.class);
		purchaseOrderService.save(purchaseService.convertPurchase(purchases));
	}

	/**
	 * 从买家ERP获取平台的订单回复
	 * 
	 * @return
	 */
	@RequestMapping(value = "/reply", method = RequestMethod.GET)
	@ResponseBody
	public List<PurchaseReply> getReply() {
		return purchaseService.convertPurchaseOrderReply(purchaseOrderService.findNotUploadReply());
	}

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

	/**
	 * 平台的订单回复传到ERP之后，修改平台里面的回复记录的状态
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/reply/back", method = RequestMethod.POST)
	@ResponseBody
	public void onReplySuccess(@RequestParam("data") String data) throws UnsupportedEncodingException {
		purchaseOrderService.onReplyUploadSuccess(URLDecoder.decode(data, "UTF-8").split(","));
	}

	/**
	 * 买家结案、反结案采购单后，传到平台
	 * 
	 * @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<PurchaseDetailEnd> ends = FlexJsonUtils.fromJsonArray(jsonStr, PurchaseDetailEnd.class);
		purchaseOrderService.saveItems(purchaseService.convertPurchaseEnd(ends));
	}

}
