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

import com.uas.platform.b2b.erp.model.PurchaseAPBill;
import com.uas.platform.b2b.erp.service.PurchaseAPBillService;
import com.uas.platform.b2b.erp.support.ErpBufferedLogger;
import com.uas.platform.b2b.model.ApbillAdjustment;
import com.uas.platform.b2b.service.ApbillAdjustmentService;
import com.uas.platform.b2b.service.PurchaseApBillService;
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/APBill")
public class PurchaseAPBillController {

	@Autowired
	private PurchaseApBillService purchaseApBillService;
	
	@Autowired
	private PurchaseAPBillService purchaseAPBillService;

	@Autowired
	private ApbillAdjustmentService apbillAdjustmentService;

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

	/**
	 * 将ERP的应付发票写到平台
	 * 
	 * @param data
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public List<PurchaseAPBill> saveApBills(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<PurchaseAPBill> purchaseAPBills = FlexJsonUtils.fromJsonArray(jsonStr, PurchaseAPBill.class);
		purchaseApBillService.save(purchaseAPBillService.convertApBills(purchaseAPBills));
		logger.log("采购变更单", "上传采购变更单", purchaseAPBills.size());
		return purchaseAPBillService.getB2bId(purchaseAPBills);
	}

	/**
	 * 将ERP的反过账的应付发票写到平台
	 * 
	 * @param data
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value="/nonPosting", method = RequestMethod.POST)
	@ResponseBody
	public void nonPostingApBills(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<PurchaseAPBill> apBills = FlexJsonUtils.fromJsonArray(jsonStr, PurchaseAPBill.class);
		purchaseApBillService.nonPosting(purchaseAPBillService.convertNonPostingApBills(apBills));
		logger.log("反过账的应付发票", "上传反过账的应付发票", apBills.size());
	}

	/**
	 * 将ERP的货款调账传到平台
	 * 
	 * @param data
	 * @return
	 * @throws UnsupportedEncodingException
	 * @author hejq
	 */
	@RequestMapping(value = "/APBillAdjustment", method = RequestMethod.POST)
	@ResponseBody
	public void APBillAdjustment(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<PurchaseAPBill> apBills = FlexJsonUtils.fromJsonArray(jsonStr, PurchaseAPBill.class);
		List<ApbillAdjustment> adjusts = purchaseAPBillService.convertApbillAdjustment(apBills);
		apbillAdjustmentService.save(adjusts);
		logger.log("货款调账", "上传货款调账", apBills.size());
	}

	/**
	 * 将ERP反过账的货款调账传到平台
	 * 
	 * @param data
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/nonPostAdjustment", method = RequestMethod.POST)
	@ResponseBody
	public void nonPostAdjustment(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<PurchaseAPBill> apBills = FlexJsonUtils.fromJsonArray(jsonStr, PurchaseAPBill.class);
		apbillAdjustmentService.nonPosting(apbillAdjustmentService.convertnonPostAdjustment(apBills));
	}
}
