---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by jlutt.
--- DateTime: 2020-12-21 11:17
--- 修图人员暂停重启订单数量变更

local orderKey = KEYS[1]
local imageKey = KEYS[2]
local idUser = ARGV[1]
local userMax = tonumber(ARGV[2])
local imagePCount = tonumber(ARGV[3])
local iKind = tonumber(ARGV[4])

local nowCount = tonumber(redis.call('HGET', orderKey, idUser))
if (nowCount == nil) then
    nowCount = 0
end

local afterNum = -1;

if (iKind == 0) then
    -- 正常的分配或者处理
    --- 设置P数
    if (imagePCount < 0) then
        afterNum = redis.call("ZSCORE", imageKey, idUser)
        if ((afterNum == nil or tonumber(afterNum) <= 0)) then
            afterNum = 0
        else
            afterNum = redis.call("ZINCRBY", imageKey, imagePCount, idUser)
        end
    else
        afterNum = redis.call("ZINCRBY", imageKey, imagePCount, idUser)
    end
elseif (iKind > 0) then
    --- 暂停
    if (nowCount < userMax) then
        afterNum = tonumber(redis.call("HINCRBY", orderKey, idUser, 1))
        --- 设置P数
        redis.call("ZINCRBY", imageKey, imagePCount, idUser)
    end
else
    --- 重启
    afterNum = tonumber(redis.call("HINCRBY", orderKey, idUser, -1))
    --- 设置P数
    redis.call("ZINCRBY", imageKey, imagePCount, idUser)
end

return tostring(afterNum);
