QueueDao.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\dao\other\queue;
  12. use app\dao\BaseDao;
  13. use app\model\other\queue\Queue;
  14. /**
  15. * 队列
  16. * Class QueueDao
  17. * @package app\dao\other
  18. */
  19. class QueueDao extends BaseDao
  20. {
  21. /**
  22. * @return string
  23. */
  24. public function setModel(): string
  25. {
  26. return Queue::class;
  27. }
  28. /**
  29. * 队列任务列表
  30. * @param array $where
  31. * @param int $page
  32. * @param int $limit
  33. * @param string $order
  34. * @return array
  35. * @throws \think\db\exception\DataNotFoundException
  36. * @throws \think\db\exception\DbException
  37. * @throws \think\db\exception\ModelNotFoundException
  38. */
  39. public function getList(array $where, int $page = 0, int $limit = 0, string $order = '')
  40. {
  41. foreach ($where as $k => $v) {
  42. if ($v == "") unset($where[$k]);
  43. }
  44. return $this->search($where)
  45. ->order(($order ? $order . ' ,' : '') . 'add_time desc')
  46. ->page($page, $limit)->select()->toArray();
  47. }
  48. /**
  49. * 获取单个队列详情
  50. * @param array $where
  51. * @return array|bool|\think\Model|null
  52. * @throws \think\db\exception\DataNotFoundException
  53. * @throws \think\db\exception\DbException
  54. * @throws \think\db\exception\ModelNotFoundException
  55. */
  56. public function getQueueOne(array $where)
  57. {
  58. if (!$where) return false;
  59. return $this->search($where)->order('id desc')->find();
  60. }
  61. /**
  62. * 加入队列数据表
  63. * @param string $queueName
  64. * @param int $queueDataNum
  65. * @param int $type
  66. * @param string $redisKey
  67. * @param string $source
  68. * @return mixed
  69. */
  70. public function addQueueList(string $queueName, int $queueDataNum, int $type, string $redisKey, string $source = "admin")
  71. {
  72. $data = [
  73. 'type' => $type,
  74. 'source' => $source,
  75. 'execute_key' => $redisKey ? $redisKey : '',
  76. 'title' => $queueName,
  77. 'status' => 0,
  78. 'surplus_num' => $queueDataNum,
  79. 'total_num' => $queueDataNum,
  80. 'add_time' => time(),
  81. ];
  82. return $this->getModel()->insertGetId($data);
  83. }
  84. /**
  85. * 将队列置为正在执行状态
  86. * @param $queueInValue
  87. * @param $queueId
  88. * @return bool|mixed
  89. */
  90. public function setQueueDoing($queueInValue, $queueId, bool $is_again = false)
  91. {
  92. $saveData['queue_in_value'] = is_array($queueInValue) ? json_encode($queueInValue) : $queueInValue;
  93. $saveData['status'] = 1;
  94. if ($is_again) {
  95. $saveData['again_time'] = time();
  96. } else {
  97. $saveData['first_time'] = time();
  98. }
  99. return $this->getModel()->update($saveData, ['id' => $queueId]);
  100. }
  101. /**
  102. * 停止队列
  103. * @param $queueId
  104. * @return \crmeb\basic\BaseModel
  105. * @throws \think\db\exception\DataNotFoundException
  106. * @throws \think\db\exception\DbException
  107. * @throws \think\db\exception\ModelNotFoundException
  108. */
  109. public function stopWrongQueue($queueId)
  110. {
  111. $queueInfo = $this->getModel()->where(['id' => $queueId])->find();
  112. if (!$queueInfo) return false;
  113. return $this->getModel()->update(['id' => $queueId], ['status' => 3]);
  114. }
  115. }