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

import com.uas.platform.b2b.erp.model.Inquiry;
import com.uas.platform.b2b.erp.model.Purchase;
import com.uas.platform.b2b.erp.model.PurchaseNotify;
import com.uas.platform.b2b.erp.service.InquiryService;
import com.uas.platform.b2b.erp.service.PurchaseNotifyService;
import com.uas.platform.b2b.erp.service.PurchaseService;
import com.uas.platform.b2b.erp.support.ErpBufferedLogger;
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.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * 针对每个数据传输进行数据传输验证（针对买家）
 *
 * Created by hejq on 2018-04-19.
 */
@RequestMapping("/erp/buyer")
@RestController
public class CheckTransController {

    @Autowired
    private InquiryService inquiryService;

    @Autowired
    private PurchaseService purchaseService;

    @Autowired
    private PurchaseNotifyService purchaseNotifyService;

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

    /**
     * 通过上传的数据查询询价单在平台是否存在
     *
     * @param data 封装的上传的数据
     * @return
     * @throws UnsupportedEncodingException
     */
    @RequestMapping(value = "/inquiry", method = RequestMethod.POST)
    public List<Inquiry> getInquiryB2bId(@RequestParam("data") String data) throws UnsupportedEncodingException {
        String jsonStr = URLDecoder.decode(data, "UTF-8");
        List<Inquiry> inquiryList = FlexJsonUtils.fromJsonArray(jsonStr, Inquiry.class);
        logger.log("单据传输检测", "采购询价单", inquiryList.size());
        return inquiryService.getB2bId(inquiryList);
    }

    /**
     * 通过上传id查询平台采购单对应的id
     *
     * @param data 封装的id信息
     * @return
     * @throws UnsupportedEncodingException
     */
    @RequestMapping(value = "/purchase", method = RequestMethod.POST)
    public List<Purchase> getPurchaseB2bId(@RequestParam("data") String data) throws UnsupportedEncodingException {
        String jsonStr = URLDecoder.decode(data, "UTF-8");
        List<Purchase> purchaseList = FlexJsonUtils.fromJsonArray(jsonStr, Purchase.class);
        logger.log("单据传输检测", "采购订单", purchaseList.size());
        return purchaseService.getB2bId(purchaseList);
    }

    /**
     * 通过上传id查询平台送货提醒对应的id
     *
     * @param data 封装的id信息
     * @return
     * @throws UnsupportedEncodingException
     */
    @RequestMapping(value = "/notify", method = RequestMethod.POST)
    public List<PurchaseNotify> getPurchaseNotifyB2bId(@RequestParam("data") String data) throws UnsupportedEncodingException {
        String jsonStr = URLDecoder.decode(data, "UTF-8");
        List<PurchaseNotify> notifyList = FlexJsonUtils.fromJsonArray(jsonStr, PurchaseNotify.class);
        logger.log("单据传输检测", "送货提醒", notifyList.size());
        return purchaseNotifyService.getB2bId(notifyList);
    }
}
