123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- <?php
- namespace app\common\model\lottery;
- use think\Model;
- /**
- * 抽奖记录模型
- */
- class LotteryDrawRecord extends Model
- {
- // 表名
- protected $name = 'shop_lottery_draw_record';
-
- // 开启自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = false;
- protected $deleteTime = false;
- // 追加属性
- protected $append = [
- 'trigger_type_text',
- 'win_info_data'
- ];
- // 触发类型常量
- const TRIGGER_GOODS = 1; // 购买商品
- const TRIGGER_ORDER = 2; // 订单消费
- const TRIGGER_RECHARGE = 3; // 充值
- const TRIGGER_ACCUMULATE = 4; // 累计消费
- /**
- * 关联活动
- */
- public function activity()
- {
- return $this->belongsTo('LotteryActivity', 'activity_id');
- }
- /**
- * 关联用户
- */
- public function user()
- {
- return $this->belongsTo('app\common\model\User', 'user_id');
- }
- /**
- * 关联奖品
- */
- public function prize()
- {
- return $this->belongsTo('LotteryPrize', 'prize_id');
- }
- /**
- * 关联中奖记录
- */
- public function winRecord()
- {
- return $this->hasOne('LotteryWinRecord', 'draw_record_id');
- }
- /**
- * 关联订单
- */
- public function order()
- {
- return $this->belongsTo('app\common\model\Order', 'trigger_order_id');
- }
- /**
- * 获取触发类型文本
- */
- public function getTriggerTypeTextAttr($value, $data)
- {
- $types = [
- self::TRIGGER_GOODS => '购买商品',
- self::TRIGGER_ORDER => '订单消费',
- self::TRIGGER_RECHARGE => '充值',
- self::TRIGGER_ACCUMULATE => '累计消费'
- ];
- return isset($types[$data['trigger_type']]) ? $types[$data['trigger_type']] : '未知';
- }
- /**
- * 获取中奖信息数据
- */
- public function getWinInfoDataAttr($value, $data)
- {
- return !empty($data['win_info']) ? json_decode($data['win_info'], true) : [];
- }
- /**
- * 设置中奖信息
- */
- public function setWinInfoAttr($value)
- {
- return is_array($value) ? json_encode($value) : $value;
- }
- /**
- * 创建抽奖记录
- */
- public static function createRecord($activityId, $userId, $prizeId, $isWin, $triggerType, $triggerOrderId = null, $triggerAmount = null, $winInfo = [])
- {
- $data = [
- 'activity_id' => $activityId,
- 'user_id' => $userId,
- 'prize_id' => $prizeId,
- 'is_win' => $isWin ? 1 : 0,
- 'trigger_type' => $triggerType,
- 'trigger_order_id' => $triggerOrderId,
- 'trigger_amount' => $triggerAmount,
- 'win_info' => $winInfo ? json_encode($winInfo) : '',
- 'draw_ip' => request()->ip(),
- 'draw_time' => time(),
- 'device_info' => request()->header('user-agent', '')
- ];
-
- return static::create($data);
- }
- /**
- * 获取用户抽奖次数
- */
- public static function getUserDrawCount($activityId, $userId, $timeRange = null)
- {
- $query = static::where('activity_id', $activityId)
- ->where('user_id', $userId);
-
- if ($timeRange) {
- if (isset($timeRange['start'])) {
- $query->where('draw_time', '>=', $timeRange['start']);
- }
- if (isset($timeRange['end'])) {
- $query->where('draw_time', '<=', $timeRange['end']);
- }
- }
-
- return $query->count();
- }
- /**
- * 获取用户中奖次数
- */
- public static function getUserWinCount($activityId, $userId, $timeRange = null)
- {
- $query = static::where('activity_id', $activityId)
- ->where('user_id', $userId)
- ->where('is_win', 1);
-
- if ($timeRange) {
- if (isset($timeRange['start'])) {
- $query->where('draw_time', '>=', $timeRange['start']);
- }
- if (isset($timeRange['end'])) {
- $query->where('draw_time', '<=', $timeRange['end']);
- }
- }
-
- return $query->count();
- }
- /**
- * 获取活动抽奖统计
- */
- public static function getActivityStats($activityId, $timeRange = null)
- {
- $query = static::where('activity_id', $activityId);
-
- if ($timeRange) {
- if (isset($timeRange['start'])) {
- $query->where('draw_time', '>=', $timeRange['start']);
- }
- if (isset($timeRange['end'])) {
- $query->where('draw_time', '<=', $timeRange['end']);
- }
- }
-
- return [
- 'total_draw' => $query->count(),
- 'total_win' => $query->where('is_win', 1)->count(),
- 'total_people' => $query->distinct('user_id')->count()
- ];
- }
- }
|