LotteryActivity.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace app\common\model\lottery;
  3. use think\Model;
  4. use traits\model\SoftDelete;
  5. /**
  6. * 抽奖活动模型
  7. */
  8. class LotteryActivity extends Model
  9. {
  10. use SoftDelete;
  11. // 表名
  12. protected $name = 'shop_lottery_activity';
  13. // 开启自动写入时间戳字段
  14. protected $autoWriteTimestamp = 'int';
  15. // 定义时间戳字段名
  16. protected $createTime = 'createtime';
  17. protected $updateTime = 'updatetime';
  18. protected $deleteTime = 'deletetime';
  19. // 追加属性
  20. protected $append = [
  21. 'status_text',
  22. 'lottery_type_text',
  23. 'user_limit_type_text'
  24. ];
  25. // 状态常量
  26. const STATUS_DRAFT = 0; // 草稿
  27. const STATUS_RUNNING = 1; // 进行中
  28. const STATUS_ENDED = 2; // 已结束
  29. const STATUS_PAUSED = 3; // 已暂停
  30. // 开奖方式常量
  31. const LOTTERY_TYPE_INSTANT = 1; // 即抽即中
  32. const LOTTERY_TYPE_TIME = 2; // 按时间开奖
  33. const LOTTERY_TYPE_PEOPLE = 3; // 按人数开奖
  34. // 用户群体类型常量
  35. const USER_LIMIT_ALL = 1; // 全部会员
  36. const USER_LIMIT_LEVEL = 2; // 会员等级
  37. const USER_LIMIT_TAG = 3; // 会员标签
  38. /**
  39. * 关联奖品
  40. */
  41. public function prizes()
  42. {
  43. return $this->hasMany('LotteryPrize', 'activity_id');
  44. }
  45. /**
  46. * 关联参与条件
  47. */
  48. public function conditions()
  49. {
  50. return $this->hasMany('LotteryCondition', 'activity_id');
  51. }
  52. /**
  53. * 关联抽奖记录
  54. */
  55. public function drawRecords()
  56. {
  57. return $this->hasMany('LotteryDrawRecord', 'activity_id');
  58. }
  59. /**
  60. * 关联中奖记录
  61. */
  62. public function winRecords()
  63. {
  64. return $this->hasMany('LotteryWinRecord', 'activity_id');
  65. }
  66. /**
  67. * 关联用户机会
  68. */
  69. public function userChances()
  70. {
  71. return $this->hasMany('LotteryUserChance', 'activity_id');
  72. }
  73. /**
  74. * 获取状态文本
  75. */
  76. public function getStatusTextAttr($value, $data)
  77. {
  78. $status = [
  79. self::STATUS_DRAFT => '草稿',
  80. self::STATUS_RUNNING => '进行中',
  81. self::STATUS_ENDED => '已结束',
  82. self::STATUS_PAUSED => '已暂停'
  83. ];
  84. return isset($status[$data['status']]) ? $status[$data['status']] : '未知';
  85. }
  86. /**
  87. * 获取开奖方式文本
  88. */
  89. public function getLotteryTypeTextAttr($value, $data)
  90. {
  91. $types = [
  92. self::LOTTERY_TYPE_INSTANT => '即抽即中',
  93. self::LOTTERY_TYPE_TIME => '按时间开奖',
  94. self::LOTTERY_TYPE_PEOPLE => '按人数开奖'
  95. ];
  96. return isset($types[$data['lottery_type']]) ? $types[$data['lottery_type']] : '未知';
  97. }
  98. /**
  99. * 获取用户群体类型文本
  100. */
  101. public function getUserLimitTypeTextAttr($value, $data)
  102. {
  103. $types = [
  104. self::USER_LIMIT_ALL => '全部会员',
  105. self::USER_LIMIT_LEVEL => '会员等级',
  106. self::USER_LIMIT_TAG => '会员标签'
  107. ];
  108. return isset($types[$data['user_limit_type']]) ? $types[$data['user_limit_type']] : '未知';
  109. }
  110. /**
  111. * 获取用户限制值(自动解析JSON)
  112. */
  113. public function getUserLimitValueAttr($value, $data)
  114. {
  115. return $value ? json_decode($value, true) : [];
  116. }
  117. /**
  118. * 设置用户限制值(自动转换JSON)
  119. */
  120. public function setUserLimitValueAttr($value)
  121. {
  122. return is_array($value) ? json_encode($value) : $value;
  123. }
  124. /**
  125. * 检查活动是否正在进行
  126. */
  127. public function isRunning()
  128. {
  129. $now = time();
  130. return $this->status == self::STATUS_RUNNING
  131. && $this->start_time <= $now
  132. && $this->end_time >= $now;
  133. }
  134. /**
  135. * 检查活动是否已结束
  136. */
  137. public function isEnded()
  138. {
  139. $now = time();
  140. return $this->status == self::STATUS_ENDED
  141. || $this->end_time < $now;
  142. }
  143. /**
  144. * 检查是否在抽奖时间内
  145. */
  146. public function isInDrawTime()
  147. {
  148. if (!$this->draw_time_enable) {
  149. return true;
  150. }
  151. $currentTime = date('H:i');
  152. return $currentTime >= $this->draw_time_start
  153. && $currentTime <= $this->draw_time_end;
  154. }
  155. /**
  156. * 获取正在进行的活动
  157. */
  158. public static function getRunningActivities()
  159. {
  160. $now = time();
  161. return static::where('status', self::STATUS_RUNNING)
  162. ->where('start_time', '<=', $now)
  163. ->where('end_time', '>=', $now)
  164. ->select();
  165. }
  166. }