BookCopyrightAlert.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace App\Console\Commands\ContentManage;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\DB;
  5. use Modules\ContentManage\Services\Notice\NoticesService;
  6. class BookCopyrightAlert extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'ContentManage:BookCopyrightAlert';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = '书籍版权快到期预警';
  20. private $alertDate;
  21. /**
  22. * Execute the console command.
  23. */
  24. public function handle(): void
  25. {
  26. $todayStr = date('Y-m-d');
  27. $alertDate = date_add(date_create($todayStr), date_interval_create_from_date_string('30 day'))->format('Y-m-d');
  28. $roleIds = DB::table('roles')
  29. ->whereIn('identify', ['admin', 'businessmaster', 'business'])
  30. ->where('deleted_at', '=', 0)
  31. ->select('id')
  32. ->get()->pluck('id')->unique();
  33. $users = DB::table('users')->join('user_has_roles', 'users.id', 'user_has_roles.user_id')
  34. ->whereIn('user_has_roles.role_id', $roleIds)
  35. ->where('users.deleted_at', '=', 0)
  36. ->where('users.status', '=', 1)
  37. ->select('id', 'username', 'email')
  38. ->get()->keyBy('id');
  39. DB::table('books')
  40. ->where('end_date', '<>', '')
  41. ->where('end_date', '<=', $alertDate)
  42. ->orderBy('id', 'desc')
  43. ->select('id','name', 'start_date', 'end_date', 'cp_name', 'cp_id')
  44. ->chunk(300, function ($books) use ($users, $todayStr){
  45. foreach ($books->chunk(30) as $iBooks) {
  46. $this->copyrightAlert($iBooks, $users, $todayStr);
  47. }
  48. });
  49. }
  50. /**
  51. * 快到期书籍,邮件通知,内容管理后台消息通知
  52. * @param $iBooks
  53. * @param $userCollect
  54. */
  55. private function copyrightAlert($iBooks, $userCollect, $todayStr) {
  56. $emailStr = '';
  57. $noticeStr = '';
  58. foreach ($iBooks as $book) {
  59. $leftDay = intval(date_diff(date_create($todayStr), date_create($book->end_date))->format("%r%a"));
  60. if($leftDay > 0) {
  61. $emailStr .= 'bid='.$book->id. "<br>";
  62. $emailStr .= '书名='.$book->name. "<br>";
  63. $emailStr .= '版权开始日期='.$book->start_date. "<br>";
  64. $emailStr .= '版权结束日期='.$book->end_date. "<br>";
  65. $emailStr .= '版权剩余天数='. $leftDay . "天<br>";
  66. $emailStr .= 'cp_name='.$book->cp_name. "<br>";
  67. $emailStr .= 'cp_id='.$book->cp_id. "<br>";
  68. $emailStr .= "==========================================<br>";
  69. } else {
  70. $noticeStr .= 'bid='.$book->id. "<br>";
  71. $noticeStr .= '书名='.$book->name. "<br>";
  72. $noticeStr .= '版权开始日期='.$book->start_date. "<br>";
  73. $noticeStr .= '版权结束日期='.$book->end_date. "<br>";
  74. $noticeStr .= 'cp_name='.$book->cp_name. "<br>";
  75. $noticeStr .= 'cp_id='.$book->cp_id. "<br>";
  76. $noticeStr .= "==========================================<br>";
  77. }
  78. }
  79. $users = [];
  80. foreach ($userCollect as $user) {
  81. $users[] = ['address' => $user->email, 'name' => $user->username];
  82. }
  83. $params = [
  84. 'subject' => sprintf('版权快到期预警[%s]', date('Y-m-d H:i:s')),
  85. 'body' => $emailStr
  86. ];
  87. sendEmail($users,$params);
  88. NoticesService::addNotice([
  89. 'title' => '书籍版权到期提醒',
  90. 'notice_type_id' => 2,
  91. 'type' => 1,
  92. 'is_popup' => 1,
  93. 'content' => $noticeStr
  94. ]);
  95. }
  96. }