| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- <?php
- namespace App\Module\Activity\Models;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use UCore\ModelCore;
- /**
- * 活动用户数据
- *
- * field start
- * @property int $id
- * @property int $user_id 用户ID
- * @property int $activity_id 活动ID
- * @property int $progress 活动进度
- * @property object|array $progress_data 详细进度数据
- * @property string $last_update
- * @property \Carbon\Carbon $created_at
- * @property \Carbon\Carbon $updated_at
- * field end
- */
- class UserActivityData extends ModelCore
- {
- /**
- * 与模型关联的表名
- *
- * @var string
- */
- protected $table = 'activity_user__data';
- /**
- * 可批量赋值的属性
- *
- * @var array
- */
- // attrlist start
- protected $fillable = [
- 'id',
- 'user_id',
- 'activity_id',
- 'progress',
- 'progress_data',
- 'last_update',
- ];
- // attrlist end
- /**
- * 应该被转换为原生类型的属性
- *
- * @var array
- */
- protected $casts = [
- 'user_id' => 'integer',
- 'activity_id' => 'integer',
- 'progress' => 'integer',
- 'progress_data' => 'json',
- 'last_update' => 'datetime',
- ];
- /**
- * 获取关联的活动
- *
- * @return BelongsTo
- */
- public function activity(): BelongsTo
- {
- return $this->belongsTo(ActivityConfig::class, 'activity_id', 'id');
- }
- /**
- * 获取关联的参与记录
- *
- * @return BelongsTo
- */
- public function participation(): BelongsTo
- {
- return $this->belongsTo(ActivityParticipation::class, ['user_id', 'activity_id'], ['user_id', 'activity_id']);
- }
- /**
- * 更新进度
- *
- * @param int $progress 新的进度值
- * @param array|null $progressData 详细进度数据
- * @return bool
- */
- public function updateProgress(int $progress, ?array $progressData = null): bool
- {
- $this->progress = $progress;
- if ($progressData !== null) {
- $this->progress_data = $progressData;
- }
- $this->last_update = now();
- return $this->save();
- }
- /**
- * 增加进度
- *
- * @param int $increment 增加的进度值
- * @param array|null $progressData 详细进度数据
- * @return bool
- */
- public function incrementProgress(int $increment, ?array $progressData = null): bool
- {
- return $this->updateProgress($this->progress + $increment, $progressData);
- }
- }
|