wesmiler 1 viikko sitten
vanhempi
commit
e8436161cb

+ 49 - 1
app/Console/Commands/SwooleTask.php

@@ -2,6 +2,7 @@
 
 
 namespace App\Console\Commands;
 namespace App\Console\Commands;
 
 
+use App\Services\Api\MemberService;
 use App\Services\Api\OrderService;
 use App\Services\Api\OrderService;
 use App\Services\Api\PriceService;
 use App\Services\Api\PriceService;
 use App\Services\ConfigService;
 use App\Services\ConfigService;
@@ -241,7 +242,7 @@ class SwooleTask extends Command
                             'params'=> $item,
                             'params'=> $item,
                         ];
                         ];
                         $res = $serv->task($taskData);
                         $res = $serv->task($taskData);
-                        RedisService::set('caches:task:lock:order_complete_loaded_'.$k, true, rand(3,5));
+                        RedisService::set('caches:task:lock:order_complete_loaded_'.$orderNo, true, rand(3,5));
                         echo "[Task completeOrder {$date}] 购物订单【{$orderNo}】自动收货处理结果:{$res}\n";
                         echo "[Task completeOrder {$date}] 购物订单【{$orderNo}】自动收货处理结果:{$res}\n";
                     }else{
                     }else{
                         echo "[Task completeOrder {$date}] 购物订单【{$orderNo}】自动收货处理间隔时间调用\n";
                         echo "[Task completeOrder {$date}] 购物订单【{$orderNo}】自动收货处理间隔时间调用\n";
@@ -252,6 +253,34 @@ class SwooleTask extends Command
             }
             }
         });
         });
 
 
+        // 用户自动复购
+        \swoole_timer_tick(120000, function ($timer) use ($serv, &$time) { // 启用定时器,每2分钟执行一次
+            $date = date('Y-m-d H:i:s');
+            $users = MemberService::make()->getReplyBuyUsers();
+            if($users){
+                foreach ($users as $k => $item){
+                    $nickname = isset($item['nickname'])?$item['nickname'] : '';
+                    $userId = isset($item['id'])?$item['id'] : '';
+                    if(!RedisService::get('caches:task:lock:user_reply_buy_loaded_'.$userId)){
+                        $taskData = [
+                            'taskName' => 'rebuyOrder',
+                            'name' => "用户【{$nickname}-{$userId}】自动复购",
+                            'date' => date('Y-m-d'),
+                            'params'=> $item,
+                        ];
+                        $res = $serv->task($taskData);
+                        RedisService::set('caches:task:lock:user_reply_buy_loaded_'.$userId, true, rand(3,5));
+                        echo "[Task rebuyOrder {$date}] 用户【{$nickname}-{$userId}】自动复购处理结果:{$res}\n";
+                    }else{
+                        echo "[Task rebuyOrder {$date}] 用户【{$nickname}-{$userId}】自动复购间隔时间调用\n";
+                    }
+                }
+            }else{
+                echo "[Task rebuyOrder {$date}] 暂无需要自动复购的用户\n";
+            }
+        });
+
+        // 更新资产价格
         \swoole_timer_tick(5000, function ($timer) use ($serv, &$time) { // 启用定时器,每5秒执行一次
         \swoole_timer_tick(5000, function ($timer) use ($serv, &$time) { // 启用定时器,每5秒执行一次
             $date = date('Y-m-d H:i:s');
             $date = date('Y-m-d H:i:s');
             $updatePriceTime = ConfigService::make()->getConfigByCode('update_price_time','');
             $updatePriceTime = ConfigService::make()->getConfigByCode('update_price_time','');
@@ -323,6 +352,25 @@ class SwooleTask extends Command
                         echo "[Task {$taskName} {$date}][{$task_id}] 购物订单【{$orderNo}】自动收货处理结果:{$error}\n";
                         echo "[Task {$taskName} {$date}][{$task_id}] 购物订单【{$orderNo}】自动收货处理结果:{$error}\n";
                     }
                     }
                     break;
                     break;
+                case 'rebuyOrder': // 自动复购
+                    $userId = isset($params['id'])? $params['id'] : 0;
+                    $nickname = isset($params['nickname'])? $params['nickname'] : '';
+                    $buyType = isset($params['buy_type'])? $params['buy_type'] : 0;
+                    if($userId<=0 || $buyType<=0){
+                        echo "[Task {$taskName} {$date}][{$task_id}] 该用户【{$nickname}】复购参数错误\n";
+                        return false;
+                    }
+
+                    // 调用处理
+                    if($res = OrderService::make()->rebuyOrder($userId, $buyType)){
+                        $res = is_array($res) && $res? json_encode($res, 256) : 'success';
+                        echo "[Task {$taskName} {$date}][{$task_id}] 用户【{$nickname}-{$userId}】自动复购处理结果:{$res}\n";
+                    }else{
+                        $error = OrderService::make()->getError();
+                        $error = $error? lang($error) : 'failed';
+                        echo "[Task {$taskName} {$date}][{$task_id}] 用户【{$nickname}-{$userId}】自动复购处理结果:{$error}\n";
+                    }
+                    break;
                 case 'updatePrice': // 更新每日数字资产价格
                 case 'updatePrice': // 更新每日数字资产价格
                     // 调用处理
                     // 调用处理
                     if($res = PriceService::make()->updatePrice(1)){
                     if($res = PriceService::make()->updatePrice(1)){

+ 31 - 0
app/Http/Controllers/Api/v1/GoodsController.php

@@ -4,6 +4,8 @@ namespace App\Http\Controllers\Api\v1;
 
 
 use App\Http\Controllers\Api\webApp;
 use App\Http\Controllers\Api\webApp;
 use App\Services\Api\GoodsService;
 use App\Services\Api\GoodsService;
+use App\Services\Common\AdService;
+use App\Services\RedisService;
 
 
 /**
 /**
  * 商品管理
  * 商品管理
@@ -70,6 +72,35 @@ class GoodsController extends webApp
     }
     }
 
 
     /**
     /**
+     * 创业大礼包
+     * @return array
+     */
+    public function data()
+    {
+        $areaId = $this->areaId;
+        $buyType = $this->buyType;
+        if($areaId<=0){
+            $cacheInfo = RedisService::get("caches:index:area_".get_client_ip());
+            $areaId = isset($cacheInfo['area_id'])?$cacheInfo['area_id'] : 1;
+            $buyType = isset($cacheInfo['buy_type'])?$cacheInfo['buy_type'] : 1;
+        }
+        try {
+            $data = [
+                // 轮播
+                'banners' => AdService::make()->getListByPosition(1),
+                'goods' => GoodsService::make()->getListByType(2, $buyType, $this->userId),
+                'areaId'=> $areaId,
+                'buyType'=> $buyType,
+            ];
+
+            return showJson(1010, true, $data);
+        } catch (\Exception $exception) {
+            $error = ['data' => $exception->getTrace(), 'err' => $exception->getMessage()];
+            return showJson(1046, false, $error);
+        }
+    }
+
+    /**
      * 收藏
      * 收藏
      */
      */
     public function collect()
     public function collect()

+ 0 - 10
app/Http/Controllers/Api/v1/IndexController.php

@@ -64,20 +64,10 @@ class IndexController extends webApp
      */
      */
     public function data()
     public function data()
     {
     {
-        $areaId = $this->areaId;
-        $buyType = $this->buyType;
-        if($areaId<=0){
-            $cacheInfo = RedisService::get("caches:index:area_".get_client_ip());
-            $areaId = isset($cacheInfo['area_id'])?$cacheInfo['area_id'] : 1;
-            $buyType = isset($cacheInfo['buy_type'])?$cacheInfo['buy_type'] : 1;
-        }
         try {
         try {
             $data = [
             $data = [
                 // 轮播
                 // 轮播
                 'banners' => AdService::make()->getListByPosition(1),
                 'banners' => AdService::make()->getListByPosition(1),
-                'goods' => GoodsService::make()->getListByType(2, $buyType, $this->userId),
-                'areaId'=> $areaId,
-                'buyType'=> $buyType,
             ];
             ];
 
 
             return showJson(1010, true, $data);
             return showJson(1010, true, $data);

+ 3 - 3
app/Http/Controllers/Api/v1/TestController.php

@@ -20,11 +20,11 @@ class TestController extends webApp
 
 
     public function check()
     public function check()
     {
     {
-        $result = $buyCount = pow(2,7+1)-2;;
-//        $result = SettleService::make()->commissionSettle(1);
+        $result = $buyCount = pow(2,7+1)-2;
+        $result = OrderService::make()->rebuyOrder(1081,2);
 //        $result = PriceService::make()->updatePrice(1);
 //        $result = PriceService::make()->updatePrice(1);
 dump($result);
 dump($result);
-        return showJson(SettleService::make()->getError(), true, $result);
+        return showJson(OrderService::make()->getError(), true, $result);
     }
     }
 
 
 
 

+ 1 - 1
app/Models/MemberModel.php

@@ -106,7 +106,7 @@ class MemberModel extends BaseModel
     {
     {
         return $this->hasOne(MemberModel::class, 'id','parent_id')
         return $this->hasOne(MemberModel::class, 'id','parent_id')
             ->where(['status'=>1,'mark'=>1])
             ->where(['status'=>1,'mark'=>1])
-            ->select(['id', 'nickname','realname','avatar','parent_id','point_id', 'mobile', 'status']);
+            ->select(['id', 'nickname','realname','avatar','member_level','parent_id','point_id', 'mobile', 'status']);
     }
     }
 
 
     /**
     /**

+ 29 - 0
app/Services/Api/MemberService.php

@@ -12,6 +12,7 @@
 namespace App\Services\Api;
 namespace App\Services\Api;
 
 
 use App\Models\ActionLogModel;
 use App\Models\ActionLogModel;
+use App\Models\GoodsModel;
 use App\Models\MemberBankModel;
 use App\Models\MemberBankModel;
 use App\Models\MemberModel;
 use App\Models\MemberModel;
 use App\Services\BaseService;
 use App\Services\BaseService;
@@ -849,4 +850,32 @@ class MemberService extends BaseService
         return true;
         return true;
     }
     }
 
 
+    /**
+     * 获取需要复购的用户列表
+     * @param int $limit
+     * @return array|mixed
+     */
+    public function getReplyBuyUsers($limit=500)
+    {
+        $cacheKey = "caches:members:replyList";
+        $data = RedisService::get($cacheKey);
+        if($data){
+            return $data;
+        }
+
+        $maxLevel = GoodsModel::where(['type'=>2,'status'=>1,'mark'=>1])->max('id');
+        $maxLevel = $maxLevel>0 && $maxLevel<=10?$maxLevel: 4;
+        $data = $this->model->where('buy_type','<=', $maxLevel)
+            ->where('balance','>',0)
+            ->where(['bonus_status'=>2,'status'=>1,'mark'=>1])
+            ->select(['id','mobile','nickname','buy_type','status'])
+            ->limit($limit)
+            ->get();
+        $data = $data? $data->toArray() : [];
+        if($data){
+            RedisService::set($cacheKey, $data, rand(300,600));
+        }
+        return $data;
+    }
+
 }
 }

+ 246 - 241
app/Services/Api/OrderService.php

@@ -500,194 +500,226 @@ class OrderService extends BaseService
      * @param $mealId 套餐ID
      * @param $mealId 套餐ID
      * @return array|false
      * @return array|false
      */
      */
-    public function replyBuy($userId, $mealId)
+    public function rebuyOrder($userId, $mealId)
     {
     {
-        $type = isset($params['type']) && $params['type'] ? $params['type'] : 1;
-        $goods = ["id_{$mealId}"=>['id'=>$mealId,'sku_id'=>0,'num'=>1]];
-        $ids = [$mealId];
+        try {
+            $type = 2;
+            $goods = ["id_{$mealId}"=>['id'=>$mealId,'sku_id'=>0,'num'=>1]];
+            $ids = [$mealId];
+
+            // 缓存锁
+            $cacheLockKey = "caches:orders:submit_lock:{$userId}";
+            if (RedisService::get($cacheLockKey)) {
+                $this->error = '订单处理中~';
+                return false;
+            }
 
 
-        // 缓存锁
-        $cacheLockKey = "caches:orders:submit_lock:{$userId}";
-        if (RedisService::get($cacheLockKey)) {
-            $this->error = '订单处理中~';
-            return false;
-        }
+            // 用户信息
+            RedisService::set($cacheLockKey, ['mealId' => $mealId, 'user_id' => $userId], rand(3, 5));
+            $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
+                ->select(['id', 'openid','nickname','member_level','parent_id','point_id','points', 'buy_type','property','balance', 'bd_score', 'status'])
+                ->first();
+            $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
+            $openid = isset($userInfo['openid']) ? $userInfo['openid'] : 0;
+            $bdScore = isset($userInfo['bd_score']) ? $userInfo['bd_score'] : 0;
+            $balance = isset($userInfo['balance']) ? $userInfo['balance'] : 0;
+            $discountPoint = isset($userInfo['discount_point']) ? $userInfo['discount_point'] : 0; // 折扣
+            if (empty($userInfo) || $status != 1) {
+                $this->error = 1045;
+                return false;
+            }
 
 
-        // 用户信息
-        RedisService::set($cacheLockKey, ['params' => $params, 'user_id' => $userId], rand(3, 5));
-        $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
-            ->select(['id', 'openid','nickname','member_level','parent_id','point_id','points', 'buy_type','property','balance', 'bd_score', 'status'])
-            ->first();
-        $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
-        $openid = isset($userInfo['openid']) ? $userInfo['openid'] : 0;
-        $bdScore = isset($userInfo['bd_score']) ? $userInfo['bd_score'] : 0;
-        $balance = isset($userInfo['balance']) ? $userInfo['balance'] : 0;
-        $discountPoint = isset($userInfo['discount_point']) ? $userInfo['discount_point'] : 0; // 折扣
-        if (empty($userInfo) || $status != 1) {
-            $this->error = 1045;
-            return false;
-        }
+            if (empty($openid)) {
+                $this->error = 1042;
+                return false;
+            }
 
 
-        if (empty($openid)) {
-            $this->error = 1042;
-            return false;
-        }
+            // 商品数据
+            $orderNo = get_order_num('JK');
+            $result = GoodsService::make()->getOrderGoods($ids, $goods, $userId, $orderNo, $discountPoint);
+            if (empty($result)) {
+                RedisService::clear($cacheLockKey);
+                $this->error = GoodsService::make()->getError();
+                return false;
+            }
 
 
-        // 商品数据
-        $orderNo = get_order_num('JK');
-        $result = GoodsService::make()->getOrderGoods($ids, $goods, $userId, $orderNo, $discountPoint);
-        if (empty($result)) {
-            RedisService::clear($cacheLockKey);
-            $this->error = GoodsService::make()->getError();
-            return false;
-        }
+            $orderGoods = isset($result['goods']) ? $result['goods'] : [];
+            $goodsTotal = isset($result['goods_total']) ? $result['goods_total'] : 0; // 商品总价
+            $orderTotal = isset($result['order_total']) ? $result['order_total'] : 0; // 订单结算金额(折后)
+            $bdScoreTotal = isset($result['bd_score_total']) ? $result['bd_score_total'] : 0; // 报单积分
+            $profitTotal = isset($result['profit_total']) ? $result['profit_total'] : 0; // 利润
+            $discountTotal = isset($result['discount_total']) ? $result['discount_total'] : 0; // 折扣金额
+            $orderCount = isset($result['count']) ? $result['count'] : 0;
+            $deliveryFee = isset($result['delivery_fee']) ? $result['delivery_fee'] : 0; // 运费
+            $storeId = isset($result['store_id']) ? $result['store_id'] : 0; // 企业/商家
+            if (empty($orderGoods)) {
+                RedisService::clear($cacheLockKey);
+                $this->error = '获取订单商品错误~';
+                return false;
+            }
 
 
-        $orderGoods = isset($result['goods']) ? $result['goods'] : [];
-        $goodsTotal = isset($result['goods_total']) ? $result['goods_total'] : 0; // 商品总价
-        $orderTotal = isset($result['order_total']) ? $result['order_total'] : 0; // 订单结算金额(折后)
-        $bdScoreTotal = isset($result['bd_score_total']) ? $result['bd_score_total'] : 0; // 报单积分
-        $profitTotal = isset($result['profit_total']) ? $result['profit_total'] : 0; // 利润
-        $discountTotal = isset($result['discount_total']) ? $result['discount_total'] : 0; // 折扣金额
-        $orderCount = isset($result['count']) ? $result['count'] : 0;
-        $deliveryFee = isset($result['delivery_fee']) ? $result['delivery_fee'] : 0; // 运费
-        $storeId = isset($result['store_id']) ? $result['store_id'] : 0; // 企业/商家
-        if (empty($orderGoods)) {
-            RedisService::clear($cacheLockKey);
-            $this->error = '获取订单商品错误~';
-            return false;
-        }
+            if ($orderTotal <= 0) {
+                RedisService::clear($cacheLockKey);
+                $this->error = '订单金额错误~';
+                return false;
+            }
 
 
-        if ($orderTotal <= 0) {
-            RedisService::clear($cacheLockKey);
-            $this->error = '订单金额错误~';
-            return false;
-        }
+            if ($orderCount <= 0) {
+                RedisService::clear($cacheLockKey);
+                $this->error = '订单商品数量错误~';
+                return false;
+            }
 
 
-        if ($orderCount <= 0) {
-            RedisService::clear($cacheLockKey);
-            $this->error = '订单商品数量错误~';
-            return false;
-        }
+            // 报单积分是否够
+            if ($bdScore < $bdScoreTotal) {
+                RedisService::clear($cacheLockKey);
+                $this->error = '抱歉,您的报单积分不足';
+                return false;
+            }
 
 
-        // 报单积分是否够
-        if ($bdScore < $bdScoreTotal) {
-            RedisService::clear($cacheLockKey);
-            $this->error = '抱歉,您的报单积分不足';
-            return false;
-        }
+            // 是否购买过
+            $buyLimit = ConfigService::make()->getConfigByCode('business_buy_limit', 0);
+            $buyLimit = $buyLimit>0?$buyLimit:0;
+            if($buyLimit>0){
+                $checkOrderCount = $this->checkBusinessOrder($userId, $mealId);
+                if ($checkOrderCount >= $buyLimit) {
+                    RedisService::clear($cacheLockKey);
+                    $this->error = $buyLimit>1?"抱歉该创业礼包仅限购买{$buyLimit}次":'抱歉,您已经购买过该礼包';
+                    return false;
+                }
+            }
 
 
-        // 是否购买过
-        $buyLimit = ConfigService::make()->getConfigByCode('business_buy_limit', 0);
-        $buyLimit = $buyLimit>0?$buyLimit:0;
-        if($buyLimit>0){
-            $checkOrderCount = $this->checkBusinessOrder($userId, $mealId);
-            if ($checkOrderCount >= $buyLimit) {
+            // 收货地址信息
+            $addressInfo = MemberAddressService::make()->getBindInfo($userId);
+            $realname = isset($addressInfo['realname']) ? $addressInfo['realname'] : '';
+            $mobile = isset($addressInfo['mobile']) ? $addressInfo['mobile'] : '';
+            $area = isset($addressInfo['area']) ? $addressInfo['area'] : '';
+            $address = isset($addressInfo['address']) ? $addressInfo['address'] : '';
+            if (empty($addressInfo) || empty($realname) || empty($mobile) || empty($area) || empty($address)) {
                 RedisService::clear($cacheLockKey);
                 RedisService::clear($cacheLockKey);
-                $this->error = $buyLimit>1?"抱歉该创业礼包仅限购买{$buyLimit}次":'抱歉,您已经购买过该礼包';
+                $this->error = '收货地址信息错误,请核对后重试~';
                 return false;
                 return false;
             }
             }
-        }
 
 
-        // 收货地址信息
-        $addressInfo = MemberAddressService::make()->getBindInfo($userId);
-        $realname = isset($addressInfo['realname']) ? $addressInfo['realname'] : '';
-        $mobile = isset($addressInfo['mobile']) ? $addressInfo['mobile'] : '';
-        $area = isset($addressInfo['area']) ? $addressInfo['area'] : '';
-        $address = isset($addressInfo['address']) ? $addressInfo['address'] : '';
-        if (empty($addressInfo) || empty($realname) || empty($mobile) || empty($area) || empty($address)) {
-            RedisService::clear($cacheLockKey);
-            $this->error = '收货地址信息错误,请核对后重试~';
-            return false;
-        }
+            // 是否开启分账功能
+            $payTotal = moneyFormat($orderTotal + $deliveryFee, 2); // 含运费支付金额
+            if($balance < $payTotal){
+                RedisService::clear($cacheLockKey);
+                $this->error = "自动复购失败,您的余额不足";
+                return false;
+            }
 
 
-        // 是否开启分账功能
-        $payTotal = moneyFormat($orderTotal + $deliveryFee, 2); // 含运费支付金额
-        if($balance < $payTotal){
-            RedisService::clear($cacheLockKey);
-            $this->error = "自动复购失败,您的余额不足";
-            return false;
-        }
+            // 订单数据
+            $order = [
+                'order_no' => $orderNo,
+                'user_id' => $userId,
+                'store_id' => $storeId,
+                'meal_id' => $mealId,
+                'type' => $type,
+                'total' => $goodsTotal, // 商品总价
+                'num' => $orderCount,
+                'bd_score' => $bdScoreTotal, // 折扣后商品总价(不含运费)
+                'profit_total' => $profitTotal, // 折扣后商品总价(不含运费)
+                'pay_money' => $orderTotal, // 折扣后商品总价(不含运费)
+                'pay_total' => $type==2 && $bdScoreTotal>0?moneyFormat($orderTotal + $bdScoreTotal,2):$orderTotal, // 折扣后商品总价(不含运费)
+                'pay_status' => 20, // 折扣后商品总价(不含运费)
+                'transaction_id' => 'PAY' . time() . rand(1000, 9999),
+                'discount_point' => $discountPoint,
+                'discount_total' => $discountTotal, // 折扣金额
+                'delivery_fee' => $deliveryFee, // 运费
+                'receiver_name' => $realname,
+                'receiver_mobile' => $mobile,
+                'receiver_area' => $area,
+                'receiver_address' => $address,
+                'remark' => '自动复购',
+                'create_time' => time(),
+                'update_time' => time(),
+                'status' => 2,
+                'mark' => 1,
+            ];
 
 
-        // 订单数据
-        $order = [
-            'order_no' => $orderNo,
-            'user_id' => $userId,
-            'store_id' => $storeId,
-            'meal_id' => $mealId,
-            'type' => $type,
-            'total' => $goodsTotal, // 商品总价
-            'num' => $orderCount,
-            'bd_score' => $bdScoreTotal, // 折扣后商品总价(不含运费)
-            'profit_total' => $profitTotal, // 折扣后商品总价(不含运费)
-            'pay_money' => $orderTotal, // 折扣后商品总价(不含运费)
-            'pay_total' => $type==2 && $bdScoreTotal>0?moneyFormat($orderTotal + $bdScoreTotal,2):$orderTotal, // 折扣后商品总价(不含运费)
-            'pay_status' => $type==2 && $bdScoreTotal>0? 30 : 10, // 折扣后商品总价(不含运费)
-            'discount_point' => $discountPoint,
-            'discount_total' => $discountTotal, // 折扣金额
-            'delivery_fee' => $deliveryFee, // 运费
-            'receiver_name' => $realname,
-            'receiver_mobile' => $mobile,
-            'receiver_area' => $area,
-            'receiver_address' => $address,
-            'create_time' => time(),
-            'update_time' => time(),
-            'status' => 1,
-            'mark' => 1,
-        ];
+            // 订单处理
+            DB::beginTransaction();
+            if (!$orderId = $this->model->insertGetId($order)) {
+                DB::rollBack();
+                $this->error = '创建订单失败';
+                RedisService::clear($cacheLockKey);
+                return false;
+            }
 
 
-        // 订单处理
-        DB::beginTransaction();
-        if (!$orderId = $this->model->insertGetId($order)) {
-            DB::rollBack();
-            $this->error = '创建订单失败';
-            RedisService::clear($cacheLockKey);
-            return false;
-        }
+            // 订单商品
+            if ($orderGoods && !OrderGoodsModel::insert($orderGoods)) {
+                DB::rollBack();
+                $this->error = '处理订单商品错误';
+                RedisService::clear($cacheLockKey);
+                return false;
+            }
 
 
-        // 订单商品
-        if ($orderGoods && !OrderGoodsModel::insert($orderGoods)) {
-            DB::rollBack();
-            $this->error = '处理订单商品错误';
-            RedisService::clear($cacheLockKey);
-            return false;
-        }
+            // 创业礼包分单
+            $data = $order;
+            $data['total'] = moneyFormat($data['total'] / 6);
+            $data['bd_score'] = moneyFormat($bdScoreTotal / 6, 2);
+            $data['profit_total'] = moneyFormat($profitTotal / 6, 2);
+            $data['pay_total'] = moneyFormat($orderTotal / 6, 2);
+            $data['pay_money'] = moneyFormat($order['pay_money'] / 6, 2);
 
 
-        // 创业礼包分单
-        $data = $order;
-        $data['total'] = moneyFormat($data['total'] / 6);
-        $data['bd_score'] = moneyFormat($bdScoreTotal / 6, 2);
-        $data['profit_total'] = moneyFormat($profitTotal / 6, 2);
-        $data['pay_total'] = moneyFormat($orderTotal / 6, 2);
-        $data['pay_money'] = moneyFormat($order['pay_money'] / 6, 2);
-
-        $subOrders = [];
-        for ($i = 1; $i <= 6; $i++) {
-            $data['order_no'] = get_order_num('BS');
-            $data['main_no'] = $orderNo;
-            if ($i == 6) {
-                $data['total'] = round($order['total'] / 6, 2);
-                $data['bd_score'] = round($bdScoreTotal / 6, 2);
-                $data['profit_total'] = round($profitTotal / 6, 2);
-                $data['pay_total'] = round($orderTotal / 6, 2);
-                $data['pay_money'] = round($order['pay_money'] / 6, 2);
+            $subOrders = [];
+            for ($i = 1; $i <= 6; $i++) {
+                $data['order_no'] = get_order_num('BS');
+                $data['main_no'] = $orderNo;
+                if ($i == 6) {
+                    $data['total'] = round($order['total'] / 6, 2);
+                    $data['bd_score'] = round($bdScoreTotal / 6, 2);
+                    $data['profit_total'] = round($profitTotal / 6, 2);
+                    $data['pay_total'] = round($orderTotal / 6, 2);
+                    $data['pay_money'] = round($order['pay_money'] / 6, 2);
+                }
+
+                $subOrders[] = $data;
             }
             }
 
 
-            $subOrders[] = $data;
-        }
+            //
+            if ($subOrders && !$this->model->insert($subOrders)) {
+                DB::rollBack();
+                $this->error = '处理商品订单失败';
+                RedisService::clear($cacheLockKey);
+                return false;
+            }
 
 
-        //
-        if ($subOrders && !$this->model->insert($subOrders)) {
-            DB::rollBack();
-            $this->error = '处理商品订单失败';
-            RedisService::clear($cacheLockKey);
-            return false;
-        }
+            // 扣除报单积分
+            if($bdScoreTotal>0){
+                if(!MemberModel::where(['id'=>$userId])->update(['bd_score'=>DB::raw("bd_score - {$bdScoreTotal}"),'update_time'=>time()])){
+                    DB::rollBack();
+                    $this->error = '报单积分扣除失败';
+                    RedisService::clear($cacheLockKey);
+                    return false;
+                }
 
 
-        // 扣除报单积分
-        if($bdScoreTotal>0){
-            if(!MemberModel::where(['id'=>$userId])->update(['bd_score'=>DB::raw("bd_score - {$bdScoreTotal}"),'update_time'=>time()])){
+                $data = [
+                    'user_id'=>$userId,
+                    'source_order_no'=>$orderNo,
+                    'user_type'=> 1,
+                    'account_type'=> 3, // 报单积分
+                    'type'=> 1,
+                    'money'=> -$bdScoreTotal,
+                    'after_money'=>moneyFormat($bdScore - $bdScoreTotal,2),
+                    'date'=>date('Y-m-d'),
+                    'create_time'=>time(),
+                    'remark'=> '创业礼包支付',
+                    'status'=>1,
+                    'mark'=>1
+                ];
+                if(!AccountLogModel::insertGetId($data)){
+                    Db::rollBack();
+                    $this->error = '付款处理失败';
+                    return false;
+                }
+            }
+
+            // 余额支付
+            if(!MemberModel::where(['id'=>$userId])->update(['balance'=>DB::raw("balance - {$payTotal}"),'update_time'=>time()])){
                 DB::rollBack();
                 DB::rollBack();
-                $this->error = '报单积分扣除失败';
+                $this->error = '余额支付失败';
                 RedisService::clear($cacheLockKey);
                 RedisService::clear($cacheLockKey);
                 return false;
                 return false;
             }
             }
@@ -696,98 +728,73 @@ class OrderService extends BaseService
                 'user_id'=>$userId,
                 'user_id'=>$userId,
                 'source_order_no'=>$orderNo,
                 'source_order_no'=>$orderNo,
                 'user_type'=> 1,
                 'user_type'=> 1,
-                'account_type'=> 3, // 报单积分
+                'account_type'=> 1, // 余额支付
                 'type'=> 1,
                 'type'=> 1,
-                'money'=> -$bdScoreTotal,
-                'after_money'=>moneyFormat($bdScore - $bdScoreTotal,2),
+                'money'=> -$payTotal,
+                'after_money'=>moneyFormat($balance - $payTotal,2),
                 'date'=>date('Y-m-d'),
                 'date'=>date('Y-m-d'),
                 'create_time'=>time(),
                 'create_time'=>time(),
-                'remark'=> '创业礼包支付',
+                'remark'=> '创业礼包自动复购支付',
                 'status'=>1,
                 'status'=>1,
                 'mark'=>1
                 'mark'=>1
             ];
             ];
             if(!AccountLogModel::insertGetId($data)){
             if(!AccountLogModel::insertGetId($data)){
                 Db::rollBack();
                 Db::rollBack();
-                $this->error = '付款处理失败';
+                $this->error = '自动复购付款处理失败';
                 return false;
                 return false;
             }
             }
-        }
 
 
-        // 余额支付
-        if(!MemberModel::where(['id'=>$userId])->update(['balance'=>DB::raw("balance - {$payTotal}"),'update_time'=>time()])){
-            DB::rollBack();
-            $this->error = '余额支付失败';
-            RedisService::clear($cacheLockKey);
-            return false;
-        }
 
 
-        $data = [
-            'user_id'=>$userId,
-            'source_order_no'=>$orderNo,
-            'user_type'=> 1,
-            'account_type'=> 1, // 余额支付
-            'type'=> 1,
-            'money'=> -$payTotal,
-            'after_money'=>moneyFormat($balance - $payTotal,2),
-            'date'=>date('Y-m-d'),
-            'create_time'=>time(),
-            'remark'=> '创业礼包自动复购支付',
-            'status'=>1,
-            'mark'=>1
-        ];
-        if(!AccountLogModel::insertGetId($data)){
-            Db::rollBack();
-            $this->error = '自动复购付款处理失败';
-            return false;
-        }
-
-
-        // 佣金数据
-        $order['id'] = $orderId;
-        if(!$commission = SettleService::make()->commissionCount($order,$orderGoods,$userInfo, $type)){
-            Db::rollBack();
-            $this->error = '订单结算处理失败';
-            return false;
-        }
-
-        // 结算
-        if(!$settle = SettleService::make()->commissionSettle($orderId)){
-            Db::rollBack();
-            $this->error = '订单结算处理失败';
-            return false;
-        }
+            // 佣金数据
+            $order['id'] = $orderId;
+            if(!$commission = SettleService::make()->commissionCount($order,$orderGoods,$userInfo, $type)){
+                Db::rollBack();
+                $this->error = '订单结算处理失败';
+                return false;
+            }
 
 
-        // 商品库存扣除
-        if ($orderGoods) {
-            foreach ($orderGoods as $item) {
-                $id = isset($item['goods_id']) ? $item['goods_id'] : 0;
-                $num = isset($item['num']) ? $item['num'] : 0;
-                $skuId = isset($item['sku_id']) ? $item['sku_id'] : 0;
-                if ($id && !GoodsModel::where(['id' => $id])->update(['stock' => DB::raw("stock - {$num}"), 'update_time' => time()])) {
-                    DB::rollBack();
-                    RedisService::clear($cacheLockKey);
-                    $this->error = '商品库存处理失败';
-                    return false;
-                }
+            // 结算
+            if(!$settle = SettleService::make()->commissionSettle($orderId)){
+                Db::rollBack();
+                $this->error = '订单结算处理失败';
+                return false;
+            }
 
 
-                if ($skuId && !GoodsSkuModel::where(['id' => $skuId])->update(['stock' => DB::raw("stock - {$num}"), 'update_time' => time()])) {
-                    DB::rollBack();
-                    RedisService::clear($cacheLockKey);
-                    $this->error = '商品库存处理失败';
-                    return false;
+            // 商品库存扣除
+            if ($orderGoods) {
+                foreach ($orderGoods as $item) {
+                    $id = isset($item['goods_id']) ? $item['goods_id'] : 0;
+                    $num = isset($item['num']) ? $item['num'] : 0;
+                    $skuId = isset($item['sku_id']) ? $item['sku_id'] : 0;
+                    if ($id && !GoodsModel::where(['id' => $id])->update(['stock' => DB::raw("stock - {$num}"), 'update_time' => time()])) {
+                        DB::rollBack();
+                        RedisService::clear($cacheLockKey);
+                        $this->error = '商品库存处理失败';
+                        return false;
+                    }
+
+                    if ($skuId && !GoodsSkuModel::where(['id' => $skuId])->update(['stock' => DB::raw("stock - {$num}"), 'update_time' => time()])) {
+                        DB::rollBack();
+                        RedisService::clear($cacheLockKey);
+                        $this->error = '商品库存处理失败';
+                        return false;
+                    }
                 }
                 }
             }
             }
-        }
 
 
-        // 用户操作记录
-        DB::commit();
-        $this->error = '自动复购成功~';
-        RedisService::clear($cacheLockKey);
-        return [
-            'order_id' => $orderId,
-            'total' => $payTotal,
-            'pay_type' => 10,
-        ];
+            // 用户操作记录
+            DB::commit();
+            $this->error = '自动复购成功~';
+            RedisService::clear($cacheLockKey);
+            return [
+                'order_id' => $orderId,
+                'total' => $payTotal,
+                'pay_type' => 10,
+            ];
+        } catch (\Exception $exception){
+            $this->error = $exception->getMessage();
+            return false;
+        }
     }
     }
 
 
     /**
     /**
@@ -808,7 +815,6 @@ class OrderService extends BaseService
             ->where('status', '<=', 5)
             ->where('status', '<=', 5)
             ->where('refund_status', '<=', 0)
             ->where('refund_status', '<=', 0)
             ->count('id');
             ->count('id');
-        $data = $data ? $data->toArray() : [];
         if ($data) {
         if ($data) {
             RedisService::set($cacheKey, $data, rand(5, 10));
             RedisService::set($cacheKey, $data, rand(5, 10));
         }
         }
@@ -1187,7 +1193,6 @@ class OrderService extends BaseService
         DB::commit();
         DB::commit();
         $this->error = '确认收货成功';
         $this->error = '确认收货成功';
         RedisService::clear("caches:orders:completeList");
         RedisService::clear("caches:orders:completeList");
-        RedisService::clear("caches:orders:revenueList");
         return ['id' => $id, 'msg' => $this->error];
         return ['id' => $id, 'msg' => $this->error];
     }
     }
 
 

Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 386 - 357
app/Services/Api/SettleService.php


+ 1 - 1
routes/api.php

@@ -55,7 +55,6 @@ Route::prefix('v1')->middleware('web.login')->group(function() {
     Route::get('/config', [\App\Http\Controllers\Api\v1\IndexController::class, 'config']);
     Route::get('/config', [\App\Http\Controllers\Api\v1\IndexController::class, 'config']);
     Route::get('/index/data', [\App\Http\Controllers\Api\v1\IndexController::class, 'data']);
     Route::get('/index/data', [\App\Http\Controllers\Api\v1\IndexController::class, 'data']);
 
 
-
     // 用户信息
     // 用户信息
     Route::get('/user/info', [\App\Http\Controllers\Api\v1\MemberController::class, 'info']);
     Route::get('/user/info', [\App\Http\Controllers\Api\v1\MemberController::class, 'info']);
     Route::post('/user/authInfo', [\App\Http\Controllers\Api\v1\MemberController::class, 'authInfo']);
     Route::post('/user/authInfo', [\App\Http\Controllers\Api\v1\MemberController::class, 'authInfo']);
@@ -101,6 +100,7 @@ Route::prefix('v1')->middleware('web.login')->group(function() {
     Route::post('/cart/count', [\App\Http\Controllers\Api\v1\CartController::class, 'count']);
     Route::post('/cart/count', [\App\Http\Controllers\Api\v1\CartController::class, 'count']);
 
 
     // 商品
     // 商品
+    Route::post('/goods/data', [\App\Http\Controllers\Api\v1\GoodsController::class, 'data']);
     Route::post('/goods/buyGoods', [\App\Http\Controllers\Api\v1\GoodsController::class, 'buyGoods']);
     Route::post('/goods/buyGoods', [\App\Http\Controllers\Api\v1\GoodsController::class, 'buyGoods']);
     Route::post('/goods/collect', [\App\Http\Controllers\Api\v1\GoodsController::class, 'collect']);
     Route::post('/goods/collect', [\App\Http\Controllers\Api\v1\GoodsController::class, 'collect']);