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

import com.uas.platform.b2b.erp.model.BillOut;
import com.uas.platform.b2b.erp.service.impl.BillOutService;
import com.uas.platform.b2b.erp.support.ErpBufferedLogger;
import com.uas.platform.b2b.service.PurchaseApBillOutService;
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;

/**
 * 应收开票数据接口
 *
 * @author huangct
 * 
 */
@Controller
@RequestMapping("/erp/purchase/BillOut")
public class BillOutController {

	@Autowired
	private PurchaseApBillOutService purchaseApBillOutService;
	
	@Autowired
	private BillOutService billOutService;

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

	/**
	 * 将ERP的应收开票写到平台
	 * 
	 * @param data
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public void saveApBills(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<BillOut> billOuts = FlexJsonUtils.fromJsonArray(jsonStr, BillOut.class);
		purchaseApBillOutService.saveByItem(billOutService.convertBillOuts(billOuts));
		logger.log("应收开票单", "上传应收开票单", billOuts.size());
	}
}
