Browse Source

fix: 抽奖记录详情

super-yimizi 1 month ago
parent
commit
a05e022f14

+ 2 - 2
addons/shop/config.php

@@ -791,7 +791,7 @@ return [
         'title' => '微信小程序AppID',
         'type' => 'string',
         'content' => [],
-        'value' => 'wx84a6c614d68ee3c5',
+        'value' => 'wx9a7153e5b40d9663',
         'rule' => '',
         'msg' => '',
         'tip' => '微信小程序的app_id',
@@ -803,7 +803,7 @@ return [
         'title' => '微信小程序AppSecret',
         'type' => 'string',
         'content' => [],
-        'value' => '207517012a3a0ea95aa142401fe16b59',
+        'value' => '8a743955faa5873af394f31d4a58c086',
         'rule' => '',
         'msg' => '',
         'tip' => '微信小程序的app_secret',

+ 17 - 2
application/api/controller/Lottery.php

@@ -207,6 +207,21 @@ class Lottery extends Api
     }
 
     /**
+     * 获取抽奖记录详情
+     */
+    public function getDrawRecordDetail()
+    {
+        $drawRecordId = $this->request->get('draw_record_id/d');
+        $validate = new LotteryValidate();
+        if (!$validate->scene('getDrawRecordDetail')->check(['draw_record_id' => $drawRecordId])) {
+            $this->error($validate->getError());
+        }
+        $userId = $this->auth->id;  
+        $drawRecord = LotteryRecordService::getDrawRecordDetail($drawRecordId, $userId);
+        $this->success('获取成功', $drawRecord);
+    }
+
+    /**
      * 获取用户中奖记录
      */
     public function getWinRecords()
@@ -287,11 +302,11 @@ class Lottery extends Api
                                      ->where('user_id', $userId)
                                      ->find();
         if (!$winRecord) {
-            throw new BusinessException('中奖记录不存在', ErrorCodeEnum::USER_WIN_RECORD_NOT_FOUND);
+            $this->error('中奖记录不存在');
         }
 
         if ($winRecord->deliver_status != LotteryEnum::DELIVER_STATUS_PENDING) {
-            throw new BusinessException('该奖品已处理,无法修改地址', ErrorCodeEnum::USER_PRIZE_ALREADY_PROCESSED);
+            $this->error('该奖品已处理,无法修改地址');
         }
 
         try {

+ 9 - 3
application/api/validate/Lottery.php

@@ -32,7 +32,8 @@ class Lottery extends Validate
         'city'                  => 'length:1,50',
         'district'              => 'length:1,50',
         'detail_address'        => 'require|length:5,100',
-        'order_id'              => 'require|integer|gt:0'
+        'order_id'              => 'require|integer|gt:0',
+        'draw_record_id'        => 'require|integer|gt:0'
     ];
 
     /**
@@ -72,7 +73,10 @@ class Lottery extends Validate
         'detail_address.length'     => '详细地址长度必须在5-100个字符之间',
         'order_id.require'          => '订单ID不能为空',
         'order_id.integer'          => '订单ID必须是整数',
-        'order_id.gt'               => '订单ID必须大于0'
+        'order_id.gt'               => '订单ID必须大于0',
+        'draw_record_id.require'    => '抽奖记录ID不能为空',
+        'draw_record_id.integer'    => '抽奖记录ID必须是整数',
+        'draw_record_id.gt'         => '抽奖记录ID必须大于0'
     ];
 
     /**
@@ -100,6 +104,8 @@ class Lottery extends Validate
         // 获取奖品列表(lottery_id可选,page、pageSize、type可选)
         'getPrizes'             => ['lottery_id_optional', 'page', 'pageSize', 'type'],
         // 获取订单完成后分发抽奖机会
-        'getLotteryChanceByOrder' => ['order_id']
+        'getLotteryChanceByOrder' => ['order_id'],
+        // 获取抽奖记录详情
+        'getDrawRecordDetail'    => ['draw_record_id']
     ];
 } 

+ 85 - 3
application/common/Service/Lottery/LotteryRecordService.php

@@ -253,12 +253,94 @@ class LotteryRecordService
 
     /**
      * 获取抽奖记录详情
+     * 
+     * @param int $drawRecordId 抽奖记录ID
+     * @param int $userId 用户ID(可选,用于验证权限)
+     * @return LotteryDrawRecord|null 抽奖记录详情,包含关联信息
      */
-    public static function getDrawRecordDetail($drawRecordId)
+    public static function getDrawRecordDetail($drawRecordId, $userId = 0)
     {
-        return LotteryDrawRecord::where('id', $drawRecordId)
-                                ->with(['activity', 'user', 'prize', 'winRecord', 'order'])
+        // 1. 查询抽奖记录基础信息
+        $query = LotteryDrawRecord::where('id', $drawRecordId);
+        
+        // 如果指定了用户ID,则验证权限
+        if ($userId) {
+            $query->where('user_id', $userId);
+        }
+        
+        $record = $query->field('id,activity_id,user_id,prize_id,is_win,trigger_type,trigger_order_id,
+                        trigger_amount,draw_time,remark,status,createtime')
+                       ->find();
+        
+        if (!$record) {
+            return null;
+        }
+        
+        // 2. 查询关联的活动信息
+        $activity = LotteryActivity::where('id', $record->activity_id)
+                                ->field('id,name,type,status,lottery_type,lottery_time')
                                 ->find();
+        
+        // 3. 查询关联的奖品信息
+        $prize = null;
+        if ($record->prize_id > 0) {
+            $prize = LotteryPrize::where('id', $record->prize_id)
+                               ->field('id,name,image,type')
+                               ->find();
+        }
+        
+        // 4. 查询关联的中奖记录信息(仅限中奖记录)
+        $winRecord = null;
+        if ($record->is_win) {
+            $winRecord = LotteryWinRecord::where('draw_record_id', $record->id)
+                                       ->field('id,draw_record_id,prize_name,prize_type,deliver_status,
+                                               deliver_time,receiver_name,receiver_mobile,receiver_address,
+                                               express_company,express_number,exchange_code,code_used_time')
+                                       ->find();
+        }
+        
+        // 5. 将关联数据作为字段附加到记录中
+        // 附加活动信息字段
+        $record->activity_name = $activity ? $activity->name : '';
+        $record->activity_type = $activity ? $activity->type : 0;
+        $record->activity_status = $activity ? $activity->status : 0;
+        $record->activity_lottery_type = $activity ? $activity->lottery_type : 0;
+        $record->activity_lottery_time = $activity ? $activity->lottery_time : 0;
+        
+        // 附加奖品信息字段
+        $record->prize_name = $prize ? $prize->name : '';
+        $record->prize_image = $prize ? cdnurl($prize->image) : '';
+        $record->prize_type = $prize ? $prize->type : 0;
+        // 附加中奖记录信息字段
+        if ($winRecord) {
+            $record->win_record_id = $winRecord->id;
+            $record->win_prize_name = $winRecord->prize_name;
+            $record->win_prize_type = $winRecord->prize_type;
+            $record->deliver_status = $winRecord->deliver_status;
+            $record->deliver_time = $winRecord->deliver_time;
+            $record->receiver_name = $winRecord->receiver_name;
+            $record->receiver_mobile = $winRecord->receiver_mobile;
+            $record->receiver_address = $winRecord->receiver_address;
+            $record->express_company = $winRecord->express_company;
+            $record->express_number = $winRecord->express_number;
+            $record->exchange_code = $winRecord->exchange_code;
+            $record->code_used_time = $winRecord->code_used_time;
+        } else {
+            $record->win_record_id = 0;
+            $record->win_prize_name = '';
+            $record->win_prize_type = 0;
+            $record->deliver_status = 0;
+            $record->deliver_time = 0;
+            $record->receiver_name = '';
+            $record->receiver_mobile = '';
+            $record->receiver_address = '';
+            $record->express_company = '';
+            $record->express_number = '';
+            $record->exchange_code = '';
+            $record->code_used_time = 0;           
+        }        
+       
+        return $record;
     }
 
     /**

+ 85 - 3
application/common/Service/lottery/LotteryRecordService.php

@@ -253,12 +253,94 @@ class LotteryRecordService
 
     /**
      * 获取抽奖记录详情
+     * 
+     * @param int $drawRecordId 抽奖记录ID
+     * @param int $userId 用户ID(可选,用于验证权限)
+     * @return LotteryDrawRecord|null 抽奖记录详情,包含关联信息
      */
-    public static function getDrawRecordDetail($drawRecordId)
+    public static function getDrawRecordDetail($drawRecordId, $userId = 0)
     {
-        return LotteryDrawRecord::where('id', $drawRecordId)
-                                ->with(['activity', 'user', 'prize', 'winRecord', 'order'])
+        // 1. 查询抽奖记录基础信息
+        $query = LotteryDrawRecord::where('id', $drawRecordId);
+        
+        // 如果指定了用户ID,则验证权限
+        if ($userId) {
+            $query->where('user_id', $userId);
+        }
+        
+        $record = $query->field('id,activity_id,user_id,prize_id,is_win,trigger_type,trigger_order_id,
+                        trigger_amount,draw_time,remark,status,createtime')
+                       ->find();
+        
+        if (!$record) {
+            return null;
+        }
+        
+        // 2. 查询关联的活动信息
+        $activity = LotteryActivity::where('id', $record->activity_id)
+                                ->field('id,name,type,status,lottery_type,lottery_time')
                                 ->find();
+        
+        // 3. 查询关联的奖品信息
+        $prize = null;
+        if ($record->prize_id > 0) {
+            $prize = LotteryPrize::where('id', $record->prize_id)
+                               ->field('id,name,image,type')
+                               ->find();
+        }
+        
+        // 4. 查询关联的中奖记录信息(仅限中奖记录)
+        $winRecord = null;
+        if ($record->is_win) {
+            $winRecord = LotteryWinRecord::where('draw_record_id', $record->id)
+                                       ->field('id,draw_record_id,prize_name,prize_type,deliver_status,
+                                               deliver_time,receiver_name,receiver_mobile,receiver_address,
+                                               express_company,express_number,exchange_code,code_used_time')
+                                       ->find();
+        }
+        
+        // 5. 将关联数据作为字段附加到记录中
+        // 附加活动信息字段
+        $record->activity_name = $activity ? $activity->name : '';
+        $record->activity_type = $activity ? $activity->type : 0;
+        $record->activity_status = $activity ? $activity->status : 0;
+        $record->activity_lottery_type = $activity ? $activity->lottery_type : 0;
+        $record->activity_lottery_time = $activity ? $activity->lottery_time : 0;
+        
+        // 附加奖品信息字段
+        $record->prize_name = $prize ? $prize->name : '';
+        $record->prize_image = $prize ? cdnurl($prize->image) : '';
+        $record->prize_type = $prize ? $prize->type : 0;
+        // 附加中奖记录信息字段
+        if ($winRecord) {
+            $record->win_record_id = $winRecord->id;
+            $record->win_prize_name = $winRecord->prize_name;
+            $record->win_prize_type = $winRecord->prize_type;
+            $record->deliver_status = $winRecord->deliver_status;
+            $record->deliver_time = $winRecord->deliver_time;
+            $record->receiver_name = $winRecord->receiver_name;
+            $record->receiver_mobile = $winRecord->receiver_mobile;
+            $record->receiver_address = $winRecord->receiver_address;
+            $record->express_company = $winRecord->express_company;
+            $record->express_number = $winRecord->express_number;
+            $record->exchange_code = $winRecord->exchange_code;
+            $record->code_used_time = $winRecord->code_used_time;
+        } else {
+            $record->win_record_id = 0;
+            $record->win_prize_name = '';
+            $record->win_prize_type = 0;
+            $record->deliver_status = 0;
+            $record->deliver_time = 0;
+            $record->receiver_name = '';
+            $record->receiver_mobile = '';
+            $record->receiver_address = '';
+            $record->express_company = '';
+            $record->express_number = '';
+            $record->exchange_code = '';
+            $record->code_used_time = 0;           
+        }        
+       
+        return $record;
     }
 
     /**