package com.fs.erp.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fs.common.exception.CustomException;
import com.fs.common.exception.ServiceException;
import com.fs.common.utils.StringUtils;
import com.fs.common.utils.http.HttpUtils;
import com.fs.erp.domain.ErpGoods;
import com.fs.erp.domain.ErpGoodsStock;
import com.fs.erp.domain.KbStockRequest;
import com.fs.erp.domain.KbStockResponse;
import com.fs.erp.dto.*;
import com.fs.erp.service.IErpGoodsService;
import com.fs.erp.utils.ScrmStoreSignUtils;
import com.fs.erp.utils.ScrmStoreUrlUtils;
import com.fs.his.utils.ConfigUtil;
import com.fs.hisStore.config.FsErpConfig;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
@Slf4j
@AllArgsConstructor
public class K9StockScrmServiceImpl implements IErpGoodsService {

    @Autowired
    private ConfigUtil configUtil;

    @Override
    public BaseResponse addGoods(ErpGoods goods) {
        return null;
    }

    @Override
    public BaseResponse addGoodsScrm(ErpGoods goods) {
        return null;
    }

    @Override
    public ErpGoodsQueryResponse getGoods(ErpGoodsQueryRequert param) {
        return null;
    }

    @Override
    public ErpGoodsQueryResponse getGoodsScrm(ErpGoodsQueryRequert param) {
        return null;
    }

    @Override
    public ErpGoodsStockQueryResponse getGoodsStock(ErpGoodsStockQueryRequert param) {
        String barcode = param.getBarcode();
        KbStockRequest request = KbStockRequest.builder().goodsCode(barcode).build();
        KbStockResponse kbStockResponse = getStock(request);
        ErpGoodsStockQueryResponse response = new ErpGoodsStockQueryResponse();

        if(kbStockResponse.getSuccess()){
            ArrayList<ErpGoodsStock> erpGoodsStocks = new ArrayList<>();
            ErpGoodsStock erpGoodsStock = new ErpGoodsStock();
            Integer stocks = kbStockResponse.getStock();
            erpGoodsStock.setBarcode(barcode);
            erpGoodsStock.setQty(stocks.toString());
            erpGoodsStock.setSalable_qty(stocks.toString());
            erpGoodsStocks.add(erpGoodsStock);
            response.setStocks(erpGoodsStocks);
        } else {
            throw new CustomException("库存不足");
        }
        return response;
    }

    @Override
    public ErpGoodsStockQueryResponse getGoodsStockScrm(ErpGoodsStockQueryRequert param) {
        return null;
    }

    /**
     * 查看库存
     * @param request 参数
     * @return  KbStockResponse
     * @throws ServiceException 异常
     */
    private KbStockResponse getStock(KbStockRequest request) throws ServiceException {
        FsErpConfig erpConfig = configUtil.getErpConfig();
        // 构造参数
        String timeStep = System.currentTimeMillis() + "";
        if (StringUtils.isBlank(request.getStockCode())) {
            request.setStockCode(erpConfig.getCwarehouseCode());
        }
        String json = JSON.toJSONString(request);
        String sign = ScrmStoreSignUtils.sign(json, erpConfig.getKingbosSecret(), erpConfig.getKingbosan(), timeStep);
        String url = erpConfig.getKingbosUrl().replace("do=k9save", "do=k9view");
        url = ScrmStoreUrlUtils.getUrl(url, sign, timeStep);

        // 发送请求
        log.info("\n【金博网络】: getStock send request url: {}", url);
        String result = HttpUtils.doPost(url, json);
        log.info("\n【金博网络】: getStock res:{}", result);
        return JSONObject.parseObject(result, KbStockResponse.class);
    }
}
