123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- <?php
- namespace app\common\model\lottery;
- use think\Model;
- use traits\model\SoftDelete;
- /**
- * 抽奖活动模型
- */
- class LotteryActivity extends Model
- {
- use SoftDelete;
- // 表名
- protected $name = 'shop_lottery_activity';
-
- // 开启自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- protected $deleteTime = 'deletetime';
- // 追加属性
- protected $append = [
- 'status_text',
- 'lottery_type_text',
- 'user_limit_type_text'
- ];
- // 状态常量
- const STATUS_DRAFT = 0; // 草稿
- const STATUS_RUNNING = 1; // 进行中
- const STATUS_ENDED = 2; // 已结束
- const STATUS_PAUSED = 3; // 已暂停
- // 开奖方式常量
- const LOTTERY_TYPE_INSTANT = 1; // 即抽即中
- const LOTTERY_TYPE_TIME = 2; // 按时间开奖
- const LOTTERY_TYPE_PEOPLE = 3; // 按人数开奖
- // 用户群体类型常量
- const USER_LIMIT_ALL = 1; // 全部会员
- const USER_LIMIT_LEVEL = 2; // 会员等级
- const USER_LIMIT_TAG = 3; // 会员标签
- /**
- * 关联奖品
- */
- public function prizes()
- {
- return $this->hasMany('LotteryPrize', 'activity_id');
- }
- /**
- * 关联参与条件
- */
- public function conditions()
- {
- return $this->hasMany('LotteryCondition', 'activity_id');
- }
- /**
- * 关联抽奖记录
- */
- public function drawRecords()
- {
- return $this->hasMany('LotteryDrawRecord', 'activity_id');
- }
- /**
- * 关联中奖记录
- */
- public function winRecords()
- {
- return $this->hasMany('LotteryWinRecord', 'activity_id');
- }
- /**
- * 关联用户机会
- */
- public function userChances()
- {
- return $this->hasMany('LotteryUserChance', 'activity_id');
- }
- /**
- * 获取状态文本
- */
- public function getStatusTextAttr($value, $data)
- {
- $status = [
- self::STATUS_DRAFT => '草稿',
- self::STATUS_RUNNING => '进行中',
- self::STATUS_ENDED => '已结束',
- self::STATUS_PAUSED => '已暂停'
- ];
- return isset($status[$data['status']]) ? $status[$data['status']] : '未知';
- }
- /**
- * 获取开奖方式文本
- */
- public function getLotteryTypeTextAttr($value, $data)
- {
- $types = [
- self::LOTTERY_TYPE_INSTANT => '即抽即中',
- self::LOTTERY_TYPE_TIME => '按时间开奖',
- self::LOTTERY_TYPE_PEOPLE => '按人数开奖'
- ];
- return isset($types[$data['lottery_type']]) ? $types[$data['lottery_type']] : '未知';
- }
- /**
- * 获取用户群体类型文本
- */
- public function getUserLimitTypeTextAttr($value, $data)
- {
- $types = [
- self::USER_LIMIT_ALL => '全部会员',
- self::USER_LIMIT_LEVEL => '会员等级',
- self::USER_LIMIT_TAG => '会员标签'
- ];
- return isset($types[$data['user_limit_type']]) ? $types[$data['user_limit_type']] : '未知';
- }
- /**
- * 获取用户限制值(自动解析JSON)
- */
- public function getUserLimitValueAttr($value, $data)
- {
- return $value ? json_decode($value, true) : [];
- }
- /**
- * 设置用户限制值(自动转换JSON)
- */
- public function setUserLimitValueAttr($value)
- {
- return is_array($value) ? json_encode($value) : $value;
- }
- /**
- * 检查活动是否正在进行
- */
- public function isRunning()
- {
- $now = time();
- return $this->status == self::STATUS_RUNNING
- && $this->start_time <= $now
- && $this->end_time >= $now;
- }
- /**
- * 检查活动是否已结束
- */
- public function isEnded()
- {
- $now = time();
- return $this->status == self::STATUS_ENDED
- || $this->end_time < $now;
- }
- /**
- * 检查是否在抽奖时间内
- */
- public function isInDrawTime()
- {
- if (!$this->draw_time_enable) {
- return true;
- }
-
- $currentTime = date('H:i');
- return $currentTime >= $this->draw_time_start
- && $currentTime <= $this->draw_time_end;
- }
- /**
- * 获取正在进行的活动
- */
- public static function getRunningActivities()
- {
- $now = time();
- return static::where('status', self::STATUS_RUNNING)
- ->where('start_time', '<=', $now)
- ->where('end_time', '>=', $now)
- ->select();
- }
- }
|