SystemCrontabServices.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. /**
  19. * 定时任务类型
  20. * @var string[]
  21. */
  22. private $markList = [
  23. 'order_cancel' => '未支付自动取消订单',
  24. 'pink_expiration' => '拼团到期订单处理',
  25. 'agent_unbind' => '到期自动解绑上级',
  26. 'live_product_status' => '自动更新直播商品状态',
  27. 'live_room_status' => '自动更新直播间状态',
  28. 'take_delivery' => '订单自动收货',
  29. 'advance_off' => '预售商品到期自动下架',
  30. 'product_replay' => '订单商品自动好评',
  31. 'clear_poster' => '清除昨日海报',
  32. ];
  33. public function __construct(SystemCrontabDao $dao)
  34. {
  35. $this->dao = $dao;
  36. }
  37. /**
  38. * 定时任务列表
  39. * @param array $where
  40. * @return array
  41. * @throws \think\db\exception\DataNotFoundException
  42. * @throws \think\db\exception\DbException
  43. * @throws \think\db\exception\ModelNotFoundException
  44. */
  45. public function getTimerList(array $where = [])
  46. {
  47. [$page, $limit] = $this->getPageValue();
  48. $list = $this->dao->selectList($where, '*', $page, $limit, 'id desc');
  49. foreach ($list as &$item) {
  50. $item['next_execution_time'] = date('Y-m-d H:i:s', $item['next_execution_time']);
  51. $item['last_execution_time'] = $item['last_execution_time'] != 0 ? date('Y-m-d H:i:s', $item['last_execution_time']) : '暂未执行';
  52. }
  53. $count = $this->dao->count($where);
  54. return compact('list', 'count');
  55. }
  56. /**
  57. * 定时任务详情
  58. * @param $id
  59. * @return array
  60. * @throws \think\db\exception\DataNotFoundException
  61. * @throws \think\db\exception\DbException
  62. * @throws \think\db\exception\ModelNotFoundException
  63. */
  64. public function getTimerInfo($id)
  65. {
  66. $info = $this->dao->get($id);
  67. if (!$info) throw new AdminException(100026);
  68. return $info->toArray();
  69. }
  70. /**
  71. * 定时任务类型
  72. * @return string[]
  73. */
  74. public function getMarkList(): array
  75. {
  76. return $this->markList;
  77. }
  78. /**
  79. * 保存定时任务
  80. * @param array $data
  81. * @return bool
  82. */
  83. public function saveTimer(array $data = [])
  84. {
  85. if (!$data['id'] && $this->dao->getCount(['mark' => $data['mark'], 'is_del' => 0])) {
  86. throw new AdminException('该定时任务已存在,请勿重复添加');
  87. }
  88. $data['name'] = $this->markList[$data['mark']];
  89. $data['add_time'] = time();
  90. if (!$data['id']) {
  91. unset($data['id']);
  92. $res = $this->dao->save($data);
  93. } else {
  94. $res = $this->dao->update(['id' => $data['id']], $data);
  95. }
  96. if (!$res) throw new AdminException(100006);
  97. return true;
  98. }
  99. /**
  100. * 删除定时任务
  101. * @param $id
  102. * @return bool
  103. */
  104. public function delTimer($id)
  105. {
  106. $res = $this->dao->update(['id' => $id], ['is_del' => 1]);
  107. if (!$res) throw new AdminException(100008);
  108. return true;
  109. }
  110. /**
  111. * 设置定时任务状态
  112. * @param $id
  113. * @param $is_open
  114. * @return bool
  115. */
  116. public function setTimerStatus($id, $is_open)
  117. {
  118. $res = $this->dao->update(['id' => $id], ['is_open' => $is_open]);
  119. if (!$res) throw new AdminException(100014);
  120. return true;
  121. }
  122. /**
  123. * 计算定时任务下次执行时间
  124. * @param $data
  125. * @param int $time
  126. * @return false|float|int|mixed
  127. */
  128. public function getTimerCycleTime($data, $time = 0)
  129. {
  130. if (!$time) $time = time();
  131. switch ($data['type']) {
  132. case 1: // 每隔几秒
  133. $cycle_time = $time + $data['second'];
  134. break;
  135. case 2: // 每隔几分
  136. $cycle_time = $time + ($data['minute'] * 60);
  137. break;
  138. case 3: // 每隔几时
  139. $cycle_time = $time + ($data['hour'] * 3600) + ($data['minute'] * 60);
  140. break;
  141. case 4: // 每隔几日
  142. $cycle_time = $time + ($data['day'] * 86400) + ($data['hour'] * 3600) + ($data['minute'] * 60);
  143. break;
  144. case 5: // 每日几时几分几秒
  145. $cycle_time = strtotime(date('Y-m-d ' . $data['hour'] . ':' . $data['minute'] . ':' . $data['second'], time()));
  146. if ($time >= $cycle_time) {
  147. $cycle_time = $cycle_time + 86400;
  148. }
  149. break;
  150. case 6: // 每周周几几时几分几秒
  151. $todayStart = strtotime(date('Y-m-d 00:00:00', time()));
  152. $w = date("w");
  153. if ($w > $data['week']) {
  154. $cycle_time = $todayStart + ((7 - $w + $data['week']) * 86400) + ($data['hour'] * 3600) + ($data['minute'] * 60) + $data['second'];
  155. } else if ($w == $data['week']) {
  156. $cycle_time = $todayStart + ($data['hour'] * 3600) + ($data['minute'] * 60) + $data['second'];
  157. if ($time >= $cycle_time) {
  158. $cycle_time = $cycle_time + (7 * 86400);
  159. }
  160. } else {
  161. $cycle_time = $todayStart + (($data['week'] - $w) * 86400) + ($data['hour'] * 3600) + ($data['minute'] * 60) + $data['second'];
  162. }
  163. break;
  164. case 7: // 每月几日几时几分几秒
  165. $d = date("d");
  166. $firstDate = date('Y-m-01', time());
  167. $maxDay = date('d', strtotime("$firstDate + 1 month -1 day"));
  168. $todayStart = strtotime(date('Y-m-d 00:00:00', time()));
  169. if ($d > $data['day']) {
  170. $cycle_time = $todayStart + (($maxDay - $d + $data['day']) * 86400) + ($data['hour'] * 3600) + ($data['minute'] * 60) + $data['second'];
  171. } elseif ($d == $data['day']) {
  172. $cycle_time = $todayStart + ($data['hour'] * 3600) + ($data['minute'] * 60) + $data['second'];
  173. if ($time >= $cycle_time) {
  174. $cycle_time = $cycle_time + (($maxDay - $d + $data['day']) * 86400) + ($data['hour'] * 3600) + ($data['minute'] * 60) + $data['second'];
  175. }
  176. } else {
  177. $cycle_time = $todayStart + (($data['day'] - $d) * 86400) + ($data['hour'] * 3600) + ($data['minute'] * 60) + $data['second'];
  178. }
  179. break;
  180. default:
  181. $cycle_time = 0;
  182. break;
  183. }
  184. return $cycle_time;
  185. }
  186. /**
  187. * 执行任务
  188. * @throws \think\db\exception\DataNotFoundException
  189. * @throws \think\db\exception\DbException
  190. * @throws \think\db\exception\ModelNotFoundException
  191. * @author 吴汐
  192. * @email 442384644@qq.com
  193. * @date 2023/02/17
  194. */
  195. public function crontabRun()
  196. {
  197. $crontabRunServices = app()->make(CrontabRunServices::class);
  198. $time = time();
  199. file_put_contents(root_path() . 'runtime/.timer', $time); //检测定时任务是否正常
  200. $list = $this->dao->selectList(['is_open' => 1, 'is_del' => 0])->toArray();
  201. foreach ($list as $item) {
  202. if ($item['next_execution_time'] < $time) {
  203. //转化小驼峰方法名
  204. $functionName = Str::camel($item['mark']);
  205. //执行定时任务
  206. $crontabRunServices->$functionName();
  207. //写入本次执行时间和下次执行时间
  208. $this->dao->update(['mark' => $item['mark']], ['last_execution_time' => $time, 'next_execution_time' => $this->getTimerCycleTime($item)]);
  209. }
  210. }
  211. }
  212. }