balance_client_moneys.lua 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. ---
  2. --- Generated by EmmyLua(https://github.com/EmmyLua)
  3. --- Created by jlutt.
  4. --- DateTime: 2020-09-07 16:26
  5. --- 客户余额操作
  6. --- 客户余额的操作基本为单个客户的操作,客户存款,预存款冲抵
  7. ---
  8. local key = KEYS[1]
  9. local jsonData = ARGV[2]
  10. local jsonCheckData = ARGV[1]
  11. local material = cjson.decode(jsonData)
  12. local checkMaterial = cjson.decode(jsonCheckData)
  13. local result = {}
  14. result['error'] = 0
  15. result['errorMsg'] = ''
  16. --- 检查能否减少数量
  17. for k, v in pairs(checkMaterial) do
  18. local numName = v['numName']
  19. local numKey = v['numKey']
  20. local numField = v['numField']
  21. local nowNum = tonumber(redis.call('HGET', numKey, numField))
  22. if (nowNum == nil) then
  23. nowNum = 0;
  24. end
  25. local incrNum = v['incrNum']
  26. if ((nowNum == nil or nowNum <= 0) and (incrNum < 0)) then
  27. result['error'] = 1
  28. result['errorMsg'] = numName .. '余额不足'
  29. break ;
  30. end
  31. if (nowNum + incrNum) < 0 then
  32. result['error'] = 1
  33. result['errorMsg'] = numName .. '余额不足'
  34. break ;
  35. end
  36. end
  37. local currIndex2 = 1
  38. local stock = {}
  39. if (result['error'] == 0) then
  40. --数量都够,可以操作
  41. for k, v in pairs(material) do
  42. local numKey = v['numKey']
  43. local numField = v['numField']
  44. local incrNum = v['incrNum']
  45. stock[currIndex2] = {}
  46. stock[currIndex2]['idClient'] = v['idClient'];
  47. local iExists = redis.call("HEXISTS", numKey, numField);
  48. if (iExists == 0) then
  49. stock[currIndex2]['insert'] = 1
  50. else
  51. stock[currIndex2]['insert'] = 0
  52. end
  53. local afterNum = redis.call("HINCRBY", numKey, numField, incrNum)
  54. stock[currIndex2]['beforeNum'] = afterNum - incrNum
  55. stock[currIndex2]['afterNum'] = afterNum
  56. stock[currIndex2]['calcNum'] = incrNum
  57. currIndex2 = currIndex2 + 1
  58. end
  59. end
  60. local time = redis.call('TIME')
  61. result['time'] = time[1]
  62. result['microseconds'] = time[2]
  63. result['clientList'] = stock
  64. local re = cjson.encode(result);
  65. return re