Task.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\ActivityApply;
  4. use app\common\model\ActivityApplyItem;
  5. use app\common\model\ActivityPrice;
  6. use app\common\model\ArticleItem;
  7. use app\common\model\DatumUrl;
  8. use app\common\model\ForumReply;
  9. use app\common\model\GoodsOrder;
  10. use app\common\model\GoodsOrderItem;
  11. use app\common\model\LabelMessage;
  12. use app\common\model\StoreGoodsItem;
  13. use app\common\model\TopSearch;
  14. use app\common\model\User;
  15. use app\common\model\UserFacility;
  16. use app\common\model\UserForum;
  17. use app\common\model\UserMessage;
  18. use app\common\model\UserSearch;
  19. use app\common\model\UserTag;
  20. use app\common\model\VideoUrl;
  21. use app\common\service\UserSynth;
  22. use think\Db;
  23. /**
  24. * @title 定时任务
  25. * Class Task
  26. * @controller Task
  27. * @group base
  28. */
  29. class Task extends Base
  30. {
  31. /**
  32. * @title 标签推送[根据标签id]
  33. * @desc 标签推送
  34. * @author qc
  35. * @url /api/Task/taskUserMessage
  36. */
  37. public function taskUserMessage(){
  38. $list = UserMessage::field('id,user_id,content')->where('jg_type',1)->where('jg_status',0)->limit(0,50)->order('id')->select();
  39. foreach ($list as $v) {
  40. UserMessage::where(['id'=>$v['id']])->update(['jg_status'=>1]);
  41. UserMessage::Jpush($v['user_id'],$v['content']);
  42. }
  43. }
  44. /**
  45. * @title 同步回答量
  46. * @desc 同步回答量
  47. * @author qc
  48. * @url /api/Task/forum_num
  49. */
  50. public function forum_num(){
  51. $forum = UserForum::where('is_deleted',0)->field('id')->select();
  52. foreach ($forum as $k => $v){
  53. $apply = ForumReply::where('forum_id',$v['id'])->where('is_deleted',0)->count();
  54. if($apply){
  55. UserForum::where('id',$v['id'])->update(['r_num'=>$apply]);
  56. }else{
  57. UserForum::where('id',$v['id'])->update(['r_num'=>0]);
  58. }
  59. }
  60. $this->success('执行完成');
  61. }
  62. /**
  63. * @title 标签推送[根据标签id]
  64. * @desc 标签推送
  65. * @author qc
  66. * @url /api/Task/taskLabelMessage
  67. */
  68. //按照标签推送
  69. public function taskLabelMessage()
  70. {
  71. $list = LabelMessage::where('jg_status',0)->where('is_deleted',0)->select()->toArray();
  72. foreach ($list as $v) {
  73. LabelMessage::where(['id'=>$v['id']])->update(['jg_status'=>1]);
  74. $ext = [];
  75. $ext['title'] = $v['title'];
  76. $channel = ['xiaomi'=>"108240",'huawei'=>'IM','oppo'=>'news']; //公信
  77. //$channel = ['xiaomi'=>"108237",'huawei'=>'IM','oppo'=>'message']; //私信
  78. // 全部推送
  79. if(!$v['label']) {
  80. $user_arr = User::where('status',1)->where('is_deleted',0)->column('id');
  81. // return json($user_arr);
  82. foreach ($user_arr as $user_id) {
  83. if($v['type'] == 2){
  84. $res = UserMessage::Jpush($user_id,$v['content'],'','','','',$channel,$ext);
  85. }else if($v['type'] == 3) {
  86. // $ext = [];
  87. // $ext['title'] = $v['title'];
  88. // $channel = ['xiaomi'=>"108240",'huawei'=>'IM','oppo'=>'news'];
  89. if(in_array($v['module'],['video','article','datum'])) {
  90. $res = UserMessage::Jpush($user_id,$v['content'],$v['module'],'',$v['first_id'],$v['second_id'],$channel,$ext);
  91. }else{
  92. // if($v['module'] == 'supplier'){
  93. // //供应商产品
  94. // $res = UserMessage::Jpush($user_id,$v['content'],$v['module'],5,$v['first_id'],'',$channel,$ext);
  95. // }
  96. // if($v['module'] == 'activity'){
  97. // //活动
  98. // $res = UserMessage::Jpush($user_id,$v['content'],$v['module'],3,$v['first_id'],$channel,$ext);
  99. // }
  100. // if($v['module'] == 'forum'){
  101. // //问答
  102. // $res = UserMessage::Jpush($user_id,$v['content'],$v['module'],6,$v['first_id'],$channel,$ext);
  103. // }
  104. // if($v['module'] == 'press'){
  105. // //新闻 暂时没有对应type
  106. // $res = UserMessage::Jpush($user_id,$v['content'],$v['module'],5,$v['first_id'],$channel,$ext);
  107. // }
  108. // if($v['module'] == 'recruit'){
  109. // //招聘 暂时没有对应type
  110. // $res = UserMessage::Jpush($user_id,$v['content'],$v['module'],5,$v['first_id'],$channel,$ext);
  111. // }
  112. $res = UserMessage::Jpush($user_id,$v['content'],$v['module'],'',$v['first_id'],$v['second_id'],$channel,$ext);
  113. }
  114. }
  115. // return json($res);
  116. // $arr = [
  117. // 'user_id' => $user_id,
  118. // 'type' => 4,
  119. // 'log' => json_encode($res),
  120. // ];
  121. // Db::name('client_log')->insert($arr);
  122. }
  123. }else{
  124. // 搜索内容推送
  125. $label_arr = explode(',',$v['label']);
  126. $where_str = '';
  127. $search_arr=[];
  128. $tag_arr = [];
  129. foreach ($label_arr as $t){
  130. $search_arr[] = " title like '".'%'.$t."%'" .' ';
  131. $tag_arr[] = $t;
  132. }
  133. if(!empty($search_arr)) $where_str = implode(' OR ',$search_arr);
  134. // $user_arr = UserSearch::where($where_str)->column('user_id');
  135. //// return json($user_arr);
  136. $user_arr = [];
  137. $user = User::where('label','<>',null)->where('label','<>','||')->where('is_deleted',0)->field('id,label')->select();
  138. $tags = UserTag::where('title','in',$tag_arr)->column('id');
  139. foreach ($user as $key => $val){
  140. $label = explode('|',$val['label']);
  141. foreach ($label as $val1){
  142. if (array_search($val1, $tags) !== false) {
  143. // echo '存在';
  144. $user_arr[] = $val['id'];
  145. } else {
  146. // echo '不存在';
  147. }
  148. }
  149. }
  150. $user_arr = array_unique($user_arr);
  151. foreach (array_unique($user_arr) as $user_id) {
  152. if($v['type'] == 2){
  153. // $ext = [];
  154. // $ext['title'] = $v['title'];
  155. // $channel = ['xiaomi'=>"108240",'huawei'=>'IM','oppo'=>'news'];
  156. // $res = UserMessage::Jpush($user_id,$v['content']);
  157. $res = UserMessage::Jpush($user_id,$v['content'],'','','','',$channel,$ext);
  158. }else if($v['type'] == 3) {
  159. // $ext = [];
  160. // $ext['title'] = $v['title'];
  161. // $channel = ['xiaomi'=>"108240",'huawei'=>'IM','oppo'=>'news'];
  162. if(in_array($v['module'],['video','article','datum'])) {
  163. $res = UserMessage::Jpush($user_id,$v['content'],$v['module'],'',$v['first_id'],$v['second_id'],$channel,$ext);
  164. }else{
  165. $res = UserMessage::Jpush($user_id,$v['content'],$v['module'],'',$v['first_id'],'',$channel,$ext);
  166. }
  167. }
  168. }
  169. }
  170. }
  171. }
  172. /**
  173. * @title 半年清理一次设备号
  174. * @desc
  175. * @author qc
  176. * @url /api/Task/removeFacility
  177. */
  178. public function removeFacility(){
  179. UserFacility::where(['set_time','< time',date('Y-m-d H:i:s',strtotime('-180 days'))])->update(['facility'=>'','set_time'=>date('Y-m-d H:i:s')]);
  180. }
  181. /**
  182. * @title 定时取消活动订单
  183. * @desc
  184. * @author qc
  185. * @url /api/Task/cancelApplyOrder
  186. */
  187. public function cancelApplyOrder()
  188. {
  189. $list = ActivityApply::with(['itemList'])
  190. ->where([['create_at','< time',date('Y-m-d H:i:s',strtotime('-5 minutes'))],['pay_type','<>',9],['pay_state','=',0],['cancel_state','=',0]])
  191. ->select()->toArray();
  192. foreach ($list as $v) {
  193. Db::startTrans();
  194. try {
  195. ActivityApply::where('id',$v['id'])->update(['cancel_state'=>1]);
  196. $act_title = \app\common\model\Activity::where('id',$v['act_id'])->value('title');
  197. $info_url = 'https://'.$_SERVER['HTTP_HOST'].'/dist/#/activity_order_info?id='.$v['id'];
  198. $message = '您报名的《'.$act_title."》活动订单,由于超时未支付,已为您取消";//,点击<a href=\"$info_url\">查看详情</a>";
  199. UserMessage::sendUserMessage($v['user_id'],'apply',4,0,0,$v['id'],$message,$v['id']);
  200. foreach ($v['item_list'] as $it) {
  201. ActivityPrice::where('id',$it['price_id'])->setInc('stock',$it['num']);
  202. }
  203. Db::commit();
  204. }catch (\Exception $e) {
  205. Db::rollback();
  206. }
  207. }
  208. }
  209. /**
  210. * @title 定时取消商城订单
  211. * @desc
  212. * @author qc
  213. * @url /api/Task/cancelGoodsOrder
  214. */
  215. public function cancelGoodsOrder()
  216. {
  217. $list = GoodsOrder::with(['orderItem'])
  218. ->where([['create_at','< time',date('Y-m-d H:i:s',strtotime('-1 day'))],['status','=',0],['pay_state','=',0],['cancel_state','=',0]])
  219. ->select()->toArray();
  220. // return json($list);
  221. $num = 0;
  222. foreach ($list as $v) {
  223. $order_id = $v['id'];
  224. Db::startTrans();
  225. try {
  226. $detail = $v;
  227. if($detail['status'] != 0) $this->exception('订单状态有误');
  228. // 取消订单状态
  229. GoodsOrder::update(['status'=>9,'cancel_state'=>1],['id'=>$order_id]);
  230. // 订单处理
  231. foreach ($detail['order_item'] as $item_info) {
  232. // 更改订单详情状态
  233. GoodsOrderItem::update(['status'=>9],['id'=>$item_info['id']]);
  234. StoreGoodsItem::stockChange($item_info['spec_id'],$item_info['goods_id'],$item_info['num']);
  235. }
  236. Db::commit();
  237. }catch (\Exception $e){
  238. $this->ret_msg = $e->getMessage();
  239. $this->is_commit = false;
  240. Db::rollback();
  241. }
  242. $num++;
  243. }
  244. $this->success('取消'.$num.'条订单');
  245. }
  246. /**
  247. * @title 定时自动收货
  248. * @desc
  249. * @author qc
  250. * @url /api/Task/confirmGoodsOrder
  251. */
  252. public function confirmGoodsOrder(){
  253. //express_send_at 发货时间
  254. //['express_send_at','< time',date('Y-m-d H:i:s',strtotime('-2 days'))]
  255. $list = GoodsOrder::where([['pay_state','=',1],['status','=',2]])
  256. ->select()->toArray();
  257. foreach ($list as $detail) {
  258. $data = get_delivery($detail['express_send_no']);
  259. Db::startTrans();
  260. if($data['issign'] == 1){
  261. $express_send_date = end($data['list']);
  262. $express_send_date = $express_send_date['time'];
  263. if($express_send_date < date('Y-m-d H:i:s',strtotime('-2 days'))){
  264. try {
  265. $order_id = $detail['id'];
  266. // $detail = GoodsOrder::where('id',$order_id)->find()->toArray();
  267. if($detail['status'] == 0) $this->error('订单未支付');
  268. if($detail['status'] == 1) $this->error('订单未发货');
  269. if($detail['status'] == 3) $this->error('订单已收货');
  270. GoodsOrder::update(['status'=>3,'express_state'=>2],['id'=>$order_id]);
  271. GoodsOrderItem::update(['status'=>3,'express_state'=>2],['order_id'=>$order_id]);
  272. UserMessage::create(['user_id'=>$detail['user_id'],'type_id'=>3,'relation_id'=>$order_id,'content'=>'订单收货成功']);
  273. Db::commit();
  274. }catch (\Exception $e) {
  275. Db::rollback();
  276. }
  277. }
  278. }
  279. }
  280. $this->success('执行成功');
  281. }
  282. /**
  283. * @title 活动报名定时提醒
  284. * @desc
  285. * @author qc
  286. * @url /api/Task/activityReminder
  287. */
  288. public function activityReminder()
  289. {
  290. $where = [];
  291. $where[] = ['task_send','=',0];
  292. $where[] = ['start_time','> time',date('Y-m-d H:i:s',strtotime('-24 hours'))];
  293. $where[] = ['status','=',1];
  294. $where[] = ['is_deleted','=',0];
  295. $list = \app\common\model\Activity::where($where)->select()->toArray();
  296. foreach ($list as $act_info)
  297. {
  298. \app\common\model\Activity::where(['id'=>$act_info['id']])->update(['task_send'=>1]);
  299. $ticket_list = ActivityApplyItem::where(['act_id'=>$act_info['id'],'sh_status'=>1,'is_hx'=>0])->select();
  300. foreach ($ticket_list as $v) {
  301. // 短信通知
  302. if($v['phone_pre'] == '86' && $v['phone']) UserSynth::phoneMessageSend($v['phone'],0,['ActivityName'=>$act_info['title'], 'time'=>$act_info['start_time'], 'adress'=>$act_info['address']]);
  303. }
  304. }
  305. }
  306. /**
  307. * @title 热搜定时
  308. * @desc
  309. * @author qc
  310. * @url /api/Task/topSearchTask
  311. */
  312. public function topSearchTask()
  313. {
  314. $where[] = ['hot_time','< time',date('Y-m-d H:i:s',strtotime('-7 days'))];
  315. // 视频
  316. $list = VideoUrl::where($where)->column('id');
  317. array_walk($list,function ($v){
  318. VideoUrl::where('id',$v)->update(['hot_time'=>date('Y-m-d H:i:s'),'hot_num'=>0]);
  319. TopSearch::saveData($v,'video');
  320. });
  321. // 图文
  322. $list = ArticleItem::where($where)->column('id');
  323. array_walk($list,function ($v){
  324. ArticleItem::where('id',$v)->update(['hot_time'=>date('Y-m-d H:i:s'),'hot_num'=>0]);
  325. TopSearch::saveData($v,'article');
  326. });
  327. // 资料
  328. $list = DatumUrl::where($where)->column('id');
  329. array_walk($list,function ($v){
  330. DatumUrl::where('id',$v)->update(['hot_time'=>date('Y-m-d H:i:s'),'hot_num'=>0]);
  331. TopSearch::saveData($v,'datum');
  332. });
  333. // 活动
  334. $list = \app\common\model\Activity::where($where)->column('id');
  335. array_walk($list,function ($v){
  336. \app\common\model\Activity::where('id',$v)->update(['hot_time'=>date('Y-m-d H:i:s'),'hot_num'=>0]);
  337. TopSearch::saveData($v,'activity');
  338. });
  339. // 需求
  340. $list = \app\common\model\PlatformDemand::where($where)->column('id');
  341. array_walk($list,function ($v){
  342. \app\common\model\PlatformDemand::where('id',$v)->update(['hot_time'=>date('Y-m-d H:i:s'),'hot_num'=>0]);
  343. TopSearch::saveData($v,'demand');
  344. });
  345. // 新闻
  346. $list = \app\common\model\Press::where($where)->column('id');
  347. array_walk($list,function ($v){
  348. \app\common\model\Press::where('id',$v)->update(['hot_time'=>date('Y-m-d H:i:s'),'hot_num'=>0]);
  349. TopSearch::saveData($v,'press');
  350. });
  351. // 问答
  352. $list = \app\common\model\UserForum::where($where)->column('id');
  353. array_walk($list,function ($v){
  354. \app\common\model\UserForum::where('id',$v)->update(['hot_time'=>date('Y-m-d H:i:s'),'hot_num'=>0]);
  355. TopSearch::saveData($v,'forum');
  356. });
  357. // 招聘
  358. $list = \app\common\model\Recruit::where($where)->column('id');
  359. array_walk($list,function ($v){
  360. \app\common\model\Recruit::where('id',$v)->update(['hot_time'=>date('Y-m-d H:i:s'),'hot_num'=>0]);
  361. TopSearch::saveData($v,'recruit');
  362. });
  363. // 供应商
  364. $list = \app\common\model\SupplierGoods::where($where)->column('id');
  365. array_walk($list,function ($v){
  366. \app\common\model\SupplierGoods::where('id',$v)->update(['hot_time'=>date('Y-m-d H:i:s'),'hot_num'=>0]);
  367. TopSearch::saveData($v,'supplier_goods');
  368. });
  369. // 商品
  370. $list = \app\common\model\StoreGoods::where($where)->column('id');
  371. array_walk($list,function ($v){
  372. \app\common\model\StoreGoods::where('id',$v)->update(['hot_time'=>date('Y-m-d H:i:s'),'hot_num'=>0]);
  373. TopSearch::saveData($v,'goods');
  374. });
  375. $this->success('执行成功');
  376. }
  377. /**
  378. * @title 热搜定时1
  379. * @desc
  380. * @author qc
  381. * @url /api/Task/emptyTopSearchTask
  382. */
  383. public function emptyTopSearchTask()
  384. {
  385. //热搜规则
  386. //根据后台输入的热搜阅读量排序
  387. //每10分钟前端更新一次
  388. //后台热搜阅读量每天晚上的23:59点,清楚7天前的那一天所有新增的内容的热搜阅读量
  389. //如:
  390. //2023年11月5日,新增了2篇文章,1个视频(不关心是几点新增的)
  391. //2023年11月12日,晚上23:59,将新增的2篇文章跟1个视频的热搜阅读清0
  392. //热搜阅读量可以手动更改,每次有用户点击时,热搜阅读量也增加1
  393. //top_search hot_num
  394. $start_time = date('Y-m-d 00:00:00',strtotime('-7 days'));
  395. $end_time = date('Y-m-d 23:59:59',strtotime('-7 days'));
  396. // return $start_time.'---'.$end_time;
  397. $where[] = ['create_at','> time',$start_time];
  398. $where[] = ['create_at','< time',$end_time];
  399. // $list = TopSearch::where($where)->column('id');
  400. // return json($list);
  401. // TopSearch::where($where)->update(['hot_num'=>0]);
  402. // foreach ($list as $k => $v){
  403. // TopSearch::where('id',$v)->update(['hot_num'=>0]);
  404. // }
  405. $list = VideoUrl::where($where)->column('id');
  406. array_walk($list,function ($v){
  407. VideoUrl::where('id',$v)->update(['hot_time'=>date('Y-m-d H:i:s'),'hot_num'=>0]);
  408. TopSearch::saveData($v,'video');
  409. });
  410. // 图文
  411. $list = ArticleItem::where($where)->column('id');
  412. array_walk($list,function ($v){
  413. ArticleItem::where('id',$v)->update(['hot_time'=>date('Y-m-d H:i:s'),'hot_num'=>0]);
  414. TopSearch::saveData($v,'article');
  415. });
  416. // 资料
  417. $list = DatumUrl::where($where)->column('id');
  418. array_walk($list,function ($v){
  419. DatumUrl::where('id',$v)->update(['hot_time'=>date('Y-m-d H:i:s'),'hot_num'=>0]);
  420. TopSearch::saveData($v,'datum');
  421. });
  422. // 活动
  423. $list = \app\common\model\Activity::where($where)->column('id');
  424. array_walk($list,function ($v){
  425. \app\common\model\Activity::where('id',$v)->update(['hot_time'=>date('Y-m-d H:i:s'),'hot_num'=>0]);
  426. TopSearch::saveData($v,'activity');
  427. });
  428. // 需求
  429. $list = \app\common\model\PlatformDemand::where($where)->column('id');
  430. array_walk($list,function ($v){
  431. \app\common\model\PlatformDemand::where('id',$v)->update(['hot_time'=>date('Y-m-d H:i:s'),'hot_num'=>0]);
  432. TopSearch::saveData($v,'demand');
  433. });
  434. // 新闻
  435. $list = \app\common\model\Press::where($where)->column('id');
  436. array_walk($list,function ($v){
  437. \app\common\model\Press::where('id',$v)->update(['hot_time'=>date('Y-m-d H:i:s'),'hot_num'=>0]);
  438. TopSearch::saveData($v,'press');
  439. });
  440. // 问答
  441. $list = \app\common\model\UserForum::where($where)->column('id');
  442. array_walk($list,function ($v){
  443. \app\common\model\UserForum::where('id',$v)->update(['hot_time'=>date('Y-m-d H:i:s'),'hot_num'=>0]);
  444. TopSearch::saveData($v,'forum');
  445. });
  446. // 招聘
  447. $list = \app\common\model\Recruit::where($where)->column('id');
  448. array_walk($list,function ($v){
  449. \app\common\model\Recruit::where('id',$v)->update(['hot_time'=>date('Y-m-d H:i:s'),'hot_num'=>0]);
  450. TopSearch::saveData($v,'recruit');
  451. });
  452. // 供应商
  453. $list = \app\common\model\SupplierGoods::where($where)->column('id');
  454. array_walk($list,function ($v){
  455. \app\common\model\SupplierGoods::where('id',$v)->update(['hot_time'=>date('Y-m-d H:i:s'),'hot_num'=>0]);
  456. TopSearch::saveData($v,'supplier_goods');
  457. });
  458. // 商品
  459. $list = \app\common\model\StoreGoods::where($where)->column('id');
  460. array_walk($list,function ($v){
  461. \app\common\model\StoreGoods::where('id',$v)->update(['hot_time'=>date('Y-m-d H:i:s'),'hot_num'=>0]);
  462. TopSearch::saveData($v,'goods');
  463. });
  464. $this->success('执行成功');
  465. }
  466. /**
  467. * @title 定时取消商城订单
  468. * @desc
  469. * @author qc
  470. * @url /api/Task/cancelStoreGoods
  471. */
  472. public function cancelStoreGoods()
  473. {
  474. $list = GoodsOrder::with(['orderItem','orderRefund'])
  475. ->where([['pay_state','=',0],['status','=',0],['create_at','< time',date('Y-m-d H:i:s',strtotime('-1 days'))]])
  476. ->select()->toArray();
  477. foreach ($list as $detail) {
  478. // 取消订单状态
  479. GoodsOrder::update(['status'=>9,'cancel_state'=>1],['id'=>$detail['id']]);
  480. // 订单处理
  481. foreach ($detail['order_item'] as $item_info) {
  482. // 更改订单详情状态
  483. GoodsOrderItem::update(['status'=>9],['id'=>$item_info['id']]);
  484. StoreGoodsItem::stockChange($item_info['spec_id'],$item_info['goods_id'],$item_info['num']);
  485. }
  486. }
  487. $this->success('执行成功');
  488. }
  489. }