123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- <?php
- namespace app\common\model\lottery;
- use think\Model;
- /**
- * 中奖记录模型
- */
- class LotteryWinRecord extends Model
- {
- // 表名
- protected $name = 'shop_lottery_win_record';
-
- // 开启自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- protected $deleteTime = false;
- // 追加属性
- protected $append = [
- 'deliver_status_text',
- 'prize_value_data',
- 'deliver_info_data'
- ];
- // 发放状态常量
- const DELIVER_STATUS_PENDING = 0; // 待发放
- const DELIVER_STATUS_SUCCESS = 1; // 已发放
- const DELIVER_STATUS_FAILED = 2; // 发放失败
- const DELIVER_STATUS_CANCELLED = 3; // 已取消
- /**
- * 关联抽奖记录
- */
- public function drawRecord()
- {
- return $this->belongsTo('LotteryDrawRecord', 'draw_record_id');
- }
- /**
- * 关联活动
- */
- 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 getDeliverStatusTextAttr($value, $data)
- {
- $status = [
- self::DELIVER_STATUS_PENDING => '待发放',
- self::DELIVER_STATUS_SUCCESS => '已发放',
- self::DELIVER_STATUS_FAILED => '发放失败',
- self::DELIVER_STATUS_CANCELLED => '已取消'
- ];
- return isset($status[$data['deliver_status']]) ? $status[$data['deliver_status']] : '未知';
- }
- /**
- * 获取奖品信息数据
- */
- public function getPrizeValueDataAttr($value, $data)
- {
- return !empty($data['prize_value']) ? json_decode($data['prize_value'], true) : [];
- }
- /**
- * 设置奖品信息
- */
- public function setPrizeValueAttr($value)
- {
- return is_array($value) ? json_encode($value) : $value;
- }
- /**
- * 获取发放信息数据
- */
- public function getDeliverInfoDataAttr($value, $data)
- {
- return !empty($data['deliver_info']) ? json_decode($data['deliver_info'], true) : [];
- }
- /**
- * 设置发放信息
- */
- public function setDeliverInfoAttr($value)
- {
- return is_array($value) ? json_encode($value) : $value;
- }
- /**
- * 创建中奖记录
- */
- public static function createWinRecord($drawRecordId, $activityId, $userId, $prizeId, $prizeName, $prizeType, $prizeValue = [])
- {
- $data = [
- 'draw_record_id' => $drawRecordId,
- 'activity_id' => $activityId,
- 'user_id' => $userId,
- 'prize_id' => $prizeId,
- 'prize_name' => $prizeName,
- 'prize_type' => $prizeType,
- 'prize_value' => is_array($prizeValue) ? json_encode($prizeValue) : $prizeValue,
- 'deliver_status' => self::DELIVER_STATUS_PENDING
- ];
-
- return static::create($data);
- }
- /**
- * 标记为发放成功
- */
- public function markAsDelivered($deliverInfo = [])
- {
- $this->deliver_status = self::DELIVER_STATUS_SUCCESS;
- $this->deliver_time = time();
- if ($deliverInfo) {
- $this->deliver_info = json_encode($deliverInfo);
- }
- return $this->save();
- }
- /**
- * 标记为发放失败
- */
- public function markAsFailed($reason = '')
- {
- $this->deliver_status = self::DELIVER_STATUS_FAILED;
- $this->fail_reason = $reason;
- return $this->save();
- }
- /**
- * 设置收货地址
- */
- public function setDeliveryAddress($name, $mobile, $address)
- {
- $this->receiver_name = $name;
- $this->receiver_mobile = $mobile;
- $this->receiver_address = $address;
- return $this->save();
- }
- /**
- * 设置快递信息
- */
- public function setExpressInfo($company, $number)
- {
- $this->express_company = $company;
- $this->express_number = $number;
- return $this->save();
- }
- /**
- * 设置兑换码
- */
- public function setExchangeCode($code)
- {
- $this->exchange_code = $code;
- return $this->save();
- }
- /**
- * 标记兑换码已使用
- */
- public function markCodeUsed()
- {
- $this->code_used_time = time();
- return $this->save();
- }
- /**
- * 获取待发放的记录
- */
- public static function getPendingRecords($limit = 100)
- {
- return static::where('deliver_status', self::DELIVER_STATUS_PENDING)
- ->order('createtime', 'asc')
- ->limit($limit)
- ->select();
- }
- /**
- * 获取用户中奖记录
- */
- public static function getUserWinRecords($userId, $page = 1, $limit = 20)
- {
- return static::where('user_id', $userId)
- ->with(['activity', 'prize'])
- ->order('createtime', 'desc')
- ->page($page, $limit)
- ->select();
- }
- /**
- * 获取活动中奖统计
- */
- public static function getActivityWinStats($activityId)
- {
- return [
- 'total_win' => static::where('activity_id', $activityId)->count(),
- 'delivered' => static::where('activity_id', $activityId)
- ->where('deliver_status', self::DELIVER_STATUS_SUCCESS)
- ->count(),
- 'pending' => static::where('activity_id', $activityId)
- ->where('deliver_status', self::DELIVER_STATUS_PENDING)
- ->count(),
- 'failed' => static::where('activity_id', $activityId)
- ->where('deliver_status', self::DELIVER_STATUS_FAILED)
- ->count()
- ];
- }
- }
|