tablecode.lua 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. if redis.call('exists', KEYS[1]) == 0 then
  2. ---redis.log(redis.LOG_WARNING, 'kkk' .. KEYS[1]);
  3. return nil
  4. else
  5. local tableCode = redis.call('get', KEYS[1]);
  6. if tableCode then
  7. redis.log(redis.LOG_WARNING, tableCode);
  8. local x = cjson.decode(tableCode);
  9. local yearlength = tonumber(x['yearLength']);
  10. local ntime = ARGV[1];
  11. if (yearlength == 2) then
  12. ntime = ARGV[2];
  13. end
  14. ---redis.log(redis.LOG_WARNING, 'a' .. ntime);
  15. local prefixlength = tonumber(x['prefixLength']);
  16. ---redis.log(redis.LOG_WARNING, 'b' .. prefixlength);
  17. local datelength = tonumber(x['dateLength']);
  18. ---redis.log(redis.LOG_WARNING, 'b' .. datelength);
  19. local currcode = x['currcode'];
  20. if currcode ~= nil then
  21. redis.log(redis.LOG_WARNING, 'c' .. currcode);
  22. end
  23. local codeprefix = '';
  24. if (prefixlength > 0) then
  25. codeprefix = x['codePrefix'];
  26. end
  27. ---redis.log(redis.LOG_WARNING, 'd' .. codeprefix);
  28. local codeFormat = x['codeFormat'];
  29. local acttime = string.sub(ntime, 1, datelength);
  30. ---redis.log(redis.LOG_WARNING, 'e' .. codeFormat);
  31. if (currcode and currcode ~= '') then
  32. ---redis.log(redis.LOG_WARNING, 'h' .. acttime);
  33. local codetime = string.sub(currcode, prefixlength + 1, datelength + prefixlength);
  34. ---redis.log(redis.LOG_WARNING, 'i' .. codetime);
  35. local codeindex = tonumber(string.sub(currcode, prefixlength + datelength + 1));
  36. ---redis.log(redis.LOG_WARNING, 'j' .. codeindex);
  37. if acttime == codetime then
  38. codeindex = codeindex + 1;
  39. x['currcode'] = codeprefix .. acttime .. string.format(codeFormat, codeindex);
  40. else
  41. codeindex = 1;
  42. x['currcode'] = codeprefix .. acttime .. string.format(codeFormat, codeindex);
  43. end
  44. else
  45. x['currcode'] = codeprefix .. acttime .. string.format(codeFormat, 1);
  46. end
  47. local re = cjson.encode(x);
  48. redis.call('set', KEYS[1], re);
  49. return re;
  50. end
  51. end
  52. return nil