0
0

Timer.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\CleanTask;
  4. use app\common\model\Config;
  5. use app\common\model\Daily;
  6. use app\common\model\DailyTask;
  7. use app\common\model\DeviceTask;
  8. use app\common\model\PatrolTask;
  9. use app\common\model\QualityTask;
  10. use app\common\util\AppMsg;
  11. use think\Controller;
  12. use think\Db;
  13. /**
  14. * 定时任务控制器
  15. * Class Timer
  16. * @package app\api\controller
  17. */
  18. //*/1 * * * * /usr/bin/curl http://127.0.0.1/api/Timer/patrol > timer.log 2>&1 &
  19. //*/1 * * * * /usr/bin/curl http://127.0.0.1/api/Timer/addPatrolTask > /dev/null 2>&1 &
  20. //*/1 * * * * /usr/bin/curl http://127.0.0.1/api/Timer/cleantask > /dev/null 2>&1 &
  21. //*/1 * * * * /usr/bin/curl http://127.0.0.1/Timer/daily > /dev/null 2>&1 &
  22. //*/1 * * * * /usr/bin/curl http://127.0.0.1/Timer/addDailyTask > /dev/null 2>&1 &
  23. //*/1 * * * * /usr/bin/curl http://127.0.0.1/api/Timer/device > /dev/null 2>&1 &
  24. //*/1 * * * * /usr/bin/curl http://127.0.0.1/api/Timer/addDeviceTask > /dev/null 2>&1 &
  25. //*/1 * * * * /usr/bin/curl http://127.0.0.1/api/Timer/waste > /dev/null 2>&1 &
  26. //*/1 * * * * /usr/bin/curl http://127.0.0.1/api/Timer/patrolStart > /dev/null 2>&1 &
  27. //*/1 * * * * /usr/bin/curl http://127.0.0.1/api/Timer/dailyStart > /dev/null 2>&1 &
  28. //*/1 * * * * /usr/bin/curl http://127.0.0.1/api/Timer/deviceStart > /dev/null 2>&1 &
  29. //0 8 * * * /usr/bin/curl http://127.0.0.1/api/Timer/cleanStart > /dev/null 2>&1 &
  30. //*/1 * * * * /usr/bin/curl http://127.0.0.1/api/Timer/patrolPlan > /dev/null 2>&1 &
  31. //*/1 * * * * /usr/bin/curl http://127.0.0.1/api/Timer/cancelGorder > /dev/null 2>&1 &
  32. //*/1 * * * * /usr/bin/curl http://127.0.0.1/api/Timer/cancelDelay > /dev/null 2>&1 &
  33. //20 1 * * * /usr/bin/curl http://127.0.0.1/api/Timer/attendance > /dev/null 2>&1 &
  34. //30 1 * * * /usr/bin/curl http://127.0.0.1/api/Timer/attendanceReissue > /dev/null 2>&1 &
  35. //*/1 * * * * /usr/bin/curl http://127.0.0.1/api/Timer/attendanceResult > /dev/null 2>&1 &
  36. //*/1 * * * * /usr/bin/curl http://127.0.0.1/api/Timer/deviceTaskRemind > /dev/null 2>&1 &
  37. //*/1 * * * * /usr/bin/curl http://127.0.0.1/api/Timer/quality > /dev/null 2>&1 &
  38. //*/1 * * * * /usr/bin/curl http://127.0.0.1/api/Timer/addQualityTask > /dev/null 2>&1 &
  39. //*/1 * * * * /usr/bin/curl http://127.0.0.1/api/Timer/complain > /dev/null 2>&1 &
  40. class Timer extends Controller
  41. {
  42. public function tt(){
  43. echo 'test';
  44. }
  45. //处理已超时的巡更任务,并从任务栏删除
  46. public function patrol(){
  47. set_time_limit(0);
  48. ini_set("memory_limit","1024M");
  49. (new PatrolTask())->timer_action();
  50. }
  51. //近12个小时的巡更任务加入到任务栏
  52. public function addPatrolTask(){
  53. (new PatrolTask())->addPatrolTask();
  54. }
  55. //处理已超时的专项保洁任务
  56. public function cleantask(){
  57. set_time_limit(0);
  58. ini_set("memory_limit","1024M");
  59. (new CleanTask())->timer_action();
  60. }
  61. //处理已超时日常工作任务,并从任务栏删除
  62. public function daily(){
  63. (new DailyTask())->timer_action();
  64. }
  65. //近12个小时的日常工作任务加入到任务栏
  66. public function addDailyTask(){
  67. (new DailyTask())->addDailyTask();
  68. }
  69. //处理已超时设备台账任务,并从任务栏删除
  70. public function device(){
  71. set_time_limit(0);
  72. ini_set("memory_limit","1024M");
  73. (new DeviceTask())->timer_action();
  74. }
  75. //近12个小时的设备台账任务加入到任务栏
  76. public function addDeviceTask(){
  77. (new DeviceTask())->addDeviceTask();
  78. }
  79. // 取消过期的延期申请
  80. public function cancelDelay(){
  81. Db::name('device_task_delay')
  82. ->where('status',0)
  83. ->where('end_time','<=',date('Y-m-d H:i:s'))
  84. ->update(['status'=>3,'update_time'=>date('Y-m-d H:i:s')]);
  85. }
  86. //医废未转运提醒 每分钟执行一次
  87. public function waste(){
  88. set_time_limit(0);
  89. ini_set("memory_limit","1024M");
  90. $orgids = Db::name('config_org')
  91. ->alias('co')
  92. ->join('config c','c.id = co.config_id')
  93. ->where('c.name','org_waste_status')
  94. ->where('co.value',1)
  95. ->column('co.org_id');
  96. if(!$orgids){
  97. exit();
  98. }
  99. $orgids2 = Db::name('config_org')
  100. ->alias('co')
  101. ->join('config c','c.id = co.config_id')
  102. ->where('c.name','org_waste_time')
  103. ->where('co.value',date('H:i'))
  104. ->column('co.org_id');
  105. if(!$orgids2){
  106. exit();
  107. }
  108. $orgids = array_unique(array_intersect($orgids,$orgids2));
  109. if(!$orgids){
  110. exit();
  111. }
  112. $users = [];
  113. foreach ($orgids as $v){
  114. //检查是否有未转运订单
  115. $ret = Db::name('waste_record')
  116. ->where('org_id',$v)
  117. ->where('status','in',[0,1])
  118. ->find();
  119. if(!$ret){
  120. continue;
  121. }
  122. // 检查是否设置通知人员
  123. $uids = model('Config')->getConfig('org_waste_user',$v);
  124. if($uids){
  125. $users = array_merge($users,explode(',',$uids));
  126. }
  127. }
  128. if($users){ //推送
  129. $length = 1000;
  130. $count = ceil(count($users)/$length);
  131. for ($i=0;$i<$count;$i++){
  132. $uids = array();
  133. $min = $i*$length;
  134. $max = $min + $length;
  135. if($max > count($users)){
  136. $max = count($users);
  137. }
  138. for($j=$min;$j<$max;$j++){
  139. $uids[] = $users[$j];
  140. }
  141. send_jpush($uids,AppMsg::PUSH_WASTE_UNFINISH);
  142. }
  143. }
  144. }
  145. // 巡更任务将要开始提醒 每分钟执行一次
  146. public function patrolStart(){
  147. $curTime = date('Y-m-d H:i').':00';
  148. $users = Db::name('patrol_task_user')
  149. ->alias('ptu')
  150. ->join('patrol_task pt','pt.id = ptu.patrol_task_id')
  151. ->where('pt.del',0)
  152. ->where('pt.status',0)
  153. ->where('pt.start_time',$curTime)
  154. ->field('ptu.user_id,patrol_mode')
  155. ->select();
  156. $userids1 = $userids2 = $userids3 = $userids4 = [];
  157. foreach ($users as $k=>$v){
  158. if($v['patrol_mode'] == 1 && !in_array($v['user_id'],$userids1)){
  159. $userids1[] = $v['user_id'];
  160. }else if($v['patrol_mode'] == 2 && !in_array($v['user_id'],$userids2)){
  161. $userids2[] = $v['user_id'];
  162. }else if($v['patrol_mode'] == 3 && !in_array($v['user_id'],$userids3)){
  163. $userids3[] = $v['user_id'];
  164. }else if($v['patrol_mode'] == 4 && !in_array($v['user_id'],$userids4)){
  165. $userids4[] = $v['user_id'];
  166. }
  167. }
  168. if($userids1){
  169. send_jpush($userids1,AppMsg::PUSH_PATROL,'您有新的巡更任务将要开始,请及时执行。');
  170. }
  171. if($userids2){
  172. send_jpush($userids2,AppMsg::PUSH_PATROL,'您有新的巡视任务将要开始,请及时执行。');
  173. }
  174. if($userids3){
  175. send_jpush($userids3,AppMsg::PUSH_PATROL,'您有新的巡查任务将要开始,请及时执行。');
  176. }
  177. if($userids4){
  178. send_jpush($userids4,AppMsg::PUSH_PATROL,'您有新的巡检任务将要开始,请及时执行。');
  179. }
  180. }
  181. // 专项保洁任务将要开始提醒 每天早上6点执行一次
  182. public function cleanStart(){
  183. $curTime = date('Y-m-d');
  184. $users = Db::name('clean_task_user')
  185. ->alias('ptu')
  186. ->join('clean_task pt','pt.id = ptu.task_id')
  187. ->where('pt.del',0)
  188. ->where('pt.status',0)
  189. ->where('pt.start_time',$curTime)
  190. ->column('ptu.user_id');
  191. if($users){
  192. send_jpush($users,AppMsg::PUSH_CLEAN,'您有新的专项保洁任务将要开始,请及时执行。');
  193. }
  194. }
  195. // 日常工作任务将要开始提醒 每分钟执行一次
  196. public function dailyStart(){
  197. $curTime = date('Y-m-d H:i').':00';
  198. $users = Db::name('daily_task_user')
  199. ->alias('ptu')
  200. ->join('daily_task pt','pt.id = ptu.task_id')
  201. ->where('pt.del',0)
  202. ->where('pt.status',0)
  203. ->where('pt.start_time',$curTime)
  204. ->column('ptu.user_id');
  205. if($users){
  206. send_jpush($users,AppMsg::PUSH_DAILY_TASK,'您有新的日常工作任务将要开始,请及时执行。');
  207. }
  208. }
  209. // 设备台账任务将要开始提醒 每分钟执行一次
  210. public function deviceStart(){
  211. $curTime = date('Y-m-d H:i').':00';
  212. $users = Db::name('device_task_user')
  213. ->alias('ptu')
  214. ->join('device_task pt','pt.id = ptu.task_id')
  215. ->where('pt.del',0)
  216. ->where('pt.status',0)
  217. ->where('pt.start_time',$curTime)
  218. ->column('ptu.user_id');
  219. if($users){
  220. send_jpush($users,AppMsg::PUSH_DEVICE_TASK,'您有新的设备台账任务将要开始,请及时执行。');
  221. }
  222. }
  223. //商城取消未支付订单
  224. public function cancelGorder(){
  225. $list = Db::name('g_orders')
  226. ->where('status',0)
  227. ->select();
  228. foreach ($list as $k=>$v){
  229. if(strtotime($v['create_time']) < (time()-30*60)){
  230. Db::name('g_orders')
  231. ->where('id',$v['id'])
  232. ->update([
  233. 'status'=>3,
  234. 'cancel_time'=>getTime()
  235. ]);
  236. $goods = Db::name('g_order_goods')
  237. ->where('order_id',$v['id'])
  238. ->select();
  239. foreach ($goods as $k1=>$v1){
  240. Db::name('g_goods')
  241. ->where('id',$v1['goods_id'])
  242. ->setInc('stock',$v1['nums']);
  243. }
  244. }
  245. }
  246. }
  247. // 修改巡更计划状态 1分钟执行一次
  248. public function patrolPlan(){
  249. $curDay = date('Y-m-d');
  250. Db::name('patrol_plan')
  251. ->where('del',0)
  252. ->where('status',0)
  253. ->where('start_time','<=',$curDay)
  254. ->update(['status'=>1,'update_time'=>date('Y-m-d H:i:s')]);
  255. Db::name('patrol_plan')
  256. ->where('del',0)
  257. ->where('status','in',[0,1])
  258. ->where('end_time','<',$curDay)
  259. ->update(['status'=>2,'update_time'=>date('Y-m-d H:i:s')]);
  260. }
  261. // 更新打卡状态,每天执行一次
  262. public function attendance(){
  263. $yday = date('Y-m-d',time() - 86400);
  264. // 昨天不跨天进行中的状态改为已完成
  265. $map1[] = ['day','=',$yday];
  266. $map1[] = ['next','=',0];
  267. $map1[] = ['status','=',0];
  268. $yyday = date('Y-m-d',time() - 2*86400);
  269. // 昨天跨天进行中的状态改为已完成
  270. $map2[] = ['day','=',$yyday];
  271. $map2[] = ['next','=',1];
  272. $map2[] = ['status','=',0];
  273. $lists = Db::name('attendance_user_class')
  274. ->whereOr([ $map1, $map2 ])
  275. ->field('id,content')
  276. ->select();
  277. $lists = $lists?$lists:[];
  278. foreach ($lists as $k=>$v){
  279. $dates = json_decode($v['content'],true);
  280. foreach ($dates as $kk=>$vv){
  281. $dates[$kk]['sstatus'] = 1;
  282. $dates[$kk]['estatus'] = 1;
  283. }
  284. Db::name('attendance_user_class')->where('id',$v['id'])->update([
  285. 'content' => json_encode($dates),
  286. 'status' => 1,
  287. 'update_time' => date('Y-m-d H:i:s')
  288. ]);
  289. }
  290. }
  291. // 昨日未打卡的日期,每天执行一次
  292. public function attendanceReissue(){
  293. $yday = date('Y-m-d',time() - 86400);
  294. model('AttendanceUserClass')->attendanceReissueFixed($yday); // 固定班制
  295. model('AttendanceUserClass')->attendanceReissueClass($yday); // 排班制
  296. }
  297. // 生成考勤结果,每分钟执行一次
  298. public function attendanceResult(){
  299. $lists = Db::name('attendance_user_class')->where('status',1)->where('result',0)->field('id,content')->limit(500)->select();
  300. $lists = $lists?$lists:[];
  301. foreach ($lists as $k=>$v){
  302. $content = json_decode($v['content'],true);
  303. $result = 1;
  304. $count = 0; // 打卡次数
  305. foreach ($content as $kk=>$vv){
  306. if($result != 1){
  307. continue;
  308. }
  309. if($vv['ssign']){
  310. $count++;
  311. if($vv['ssign'] > $vv['stime']){
  312. $result = 2;
  313. }
  314. }
  315. if($vv['esign']){
  316. $count++;
  317. if($vv['esign'] < $vv['etime']){
  318. $result = 2;
  319. }
  320. }
  321. }
  322. if($count == 0){
  323. $result = 3;
  324. }
  325. Db::name('attendance_user_class')->where('id',$v['id'])->update(['result' => $result]);
  326. }
  327. }
  328. //提前五分钟提醒
  329. public function deviceTaskRemind(){
  330. $curStr= time() - 60*5;
  331. $date = date('Y-m-d H:i',$curStr).':00';
  332. $users = Db::name('device_task_user')
  333. ->alias('ptu')
  334. ->join('device_task pt','pt.id = ptu.task_id')
  335. ->where('pt.del',0)
  336. ->where('pt.status',0)
  337. ->where('pt.start_time',$date)
  338. ->column('ptu.user_id');
  339. send_jpush($users,AppMsg::PUSH_DEVICE_TASK,'您有新的设备维保任务将要开始,请及时执行。');
  340. }
  341. //处理已超时的巡更任务,并从任务栏删除
  342. public function quality(){
  343. set_time_limit(0);
  344. ini_set("memory_limit","1024M");
  345. (new QualityTask())->timerAction();
  346. }
  347. //近12个小时的品控任务加入到任务栏
  348. public function addQualityTask(){
  349. (new QualityTask())->addPatrolTask();
  350. }
  351. public function complain(){
  352. $lists = Db::name('config_org')
  353. ->alias('a')
  354. ->join('config b','a.config_id = b.id')
  355. ->where('b.name','org_complain_hour')
  356. ->field('a.org_id,a.value')
  357. ->select();
  358. $lists = $lists?$lists:[];
  359. foreach ($lists as $k=>$v){
  360. if($v['value'] > 0){
  361. // 获取当前组织是否设置人员,设置人员才发送超时提醒
  362. $users = (new Config())->getConfig('org_complain_user',$v['org_id']);
  363. if($users){
  364. $uids = Db::name('user')->where('id','in',$users)->where('del',0)->where('enable')->column('id');
  365. if($uids){
  366. // 检查是否有超时未处理投诉
  367. $map = [];
  368. $endTime = date('Y-m-d H:i:s',time() - $v['value']*60*60);
  369. $map[] = ['create_time','<',$endTime];
  370. $map[] = ['status','=',0];
  371. $map[] = ['org_id','=',$v['org_id']];
  372. $map[] = ['del','=',0];
  373. $ret = Db::name('complain')
  374. ->where($map)
  375. ->field('id,content,create_time,status,order_id')
  376. ->find();
  377. if($ret){
  378. send_jpush($uids,AppMsg::PUSH_COMPLAIN_TIMEOUT);
  379. }
  380. }
  381. }
  382. }
  383. }
  384. }
  385. }