---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by 81460.
--- DateTime: 2020-12-09 16:08
--- 批量操作物料虚拟库存
--- 前提条件
--- 1、不允许一个进销存单据明细中同一种物料有不同的库房库位，如果需要做不同的库房库位操作，可以先做库存转移，然后在入库或者出库
--- 2、numKey为一个hash表，用于存储所有的物料的 numField的当前数量，numField可以用库房+库位字符串来区分
--- 4、计算逻辑只是简单的先判断传入的每个物料的当前数量是否大于需要增减的数量（减少数量传入负值即可），全部都符合条件则进行增减
--- 5、增减物料数量
--- 6、传进来的数据明细为正常的list转json即可
--- 7、传进来的数据明细对象为RedisStockCalcItem，输出对象为RedisStockCalcInfo
---[
---
---      {
---        "name":"Porsche",
---       "stock": 11.1
---      },
---      {
---     "name":"BMW"
---   },
---   {
---     "name":"Volvo"
---   }
---]
---Result返回对象为json
---Result.error = 0表示无措
---Result.errorMsg 表示错误信息
---
---
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 numVirtualKey = v['numVirtualKey']
    local numField = v['numField']

    local nowVirtualNum = tonumber(redis.call('HGET', numVirtualKey, numField))
    local nowQtyNum = tonumber(redis.call('HGET', numKey, numField))

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

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

    local incrNum = v['incrNum']

    --- 判断虚拟库存
    if (incrNum > 0) then
        if (nowVirtualNum + incrNum) > nowQtyNum then
            result['error'] = 1
            result['errorMsg'] = numName .. '数量不足'
            break ;
        end
    end
end

local currIndex2 = 1
local stock = {}
if (result['error'] == 0) then
    --数量都够，可以操作
    for k, v in pairs(material) do
        local numVirtualKey = v['numVirtualKey']
        local numField = v['numField']
        local incrNum = v['incrNum']

        ---local storeKey = v['storeKey']
        ---local storeField = v['storeField']

        stock[currIndex2] = {}

        stock[currIndex2]['idCompany'] = v['idCompany'];
        stock[currIndex2]['idToCompany'] = v['idToCompany'];
        stock[currIndex2]['idMaterial'] = v['idMaterial'];
        stock[currIndex2]['materialFlag'] = v['materialFlag'];
        stock[currIndex2]['qtyPrecise'] = v['qtyPrecise'];
        stock[currIndex2]['idStockRoom'] = v['idStockRoom'];

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

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

        stock[currIndex2]['beforeStock'] = afterNum - incrNum
        stock[currIndex2]['afterStock'] = afterNum
        stock[currIndex2]['stockNum'] = incrNum
        stock[currIndex2]['companyInsert'] = -1

        ---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['stockList'] = stock

local re = cjson.encode(result);

return re
