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.Make;
import com.uas.platform.b2b.erp.model.MakeEnd;
import com.uas.platform.b2b.erp.model.MakeReply;
import com.uas.platform.b2b.erp.service.MakeService;
import com.uas.platform.b2b.erp.support.ErpBufferedLogger;
import com.uas.platform.b2b.service.MakeOrderService;
import com.uas.platform.core.logging.BufferedLoggerManager;
import com.uas.platform.core.util.serializer.FlexJsonUtils;

/**
 * 对买家ERP的数据接口<br>
 * 买家委外加工单
 * 
 * @author suntg
 * 
 */
@Controller
@RequestMapping("/erp/make")
public class MakeController {

	@Autowired
	private MakeService makeService;

	@Autowired
	private MakeOrderService makeOrderService;

	private final static ErpBufferedLogger logger = BufferedLoggerManager.getLogger(ErpBufferedLogger.class);

	/**
	 * 买家ERP将ERP的委外加工单写到平台
	 * 
	 * @param data
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public void saveMakes(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<Make> makes = FlexJsonUtils.fromJsonArray(jsonStr, Make.class);
		makeOrderService.saveOrders(makeService.convertMakes(makes));
		logger.log("委外加工单", "上传委外加工单", makes.size());
	}

	/**
	 * 买家ERP从平台获取新增的订单回复
	 * 
	 * @return
	 */
	@RequestMapping(value = "/reply", method = RequestMethod.GET)
	@ResponseBody
	public List<MakeReply> getReply() {
		List<MakeReply> replies = makeService.convertMakeOrderReply(makeOrderService.findNotSendReply());
		logger.log("委外加工单", "下载委外加工单回复记录", replies.size());
		return replies;
	}

	/**
	 * 买家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));
//		logger.log("采购单", "上传采购单主动回复", replies.size());
	}

	/**
	 * 平台的委外加工单回复传到ERP之后，修改平台里面的回复记录的状态
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/reply/back", method = RequestMethod.POST)
	@ResponseBody
	public void onReplySuccess(@RequestParam("data") String data) throws UnsupportedEncodingException {
		makeOrderService.onReplySendSuccess(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<MakeEnd> ends = FlexJsonUtils.fromJsonArray(jsonStr, MakeEnd.class);
		makeOrderService.saveOrders(makeService.convertMakeEnd(ends));
		logger.log("委外加工单", "上传结案、反结案委外加工单", ends.size());
	}


}
