-- 扣减库存(必须足够扣除的时候才会扣除，如果不够扣除返回负数，如果足够扣除返回剩余库存数量，否则不进行扣减)

-- 检验库存信息 这里的key是
-- 总库存key
local skuKey = KEYS[1]
-- 区域库存key
local skuPointKey = KEYS[2]
-- 库存变化数量
local stockChangeNum = tonumber(KEYS[1])

-- 获取区域库存数量
local pointStock = tonumber(redis.call('get', skuPointKey))
-- 获取总库存数量
local stock = tonumber(redis.call('get', skuKey))
if (pointStock == 0) then
    return 0
end
local canUse = pointStock - stockChangeNum
if (canUse > 0) then
    stock = stock - stockChangeNum
    pointStock = canUse
    redis.call('mset', skuPointKey, canUse, skuKey, stock)
else
    stock = stock - pointStock
    -- 直到扣减到0为止
    pointStock = 0
end
redis.call('mset', skuPointKey, pointStock, skuKey, stock)
return canUse



