SystemTimerServices.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace app\services\system\timer;
  3. use app\dao\system\timer\SystemTimerDao;
  4. use app\services\BaseServices;
  5. use crmeb\exceptions\AdminException;
  6. class SystemTimerServices extends BaseServices
  7. {
  8. /**
  9. * 定时任务类型
  10. * @var string[]
  11. */
  12. private $markList = [
  13. 'order_cancel' => '未支付自动取消订单',
  14. 'pink_expiration' => '拼团到期订单处理',
  15. 'agent_unbind' => '到期自动解绑上级',
  16. 'live_product_status' => '自动更新直播商品状态',
  17. 'live_room_status' => '自动更新直播间状态',
  18. 'take_delivery' => '订单自动收货',
  19. 'advance_off' => '预售商品到期自动下架',
  20. 'product_replay' => '订单商品自动好评',
  21. 'clear_poster' => '清除昨日海报',
  22. ];
  23. public function __construct(SystemTimerDao $dao)
  24. {
  25. $this->dao = $dao;
  26. }
  27. /**
  28. * 定时任务列表
  29. * @param array $where
  30. * @return array
  31. * @throws \think\db\exception\DataNotFoundException
  32. * @throws \think\db\exception\DbException
  33. * @throws \think\db\exception\ModelNotFoundException
  34. */
  35. public function getTimerList(array $where = [])
  36. {
  37. [$page, $limit] = $this->getPageValue();
  38. $list = $this->dao->selectList($where, '*', $page, $limit, 'id desc');
  39. foreach ($list as &$item) {
  40. $item['next_execution_time'] = date('Y-m-d H:i:s', $item['next_execution_time']);
  41. $item['last_execution_time'] = $item['last_execution_time'] != 0 ? date('Y-m-d H:i:s', $item['last_execution_time']) : '暂未执行';
  42. }
  43. $count = $this->dao->count($where);
  44. return compact('list', 'count');
  45. }
  46. /**
  47. * 定时任务详情
  48. * @param $id
  49. * @return array
  50. * @throws \think\db\exception\DataNotFoundException
  51. * @throws \think\db\exception\DbException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. */
  54. public function getTimerInfo($id)
  55. {
  56. $info = $this->dao->get($id);
  57. if (!$info) throw new AdminException(100026);
  58. return $info->toArray();
  59. }
  60. /**
  61. * 定时任务类型
  62. * @return string[]
  63. */
  64. public function getMarkList(): array
  65. {
  66. return $this->markList;
  67. }
  68. /**
  69. * 保存定时任务
  70. * @param array $data
  71. * @return bool
  72. */
  73. public function saveTimer(array $data = [])
  74. {
  75. if (!$data['id'] && $this->dao->getCount(['mark' => $data['mark'], 'is_del' => 0])) {
  76. throw new AdminException('该定时任务已存在,请勿重复添加');
  77. }
  78. $data['name'] = $this->markList[$data['mark']];
  79. $data['add_time'] = time();
  80. if (!$data['id']) {
  81. unset($data['id']);
  82. $res = $this->dao->save($data);
  83. } else {
  84. $res = $this->dao->update(['id' => $data['id']], $data);
  85. }
  86. if (!$res) throw new AdminException(100006);
  87. return true;
  88. }
  89. /**
  90. * 删除定时任务
  91. * @param $id
  92. * @return bool
  93. */
  94. public function delTimer($id)
  95. {
  96. $res = $this->dao->update(['id' => $id], ['is_del' => 1]);
  97. if (!$res) throw new AdminException(100008);
  98. return true;
  99. }
  100. /**
  101. * 设置定时任务状态
  102. * @param $id
  103. * @param $is_open
  104. * @return bool
  105. */
  106. public function setTimerStatus($id, $is_open)
  107. {
  108. $res = $this->dao->update(['id' => $id], ['is_open' => $is_open]);
  109. if (!$res) throw new AdminException(100014);
  110. return true;
  111. }
  112. /**
  113. * 计算定时任务下次执行时间(弃用)
  114. * @param $data
  115. * @param int $time
  116. * @return false|float|int|mixed
  117. */
  118. public function getTimerCycleTime($data, $time = 0)
  119. {
  120. if (!$time) $time = time();
  121. switch ($data['type']) {
  122. case 1: // 每隔几秒
  123. $cycle_time = $time + $data['second'];
  124. break;
  125. case 2: // 每隔几分
  126. $cycle_time = $time + ($data['minute'] * 60);
  127. break;
  128. case 3: // 每隔几时
  129. $cycle_time = $time + ($data['hour'] * 3600);
  130. break;
  131. case 4: // 每隔几日
  132. $cycle_time = $time + ($data['day'] * 86400);
  133. break;
  134. case 5: // 每日几时几分几秒
  135. $cycle_time = strtotime(date('Y-m-d ' . $data['hour'] . ':' . $data['minute'] . ':' . $data['second'], time()));
  136. if ($time >= $cycle_time) {
  137. $cycle_time = $cycle_time + 86400;
  138. }
  139. break;
  140. case 6: // 每周周几几时几分几秒
  141. $todayStart = strtotime(date('Y-m-d 00:00:00', time()));
  142. $w = date("w");
  143. if ($w > $data['week']) {
  144. $cycle_time = $todayStart + ((7 - $w + $data['week']) * 86400) + ($data['hour'] * 3600) + ($data['minute'] * 60) + $data['second'];
  145. } else if ($w == $data['week']) {
  146. $cycle_time = $todayStart + ($data['hour'] * 3600) + ($data['minute'] * 60) + $data['second'];
  147. if ($time >= $cycle_time) {
  148. $cycle_time = $cycle_time + (7 * 86400);
  149. }
  150. } else {
  151. $cycle_time = $todayStart + (($data['week'] - $w) * 86400) + ($data['hour'] * 3600) + ($data['minute'] * 60) + $data['second'];
  152. }
  153. break;
  154. case 7: // 每月几日几时几分几秒
  155. $d = date("d");
  156. $firstDate = date('Y-m-01', time());
  157. $maxDay = date('d', strtotime("$firstDate + 1 month -1 day"));
  158. $todayStart = strtotime(date('Y-m-d 00:00:00', time()));
  159. if ($d > $data['day']) {
  160. $cycle_time = $todayStart + (($maxDay - $d + $data['day']) * 86400) + ($data['hour'] * 3600) + ($data['minute'] * 60) + $data['second'];
  161. } elseif ($d == $data['day']) {
  162. $cycle_time = $todayStart + ($data['hour'] * 3600) + ($data['minute'] * 60) + $data['second'];
  163. if ($time >= $cycle_time) {
  164. $cycle_time = $cycle_time + (($maxDay - $d + $data['day']) * 86400) + ($data['hour'] * 3600) + ($data['minute'] * 60) + $data['second'];
  165. }
  166. } else {
  167. $cycle_time = $todayStart + (($data['day'] - $d) * 86400) + ($data['hour'] * 3600) + ($data['minute'] * 60) + $data['second'];
  168. }
  169. break;
  170. default:
  171. $cycle_time = 0;
  172. break;
  173. }
  174. return $cycle_time;
  175. }
  176. }