---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by jlutt.
--- DateTime: 2020-09-07 16:26
--- 批量操作科目余额
--- 科目erp中允许负值
--- 传进来的数据明细对象为RedisAccountCalcItem，输出对象为RedisAccountCalcInfo

local key = KEYS[1]
local jsonData = ARGV[1]

local material = cjson.decode(jsonData)
local result = {}

result['error'] = 0
result['errorMsg'] = ''

--- 检查能否减少数量
--[[
for k, v in pairs(material) do
    local numName = v['numName']
    local numKey = v['numKey']
    local numField = v['numField']
    local nowNum = tonumber(redis.call('HGET', numKey, numField))

    if (nowNum == nil) then
        nowNum = 0;
    end

    local incrNum = v['incrNum']

    if ((nowNum == nil or nowNum <= 0) and (incrNum < 0)) then
        result['error'] = 1
        result['errorMsg'] = numName .. '余额不足'
        break ;
    end

    if (nowNum + incrNum) < 0 then
        result['error'] = 1
        result['errorMsg'] = numName .. '余额不足'
        break ;
    end

    local idToAccount = v['idToAccount']
    if (idToAccount > 0) then
        --- 判断目标库存
        local numToField = v['numToField']
        local numToName = v['numToName']
        local nowToNum = tonumber(redis.call('HGET', numKey, numToField))

        if (nowToNum == nil) then
            nowToNum = 0;
        end

        local incrToNum = v['incrToNum']

        if ((nowToNum == nil or nowToNum <= 0) and (incrToNum < 0)) then
            result['error'] = 1
            result['errorMsg'] = numToName .. '余额不足'
            break ;
        end

        if (nowToNum + incrToNum) < 0 then
            result['error'] = 1
            result['errorMsg'] = numToName .. '余额不足'
            break ;
        end
    end
end
]]
local currIndex2 = 1
local stock = {}
if (result['error'] == 0) then
    --数量都够，可以操作
    for k, v in pairs(material) do
        local numKey = v['numKey']
        local numField = v['numField']
        local incrNum = v['incrNum']

        stock[currIndex2] = {}

        stock[currIndex2]['idAccount'] = v['idAccount'];

        local iExists = redis.call("HEXISTS", numKey, numField);
        if (iExists == 0) then
            stock[currIndex2]['insert'] = 1
        else
            stock[currIndex2]['insert'] = 0
        end

        local afterNum = redis.call("HINCRBY", numKey, numField, incrNum)

        stock[currIndex2]['beforeNum'] = afterNum - incrNum
        stock[currIndex2]['afterNum'] = afterNum
        stock[currIndex2]['calcNum'] = incrNum

        local idToAccount = v['idToAccount']
        if (idToAccount > 0) then

            local numToField = v['numToField']
            local incrToNum = v['incrToNum']

            stock[currIndex2]['idToAccount'] = v['idToAccount'];

            local iToExists = redis.call("HEXISTS", numKey, numToField);
            if (iToExists == 0) then
                stock[currIndex2]['toInsert'] = 1
            else
                stock[currIndex2]['toInsert'] = 0
            end

            local afterToNum = redis.call("HINCRBY", numKey, numToField, incrToNum)

            stock[currIndex2]['toBeforeNum'] = afterToNum - incrToNum
            stock[currIndex2]['toAfterNum'] = afterToNum
            stock[currIndex2]['toCalcNum'] = incrToNum
        end

        stock[currIndex2]['calcMemo'] = v['numMemo']
        stock[currIndex2]['internalFlag'] = v['internalFlag']

        ---stock[currIndex2]['insert'] = redis.call("HSET", storeKey, storeField, afterNum)
        currIndex2 = currIndex2 + 1
    end
end

local time = redis.call('TIME')
result['time'] = time[1]
result['microseconds'] = time[2]

result['accountList'] = stock

local re = cjson.encode(result);

return re
