diff --git a/app/Console/Commands/PushCourses.php b/app/Console/Commands/PushCourses.php new file mode 100755 index 0000000..e6fa606 --- /dev/null +++ b/app/Console/Commands/PushCourses.php @@ -0,0 +1,74 @@ +option('course_id'); + $courses = Course::where(function ($query) use ($course_id) { + if ($course_id) { + $query->where('id', $course_id); + } + })->whereHas('typeDetail', function ($query) { + $query->where('is_push', 1); + })->get(); + if ($courses->isEmpty()) { + $this->info('没有可推送的课程'); + return; + } + $YuanheRepository = new YuanheRepository(); + foreach ($courses as $course) { + // 所有报名审核成功的用户id + $userIds = $course->courseSigns()->where('status', 1)->pluck('user_id'); + $users = User::whereIn('id', $userIds)->whereNotNull('company_id')->get(); + foreach ($users as $user) { + $result = $YuanheRepository->pushCourses($course, $user); + if ($result) { + $this->info("推送成功:{$course->name}-{$user->name}"); + } else { + $this->info("推送失败:{$course->name}-{$user->name}"); + } + } + } + return $this->info('全部更新完成'); + } +} diff --git a/app/Http/Controllers/Admin/CourseController.php b/app/Http/Controllers/Admin/CourseController.php index 813af48..b1dd692 100755 --- a/app/Http/Controllers/Admin/CourseController.php +++ b/app/Http/Controllers/Admin/CourseController.php @@ -11,6 +11,7 @@ use App\Models\CustomForm; use App\Models\User; use App\Notifications\CourseContentNotify; use EasyWeChat\Factory; +use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Notification; use Illuminate\Support\Facades\Validator; @@ -238,6 +239,11 @@ class CourseController extends BaseController if ($model->status == 1 && $model->start_date) { CourseAppointmentTotal::addByCourse($model->id); } + // 如果添加了开课时间,就推送数据 + if ($model->start_date) { + // 调用命令行更新 + Artisan::call("push_courses --course_id={$model->id}"); + } // 记录日志 $this->saveLogs($original, $model); return $this->success($model); diff --git a/app/Models/User.php b/app/Models/User.php index 09c92b2..570d36d 100755 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -164,6 +164,11 @@ class User extends Authenticatable implements Auditable return $this->hasMany(Appointment::class, 'user_id', 'id'); } + public function company() + { + return $this->hasOne(Company::class, 'id', 'company_id'); + } + /** * 获取预约剩余次数 */ diff --git a/app/Repositories/YuanheRepository.php b/app/Repositories/YuanheRepository.php index 8a29e50..9da24e5 100755 --- a/app/Repositories/YuanheRepository.php +++ b/app/Repositories/YuanheRepository.php @@ -3,7 +3,9 @@ namespace App\Repositories; use App\Models\AppointmentConfig; +use App\Models\Course; use App\Models\ThirdAppointmentLog; +use App\Models\User; /** * 元禾控股 @@ -59,4 +61,37 @@ class YuanheRepository } } + /** + * 数据推送 + */ + public function pushCourses(Course $course, User $user) + { + if (empty($user->company)) { + return false; + } + $params = [ + 'classTeacher' => $course->teacher->name, + 'courseName' => $course->name, + 'description' => $user->company->businessScope, + 'enterpriseName' => $user->company->company_name, + 'creditCode' => $user->company->credit_code, + 'groupId' => '1030004', + 'openTime' => $course->start_date + ]; + $params = json_encode($params, JSON_UNESCAPED_UNICODE); + $url = $this->baseUrl . '/master-service/openapi/businessCollege/shareInfo/push'; + $header = $this->getHeader(); + try { + $result = httpCurl($url, 'POST', $params, $header); + $result = json_decode($result, true); + if ($result['code'] == 200) { + return true; + } else { + return false; + } + } catch (\Exception $e) { + return false; + } + } + } diff --git a/database/migrations/2025_07_18_102212_alert_course_types_table.php b/database/migrations/2025_07_18_102212_alert_course_types_table.php index 0b2c1ff..b707dfe 100644 --- a/database/migrations/2025_07_18_102212_alert_course_types_table.php +++ b/database/migrations/2025_07_18_102212_alert_course_types_table.php @@ -4,8 +4,7 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration -{ +return new class extends Migration { /** * Run the migrations. * @@ -16,6 +15,8 @@ return new class extends Migration Schema::table('course_types', function (Blueprint $table) { // 是否参与统计 $table->boolean('is_chart')->default(true)->comment('是否参与统计0否1是'); + // 是否推送 + $table->boolean('is_push')->default(false)->comment('是否推送0否1是'); }); }