LotteryUserChance.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace app\common\model\lottery;
  3. use think\Model;
  4. /**
  5. * 用户抽奖机会模型
  6. */
  7. class LotteryUserChance extends Model
  8. {
  9. // 表名
  10. protected $name = 'shop_lottery_user_chance';
  11. // 开启自动写入时间戳字段
  12. protected $autoWriteTimestamp = 'int';
  13. // 定义时间戳字段名
  14. protected $createTime = 'createtime';
  15. protected $updateTime = 'updatetime';
  16. protected $deleteTime = false;
  17. // 追加属性
  18. protected $append = [
  19. 'get_detail_data'
  20. ];
  21. /**
  22. * 关联活动
  23. */
  24. public function activity()
  25. {
  26. return $this->belongsTo('LotteryActivity', 'activity_id');
  27. }
  28. /**
  29. * 关联用户
  30. */
  31. public function user()
  32. {
  33. return $this->belongsTo('app\common\model\User', 'user_id');
  34. }
  35. /**
  36. * 获取获得详情数据
  37. */
  38. public function getGetDetailDataAttr($value, $data)
  39. {
  40. return !empty($data['get_detail']) ? json_decode($data['get_detail'], true) : [];
  41. }
  42. /**
  43. * 设置获得详情
  44. */
  45. public function setGetDetailAttr($value)
  46. {
  47. return is_array($value) ? json_encode($value) : $value;
  48. }
  49. /**
  50. * 获取用户抽奖机会
  51. */
  52. public static function getUserChance($activityId, $userId)
  53. {
  54. return static::where('activity_id', $activityId)
  55. ->where('user_id', $userId)
  56. ->find();
  57. }
  58. /**
  59. * 增加抽奖机会
  60. */
  61. public static function addChance($activityId, $userId, $times = 1, $detail = [])
  62. {
  63. $chance = static::getUserChance($activityId, $userId);
  64. if (!$chance) {
  65. // 创建新记录
  66. $data = [
  67. 'activity_id' => $activityId,
  68. 'user_id' => $userId,
  69. 'total_chances' => $times,
  70. 'used_chances' => 0,
  71. 'remain_chances' => $times,
  72. 'last_get_time' => time(),
  73. 'get_detail' => json_encode([$detail])
  74. ];
  75. return static::create($data);
  76. } else {
  77. // 更新现有记录
  78. $chance->total_chances += $times;
  79. $chance->remain_chances += $times;
  80. $chance->last_get_time = time();
  81. // 更新获得详情
  82. $getDetail = $chance->get_detail_data;
  83. $getDetail[] = $detail;
  84. $chance->get_detail = json_encode($getDetail);
  85. return $chance->save() ? $chance : false;
  86. }
  87. }
  88. /**
  89. * 使用抽奖机会
  90. */
  91. public function useChance($times = 1)
  92. {
  93. if ($this->remain_chances < $times) {
  94. return false;
  95. }
  96. $this->used_chances += $times;
  97. $this->remain_chances -= $times;
  98. $this->last_use_time = time();
  99. return $this->save();
  100. }
  101. /**
  102. * 检查是否有剩余机会
  103. */
  104. public function hasChance($times = 1)
  105. {
  106. return $this->remain_chances >= $times;
  107. }
  108. /**
  109. * 重置抽奖机会(用于测试或特殊情况)
  110. */
  111. public function resetChance()
  112. {
  113. $this->used_chances = 0;
  114. $this->remain_chances = $this->total_chances;
  115. return $this->save();
  116. }
  117. /**
  118. * 获取活动总参与人数
  119. */
  120. public static function getActivityParticipants($activityId)
  121. {
  122. return static::where('activity_id', $activityId)->count();
  123. }
  124. /**
  125. * 获取用户在多个活动中的机会统计
  126. */
  127. public static function getUserChancesStats($userId, $activityIds = [])
  128. {
  129. $query = static::where('user_id', $userId);
  130. if (!empty($activityIds)) {
  131. $query->where('activity_id', 'in', $activityIds);
  132. }
  133. return $query->field([
  134. 'activity_id',
  135. 'total_chances',
  136. 'used_chances',
  137. 'remain_chances'
  138. ])
  139. ->select();
  140. }
  141. /**
  142. * 批量创建用户机会(用于活动启动时)
  143. */
  144. public static function batchCreateChances($activityId, $userIds, $times = 1)
  145. {
  146. $data = [];
  147. $now = time();
  148. foreach ($userIds as $userId) {
  149. $data[] = [
  150. 'activity_id' => $activityId,
  151. 'user_id' => $userId,
  152. 'total_chances' => $times,
  153. 'used_chances' => 0,
  154. 'remain_chances' => $times,
  155. 'last_get_time' => $now,
  156. 'createtime' => $now,
  157. 'updatetime' => $now
  158. ];
  159. }
  160. return static::insertAll($data);
  161. }
  162. }