---
--- 业务锁定
--- 键值为key，参数为通过eval传递的数组
---

function string:split(sep)
    local sep, fields = sep or ",", {};
    local pattern = string.format("([^%s]+)", sep);
    self:gsub(pattern, function(c) fields[#fields+1] = c end);
    return fields;
end;

local key = KEYS[1]
local keyUser = KEYS[2]
local user = tonumber(ARGV[1])
--- op = 0 锁定 op = 1 解锁
local op = tonumber(ARGV[2])
local idStr = ARGV[3]

local idArr = idStr:split()
---unpack如果报错，可能是redis集成的lua版本问题，可改为table.unpack
local vals = redis.call("HMGET", key, unpack(idArr))

local result = {}
result['error'] = 0
result['errorMsg'] = ''

---需要判断两种情况，1返回值为空键值不存在 2返回的所有值都为空，表示无人锁定
if (next(vals) == nil) then
    if (op == 0) then
        ---整个都为空，理论为键值不存在 表示无人锁定
        local newTable = {}
        local newTableUser = {}
        local i = 1
        for k, v in pairs(idArr) do
            newTable[i] = v;
            newTable[i + 1] = tostring(user)
            newTableUser[i] = tostring(v) .. "-" .. tostring(user) .. "-0"
            newTableUser[i + 1] = "1"
            i = i + 2
        end
        redis.call("HMSET", key, unpack(newTable))
        redis.call("HMSET", keyUser, unpack(newTableUser))
        ---返回所有id
        result['successIds'] = idArr
        result['realSuccessIds'] = idArr
        result['failIds'] = { };
    else
        --- 解锁时，无人锁定
        result['error'] = 1
        result['errorMsg'] = '无锁定数据'
        result['successIds'] = {}
        result['realSuccessIds'] = {}
        result['failIds'] = idArr
    end
    return cjson.encode(result);
else
    ---可锁定ids
    local waitSuccessLockIds = {};
    local waitRealSuccessLockIds = {};
    local waitFailLockIds = {};

    ---可解锁ids
    local waitSuccessDelIds = {};
    local waitSuccessDelUserIds = {};
    local waitFailDelIds = {};
    for k, v in pairs(vals) do
        if (v == false) then
            ---表示不存在此值，即数据未锁定
            if (op == 0) then
                waitSuccessLockIds[#waitSuccessLockIds + 1] = idArr[k];
                waitRealSuccessLockIds[#waitRealSuccessLockIds + 1] = idArr[k];
            else
                waitFailDelIds[#waitFailDelIds + 1] = idArr[k];
            end
        else
            if (op == 0) then
                ---锁定情况下 表示有值，但需要做进一步判断是否锁定人和传入的操作人不同
                if (tonumber(v) ~= user) then
                    waitFailLockIds[#waitFailLockIds + 1] = idArr[k];
                else
                    waitSuccessLockIds[#waitSuccessLockIds + 1] = idArr[k];
                end
            else
                ---解锁情况下 表示有值，但需要做进一步判断是否锁定人和传入的操作人不同
                if (tonumber(v) == user) then
                    waitSuccessDelIds[#waitSuccessDelIds + 1] = idArr[k];
                    waitSuccessDelUserIds[#waitSuccessDelUserIds + 1] = tostring(idArr[k]) .. "-" .. tostring(user) .. "-0"
                else
                    waitFailDelIds[#waitFailDelIds + 1] = idArr[k];
                end
            end
        end
    end

    if (op == 0) then
        if (next(waitSuccessLockIds) ~= nil) then
            local newTable = {}
            local newTableUser = {}
            local i = 1
            for k, v in pairs(waitSuccessLockIds) do
                newTable[i] = v;
                newTable[i + 1] = tostring(user)
                newTableUser[i] = tostring(v) .. "-" .. tostring(user) .. "-0"
                newTableUser[i + 1] = "1"
                i = i + 2
            end
            redis.call("HMSET", key, unpack(newTable))
            redis.call("HMSET", keyUser, unpack(newTableUser))
            result['error'] = 0
            result['errorMsg'] = ''
            result['successIds'] = waitSuccessLockIds
            result['realSuccessIds'] = waitRealSuccessLockIds
            result['failIds'] = waitFailLockIds
        else
            result['error'] = 1
            result['errorMsg'] = '无可锁定数据'
            result['successIds'] = waitSuccessLockIds
            result['realSuccessIds'] = waitRealSuccessLockIds
            result['failIds'] = waitFailLockIds
        end
        return cjson.encode(result);
    else
        if (next(waitSuccessDelIds) ~= nil) then
            redis.call("HDEL", key, unpack(waitSuccessDelIds))
            redis.call("HDEL", keyUser, unpack(waitSuccessDelUserIds))
            result['error'] = 0
            result['errorMsg'] = ''
            result['successIds'] = waitSuccessDelIds
            result['realSuccessIds'] = waitSuccessDelIds
            result['failIds'] = waitFailDelIds
        else
            result['error'] = 1
            result['errorMsg'] = '无可解锁数据'
            result['successIds'] = waitSuccessDelIds
            result['realSuccessIds'] = waitSuccessDelIds
            result['failIds'] = waitFailDelIds
        end
        return cjson.encode(result);
    end
end





