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

import com.uas.platform.b2b.erp.model.ProdInOutRefreshPrice;
import com.uas.platform.b2b.erp.model.PurchaseProdInOut;
import com.uas.platform.b2b.erp.service.PurchaseProdInOutService;
import com.uas.platform.b2b.erp.service.PurchaseService;
import com.uas.platform.b2b.erp.support.ErpBufferedLogger;
import com.uas.platform.b2b.model.PurchaseAcceptItem;
import com.uas.platform.b2b.service.*;
import com.uas.platform.core.logging.BufferedLoggerManager;
import com.uas.platform.core.util.serializer.FlexJsonUtils;
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 java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * 对买家ERP的数据接口<br>
 * 买家采购验收单接口
 * 
 * @author suntg
 * 
 */
@Controller
@RequestMapping("/erp/purchase/prodInOut")
public class PurchaseProdInOutController {

	@Autowired
	private PurchaseAcceptService purchaseAcceptService;
	
	@Autowired
	private PurchaseProdInOutService purchaseProdInOutService;

	@Autowired
	private PurchaseApBillService purchaseApBillService;

	@Autowired
	private MakeAcceptService makeAcceptService;

	@Autowired
	private MakeReturnService makeReturnService;

	@Autowired
	private PurchaseReturnService returnService;

	@Autowired
	private PurchaseService purchaseService;

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

	/**
	 * 将ERP的采购验收单写到平台
	 * 
	 * @param data
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public List<PurchaseProdInOut> saveProdInOuts(@RequestParam("data") String data) throws Exception {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<PurchaseProdInOut> prodInOuts = FlexJsonUtils.fromJsonArray(jsonStr, PurchaseProdInOut.class);
		purchaseAcceptService.save(purchaseProdInOutService.convertProdInOuts(prodInOuts));
		logger.log("采购验收单", "上传采购验收单", prodInOuts.size());
		return purchaseProdInOutService.getAcceptB2bId(prodInOuts);
	}

	/**
	 * 将ERP的反过账的采购验收单写到平台
	 * 
	 * @param data
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value="/nonPosting", method = RequestMethod.POST)
	@ResponseBody
	public void nonPostingProdInOuts(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<PurchaseProdInOut> prodInOuts = FlexJsonUtils.fromJsonArray(jsonStr, PurchaseProdInOut.class);
		purchaseAcceptService.nonPosting(purchaseProdInOutService.convertNonPostingProdInOuts(prodInOuts));
		logger.log("反过账的采购验收单", "上传反过账的采购验收单", prodInOuts.size());
	}

	/**
	 * 批量更新采购验收单单价
	 * 
	 * @param data
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/refreshPrice", method = RequestMethod.POST)
	@ResponseBody
	public void refreshPrice(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<ProdInOutRefreshPrice> prodInOuts = FlexJsonUtils.fromJsonArray(jsonStr, ProdInOutRefreshPrice.class);
		// 更新采购验收单明细的价格
		purchaseAcceptService.save(purchaseProdInOutService.convertPirceForProdIn(prodInOuts));
		// 更新关联的应付发票的价格
		purchaseApBillService.refreshPrice(prodInOuts);
		logger.log("批量更新采购验收单单价", "上传批量更新采购验收单单价", prodInOuts.size());
	}


	/**
	 * 批量更新采购验退单单价
	 *
	 * @param data
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/refreshPrice/purc/return", method = RequestMethod.POST)
	@ResponseBody
	public void refreshPriceForPurcReturn(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<ProdInOutRefreshPrice> prodInOuts = FlexJsonUtils.fromJsonArray(jsonStr, ProdInOutRefreshPrice.class);
		// 更新采购验退单的价格
		returnService.save(returnService.convertPirceForProdIn(prodInOuts));
		// 更新关联的应付发票的价格
		purchaseApBillService.refreshPrice(prodInOuts);
		logger.log("批量更新采购验退单单价", "上传批量更新采购验退单单价", prodInOuts.size());
	}

	/**
	 * 批量更新委外验收单单价
	 *
	 * @param data
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/refreshPrice/make/accept", method = RequestMethod.POST)
	@ResponseBody
	public void refreshPriceForMakeAccept(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<ProdInOutRefreshPrice> prodInOuts = FlexJsonUtils.fromJsonArray(jsonStr, ProdInOutRefreshPrice.class);
		// 更新委外验收单的价格
		makeAcceptService.save(makeAcceptService.convertPirceForProdIn(prodInOuts));
		// 更新关联的应付发票的价格
		purchaseApBillService.refreshPrice(prodInOuts);
		logger.log("批量更新委外验收单单价", "上传批量更新委外验收单单价", prodInOuts.size());
	}

	/**
	 * 批量更新委外验退单单价
	 *
	 * @param data
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/refreshPrice/make/return", method = RequestMethod.POST)
	@ResponseBody
	public void refreshPriceForMakeReturn(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<ProdInOutRefreshPrice> prodInOuts = FlexJsonUtils.fromJsonArray(jsonStr, ProdInOutRefreshPrice.class);
		// 更新委外验退单的价格
		makeReturnService.save(makeReturnService.convertPirceForProdIn(prodInOuts));
		// 更新关联的应付发票的价格
		purchaseApBillService.refreshPrice(prodInOuts);
		logger.log("批量更新委外验退单单价", "上传批量更新委外验退单单价", prodInOuts.size());
	}
}
