123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- <?php
- namespace app\common\model\lottery;
- use think\Model;
- use traits\model\SoftDelete;
- use app\common\Enum\LotteryEnum;
- /**
- * 抽奖活动模型
- */
- class LotteryActivity extends Model
- {
- use SoftDelete;
- // 表名
- protected $table = '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'
- ];
- /**
- * 关联奖品
- */
- 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)
- {
- return LotteryEnum::getActivityStatusText($data['status']);
- }
- /**
- * 获取开奖方式文本
- */
- public function getLotteryTypeTextAttr($value, $data)
- {
- return LotteryEnum::getLotteryTypeText($data['lottery_type']);
- }
- /**
- * 获取用户群体类型文本
- */
- // public function getUserLimitTypeTextAttr($value, $data)
- // {
- // $value = $value ?: ($data['user_limit_type'] ?? '');
- // $types = LotteryEnum::getUserLimitTypeMap();
- // return $types[$value];
-
- // }
- /**
- * 获取用户限制值(自动解析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;
- }
- }
|