
package com.tmerclub.cloud.product.util;

import cn.hutool.core.util.StrUtil;
import com.tmerclub.cloud.api.product.dto.StockBillLogItemDTO;
import com.tmerclub.cloud.api.product.vo.SkuStockVO;
import com.tmerclub.cloud.common.cache.constant.CacheNames;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.constant.StatusEnum;
import com.tmerclub.cloud.product.manager.StockManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Objects;

/**
 * 库存工具类
 * @author YXF
 * @date 2021/05/19
 */
public class StockUtil {

    private static final Logger LOG = LoggerFactory.getLogger(StockUtil.class);


    public static final int SKU_STOCK_INDEX = 0;
    public static final int SKU_LOCK_STOCK_INDEX = 1;
    public static final int SKU_SALE_INDEX = 2;
    public static final int ADD = 1;
    public static final int SUBTRACT = 0;


    /**
     *=========================== 拼接key ===================================
     */
    public static String getSkuStockLogKey(StockBillLogItemDTO stockBillLogItem) {
        return getSkuStockLogKey(stockBillLogItem.getOrderId(), stockBillLogItem.getSkuId());
    }

    public static String getSkuStockLogKey(Long orderId, Long skuId) {
        return CacheNames.ORDER_SKU_LOCK +
                orderId +
                Constant.UNDERLINE +
                StockManager.WAREHOUSE_ID +
                Constant.UNDERLINE + skuId;
    }
    public static String getSkuStockKey(Long skuId, Long stockPointId, Integer stockPointType) {
        return CacheNames.SKU_SOCK + stockPointId + Constant.UNDERLINE + stockPointType +
                Constant.UNDERLINE + skuId;
    }

    public static SkuStockVO getSkuStockVO(String skuKey, String value) {
        // 切割出skuId
        Long skuId = Long.parseLong(skuKey.split(Constant.UNDERLINE)[0]);
        Long stockPointId = skuKey.split(Constant.UNDERLINE).length == 1 ? 0L : Long.parseLong(skuKey.split(Constant.UNDERLINE)[1]);
        return getSkuStockVO(skuId, stockPointId, value);
    }

    public static SkuStockVO getSkuStockVO(Long skuId, String value) {
        return getSkuStockVO(skuId, Constant.ZERO_LONG, value);
    }

    private static SkuStockVO getSkuStockVO(Long skuId, Long stockPointId, String value) {
        // sku字符串分割为数组
        if (StrUtil.isBlank(value)) {
            LOG.error("sku字符串分割为数组异常，skuId：{}，stockPointId：{}，value：{}", skuId, stockPointId, value);
            SkuStockVO skuStockVO = new SkuStockVO(skuId, 0);
            skuStockVO.setStockPointId(stockPointId);
            skuStockVO.setStatus(StatusEnum.DELETE.value());
            return skuStockVO;
        }
        String[] skuArray = value.split(Constant.UNDERLINE);
        // 封装sku对象
        SkuStockVO skuStock = new SkuStockVO(skuId);
        skuStock.setStockPointId(stockPointId);
        for (int i = 0; i < skuArray.length; i++) {
            Integer num = 0;
            if (StrUtil.isNotBlank(skuArray[i]) && !StrUtil.equals(skuArray[i], "null")) {
                num = Integer.parseInt(skuArray[i]);
            }
            switch (i) {
                case 0 -> skuStock.setStock(num);
                case 1 -> skuStock.setLockStock(num);
                case 2 -> skuStock.setSale(num);
                case 3 -> skuStock.setStatus(num);
                default -> {}
            }
        }
        return skuStock;
    }

    public static String[] loadQueryArray(Object... objects) {
        int length = objects.length;
        String[] queryArray = new String[length];
        for (int i = 0; i < length; i++) {
            Object object = objects[i];
            // 默认是0
            if (Objects.isNull(object)) {
                queryArray[i] = "0";
                continue;
            }
            queryArray[i] = objects[i].toString();
        }
        return queryArray;
    }

    public static String skuPointKey(Long skuId, Long stockPointId) {
        if (Objects.isNull(stockPointId) || stockPointId == 0) {
            return String.valueOf(skuId);
        }
        return skuId + Constant.UNDERLINE + stockPointId;
    }
}
