123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <?php
- namespace app\common\model\lottery;
- use think\Model;
- /**
- * 用户抽奖机会模型
- */
- class LotteryUserChance extends Model
- {
- // 表名
- protected $name = 'shop_lottery_user_chance';
-
- // 开启自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- protected $deleteTime = false;
- // 追加属性
- protected $append = [
- 'get_detail_data'
- ];
- /**
- * 关联活动
- */
- public function activity()
- {
- return $this->belongsTo('LotteryActivity', 'activity_id');
- }
- /**
- * 关联用户
- */
- public function user()
- {
- return $this->belongsTo('app\common\model\User', 'user_id');
- }
- /**
- * 获取获得详情数据
- */
- public function getGetDetailDataAttr($value, $data)
- {
- return !empty($data['get_detail']) ? json_decode($data['get_detail'], true) : [];
- }
- /**
- * 设置获得详情
- */
- public function setGetDetailAttr($value)
- {
- return is_array($value) ? json_encode($value) : $value;
- }
- /**
- * 获取用户抽奖机会
- */
- public static function getUserChance($activityId, $userId)
- {
- return static::where('activity_id', $activityId)
- ->where('user_id', $userId)
- ->find();
- }
- /**
- * 增加抽奖机会
- */
- public static function addChance($activityId, $userId, $times = 1, $detail = [])
- {
- $chance = static::getUserChance($activityId, $userId);
-
- if (!$chance) {
- // 创建新记录
- $data = [
- 'activity_id' => $activityId,
- 'user_id' => $userId,
- 'total_chances' => $times,
- 'used_chances' => 0,
- 'remain_chances' => $times,
- 'last_get_time' => time(),
- 'get_detail' => json_encode([$detail])
- ];
- return static::create($data);
- } else {
- // 更新现有记录
- $chance->total_chances += $times;
- $chance->remain_chances += $times;
- $chance->last_get_time = time();
-
- // 更新获得详情
- $getDetail = $chance->get_detail_data;
- $getDetail[] = $detail;
- $chance->get_detail = json_encode($getDetail);
-
- return $chance->save() ? $chance : false;
- }
- }
- /**
- * 使用抽奖机会
- */
- public function useChance($times = 1)
- {
- if ($this->remain_chances < $times) {
- return false;
- }
-
- $this->used_chances += $times;
- $this->remain_chances -= $times;
- $this->last_use_time = time();
-
- return $this->save();
- }
- /**
- * 检查是否有剩余机会
- */
- public function hasChance($times = 1)
- {
- return $this->remain_chances >= $times;
- }
- /**
- * 重置抽奖机会(用于测试或特殊情况)
- */
- public function resetChance()
- {
- $this->used_chances = 0;
- $this->remain_chances = $this->total_chances;
- return $this->save();
- }
- /**
- * 获取活动总参与人数
- */
- public static function getActivityParticipants($activityId)
- {
- return static::where('activity_id', $activityId)->count();
- }
- /**
- * 获取用户在多个活动中的机会统计
- */
- public static function getUserChancesStats($userId, $activityIds = [])
- {
- $query = static::where('user_id', $userId);
-
- if (!empty($activityIds)) {
- $query->where('activity_id', 'in', $activityIds);
- }
-
- return $query->field([
- 'activity_id',
- 'total_chances',
- 'used_chances',
- 'remain_chances'
- ])
- ->select();
- }
- /**
- * 批量创建用户机会(用于活动启动时)
- */
- public static function batchCreateChances($activityId, $userIds, $times = 1)
- {
- $data = [];
- $now = time();
-
- foreach ($userIds as $userId) {
- $data[] = [
- 'activity_id' => $activityId,
- 'user_id' => $userId,
- 'total_chances' => $times,
- 'used_chances' => 0,
- 'remain_chances' => $times,
- 'last_get_time' => $now,
- 'createtime' => $now,
- 'updatetime' => $now
- ];
- }
-
- return static::insertAll($data);
- }
- }
|