package com.uas.platform.b2b.erp.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.uas.platform.b2b.erp.model.PurchaseForecast;
import com.uas.platform.b2b.erp.service.PurchaseForecastService;
import com.uas.platform.b2b.erp.support.ErpBufferedLogger;
import com.uas.platform.b2b.service.PurchaseForecastOrderService;
import com.uas.platform.core.logging.BufferedLoggerManager;
import com.uas.platform.core.util.serializer.FlexJsonUtils;
import org.apache.axis.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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 java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

@Controller
@RequestMapping("/erp/purchase/forecast")
public class PurchaseForecastController {
	@Autowired
	private PurchaseForecastService purchaseForecastService;

	@Autowired
	private PurchaseForecastOrderService purchaseForecastOrderService;

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

	/**
	 * 将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<PurchaseForecast> purchaseForecasts = FlexJsonUtils.fromJsonArray(jsonStr, PurchaseForecast.class);
		purchaseForecastOrderService.save(purchaseForecastService.convertPurchaseForecast(purchaseForecasts));
		logger.log("采购预测", "上传采购预测", purchaseForecasts.size());
	}

	/**
	 * 将ERP的采购预测写到平台(EDI)
	 *
	 * @param data
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/EDI", method = RequestMethod.POST)
	@ResponseBody
	public ModelMap saveEDIPurchases(@RequestParam("data") String data) throws UnsupportedEncodingException {
		ModelMap map = new ModelMap();
		if (null == data) {
			map.put("success", false);
			map.put("message", "未传入参数！");
			return map;
		}
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		JSONObject jsonObject = JSON.parseObject(jsonStr);
		if (StringUtils.isEmpty(jsonObject.getString("enUU")) || StringUtils.isEmpty(jsonObject.getString("PurchaseForecast"))) {
			map.put("success", false);
			map.put("message", "参数名有误！");
			return map;

		}
		List<PurchaseForecast> purchaseForecasts = FlexJsonUtils.fromJsonArray(jsonObject.getString("PurchaseForecast"), PurchaseForecast.class);
		Long enUU = FlexJsonUtils.fromJson(jsonObject.getString("enUU"), Long.class);
		purchaseForecastOrderService.save(purchaseForecastService.convertEDIPurchaseForecast(purchaseForecasts, enUU));
		logger.log("采购预测", "上传采购预测", purchaseForecasts.size());
		map.put("success", true);
		return map;
	}

}
