qty_stock_precise.lua 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. ---
  2. --- Generated by EmmyLua(https://github.com/EmmyLua)
  3. --- Created by jlutt.
  4. --- DateTime: 2020-09-03 9:38
  5. --- 库存调整精度
  6. ---
  7. local key = KEYS[1]
  8. local virualKey = KEYS[2]
  9. local field = ARGV[1]..'-*'
  10. local oldPrecise = tonumber(ARGV[2])
  11. local newPrecise = tonumber(ARGV[3])
  12. local scanTable = {}
  13. local scanVirsualTable = {}
  14. local cursor = 0
  15. repeat
  16. local rep = redis.call('HSCAN', key, cursor, 'MATCH', field)
  17. cursor = rep[1]
  18. for k, v in pairs(rep[2]) do
  19. --- lua中k为序号,从1开始
  20. if k % 2 == 0 then
  21. local numField = rep[2][k -1]
  22. local numValue = v
  23. ---hscan的缺点:同一个元素可能会被返回多次
  24. if (scanTable[numField] == nil) then
  25. ---不能在hscan中直接调用hset命令,报错replicate_commands
  26. ---local newValue = v * (10 ^ (newPrecise - oldPrecise))
  27. ---redis.call("HSET", key, numField, newValue)
  28. ---redis.log(redis.LOG_WARNING, 'c----' ..numField..'='.. numValue..'=='..newValue);
  29. scanTable[numField] = numValue
  30. end
  31. end
  32. end
  33. until cursor == '0'
  34. cursor = 0
  35. repeat
  36. local rep = redis.call('HSCAN', virualKey, cursor, 'MATCH', field)
  37. cursor = rep[1]
  38. for k, v in pairs(rep[2]) do
  39. --- lua中k为序号,从1开始
  40. if k % 2 == 0 then
  41. local numField = rep[2][k -1]
  42. local numValue = v
  43. ---hscan的缺点:同一个元素可能会被返回多次
  44. if (scanVirsualTable[numField] == nil) then
  45. ---不能在hscan中直接调用hset命令,报错replicate_commands
  46. ---local newValue = v * (10 ^ (newPrecise - oldPrecise))
  47. ---redis.call("HSET", key, numField, newValue)
  48. ---redis.log(redis.LOG_WARNING, 'c----' ..numField..'='.. numValue..'=='..newValue);
  49. scanVirsualTable[numField] = numValue
  50. end
  51. end
  52. end
  53. until cursor == '0'
  54. ---https://redis.io/commands/EVAL#replicating-commands-instead-of-scripts
  55. redis.replicate_commands()
  56. local stock = {}
  57. local currIndex2 = 1
  58. local x = {}
  59. for k, v in pairs(scanTable) do
  60. local newValue = math.floor(v * (10 ^ (newPrecise - oldPrecise)))
  61. redis.call("HSET", key, k, newValue)
  62. stock[currIndex2] = {}
  63. stock[currIndex2]['stockKey'] = k
  64. stock[currIndex2]['stockNum'] = newValue
  65. ---redis.log(redis.LOG_WARNING, 'b----' ..k..'='.. v .. '==='..newValue)
  66. currIndex2 = currIndex2 + 1
  67. end
  68. local currIndex3 = 1
  69. for k, v in pairs(scanVirsualTable) do
  70. local newValue = math.floor(v * (10 ^ (newPrecise - oldPrecise)))
  71. redis.call("HSET", virualKey, k, newValue)
  72. ---redis.log(redis.LOG_WARNING, 'b----' ..k..'='.. v .. '==='..newValue)
  73. currIndex3 = currIndex3 + 1
  74. end
  75. ---释放变量
  76. scanTable = nil
  77. scanVirsualTable = nil
  78. ---返回值
  79. local time = redis.call('TIME')
  80. x['time'] = time[1]
  81. x['microseconds'] = time[2]
  82. x['stockList'] = stock
  83. local re = cjson.encode(x);
  84. return re