Browse Source

fix: 配置

super-yimizi 2 months ago
parent
commit
f0de0dae01

+ 1 - 1
addons/shop/config.php

@@ -6,7 +6,7 @@ return [
         'title' => '站点名称',
         'title' => '站点名称',
         'type' => 'string',
         'type' => 'string',
         'content' => [],
         'content' => [],
-        'value' => '商城网站',
+        'value' => '亮T裁衣',
         'rule' => 'required',
         'rule' => 'required',
         'msg' => '',
         'msg' => '',
         'tip' => '',
         'tip' => '',

+ 39 - 1
application/api/controller/Lottery.php

@@ -17,7 +17,7 @@ use app\common\Enum\LotteryEnum;
 use app\common\exception\BusinessException;
 use app\common\exception\BusinessException;
 use app\common\Enum\ErrorCodeEnum;
 use app\common\Enum\ErrorCodeEnum;
 use think\Exception;
 use think\Exception;
-
+use app\common\Service\OrderService;
 /**
 /**
  * 抽奖API控制器
  * 抽奖API控制器
  */
  */
@@ -394,4 +394,42 @@ class Lottery extends Api
         }
         }
         $this->success('获取成功', $paginate);
         $this->success('获取成功', $paginate);
     }
     }
+
+    //  订单完成后 分发抽奖机会 
+    public function getLotteryChanceByOrder()
+    {
+        $orderId = $this->request->post('order_id/d');
+        //  验证器
+        $validate = new LotteryValidate();
+        if (!$validate->scene('getLotteryChanceByOrder')->check(['order_id' => $orderId])) {
+            $this->error($validate->getError());
+        }
+
+        $userId = $this->auth->id;
+        $orderInfo = OrderService::getDetail($orderId, $userId);
+
+        if(!$orderInfo){
+            $this->error('订单不存在');
+        }
+        // 是否是已支付
+        if(!$orderInfo->isPayStatus()){
+            $this->error('订单未支付');
+        }
+
+        // 构造数据
+        $goodsList = $orderInfo->orderGoods;
+        $goodsList = collection($goodsList)->toArray();
+        $goodsList = array_map(function($item){
+            return [
+                'goods_id' => $item['goods_id'],
+            ];
+        }, $goodsList);
+        $orderInfo = [
+            'id' => $orderInfo->id,
+            'total_amount' => $orderInfo->amount,
+            'goods' => $goodsList            
+        ];
+        $grantedChances = LotteryChanceService::checkAndGrantChanceForOrderOne($orderInfo, $userId);
+        $this->success('获取成功', $grantedChances);
+    }
 }
 }

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

@@ -24,7 +24,8 @@ class Lottery extends Validate
         'province'              => 'length:1,50',
         'province'              => 'length:1,50',
         'city'                  => 'length:1,50',
         'city'                  => 'length:1,50',
         'district'              => 'length:1,50',
         'district'              => 'length:1,50',
-        'detail_address'        => 'require|length:5,100'
+        'detail_address'        => 'require|length:5,100',
+        'order_id'              => 'require|integer|gt:0'
     ];
     ];
 
 
     /**
     /**
@@ -59,7 +60,10 @@ class Lottery extends Validate
         'city.length'               => '城市名称不能超过50个字符',
         'city.length'               => '城市名称不能超过50个字符',
         'district.length'           => '区县名称不能超过50个字符',
         'district.length'           => '区县名称不能超过50个字符',
         'detail_address.require'    => '详细地址不能为空',
         'detail_address.require'    => '详细地址不能为空',
-        'detail_address.length'     => '详细地址长度必须在5-100个字符之间'
+        'detail_address.length'     => '详细地址长度必须在5-100个字符之间',
+        'order_id.require'          => '订单ID不能为空',
+        'order_id.integer'          => '订单ID必须是整数',
+        'order_id.gt'               => '订单ID必须大于0'
     ];
     ];
 
 
     /**
     /**
@@ -85,6 +89,8 @@ class Lottery extends Validate
         // 获取活动排行榜(lottery_id必填,page和limit可选)
         // 获取活动排行榜(lottery_id必填,page和limit可选)
         'getRanking'            => ['lottery_id', 'page', 'limit'],
         'getRanking'            => ['lottery_id', 'page', 'limit'],
         // 获取奖品列表(lottery_id可选,page、limit、type可选)
         // 获取奖品列表(lottery_id可选,page、limit、type可选)
-        'getPrizes'             => ['lottery_id_optional', 'page', 'limit', 'type']
+        'getPrizes'             => ['lottery_id_optional', 'page', 'limit', 'type'],
+        // 获取订单完成后分发抽奖机会
+        'getLotteryChanceByOrder' => ['order_id']
     ];
     ];
 } 
 } 

+ 5 - 16
application/common/Service/InspectionService.php

@@ -117,23 +117,12 @@ class InspectionService
         if (InspectionApplication::isPhoneApplied($data['phone'], $applicationId)) {
         if (InspectionApplication::isPhoneApplied($data['phone'], $applicationId)) {
             throw new BusinessException('该手机号已被其他用户申请');
             throw new BusinessException('该手机号已被其他用户申请');
         }
         }
-
-        // 启动事务
-        Db::startTrans();
-        try {
-            // 更新申请信息
-            $application->allowField(true)->save($data);
-            
-            // 提交事务
-            Db::commit();
-            
-            return $application;
-        } catch (Exception $e) {
-            // 回滚事务
-            Db::rollback();
-            throw new BusinessException('修改失败:' . $e->getMessage());
-        }
+        // 更新申请信息
+        $application->allowField(true)->save($data);       
+    
+        return $application;
     }
     }
+       
 
 
     /**
     /**
      * 检查申请资格
      * 检查申请资格

+ 127 - 22
application/common/Service/Lottery/LotteryChanceService.php

@@ -11,7 +11,8 @@ use app\common\model\Order;
 use app\common\Enum\LotteryEnum;
 use app\common\Enum\LotteryEnum;
 use think\Exception;
 use think\Exception;
 use think\Db;
 use think\Db;
-
+use app\common\exception\BusinessException;
+use app\common\Enum\OrderEnum;
 /**
 /**
  * 抽奖机会服务类
  * 抽奖机会服务类
  * 处理用户获得抽奖机会的逻辑
  * 处理用户获得抽奖机会的逻辑
@@ -33,8 +34,51 @@ class LotteryChanceService
     const MAX_BATCH_SIZE = 1000;
     const MAX_BATCH_SIZE = 1000;
 
 
 
 
+        /**
+     * 订单完成后检查并分发抽奖机会(单个活动版本)
+     * 查询一个正在进行的活动,返回单个结果
+     * 
+     * @param array $orderInfo 订单信息 ['id', 'total_amount', 'goods' => [['goods_id']]]
+     * @param int $userId 用户ID
+     * @return array|null 获得的抽奖机会信息,如果没有则返回null
+     * @throws Exception
+     */
+    public static function checkAndGrantChanceForOrderOne($orderInfo, $userId)
+    {
+        if (empty($orderInfo) || empty($userId)) {
+            throw new BusinessException('订单信息或用户ID不能为空');
+        }
+
+        // try {
+            // 获取当前正在进行的单个抽奖活动
+            $activity = static::getCurrentRunningActivity();
+            
+            // 如果没有正在进行的活动,返回null
+            if (!$activity) {
+                return null;
+            }
+            
+            // 处理当前活动
+            $chances = static::processActivityForOrder($activity, $orderInfo, $userId);
+            
+            if ($chances > 0) {
+                return [
+                    'lottery_id' => $activity->id,
+                    'lottery_name' => $activity->name,
+                    'chances' => $chances,
+                    'granted_time' => time()
+                ];
+            }
+            
+            return null;
+            
+        // } catch (Exception $e) {
+        //     trace("订单抽奖机会分发失败 - 用户ID: {$userId}, 错误: " . $e->getMessage(), 'error');
+        //     throw new BusinessException('订单抽奖机会分发失败');
+        // }
+    }
     /**
     /**
-     * 订单完成后检查并分发抽奖机会     * 
+     * 订单完成后检查并分发抽奖机会  多个的 
      * @param array $orderInfo 订单信息 ['id', 'total_amount', 'goods' => [['goods_id']]]
      * @param array $orderInfo 订单信息 ['id', 'total_amount', 'goods' => [['goods_id']]]
      * @param int $userId 用户ID
      * @param int $userId 用户ID
      * @return array 获得的抽奖机会信息
      * @return array 获得的抽奖机会信息
@@ -43,13 +87,13 @@ class LotteryChanceService
     public static function checkAndGrantChanceForOrder($orderInfo, $userId)
     public static function checkAndGrantChanceForOrder($orderInfo, $userId)
     {
     {
         if (empty($orderInfo) || empty($userId)) {
         if (empty($orderInfo) || empty($userId)) {
-            throw new Exception('订单信息或用户ID不能为空');
+            throw new BusinessException('订单信息或用户ID不能为空');
         }
         }
 
 
         $grantedChances = [];
         $grantedChances = [];
         
         
         try {
         try {
-        // 获取所有正在进行的抽奖活动
+        // 获取所有正在进行的抽奖活动  一段时间 有且只有一个抽奖活动
             $activities = LotteryService::getRunningActivities();
             $activities = LotteryService::getRunningActivities();
         
         
         foreach ($activities as $activity) {
         foreach ($activities as $activity) {
@@ -77,7 +121,51 @@ class LotteryChanceService
     }
     }
 
 
     /**
     /**
-     * 充值完成后检查并分发抽奖机会
+     * 充值完成后检查并分发抽奖机会(单个活动版本)
+     * 查询一个正在进行的活动,返回单个结果
+     * 
+     * @param array $rechargeInfo 充值信息 ['amount', 'type' => 'recharge']
+     * @param int $userId 用户ID
+     * @return array|null 获得的抽奖机会信息,如果没有则返回null
+     * @throws Exception
+     */
+    public static function checkAndGrantChanceForRechargeOne($rechargeInfo, $userId)
+    {
+        if (empty($rechargeInfo) || empty($userId)) {
+            throw new Exception('充值信息或用户ID不能为空');
+        }
+
+        try {
+            // 获取当前正在进行的单个抽奖活动
+            $activity = static::getCurrentRunningActivity();
+            
+            // 如果没有正在进行的活动,返回null
+            if (!$activity) {
+                return null;
+            }
+            
+            // 处理当前活动
+            $chances = static::processActivityForRecharge($activity, $rechargeInfo, $userId);
+            
+            if ($chances > 0) {
+                return [
+                    'activity_id' => $activity->id,
+                    'activity_name' => $activity->name,
+                    'chances' => $chances,
+                    'granted_time' => time()
+                ];
+            }
+            
+            return null;
+            
+        } catch (Exception $e) {
+            trace("充值抽奖机会分发失败 - 用户ID: {$userId}, 错误: " . $e->getMessage(), 'error');
+            throw $e;
+        }
+    }
+
+    /**
+     * 充值完成后检查并分发抽奖机会(多个活动版本)
      * 
      * 
      * @param array $rechargeInfo 充值信息 ['amount', 'type' => 'recharge']
      * @param array $rechargeInfo 充值信息 ['amount', 'type' => 'recharge']
      * @param int $userId 用户ID
      * @param int $userId 用户ID
@@ -207,11 +295,19 @@ class LotteryChanceService
         return $result;
         return $result;
     }
     }
 
 
+    // ============ 活动查询方法 ============
 
 
-
-
-
-
+    /**
+     * 获取当前正在进行的单个抽奖活动
+     * 假设同一时间段只有一个活动在进行
+     * 
+     * @return LotteryActivity|null
+     */
+    public static function getCurrentRunningActivity()
+    {
+        $activities = LotteryService::getRunningActivities();
+        return empty($activities) ? null : $activities[0];
+    }
 
 
     // ============ 私有处理方法 ============
     // ============ 私有处理方法 ============
 
 
@@ -234,7 +330,6 @@ class LotteryChanceService
         $conditions = static::getValidConditions($activity->id);
         $conditions = static::getValidConditions($activity->id);
         $totalChances = 0;
         $totalChances = 0;
         $chanceDetails = [];
         $chanceDetails = [];
-
         foreach ($conditions as $condition) {
         foreach ($conditions as $condition) {
             // 跳过充值条件
             // 跳过充值条件
             if ($condition->type == LotteryEnum::CONDITION_TYPE_RECHARGE_AMOUNT) {
             if ($condition->type == LotteryEnum::CONDITION_TYPE_RECHARGE_AMOUNT) {
@@ -248,7 +343,10 @@ class LotteryChanceService
                 $chanceDetails[] = $result['detail'];
                 $chanceDetails[] = $result['detail'];
             }
             }
         }
         }
-
+        echo '<pre>';
+        print_r($chanceDetails);
+        echo '</pre>';
+        die();
         // 如果获得了抽奖机会,记录到数据库
         // 如果获得了抽奖机会,记录到数据库
         if ($totalChances > 0) {
         if ($totalChances > 0) {
             // 为每个条件分别记录
             // 为每个条件分别记录
@@ -262,6 +360,7 @@ class LotteryChanceService
                 ]);
                 ]);
             }
             }
         }
         }
+        
 
 
         return $totalChances;
         return $totalChances;
     }
     }
@@ -285,7 +384,7 @@ class LotteryChanceService
         $conditions = static::getValidConditions($activity->id);
         $conditions = static::getValidConditions($activity->id);
         $totalChances = 0;
         $totalChances = 0;
         $chanceDetails = [];
         $chanceDetails = [];
-
+       
         foreach ($conditions as $condition) {
         foreach ($conditions as $condition) {
             // 只处理充值条件
             // 只处理充值条件
             if ($condition->type != LotteryEnum::CONDITION_TYPE_RECHARGE_AMOUNT) {
             if ($condition->type != LotteryEnum::CONDITION_TYPE_RECHARGE_AMOUNT) {
@@ -347,7 +446,8 @@ class LotteryChanceService
                     
                     
                     // 如果可重复获得,根据金额倍数计算次数
                     // 如果可重复获得,根据金额倍数计算次数
                     if (static::canRepeat($condition)) {
                     if (static::canRepeat($condition)) {
-                        $multiple = floor($orderInfo['total_amount'] / $condition->condition_value);
+                        // 使用bcdiv进行精确的金额除法运算,然后取整
+                        $multiple = floor(bcdiv($orderInfo['total_amount'], $condition->condition_value, 2));
                         $chances *= $multiple;
                         $chances *= $multiple;
                     }
                     }
                 }
                 }
@@ -391,7 +491,8 @@ class LotteryChanceService
             
             
             // 如果可重复获得,根据金额倍数计算次数
             // 如果可重复获得,根据金额倍数计算次数
             if (static::canRepeat($condition)) {
             if (static::canRepeat($condition)) {
-                $multiple = floor($rechargeInfo['amount'] / $condition->condition_value);
+                // 使用bcdiv进行精确的金额除法运算,然后取整
+                $multiple = floor(bcdiv($rechargeInfo['amount'], $condition->condition_value, 2));
                 $chances *= $multiple;
                 $chances *= $multiple;
             }
             }
         }
         }
@@ -545,7 +646,9 @@ class LotteryChanceService
     public static function validateOrderAmountCondition(LotteryCondition $condition, $orderInfo)
     public static function validateOrderAmountCondition(LotteryCondition $condition, $orderInfo)
     {
     {
         $orderAmount = $orderInfo['total_amount'] ?? 0;
         $orderAmount = $orderInfo['total_amount'] ?? 0;
-        return $orderAmount >= $condition->condition_value;
+        
+        // 使用bccomp进行精确的金额比较,返回值:-1(小于)、0(等于)、1(大于)
+        return bccomp($orderAmount, $condition->condition_value, 2) >= 0;
     }
     }
 
 
     /**
     /**
@@ -562,7 +665,8 @@ class LotteryChanceService
         }
         }
         
         
         $rechargeAmount = $orderInfo['amount'] ?? 0;
         $rechargeAmount = $orderInfo['amount'] ?? 0;
-        return $rechargeAmount >= $condition->condition_value;
+        // 使用bccomp进行精确的金额比较
+        return bccomp($rechargeAmount, $condition->condition_value, 2) >= 0;
     }
     }
 
 
     /**
     /**
@@ -582,12 +686,13 @@ class LotteryChanceService
 
 
         // 计算活动期间用户累计消费
         // 计算活动期间用户累计消费
         $totalAmount = Order::where('user_id', $userId)
         $totalAmount = Order::where('user_id', $userId)
-                           ->where('status', 'paid')
-                           ->where('createtime', '>=', $activity->start_time)
-                           ->where('createtime', '<=', $activity->end_time)
-                           ->sum('total_amount');
+                           ->where('order_status', OrderEnum::STATUS_PAY)
+                           ->where('pay_time', '>=', $activity->start_time)
+                           ->where('pay_time', '<=', $activity->end_time)
+                           ->sum('amount');
 
 
-        return $totalAmount >= $condition->condition_value;
+        // 使用bccomp进行精确的金额比较
+        return bccomp($totalAmount, $condition->condition_value, 2) >= 0;
     }
     }
 
 
 
 
@@ -906,7 +1011,7 @@ class LotteryChanceService
         return $userChance->save();
         return $userChance->save();
     }
     }
 
 
-    
+
     // ============ 统计和查询方法 ============
     // ============ 统计和查询方法 ============
 
 
     /**
     /**

+ 127 - 22
application/common/Service/lottery/LotteryChanceService.php

@@ -11,7 +11,8 @@ use app\common\model\Order;
 use app\common\Enum\LotteryEnum;
 use app\common\Enum\LotteryEnum;
 use think\Exception;
 use think\Exception;
 use think\Db;
 use think\Db;
-
+use app\common\exception\BusinessException;
+use app\common\Enum\OrderEnum;
 /**
 /**
  * 抽奖机会服务类
  * 抽奖机会服务类
  * 处理用户获得抽奖机会的逻辑
  * 处理用户获得抽奖机会的逻辑
@@ -33,8 +34,51 @@ class LotteryChanceService
     const MAX_BATCH_SIZE = 1000;
     const MAX_BATCH_SIZE = 1000;
 
 
 
 
+        /**
+     * 订单完成后检查并分发抽奖机会(单个活动版本)
+     * 查询一个正在进行的活动,返回单个结果
+     * 
+     * @param array $orderInfo 订单信息 ['id', 'total_amount', 'goods' => [['goods_id']]]
+     * @param int $userId 用户ID
+     * @return array|null 获得的抽奖机会信息,如果没有则返回null
+     * @throws Exception
+     */
+    public static function checkAndGrantChanceForOrderOne($orderInfo, $userId)
+    {
+        if (empty($orderInfo) || empty($userId)) {
+            throw new BusinessException('订单信息或用户ID不能为空');
+        }
+
+        // try {
+            // 获取当前正在进行的单个抽奖活动
+            $activity = static::getCurrentRunningActivity();
+            
+            // 如果没有正在进行的活动,返回null
+            if (!$activity) {
+                return null;
+            }
+            
+            // 处理当前活动
+            $chances = static::processActivityForOrder($activity, $orderInfo, $userId);
+            
+            if ($chances > 0) {
+                return [
+                    'lottery_id' => $activity->id,
+                    'lottery_name' => $activity->name,
+                    'chances' => $chances,
+                    'granted_time' => time()
+                ];
+            }
+            
+            return null;
+            
+        // } catch (Exception $e) {
+        //     trace("订单抽奖机会分发失败 - 用户ID: {$userId}, 错误: " . $e->getMessage(), 'error');
+        //     throw new BusinessException('订单抽奖机会分发失败');
+        // }
+    }
     /**
     /**
-     * 订单完成后检查并分发抽奖机会     * 
+     * 订单完成后检查并分发抽奖机会  多个的 
      * @param array $orderInfo 订单信息 ['id', 'total_amount', 'goods' => [['goods_id']]]
      * @param array $orderInfo 订单信息 ['id', 'total_amount', 'goods' => [['goods_id']]]
      * @param int $userId 用户ID
      * @param int $userId 用户ID
      * @return array 获得的抽奖机会信息
      * @return array 获得的抽奖机会信息
@@ -43,13 +87,13 @@ class LotteryChanceService
     public static function checkAndGrantChanceForOrder($orderInfo, $userId)
     public static function checkAndGrantChanceForOrder($orderInfo, $userId)
     {
     {
         if (empty($orderInfo) || empty($userId)) {
         if (empty($orderInfo) || empty($userId)) {
-            throw new Exception('订单信息或用户ID不能为空');
+            throw new BusinessException('订单信息或用户ID不能为空');
         }
         }
 
 
         $grantedChances = [];
         $grantedChances = [];
         
         
         try {
         try {
-        // 获取所有正在进行的抽奖活动
+        // 获取所有正在进行的抽奖活动  一段时间 有且只有一个抽奖活动
             $activities = LotteryService::getRunningActivities();
             $activities = LotteryService::getRunningActivities();
         
         
         foreach ($activities as $activity) {
         foreach ($activities as $activity) {
@@ -77,7 +121,51 @@ class LotteryChanceService
     }
     }
 
 
     /**
     /**
-     * 充值完成后检查并分发抽奖机会
+     * 充值完成后检查并分发抽奖机会(单个活动版本)
+     * 查询一个正在进行的活动,返回单个结果
+     * 
+     * @param array $rechargeInfo 充值信息 ['amount', 'type' => 'recharge']
+     * @param int $userId 用户ID
+     * @return array|null 获得的抽奖机会信息,如果没有则返回null
+     * @throws Exception
+     */
+    public static function checkAndGrantChanceForRechargeOne($rechargeInfo, $userId)
+    {
+        if (empty($rechargeInfo) || empty($userId)) {
+            throw new Exception('充值信息或用户ID不能为空');
+        }
+
+        try {
+            // 获取当前正在进行的单个抽奖活动
+            $activity = static::getCurrentRunningActivity();
+            
+            // 如果没有正在进行的活动,返回null
+            if (!$activity) {
+                return null;
+            }
+            
+            // 处理当前活动
+            $chances = static::processActivityForRecharge($activity, $rechargeInfo, $userId);
+            
+            if ($chances > 0) {
+                return [
+                    'activity_id' => $activity->id,
+                    'activity_name' => $activity->name,
+                    'chances' => $chances,
+                    'granted_time' => time()
+                ];
+            }
+            
+            return null;
+            
+        } catch (Exception $e) {
+            trace("充值抽奖机会分发失败 - 用户ID: {$userId}, 错误: " . $e->getMessage(), 'error');
+            throw $e;
+        }
+    }
+
+    /**
+     * 充值完成后检查并分发抽奖机会(多个活动版本)
      * 
      * 
      * @param array $rechargeInfo 充值信息 ['amount', 'type' => 'recharge']
      * @param array $rechargeInfo 充值信息 ['amount', 'type' => 'recharge']
      * @param int $userId 用户ID
      * @param int $userId 用户ID
@@ -207,11 +295,19 @@ class LotteryChanceService
         return $result;
         return $result;
     }
     }
 
 
+    // ============ 活动查询方法 ============
 
 
-
-
-
-
+    /**
+     * 获取当前正在进行的单个抽奖活动
+     * 假设同一时间段只有一个活动在进行
+     * 
+     * @return LotteryActivity|null
+     */
+    public static function getCurrentRunningActivity()
+    {
+        $activities = LotteryService::getRunningActivities();
+        return empty($activities) ? null : $activities[0];
+    }
 
 
     // ============ 私有处理方法 ============
     // ============ 私有处理方法 ============
 
 
@@ -234,7 +330,6 @@ class LotteryChanceService
         $conditions = static::getValidConditions($activity->id);
         $conditions = static::getValidConditions($activity->id);
         $totalChances = 0;
         $totalChances = 0;
         $chanceDetails = [];
         $chanceDetails = [];
-
         foreach ($conditions as $condition) {
         foreach ($conditions as $condition) {
             // 跳过充值条件
             // 跳过充值条件
             if ($condition->type == LotteryEnum::CONDITION_TYPE_RECHARGE_AMOUNT) {
             if ($condition->type == LotteryEnum::CONDITION_TYPE_RECHARGE_AMOUNT) {
@@ -248,7 +343,10 @@ class LotteryChanceService
                 $chanceDetails[] = $result['detail'];
                 $chanceDetails[] = $result['detail'];
             }
             }
         }
         }
-
+        echo '<pre>';
+        print_r($chanceDetails);
+        echo '</pre>';
+        die();
         // 如果获得了抽奖机会,记录到数据库
         // 如果获得了抽奖机会,记录到数据库
         if ($totalChances > 0) {
         if ($totalChances > 0) {
             // 为每个条件分别记录
             // 为每个条件分别记录
@@ -262,6 +360,7 @@ class LotteryChanceService
                 ]);
                 ]);
             }
             }
         }
         }
+        
 
 
         return $totalChances;
         return $totalChances;
     }
     }
@@ -285,7 +384,7 @@ class LotteryChanceService
         $conditions = static::getValidConditions($activity->id);
         $conditions = static::getValidConditions($activity->id);
         $totalChances = 0;
         $totalChances = 0;
         $chanceDetails = [];
         $chanceDetails = [];
-
+       
         foreach ($conditions as $condition) {
         foreach ($conditions as $condition) {
             // 只处理充值条件
             // 只处理充值条件
             if ($condition->type != LotteryEnum::CONDITION_TYPE_RECHARGE_AMOUNT) {
             if ($condition->type != LotteryEnum::CONDITION_TYPE_RECHARGE_AMOUNT) {
@@ -347,7 +446,8 @@ class LotteryChanceService
                     
                     
                     // 如果可重复获得,根据金额倍数计算次数
                     // 如果可重复获得,根据金额倍数计算次数
                     if (static::canRepeat($condition)) {
                     if (static::canRepeat($condition)) {
-                        $multiple = floor($orderInfo['total_amount'] / $condition->condition_value);
+                        // 使用bcdiv进行精确的金额除法运算,然后取整
+                        $multiple = floor(bcdiv($orderInfo['total_amount'], $condition->condition_value, 2));
                         $chances *= $multiple;
                         $chances *= $multiple;
                     }
                     }
                 }
                 }
@@ -391,7 +491,8 @@ class LotteryChanceService
             
             
             // 如果可重复获得,根据金额倍数计算次数
             // 如果可重复获得,根据金额倍数计算次数
             if (static::canRepeat($condition)) {
             if (static::canRepeat($condition)) {
-                $multiple = floor($rechargeInfo['amount'] / $condition->condition_value);
+                // 使用bcdiv进行精确的金额除法运算,然后取整
+                $multiple = floor(bcdiv($rechargeInfo['amount'], $condition->condition_value, 2));
                 $chances *= $multiple;
                 $chances *= $multiple;
             }
             }
         }
         }
@@ -545,7 +646,9 @@ class LotteryChanceService
     public static function validateOrderAmountCondition(LotteryCondition $condition, $orderInfo)
     public static function validateOrderAmountCondition(LotteryCondition $condition, $orderInfo)
     {
     {
         $orderAmount = $orderInfo['total_amount'] ?? 0;
         $orderAmount = $orderInfo['total_amount'] ?? 0;
-        return $orderAmount >= $condition->condition_value;
+        
+        // 使用bccomp进行精确的金额比较,返回值:-1(小于)、0(等于)、1(大于)
+        return bccomp($orderAmount, $condition->condition_value, 2) >= 0;
     }
     }
 
 
     /**
     /**
@@ -562,7 +665,8 @@ class LotteryChanceService
         }
         }
         
         
         $rechargeAmount = $orderInfo['amount'] ?? 0;
         $rechargeAmount = $orderInfo['amount'] ?? 0;
-        return $rechargeAmount >= $condition->condition_value;
+        // 使用bccomp进行精确的金额比较
+        return bccomp($rechargeAmount, $condition->condition_value, 2) >= 0;
     }
     }
 
 
     /**
     /**
@@ -582,12 +686,13 @@ class LotteryChanceService
 
 
         // 计算活动期间用户累计消费
         // 计算活动期间用户累计消费
         $totalAmount = Order::where('user_id', $userId)
         $totalAmount = Order::where('user_id', $userId)
-                           ->where('status', 'paid')
-                           ->where('createtime', '>=', $activity->start_time)
-                           ->where('createtime', '<=', $activity->end_time)
-                           ->sum('total_amount');
+                           ->where('order_status', OrderEnum::STATUS_PAY)
+                           ->where('pay_time', '>=', $activity->start_time)
+                           ->where('pay_time', '<=', $activity->end_time)
+                           ->sum('amount');
 
 
-        return $totalAmount >= $condition->condition_value;
+        // 使用bccomp进行精确的金额比较
+        return bccomp($totalAmount, $condition->condition_value, 2) >= 0;
     }
     }
 
 
 
 
@@ -906,7 +1011,7 @@ class LotteryChanceService
         return $userChance->save();
         return $userChance->save();
     }
     }
 
 
-    
+
     // ============ 统计和查询方法 ============
     // ============ 统计和查询方法 ============
 
 
     /**
     /**

+ 6 - 32
application/common/model/Order.php

@@ -46,33 +46,6 @@ class Order extends Model
     {
     {
         return OrderEnum::getOrderStatusText($data['order_status']);
         return OrderEnum::getOrderStatusText($data['order_status']);
     }
     }
-
-    public function getShippingstateList()
-    {
-        return ['0' => __('Shippingstate 0'), '1' => __('Shippingstate 1'), '2' => __('Shippingstate 2'), '3' => __('Shippingstate 3')];
-    }
-
-    public function getPaystateList()
-    {
-        return ['0' => __('Paystate 0'), '1' => __('Paystate 1')];
-    }
-
-
-
-    public function getShippingstateTextAttr($value, $data)
-    {
-        $value = $value ? $value : $data['shippingstate'];
-        $list = $this->getShippingstateList();
-        return $list[$value] ?? '';
-    }
-
-    public function getPaystateTextAttr($value, $data)
-    {
-        $value = $value ? $value : $data['paystate'];
-        $list = $this->getPaystateList();
-        return $list[$value] ?? '';
-    }
-
     /**
     /**
      * @ 支付
      * @ 支付
      * @param string $orderid
      * @param string $orderid
@@ -167,12 +140,8 @@ class Order extends Model
         }
         }
         return $response;
         return $response;
     }
     }
-
-
-
-
     /**
     /**
-     * @ DateTime 2021-06-01
+     * 
      * @ 订单结算
      * @ 订单结算
      * @param string $order_sn      订单号
      * @param string $order_sn      订单号
      * @param float  $payamount     支付金额
      * @param float  $payamount     支付金额
@@ -252,4 +221,9 @@ class Order extends Model
     {
     {
         return $this->hasMany('OrderAction', 'order_sn', 'order_sn');
         return $this->hasMany('OrderAction', 'order_sn', 'order_sn');
     }
     }
+
+    public function orderAddress()
+    {
+        return $this->hasOne('OrderAddress', 'order_id', 'id');
+    }
 }
 }