SystemCrontabServices.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace app\services\system\crontab;
  3. use app\dao\system\crontab\SystemCrontabDao;
  4. use app\services\activity\combination\StorePinkServices;
  5. use app\services\activity\live\LiveGoodsServices;
  6. use app\services\activity\live\LiveRoomServices;
  7. use app\services\agent\AgentManageServices;
  8. use app\services\BaseServices;
  9. use app\services\order\StoreOrderServices;
  10. use app\services\order\StoreOrderTakeServices;
  11. use app\services\product\product\StoreProductServices;
  12. use app\services\system\attachment\SystemAttachmentServices;
  13. use crmeb\exceptions\AdminException;
  14. use think\facade\Log;
  15. use think\helper\Str;
  16. class SystemCrontabServices extends BaseServices
  17. {
  18. public function __construct(SystemCrontabDao $dao)
  19. {
  20. $this->dao = $dao;
  21. }
  22. /**
  23. * 定时任务列表
  24. * @param array $where
  25. * @return array
  26. * @throws \think\db\exception\DataNotFoundException
  27. * @throws \think\db\exception\DbException
  28. * @throws \think\db\exception\ModelNotFoundException
  29. */
  30. public function getTimerList(array $where = [])
  31. {
  32. [$page, $limit] = $this->getPageValue();
  33. $list = $this->dao->selectList($where, '*', $page, $limit, 'id desc');
  34. foreach ($list as &$item) {
  35. $item['next_execution_time'] = date('Y-m-d H:i:s', $item['next_execution_time']);
  36. $item['last_execution_time'] = $item['last_execution_time'] != 0 ? date('Y-m-d H:i:s', $item['last_execution_time']) : '暂未执行';
  37. }
  38. $count = $this->dao->count($where);
  39. return compact('list', 'count');
  40. }
  41. /**
  42. * 定时任务详情
  43. * @param $id
  44. * @return array
  45. * @throws \think\db\exception\DataNotFoundException
  46. * @throws \think\db\exception\DbException
  47. * @throws \think\db\exception\ModelNotFoundException
  48. */
  49. public function getTimerInfo($id)
  50. {
  51. $info = $this->dao->get($id);
  52. if (!$info) throw new AdminException(100026);
  53. return $info->toArray();
  54. }
  55. /**
  56. * 定时任务类型
  57. * @return string[]
  58. */
  59. public function getMarkList(): array
  60. {
  61. return app()->make(CrontabRunServices::class)->markList;
  62. }
  63. /**
  64. * 保存定时任务
  65. * @param array $data
  66. * @return bool
  67. */
  68. public function saveTimer(array $data = [])
  69. {
  70. if (!$data['id'] && $this->dao->getCount(['mark' => $data['mark'], 'is_del' => 0])) {
  71. throw new AdminException('该定时任务已存在,请勿重复添加');
  72. }
  73. $data['name'] = $this->getMarkList()[$data['mark']];
  74. $data['add_time'] = time();
  75. if (!$data['id']) {
  76. unset($data['id']);
  77. $res = $this->dao->save($data);
  78. } else {
  79. $res = $this->dao->update(['id' => $data['id']], $data);
  80. }
  81. if (!$res) throw new AdminException(100006);
  82. return true;
  83. }
  84. /**
  85. * 删除定时任务
  86. * @param $id
  87. * @return bool
  88. */
  89. public function delTimer($id)
  90. {
  91. $res = $this->dao->update(['id' => $id], ['is_del' => 1]);
  92. if (!$res) throw new AdminException(100008);
  93. return true;
  94. }
  95. /**
  96. * 设置定时任务状态
  97. * @param $id
  98. * @param $is_open
  99. * @return bool
  100. */
  101. public function setTimerStatus($id, $is_open)
  102. {
  103. $res = $this->dao->update(['id' => $id], ['is_open' => $is_open]);
  104. if (!$res) throw new AdminException(100014);
  105. return true;
  106. }
  107. /**
  108. * 计算定时任务下次执行时间
  109. * @param $data
  110. * @param int $time
  111. * @return false|float|int|mixed
  112. */
  113. public function getTimerCycleTime($data, $time = 0)
  114. {
  115. if (!$time) $time = time();
  116. switch ($data['type']) {
  117. case 1: // 每隔几秒
  118. $cycle_time = $time + $data['second'];
  119. break;
  120. case 2: // 每隔几分
  121. $cycle_time = $time + ($data['minute'] * 60);
  122. break;
  123. case 3: // 每隔几时
  124. $cycle_time = $time + ($data['hour'] * 3600) + ($data['minute'] * 60);
  125. break;
  126. case 4: // 每隔几日
  127. $cycle_time = $time + ($data['day'] * 86400) + ($data['hour'] * 3600) + ($data['minute'] * 60);
  128. break;
  129. case 5: // 每日几时几分几秒
  130. $cycle_time = strtotime(date('Y-m-d ' . $data['hour'] . ':' . $data['minute'] . ':' . $data['second'], time()));
  131. if ($time >= $cycle_time) {
  132. $cycle_time = $cycle_time + 86400;
  133. }
  134. break;
  135. case 6: // 每周周几几时几分几秒
  136. $todayStart = strtotime(date('Y-m-d 00:00:00', time()));
  137. $w = date("w");
  138. if ($w > $data['week']) {
  139. $cycle_time = $todayStart + ((7 - $w + $data['week']) * 86400) + ($data['hour'] * 3600) + ($data['minute'] * 60) + $data['second'];
  140. } else if ($w == $data['week']) {
  141. $cycle_time = $todayStart + ($data['hour'] * 3600) + ($data['minute'] * 60) + $data['second'];
  142. if ($time >= $cycle_time) {
  143. $cycle_time = $cycle_time + (7 * 86400);
  144. }
  145. } else {
  146. $cycle_time = $todayStart + (($data['week'] - $w) * 86400) + ($data['hour'] * 3600) + ($data['minute'] * 60) + $data['second'];
  147. }
  148. break;
  149. case 7: // 每月几日几时几分几秒
  150. $d = date("d");
  151. $firstDate = date('Y-m-01', time());
  152. $maxDay = date('d', strtotime("$firstDate + 1 month -1 day"));
  153. $todayStart = strtotime(date('Y-m-d 00:00:00', time()));
  154. if ($d > $data['day']) {
  155. $cycle_time = $todayStart + (($maxDay - $d + $data['day']) * 86400) + ($data['hour'] * 3600) + ($data['minute'] * 60) + $data['second'];
  156. } elseif ($d == $data['day']) {
  157. $cycle_time = $todayStart + ($data['hour'] * 3600) + ($data['minute'] * 60) + $data['second'];
  158. if ($time >= $cycle_time) {
  159. $cycle_time = $cycle_time + (($maxDay - $d + $data['day']) * 86400) + ($data['hour'] * 3600) + ($data['minute'] * 60) + $data['second'];
  160. }
  161. } else {
  162. $cycle_time = $todayStart + (($data['day'] - $d) * 86400) + ($data['hour'] * 3600) + ($data['minute'] * 60) + $data['second'];
  163. }
  164. break;
  165. default:
  166. $cycle_time = 0;
  167. break;
  168. }
  169. return $cycle_time;
  170. }
  171. /**
  172. * 执行任务
  173. * @throws \think\db\exception\DataNotFoundException
  174. * @throws \think\db\exception\DbException
  175. * @throws \think\db\exception\ModelNotFoundException
  176. * @author 吴汐
  177. * @email 442384644@qq.com
  178. * @date 2023/02/17
  179. */
  180. public function crontabRun()
  181. {
  182. $crontabRunServices = app()->make(CrontabRunServices::class);
  183. $time = time();
  184. file_put_contents(root_path() . 'runtime/.timer', $time); //检测定时任务是否正常
  185. $list = $this->dao->selectList(['is_open' => 1, 'is_del' => 0])->toArray();
  186. foreach ($list as $item) {
  187. if ($item['next_execution_time'] < $time) {
  188. //转化小驼峰方法名
  189. $functionName = Str::camel($item['mark']);
  190. //执行定时任务
  191. $crontabRunServices->$functionName();
  192. //写入本次执行时间和下次执行时间
  193. $this->dao->update(['mark' => $item['mark']], ['last_execution_time' => $time, 'next_execution_time' => $this->getTimerCycleTime($item)]);
  194. }
  195. }
  196. }
  197. }