IndentNumServer.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace App\Servers;
  3. use App\Events\NumEvent;
  4. use App\Jobs\IndentNumJob;
  5. use App\Models\IndentNum;
  6. use App\Models\Menu;
  7. use App\Models\MenuUrl;
  8. use App\Models\Permission;
  9. use App\Models\Role;
  10. use App\Models\RolePermission;
  11. use App\Models\User;
  12. use Illuminate\Support\Facades\DB;
  13. use Illuminate\Support\Facades\Redis;
  14. /**
  15. * Redis数据缓存类
  16. */
  17. class IndentNumServer
  18. {
  19. /**
  20. * 单列对象
  21. * @var
  22. */
  23. static $server;
  24. private $key_name = 'ac_num';
  25. private $max_num;
  26. private function __construct()
  27. {
  28. $this->max_num=env('CODE_NUM_LENGTH',100);
  29. }
  30. /**
  31. * 创建对象
  32. * @return IndentNumServer
  33. */
  34. static function creatServer()
  35. {
  36. if (empty(self::$server)) {
  37. self::$server = new IndentNumServer();
  38. }
  39. return self::$server;
  40. }
  41. /**
  42. * 获取最新编号
  43. * @param $type
  44. * @param int $length
  45. * @return string
  46. */
  47. function getIndentNum($type, $length = 0)
  48. {
  49. $redis_name = $this->key_name . '_' . $type;
  50. // event(new NumEvent($type));
  51. $row=RedisDataServer::creatServer()->getData($redis_name.'_data','json');
  52. if(empty($row)){
  53. $row = IndentNum::where('type', $type)->first()->toArray();
  54. }
  55. $redis_num=$row['number']+1;
  56. RedisDataServer::creatServer()->setData($redis_name.'_data',$row,'json',60);
  57. $row['digits'] = $length > 0 ? $length : $row['digits'];
  58. $date = date("Ymd");
  59. if (!$redis_num) {
  60. if ($row['is_date'] == 1) {
  61. $num = sprintf("%0{$row['digits']}s", $row['number'] + 1);
  62. $new_cod = $date . $num;
  63. } else {
  64. $new_cod = sprintf("%0{$row['digits']}s", $row['number'] + 1);
  65. }
  66. } else {
  67. if ($row['is_date'] == 1) {
  68. $num = sprintf("%0{$row['digits']}s", $redis_num);
  69. $new_cod = $date . $num;
  70. } else {
  71. $new_cod = sprintf("%0{$row['digits']}s", $redis_num);
  72. }
  73. }
  74. IndentNum::where('id',$row['id'])->increment('number');
  75. return $row['code_str'] . $new_cod;
  76. }
  77. /**
  78. * 验证队列的数据并自动生成
  79. * @param $type
  80. */
  81. function verifyNum($type)
  82. {
  83. $redis_name = $this->key_name . '_' . $type;
  84. $key_num = RedisDataServer::creatServer()->getQueueList($redis_name);
  85. if ($key_num <35) {
  86. //开启事务,并启动悲观锁
  87. DB::beginTransaction();
  88. $row = IndentNum::where('type', $type)->select(['id','number','max_num'])->lockForUpdate()->first();
  89. $max_num = $row->{'number'} > $row->{'max_num'} ? $row->{'number'} : $row->{'max_num'};
  90. $num_arr=range($max_num+1,$max_num+100);
  91. // $num_arr=array_reverse($num_arr);
  92. RedisDataServer::creatServer()->addQueue($redis_name,$num_arr);
  93. IndentNum::where('id',$row->{'id'})->update(['max_num'=>$max_num+100]);
  94. DB::commit();
  95. }
  96. }
  97. }