SystemTimerServices.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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\services\system\timer;
  12. use app\services\BaseServices;
  13. use crmeb\exceptions\AdminException;
  14. use app\dao\system\timer\SystemTimerDao;
  15. /**
  16. * 定时器service
  17. * Class SystemTimerServices
  18. * @package app\services\system\timer
  19. * @mixin SystemTimerDao
  20. */
  21. class SystemTimerServices extends BaseServices
  22. {
  23. /**
  24. * SystemTimerServices constructor.
  25. * @param SystemTimerDao $dao
  26. */
  27. public function __construct(SystemTimerDao $dao)
  28. {
  29. $this->dao = $dao;
  30. }
  31. /**
  32. * 获取定时器列表
  33. * @param array $where
  34. * @return array
  35. */
  36. public function getTimerList(array $where)
  37. {
  38. [$page, $limit] = $this->getPageValue();
  39. $list = $this->dao->getList($where, $page, $limit);
  40. foreach ($list as $key => &$item) {
  41. $last_execution_time = $this->dao->cacheHander()->get($item['mark']);
  42. $item['last_execution_time'] = $last_execution_time > 0 ? date('Y-m-d H:i:s', $last_execution_time) : '';
  43. switch ($item['type']) {
  44. case 1:
  45. $item['execution_cycle'] = '每隔' . $item['cycle'] . '分钟执行';
  46. break;
  47. case 2:
  48. $arr = explode('/', $item['cycle']);
  49. $item['execution_cycle'] = '每' . $arr[0] . '小时, 第' . $arr[1] . '分钟 执行';
  50. break;
  51. case 3:
  52. $item['execution_cycle'] = '每小时, 第' . $item['cycle'] . '分钟执行';
  53. break;
  54. case 4:
  55. $arr = explode('/', $item['cycle']);
  56. $item['execution_cycle'] = '每天, ' . $arr[0] . '点' . $arr[1] . '分 执行';
  57. break;
  58. case 5:
  59. $arr = explode('/', $item['cycle']);
  60. $item['execution_cycle'] = '每隔' . $arr[0] . '天, ' . $arr[1] . '点' . $arr[2] . '分 执行';
  61. break;
  62. case 6:
  63. $arr = explode('/', $item['cycle']);
  64. $item['execution_cycle'] = '每周' . $arr[0] . ', ' . $arr[1] . '点' . $arr[2] . '分执行';
  65. break;
  66. case 7:
  67. $arr = explode('/', $item['cycle']);
  68. $item['execution_cycle'] = '每月' . $arr[0] . '日 ' . $arr[1] . '点' . $arr[2] . '分执行';
  69. break;
  70. case 8:
  71. $arr = explode('/', $item['cycle']);
  72. $item['execution_cycle'] = '每年' . $arr[0] . '月' . $arr[1] . '日 ' . $arr[2] . '点' . $arr[3] . '分执行';
  73. break;
  74. default:
  75. $item['execution_cycle'] = '周期有误!';
  76. break;
  77. }
  78. }
  79. $count = $this->dao->count($where);
  80. return compact('list', 'count');
  81. }
  82. /**设置或更新定时任务缓存
  83. * @return void
  84. * @throws \think\db\exception\DataNotFoundException
  85. * @throws \think\db\exception\DbException
  86. * @throws \think\db\exception\ModelNotFoundException
  87. */
  88. public function setAllTimerCache()
  89. {
  90. $where['is_del'] = 0;
  91. $list = $this->dao->getList($where, 0, 0);
  92. foreach ($list as $key => &$value) {
  93. if (!$value['update_execution_time']) {
  94. $update_time = time();
  95. $value['update_execution_time'] = $update_time;
  96. $this->dao->update($value['id'], ['update_execution_time' => $update_time]);
  97. }
  98. }
  99. $this->dao->cacheCreate($list);
  100. }
  101. /**
  102. * 设置定时器状态
  103. * @param $id
  104. * @param $is_show
  105. */
  106. public function setShow(int $id, int $is_show)
  107. {
  108. $this->dao->update($id, ['is_open' => $is_show]);
  109. }
  110. /**
  111. * 删除定时器
  112. * @param string $id
  113. */
  114. public function del(int $id)
  115. {
  116. if (!$id) throw new AdminException('请选择要删除的定时任务');
  117. $this->dao->delete((int)$id);
  118. }
  119. /**
  120. * 新增数据
  121. * @param $data
  122. */
  123. public function createData($data)
  124. {
  125. if ($this->dao->be(['mark' => $data['mark'], 'name' => $data['name']])) {
  126. throw new AdminException('该定时任务已经存在');
  127. }
  128. $data['add_time'] = time();
  129. $data['update_execution_time'] = time();
  130. $res = $this->dao->save($data);
  131. if (!$res) throw new AdminException('添加失败');
  132. return $res;
  133. }
  134. /**
  135. * 保存修改数据
  136. * @param $id
  137. * @param $data
  138. */
  139. public function editData($id, $data)
  140. {
  141. if (!$this->dao->be(['id' => $id])) {
  142. throw new AdminException('该定时任务不存在');
  143. }
  144. $data['update_execution_time'] = time();
  145. $res = $this->dao->update($id, $data);
  146. if (!$res) throw new AdminException('修改失败');
  147. return $res;
  148. }
  149. /**更新缓存
  150. * @param $id
  151. * @return bool
  152. * @throws \think\db\exception\DataNotFoundException
  153. * @throws \think\db\exception\DbException
  154. * @throws \think\db\exception\ModelNotFoundException
  155. */
  156. public function updateOneTimerCache($id)
  157. {
  158. if (!$id) throw new AdminException('参数错误');
  159. $timer = $this->dao->get($id);
  160. $this->dao->cacheUpdate($timer->toArray());
  161. return true;
  162. }
  163. /**删除缓存
  164. * @param $id
  165. * @return bool
  166. * @throws \think\db\exception\DataNotFoundException
  167. * @throws \think\db\exception\DbException
  168. * @throws \think\db\exception\ModelNotFoundException
  169. */
  170. public function delOneTimerCache($id)
  171. {
  172. if (!$id) throw new AdminException('参数错误');
  173. $this->dao->cacheDelById($id);
  174. return true;
  175. }
  176. /**获取单条定时器数据
  177. * @param $id
  178. * @return array
  179. */
  180. public function getOneTimer($id)
  181. {
  182. if (!$id) throw new AdminException('请选择要删除的定时任务');
  183. $timer = $this->dao->get($id);
  184. if (!$timer) throw new AdminException('该定时任务不存在');
  185. return $timer->toArray();
  186. }
  187. /**获取下次执行时间
  188. * @param $type
  189. * @param $cycle
  190. * @param $time
  191. * @param $start_time
  192. * @return array
  193. */
  194. public function getTimerCycleTime($type, $cycle, $time, $start_time = 0)
  195. {
  196. if (!$time) $time = time();
  197. switch ($type) {
  198. case 1: // N分钟
  199. $cycle_time = 60 * $cycle;
  200. $i = $time - $start_time;
  201. $more = $i % $cycle_time;
  202. if ($more == 0) {
  203. $cycle_time = $time;
  204. } else {
  205. $cycle_time = $time - $more + $cycle_time;
  206. }
  207. break;
  208. case 2: //N小时
  209. $arr = explode('/', $cycle);
  210. $todaystart = strtotime(date('Y-m-d H' . ':00:00', $time));
  211. $start_time = strtotime(date('Y-m-d H' . ':00:00', $start_time));
  212. $h = ($todaystart - $start_time) / 3600;
  213. $h = floor($h);
  214. $more = $h % $arr[0];
  215. if ($more == 0) {
  216. $cycle_time = 60 * $arr[1];
  217. $cycle_time = $todaystart + $cycle_time;
  218. if ($cycle_time <= $time) {
  219. $cycle_time = 60 * 60 * $arr[0] + 60 * $arr[1];
  220. }
  221. } else {
  222. $sh = $arr[0] - $more;
  223. $cycle_time = 60 * 60 * $sh + 60 * $arr[1];
  224. }
  225. $cycle_time = $todaystart + $cycle_time;
  226. break;
  227. case 3: //每小时
  228. $cycle_time = strtotime(date('Y-m-d ' . 'H:' . $cycle . ':00', $time));
  229. break;
  230. case 4: //每天
  231. $arr = explode('/', $cycle);
  232. $cycle_time = strtotime(date('Y-m-d ' . $arr[0] . ':' . $arr[1] . ':00', $time));
  233. break;
  234. case 5: //N天
  235. $arr = explode('/', $cycle);
  236. $todaystart = strtotime(date('Y-m-d ' . '00:00:00', $time));
  237. $start_time = strtotime(date('Y-m-d ' . '00:00:00', $start_time));
  238. $d = ($todaystart - $start_time) / 86400;
  239. $d = floor($d);
  240. $more = $d % $arr[0];
  241. if ($more == 0) {
  242. $cycle_time = 60 * 60 * $arr[1] + 60 * $arr[2];
  243. $cycle_time = $todaystart + $cycle_time;
  244. if ($cycle_time < $time) {
  245. $cycle_time = 60 * 60 * 24 * $more + 60 * 60 * $arr[1] + 60 * $arr[2];
  246. }
  247. } else {
  248. $sd = $arr[0] - $more;
  249. $cycle_time = 60 * 60 * 24 * $sd + 60 * 60 * $arr[1] + 60 * $arr[2];
  250. }
  251. $cycle_time = $todaystart + $cycle_time;
  252. break;
  253. case 6: //每星期
  254. $arr = explode('/', $cycle);
  255. $todaystart = strtotime(date('Y-m-d ' . '00:00:00', $time));
  256. $w = date("w");
  257. if ($w > $arr[0]) {
  258. $d = 7 - $w + $arr[0];
  259. $cycle_time = $todaystart + 60 * 60 * 24 * $d + 60 * 60 * $arr[1] + 60 * $arr[2];
  260. } else if ($w == $arr[0]) {
  261. $to_time = 60 * 60 * $arr[1] + 60 * $arr[2];
  262. $to_time = $todaystart + $to_time;
  263. if ($time > $to_time) {
  264. $d = 7 - $w + $arr[0];
  265. $cycle_time = $todaystart + 60 * 60 * 24 * $d + 60 * 60 * $arr[1] + 60 * $arr[2];
  266. } else {
  267. $cycle_time = $to_time;
  268. }
  269. } else {
  270. $d = $arr[0] - $w;
  271. $cycle_time = $todaystart + 60 * 60 * 24 * $d + 60 * 60 * $arr[1] + 60 * $arr[2];
  272. }
  273. break;
  274. case 7: //每月
  275. $arr = explode('/', $cycle);
  276. $current_d = date("d");
  277. $firstDate = date('Y-m-01', $time);
  278. $max_d = date('d', strtotime("$firstDate + 1 month -1 day"));
  279. $todaystart = strtotime(date('Y-m-d ' . '00:00:00', $time));
  280. if ($current_d > $arr[0]) {
  281. $d = $max_d - $current_d + $arr[0];
  282. $cycle_time = $todaystart + 60 * 60 * 24 * $d + 60 * 60 * $arr[1] + 60 * $arr[2];
  283. } elseif ($current_d == $arr[0]) {
  284. $to_time = 60 * 60 * $arr[1] + 60 * $arr[2];
  285. $to_time = $todaystart + $to_time;
  286. if ($time > $to_time) {
  287. $d = $max_d - $current_d + $arr[0];
  288. $cycle_time = $todaystart + 60 * 60 * 24 * $d + 60 * 60 * $arr[1] + 60 * $arr[2];
  289. } else {
  290. $cycle_time = $to_time;
  291. }
  292. } else {
  293. $d = $arr[0] - $current_d;
  294. $cycle_time = $todaystart + 60 * 60 * 24 * $d + 60 * 60 * $arr[1] + 60 * $arr[2];
  295. }
  296. break;
  297. case 8: //每年
  298. $arr = explode('/', $cycle);
  299. $cycle_time = strtotime(date('Y-' . $arr[0] . '-' . $arr[1] . ' ' . $arr[2] . ':' . $arr[3] . ':00', $time));
  300. break;
  301. default:
  302. $cycle_time = 0;
  303. break;
  304. }
  305. return ['cycle_time' => $cycle_time];
  306. }
  307. }