123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- <?php
- namespace App\Console\Commands\ContentManage;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\DB;
- use Modules\ContentManage\Services\Notice\NoticesService;
- class BookCopyrightAlert extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'ContentManage:BookCopyrightAlert';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '书籍版权快到期预警';
- private $alertDate;
- /**
- * Execute the console command.
- */
- public function handle(): void
- {
- $todayStr = date('Y-m-d');
- $alertDate = date_add(date_create($todayStr), date_interval_create_from_date_string('30 day'))->format('Y-m-d');
- $roleIds = DB::table('roles')
- ->whereIn('identify', ['admin', 'businessmaster', 'business'])
- ->where('deleted_at', '=', 0)
- ->select('id')
- ->get()->pluck('id')->unique();
- $users = DB::table('users')->join('user_has_roles', 'users.id', 'user_has_roles.user_id')
- ->whereIn('user_has_roles.role_id', $roleIds)
- ->where('users.deleted_at', '=', 0)
- ->where('users.status', '=', 1)
- ->select('id', 'username', 'email')
- ->get()->keyBy('id');
- DB::table('books')
- ->where('end_date', '<>', '')
- ->where('end_date', '<=', $alertDate)
- ->orderBy('id', 'desc')
- ->select('id','name', 'start_date', 'end_date', 'cp_name', 'cp_id')
- ->chunk(300, function ($books) use ($users, $todayStr){
- foreach ($books->chunk(30) as $iBooks) {
- $this->copyrightAlert($iBooks, $users, $todayStr);
- }
- });
- }
- /**
- * 快到期书籍,邮件通知,内容管理后台消息通知
- * @param $iBooks
- * @param $userCollect
- */
- private function copyrightAlert($iBooks, $userCollect, $todayStr) {
- $emailStr = '';
- $noticeStr = '';
- foreach ($iBooks as $book) {
- $leftDay = intval(date_diff(date_create($todayStr), date_create($book->end_date))->format("%r%a"));
- if($leftDay > 0) {
- $emailStr .= 'bid='.$book->id. "<br>";
- $emailStr .= '书名='.$book->name. "<br>";
- $emailStr .= '版权开始日期='.$book->start_date. "<br>";
- $emailStr .= '版权结束日期='.$book->end_date. "<br>";
- $emailStr .= '版权剩余天数='. $leftDay . "天<br>";
- $emailStr .= 'cp_name='.$book->cp_name. "<br>";
- $emailStr .= 'cp_id='.$book->cp_id. "<br>";
- $emailStr .= "==========================================<br>";
- } else {
- $noticeStr .= 'bid='.$book->id. "<br>";
- $noticeStr .= '书名='.$book->name. "<br>";
- $noticeStr .= '版权开始日期='.$book->start_date. "<br>";
- $noticeStr .= '版权结束日期='.$book->end_date. "<br>";
- $noticeStr .= 'cp_name='.$book->cp_name. "<br>";
- $noticeStr .= 'cp_id='.$book->cp_id. "<br>";
- $noticeStr .= "==========================================<br>";
- }
- }
- $users = [];
- foreach ($userCollect as $user) {
- $users[] = ['address' => $user->email, 'name' => $user->username];
- }
- $params = [
- 'subject' => sprintf('版权快到期预警[%s]', date('Y-m-d H:i:s')),
- 'body' => $emailStr
- ];
- sendEmail($users,$params);
- NoticesService::addNotice([
- 'title' => '书籍版权到期提醒',
- 'notice_type_id' => 2,
- 'type' => 1,
- 'is_popup' => 1,
- 'content' => $noticeStr
- ]);
- }
- }
|