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

import com.uas.platform.b2b.erp.model.*;
import com.uas.platform.b2b.erp.service.PurchaseService;
import com.uas.platform.b2b.erp.support.ErpBufferedLogger;
import com.uas.platform.b2b.openapi.model.SaleItem;
import com.uas.platform.b2b.service.PurchaseOrderService;
import com.uas.platform.b2b.service.SaleOrderItemService;
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 yingp
 * 
 */
@Controller
@RequestMapping("/erp/purchase")
public class PurchaseController {

	@Autowired
	private PurchaseService purchaseService;

	@Autowired
	private PurchaseOrderService purchaseOrderService;

	@Autowired
    private SaleOrderItemService itemService;

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

	/**
	 * 将ERP的采购订单写到平台
	 * 
	 * @param data
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public List<Purchase> savePurchases(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<Purchase> purchases = FlexJsonUtils.fromJsonArray(jsonStr, Purchase.class);
		purchaseOrderService.save(purchaseService.convertPurchase(purchases));
		logger.log("采购单", "上传采购单", purchases.size());
		return purchaseService.getB2bId(purchases);
	}

	/**
     * 从买家ERP获取平台的订单回复
     *
     * @return
     */
    @RequestMapping(value = "/reply", method = RequestMethod.GET)
    @ResponseBody
    public List<PurchaseReply> getReply() {
        List<PurchaseReply> replies = purchaseService.convertPurchaseOrderReply(purchaseOrderService.findNotUploadReply());
        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 {
		purchaseOrderService.onReplyUploadSuccess(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<PurchaseDetailEnd> ends = FlexJsonUtils.fromJsonArray(jsonStr, PurchaseDetailEnd.class);
		purchaseOrderService.saveItems(purchaseService.convertPurchaseEnd(ends));
		logger.log("采购单", "上传结案、反结案采购单", ends.size());
	}

    /**
     * 从买家ERP获取平台的打印状态
     *
     * @return
     */
    @RequestMapping(value = "/print", method = RequestMethod.GET)
    @ResponseBody
    public List<MessageLog> getPrint() {
        List<MessageLog> messageLogs = purchaseService.convertMessageLog(purchaseOrderService.findPrintNotUploadReply());
        logger.log("采购单", "下载打印记录", messageLogs.size());
        return messageLogs;
    }

    /**
     * 平台的打印记录传到ERP之后，修改平台里面的打印记录的传输状态
     *
     * @return
     * @throws UnsupportedEncodingException
     */
    @RequestMapping(value = "/print/back", method = RequestMethod.POST)
    @ResponseBody
    public void onPrintSendSuccess(@RequestParam("data") String data) throws UnsupportedEncodingException {
        purchaseOrderService.onPrintSendSuccess(URLDecoder.decode(data, "UTF-8").split(","));
    }

    /**
     * 将未上传的采购明细上传到平台
     *
     * @param data
     * @throws UnsupportedEncodingException
     */
    @RequestMapping(value = "/item", method = RequestMethod.POST)
    @ResponseBody
    public void saveSaleItem(@RequestParam("data") String data) throws UnsupportedEncodingException {
        String jsonStr = URLDecoder.decode(data, "UTF-8");
        List<PurchaseDetail> details = FlexJsonUtils.fromJsonArray(jsonStr, PurchaseDetail.class);
        itemService.save(itemService.covert(details));
        logger.log("采购单", "上传采购单明细", details.size());
    }
}
