UserChance.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace app\admin\model\lottery;
  3. use think\Model;
  4. class UserChance extends Model
  5. {
  6. // 表名
  7. protected $table = 'shop_lottery_user_chance';
  8. // 自动写入时间戳字段
  9. protected $autoWriteTimestamp = 'integer';
  10. // 定义时间戳字段名
  11. protected $createTime = 'createtime';
  12. protected $updateTime = 'updatetime';
  13. // 追加属性
  14. protected $append = [
  15. 'source_type_text',
  16. 'status_text',
  17. 'expire_time_text',
  18. 'remain_count'
  19. ];
  20. /**
  21. * 来源类型列表
  22. */
  23. public function getSourceTypeList()
  24. {
  25. return [
  26. '1' => __('购买商品'),
  27. '2' => __('订单消费'),
  28. '3' => __('充值'),
  29. '4' => __('累计消费'),
  30. '5' => __('管理员发放'),
  31. '6' => __('签到奖励'),
  32. '7' => __('邀请奖励'),
  33. '8' => __('活动奖励')
  34. ];
  35. }
  36. /**
  37. * 状态列表
  38. */
  39. public function getStatusList()
  40. {
  41. return [
  42. '0' => __('已失效'),
  43. '1' => __('有效')
  44. ];
  45. }
  46. // 获取器
  47. public function getSourceTypeTextAttr($value, $data)
  48. {
  49. $value = $value ? $value : (isset($data['source_type']) ? $data['source_type'] : '');
  50. $list = $this->getSourceTypeList();
  51. return isset($list[$value]) ? $list[$value] : '';
  52. }
  53. public function getStatusTextAttr($value, $data)
  54. {
  55. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  56. $list = $this->getStatusList();
  57. return isset($list[$value]) ? $list[$value] : '';
  58. }
  59. public function getExpireTimeTextAttr($value, $data)
  60. {
  61. $value = $value ? $value : (isset($data['expire_time']) ? $data['expire_time'] : '');
  62. if ($value && is_numeric($value)) {
  63. return date("Y-m-d H:i:s", $value);
  64. }
  65. return $value ?: '永不过期';
  66. }
  67. public function getRemainCountAttr($value, $data)
  68. {
  69. $chance_count = isset($data['chance_count']) ? $data['chance_count'] : 0;
  70. $used_count = isset($data['used_count']) ? $data['used_count'] : 0;
  71. return max(0, $chance_count - $used_count);
  72. }
  73. // 修改器
  74. protected function setExpireTimeAttr($value)
  75. {
  76. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  77. }
  78. // 关联关系
  79. public function activity()
  80. {
  81. return $this->belongsTo('Activity', 'activity_id', 'id');
  82. }
  83. public function user()
  84. {
  85. return $this->belongsTo('app\\common\\model\\User', 'user_id', 'id');
  86. }
  87. /**
  88. * 检查机会是否有效
  89. */
  90. public function isValid()
  91. {
  92. // 检查状态
  93. if ($this->status != 1) {
  94. return false;
  95. }
  96. // 检查是否过期
  97. if ($this->expire_time && $this->expire_time < time()) {
  98. return false;
  99. }
  100. // 检查是否还有剩余次数
  101. if ($this->chance_count <= $this->used_count) {
  102. return false;
  103. }
  104. return true;
  105. }
  106. /**
  107. * 使用抽奖机会
  108. */
  109. public function useChance()
  110. {
  111. if (!$this->isValid()) {
  112. return false;
  113. }
  114. $this->used_count += 1;
  115. return $this->save();
  116. }
  117. /**
  118. * 获取用户在指定活动的有效抽奖机会
  119. */
  120. public static function getUserValidChances($user_id, $activity_id)
  121. {
  122. return self::where([
  123. 'user_id' => $user_id,
  124. 'activity_id' => $activity_id,
  125. 'status' => 1
  126. ])->where(function($query) {
  127. $query->where('expire_time', 0)
  128. ->whereOr('expire_time', '>', time());
  129. })->where('chance_count', '>', 'used_count')->sum('chance_count - used_count');
  130. }
  131. /**
  132. * 获取用户总的剩余抽奖次数
  133. */
  134. public static function getUserRemainCount($user_id, $activity_id)
  135. {
  136. $list = self::where([
  137. 'user_id' => $user_id,
  138. 'activity_id' => $activity_id,
  139. 'status' => 1
  140. ])->where(function($query) {
  141. $query->where('expire_time', 0)
  142. ->whereOr('expire_time', '>', time());
  143. })->select();
  144. $total = 0;
  145. foreach ($list as $item) {
  146. $remain = $item->chance_count - $item->used_count;
  147. if ($remain > 0) {
  148. $total += $remain;
  149. }
  150. }
  151. return $total;
  152. }
  153. }