wesmiler 2 лет назад
Родитель
Сommit
2366767bbd
3 измененных файлов с 232 добавлено и 447 удалено
  1. 11 0
      app/Models/OrderModel.php
  2. 216 446
      app/Services/Api/OrderService.php
  3. 5 1
      resources/lang/zh-cn/api.php

+ 11 - 0
app/Models/OrderModel.php

@@ -34,6 +34,17 @@ class OrderModel extends BaseModel
     }
     }
 
 
     /**
     /**
+     * 订单用户
+     * @return \Illuminate\Database\Eloquent\Relations\HasOne
+     */
+    public function member()
+    {
+        return $this->hasOne(MemberModel::class, 'id','user_id')
+            ->where(['mark'=>1])
+            ->select(['id','nickname','parent_id','balance','usdt','score','wait_score','status']);
+    }
+
+    /**
      * 获取信息
      * 获取信息
      * @param int $id
      * @param int $id
      * @return array|string
      * @return array|string

+ 216 - 446
app/Services/Api/OrderService.php

@@ -532,449 +532,6 @@ class OrderService extends BaseService
     }
     }
 
 
     /**
     /**
-     * 购买商品
-     * @param $userId 用户ID
-     * @param $params
-     * @return array|false
-     */
-    public function buySubmit1($userId, $params)
-    {
-        $payType = isset($params['pay_type'])? intval($params['pay_type']) : 0;
-        $type = isset($params['type']) && $params['type']? intval($params['type']) : 1;
-        $amount = isset($params['total']) && $params['total']? floatval($params['total']) : 0;
-        $couponId = isset($params['coupon_id'])? intval($params['coupon_id']) : 0;
-        $addressId = isset($params['address_id'])? intval($params['address_id']) : 0;
-        $freightAddressId = isset($params['freight_address_id'])? intval($params['freight_address_id']) : 0;
-        $payPassword = isset($params['pay_password'])? trim($params['pay_password']) : '';
-        $skuList = isset($params['sku_list'])? $params['sku_list'] : [];
-        $ids = isset($params['ids'])? $params['ids'] : [];
-        $cartIds = isset($params['cart_ids'])? trim($params['cart_ids']) : '';
-        $cartIds = $cartIds? explode('|', $cartIds) : [];
-        if(empty($skuList) || empty($ids)  || $addressId<=0 || $freightAddressId<=0 || $payType<=0){
-            $this->error = 2420;
-            return false;
-        }
-
-        // 锁
-        $cacheKey = "caches:orders:buy:{$userId}";
-        if(RedisService::get($cacheKey)){
-            $this->error = 1053;
-            return false;
-        }
-
-        // 订单商品
-        $goods = GoodsService::make()->getOrderGoods($userId, $ids);
-        if(empty($goods)){
-            $this->error = 2903;
-            return false;
-        }
-
-        $goodsNum = 0;
-        $orderTotal = 0;
-        $orderXdTotal = 0;
-        $merchId = 0;
-        $goods = [];
-        $orderNo = get_order_num('XS');
-
-        // 价格参数
-        $usdtPrice = RedisService::get("caches:wallets:usdt_rate");
-        if($usdtPrice<=0){
-            $usdtCnyPrice = ConfigService::make()->getConfigByCode('usdt_cny_price', 7.2);
-            $usdtPrice = $usdtCnyPrice>0 && $usdtCnyPrice< 100? $usdtCnyPrice : 0;
-        }
-        $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
-        $xdPrice = $xdPrice > 0 && $xdPrice <= 10000 ? $xdPrice : 100;
-        foreach($goods as &$item){
-            $goodsId = isset($item['goods_id'])? $item['goods_id'] : 0;
-            $merchId = isset($item['merch_id'])? $item['merch_id'] : 0;
-            $retailPrice = isset($item['retail_price'])? $item['retail_price'] : 0;
-            $num = isset($skuList[$goodsId]['num'])? intval($skuList[$goodsId]['num']) : 0;
-            $skuId = isset($skuList[$goodsId]['sku_id'])? intval($skuList[$goodsId]['sku_id']) : 0;
-            if($num>0 && $skuId >0 && $retailPrice>0){
-                $item['price'] = moneyFormat($retailPrice/$usdtPrice * $xdPrice,2);
-                $total = moneyFormat($retailPrice * $num, 2);
-                $xdTotal = moneyFormat($item['price'] * $num, 2);
-
-                // 订单商品
-                $item['order_no'] = $orderNo;
-                $item['total'] = $total;
-                $item['xd_total'] = $xdTotal;
-                $item['num'] = $num;
-                $item['create_time'] = time();
-                $item['update_time'] = time();
-                $item['status'] = 1;
-                $item['mark'] = 1;
-
-                // 订单金额
-                $orderTotal += $total;
-                $orderXdTotal += $xdTotal;
-                $goodsNum += $num;
-            }
-        }
-
-        if($orderXdTotal<=0 || $goodsNum<=0){
-            $this->error ='2904';
-            return false;
-        }
-
-        // 优惠券
-        $couponPrice = 0;
-        if($couponId > 0){
-
-        }
-
-        // 运费
-        $skus = array_values($skuList);
-        if(!$freightData = GoodsService::make()->getFreight($userId, $freightAddressId, $skus)){
-            $this->error ='2905';
-            return false;
-        }
-
-        $freight = isset($freightData['freight'])? floatval($freightData['freight']) : 0;
-        $payTotal = moneyFormat($orderXdTotal+$freight,2);
-
-        if($amount != $payTotal){
-            $this->error ='2906';
-            return false;
-        }
-
-        // 验证用户
-        $userInfo = MemberModel::with(['parent'])->where(['id'=> $userId,'mark'=> 1,'status'=> 1])
-            ->select(['id','nickname','pay_password','balance','usdt','power_num','wait_score','parent_id','parents'])
-            ->first();
-        $userInfo = $userInfo? $userInfo->toArray() : [];
-        $parentInfo = isset($userInfo['parent'])? $userInfo['parent'] : [];
-        $userPayPassword = isset($userInfo['pay_password'])? $userInfo['pay_password'] : '';
-        $userXd = isset($userInfo['balance'])? floatval($userInfo['balance']) : 0.00;
-        $userWaitScore = isset($userInfo['wait_score'])? floatval($userInfo['wait_score']) : 0.00;
-        $userPowerNum = isset($userInfo['power_num'])? floatval($userInfo['power_num']) : 0.00;
-        if(empty($userInfo)){
-            $this->error = 2024;
-            return false;
-        }
-
-        // 余额支付支付密码验证
-        if($payType == 10 && empty($userPayPassword)){
-            $this->error =1040;
-            return false;
-        }
-
-        // 星豆余额支付交易密码
-        $payPassword = get_password($payPassword);
-        if($payType == 10 && $payPassword != $userPayPassword){
-            $this->error = 2038;
-            return false;
-        }
-
-        // 星豆余额支付验证
-        if($payType == 10 && $userXd < $payTotal){
-            $this->error = 2304;
-            return false;
-        }
-
-        // 收货地址
-        $addressInfo = MemberAddressService::make()->getBindInfo($userId, $addressId);
-        $addressText = isset($addressInfo['address_text'])? $addressInfo['address_text'] : '';
-        $city = isset($addressInfo['city'])? $addressInfo['city'] : '';
-        $realname = isset($addressInfo['realname'])? $addressInfo['realname'] : '';
-        $mobile = isset($addressInfo['mobile'])? $addressInfo['mobile'] : '';
-        $address = isset($addressInfo['address'])? $addressInfo['address'] : '';
-        if(empty($addressText) || empty($addressInfo) || empty($realname) || empty($mobile)){
-            $this->error = 2902;
-            return false;
-        }
-
-        $merchUId = 0;
-
-        // 奖励待返积分
-        $waitScoreRate = ConfigService::make()->getConfigByCode('shop_award_score_rate',0);
-        $waitScoreRate = $waitScoreRate>=0 && $waitScoreRate<=1000? $waitScoreRate : 0;
-        $awardWaitScore = moneyFormat($orderXdTotal * $waitScoreRate/100, 2);
-
-        // 算力奖励
-        $powerRate = ConfigService::make()->getConfigByCode('shop_award_power_rate',0);
-        $powerRate = $powerRate>=0 && $powerRate<=1000? $powerRate : 0;
-        $awardPowerNum = moneyFormat($orderXdTotal * $powerRate/100, 2);
-
-        // 订单数据
-        $order = [
-            'user_id'=> $userId,
-            'order_no'=> $orderNo,
-            'merch_id'=> $merchId,
-            'merch_uid'=> $merchUId,
-            'type'=> $type,
-            'price'=> 0.00,
-            'xd_price'=> $xdPrice,
-            'num'=> $goodsNum,
-            'city'=> $city,
-            'real_name'=> $realname,
-            'mobile'=> $mobile,
-            'address'=> $address && $addressText? $addressText.' '.$address : $addressText,
-            'total'=> $orderTotal,
-            'xd_total'=> $orderXdTotal,
-            'pay_type'=> $payType,
-            'pay_money'=> $payTotal,
-            'delivery_type'=> 1,
-            'coupon_id'=> $couponId,
-            'coupon_price'=> $couponPrice,
-            'postage'=> $freight,
-            'award_wait_score'=> $awardWaitScore,
-            'award_power_num'=> $awardPowerNum,
-            'create_time'=> time(),
-            'update_time'=> time(),
-            'remark'=> isset($params['remark'])? trim($params['remark']) : '',
-            'status'=> 1,
-            'mark'=> 1,
-        ];
-
-        // 创建订单
-        DB::beginTransaction();
-        RedisService::set($cacheKey, $order, rand(2,3));
-        if(!$orderId = $this->model->insertGetId($order)){
-            DB::rollBack();
-            $this->error = 2907;
-            RedisService::clear($cacheKey);
-            return false;
-        }
-
-        // 写入订单商品
-        if(!OrderGoodsModel::insert($goods)){
-            DB::rollBack();
-            $this->error = 2908;
-            RedisService::clear($cacheKey);
-            return false;
-        }
-
-        // 支付处理
-        $payment = [];
-        $payStatus = 2;
-        $userAwardWaitScore = 0;
-        $inviteAwardWaitScore = 0;
-        $parentId = isset($parentInfo['id'])? $parentInfo['id'] : 0;
-        $dateTime = date('Y-m-d H:i:s');
-        switch($payType){
-            case 10: // 星豆余额支付
-                // 推荐用户奖励
-                $parentStatus = isset($parentInfo['status'])? $parentInfo['status'] : 0;
-                if($parentInfo && $parentStatus==1 && $awardWaitScore>0){
-                    // 奖励上级待返积分
-                    $inviteWaitScoreRate = ConfigService::make()->getConfigByCode('cost_award_invite_wait_score',0);
-                    $inviteWaitScoreRate = $inviteWaitScoreRate>0 && $inviteWaitScoreRate<100? $inviteWaitScoreRate : 0;
-                    $inviteAwardWaitScore = moneyFormat($awardWaitScore * $inviteWaitScoreRate/100, 2);
-                    $userAwardWaitScore = moneyFormat($awardWaitScore - $inviteAwardWaitScore, 2);
-                }
-
-
-                // 扣除余额
-                $updateData = [
-                    'usdt'=> DB::raw("usdt - {$payTotal}"), // 扣除USDT
-//                    'wait_score'=>DB::raw("wait_score + {$userAwardWaitScore}"),  // 用户待返积分奖励
-//                    'power_num'=>DB::raw("power_num + {$awardPowerNum}"),  // 算力奖励
-                    'update_time'=>time()
-                ];
-                if(!MemberModel::where(['id'=> $userId])->update($updateData)){
-                    DB::rollBack();
-                    $this->error = 1042;
-                    RedisService::clear($cacheKey);
-                    return false;
-                }
-
-                // 余额明细
-                $log = [
-                    'user_id' => $userId,
-                    'source_id' => $orderId,
-                    'source_order_no' => $orderNo,
-                    'type' => 3,
-                    'coin_type' => 2,
-                    'user_type'=> 1,
-                    'money' => $payTotal,
-                    'actual_money' => $payTotal,
-                    'balance' => $userXd,
-                    'create_time' => time(),
-                    'update_time' => time(),
-                    'remark' => "商城购物消费",
-                    'status' => 1,
-                    'mark' => 1,
-                ];
-                if(!AccountLogModel::insertGetId($log)){
-                    DB::rollBack();
-                    $this->error = 2029;
-                    RedisService::clear($cacheKey);
-                    return false;
-                }
-
-                // 更新订单状态
-                $updateData = ['status'=>2,'pay_status'=> 1,'pay_time'=>date('Y-m-d H:i:s'),'update_time'=>time()];
-                if(!$this->model->where(['id'=> $orderId,'mark'=>1])->update($updateData)){
-                    DB::rollBack();
-                    $this->error = 2909;
-                    RedisService::clear($cacheKey);
-                    return false;
-                }
-
-                // 待返积分奖励明细
-                if($userAwardWaitScore>0){
-                    $log = [
-                        'user_id' => $userId,
-                        'source_id' => $orderId,
-                        'source_order_no' => $orderNo,
-                        'type' => 9,
-                        'coin_type' => 5,
-                        'user_type'=> 1,
-                        'money' => $userAwardWaitScore,
-                        'actual_money' => $userAwardWaitScore,
-                        'balance' => $userWaitScore,
-                        'create_time' => time(),
-                        'update_time' => time(),
-                        'remark' => "商城消费奖励",
-                        'status' => 1,
-                        'mark' => 1,
-                    ];
-                    if(!AccountLogModel::insertGetId($log)){
-                        DB::rollBack();
-                        $this->error = 2029;
-                        RedisService::clear($cacheKey);
-                        return false;
-                    }
-                }
-
-                // 算力奖励明细
-                if($awardPowerNum>0){
-                    $log = [
-                        'user_id' => $userId,
-                        'source_id' => $orderId,
-                        'source_order_no' => $orderNo,
-                        'type' => 9,
-                        'coin_type' => 3,
-                        'user_type'=> 1,
-                        'money' => $awardPowerNum,
-                        'actual_money' => $awardPowerNum,
-                        'balance' => $userPowerNum,
-                        'create_time' => time(),
-                        'update_time' => time(),
-                        'remark' => "商城消费奖励",
-                        'status' => 1,
-                        'mark' => 1,
-                    ];
-                    if(!AccountLogModel::insertGetId($log)){
-                        DB::rollBack();
-                        $this->error = 2029;
-                        RedisService::clear($cacheKey);
-                        return false;
-                    }
-
-                }
-
-                // 推荐用户待返积分奖励明细
-                if($inviteAwardWaitScore>0 && $parentId>0){
-                    $updateData = [
-                        'wait_score'=>DB::raw("wait_score + {$inviteAwardWaitScore}"),  // 推荐用户待返积分奖励
-                        'update_time'=>time()
-                    ];
-                    if(!MemberModel::where(['id'=> $parentId])->update($updateData)){
-                        DB::rollBack();
-                        $this->error = 2028;
-                        RedisService::clear($cacheKey);
-                        return false;
-                    }
-
-                    $log = [
-                        'user_id' => $parentId,
-                        'source_id' => $userId,
-                        'source_order_no' => $orderNo,
-                        'type' => 9,
-                        'coin_type' => 5,
-                        'user_type'=> 1,
-                        'money' => $userAwardWaitScore,
-                        'actual_money' => $userAwardWaitScore,
-                        'balance' => $userWaitScore,
-                        'create_time' => time(),
-                        'update_time' => time(),
-                        'remark' => "推荐用户商城消费奖励",
-                        'status' => 1,
-                        'mark' => 1,
-                    ];
-                    if(!AccountLogModel::insertGetId($log)){
-                        DB::rollBack();
-                        $this->error = 2029;
-                        RedisService::clear($cacheKey);
-                        return false;
-                    }
-                }
-
-                $payStatus = 1;
-                break;
-            default:
-                $this->error = 1030;
-                return false;
-        }
-
-        // 更新优惠券状态
-        if($couponId>0 && !MemberCouponModel::where(['id'=> $couponId,'mark'=>1])->update(['status'=> 2,'update_time'=>time()])){
-            DB::rollBack();
-            $this->error = 2908;
-            RedisService::clear($cacheKey);
-            return false;
-        }
-
-        // 购物车处理
-        if($cartIds){
-            CartsModel::where(['user_id'=> $userId,'status'=>1,'mark'=>1])
-                ->whereIn('id', $cartIds)
-                ->update(['status'=>2,'update_time'=>time()]);
-        }
-
-        // 已支付
-        RedisService::clear($cacheKey);
-        if($payStatus == 1){
-
-            // 支付成功订单通知
-            $message = "您在{$dateTime}(UTC+8)创建的订单【{$orderNo}】,金额{$payTotal}星豆,已支付成功。";
-            MessageService::make()->pushMessage($userId,"订单支付成功",$message, 2);
-
-            // 奖励消息
-//            if($userAwardWaitScore>0){
-//                $message = "您在{$dateTime}(UTC+8)成功支付{$payTotal}星豆完成购物,奖励{$userAwardWaitScore}待返积分已到账。";
-//                MessageService::make()->pushMessage($userId,"商城消费奖励",$message, 3);
-//            }
-//
-//
-//            // 算力奖励消息
-//            if($awardPowerNum>0){
-//                $message = "您在{$dateTime}(UTC+8)成功支付{$payTotal}星豆完成购物,奖励{$awardPowerNum}算力已到账。";
-//                MessageService::make()->pushMessage($userId,"商城消费奖励",$message, 3);
-//            }
-//
-//
-//            // 推荐奖励消息
-//            if($parentId>0 && $inviteAwardWaitScore>0){
-//                $message = "您推荐的用户【{$userId}】,在{$dateTime}(UTC+8)购物消费{$payTotal}星豆,奖励{$inviteAwardWaitScore}待返积分已到账。";
-//                MessageService::make()->pushMessage($parentId,"推荐用户商城消费奖励",$message, 3);
-//            }
-
-            DB::commit();
-            $this->error = 2910;
-            return [
-                'id'=> $orderId,
-                'total'=> $payTotal,
-                'pay_type'=> $payType,
-            ];
-        }else{
-            // 下单成功订单通知
-            $message = "您在{$dateTime}(UTC+8)创建订单【{$orderNo}】成功,请尽快完成支付。";
-            MessageService::make()->pushMessage($userId,"订单创建成功",$message, 2);
-
-            DB::commit();
-            $this->error = 2911;
-            return [
-                'id'=> $orderId,
-                'total'=> $payTotal,
-                'pay_type'=> $payType,
-            ];
-        }
-    }
-
-    /**
      * 详情
      * 详情
      * @param $id
      * @param $id
      * @return mixed
      * @return mixed
@@ -1023,9 +580,14 @@ class OrderService extends BaseService
         return $info;
         return $info;
     }
     }
 
 
+    /**
+     * 订单支付
+     * @param $userId
+     * @param $params
+     */
     public function pay($userId, $params)
     public function pay($userId, $params)
     {
     {
-
+        return true;
     }
     }
 
 
     /**
     /**
@@ -1234,6 +796,134 @@ class OrderService extends BaseService
         return ['status'=> $tempStatus,'refund_text'=>'','status_text'=>isset($statusArr[$tempStatus])? $statusArr[$tempStatus]:''];
         return ['status'=> $tempStatus,'refund_text'=>'','status_text'=>isset($statusArr[$tempStatus])? $statusArr[$tempStatus]:''];
     }
     }
 
 
+
+    /**
+     * 售后确认通过
+     * @param $userId
+     * @param $params
+     * @return bool
+     */
+    public function refundConfirm($userId, $params)
+    {
+        $orderId = isset($params['id']) ? $params['id'] : 0;
+        $goodsId = isset($params['goods_id']) ? $params['goods_id'] : 0;
+        $platAfterNo = isset($params['plat_after_no']) ? $params['plat_after_no'] : '';
+        $info = $this->model->with(['goods','member'])->where(['id' => $orderId, 'mark' => 1])
+            ->select(['id','user_id','merch_id','order_no','xd_total','pay_money','coupon_id','status','refund_status','pay_time'])
+            ->first();
+        $status = isset($info['status']) ? $info['status'] : 0;
+        $orderNo = isset($info['order_no']) ? $info['order_no'] : '';
+        $orderUserId = isset($info['user_id']) ? $info['user_id'] : 0;
+        $refundStatus = isset($info['refund_status']) ? $info['refund_status'] : 0;
+        $payTotal = isset($info['pay_money']) ? $info['pay_money'] : 0;
+        $userInfo = isset($info['member']) ? $info['member'] : [];
+        $goods = isset($info['goods']) ? $info['goods'] : [];
+        if (!$orderId || empty($info)) {
+            $this->error = 2912;
+            return false;
+        }
+
+        if($status != 5){
+            $this->error = 2914;
+            return false;
+        }
+
+        if($refundStatus != 1){
+            $this->error = 2927;
+            return false;
+        }
+
+        if(empty($goods)){
+            $this->error = 2933;
+            return false;
+        }
+
+        if(empty($userInfo)){
+            $this->error = 3003;
+            return false;
+        }
+
+        // 售后商品处理
+        DB::beginTransaction();
+        $refundTotal = 0;
+        $goodsName = '';
+        foreach ($goods as $item){
+            $id = isset($item['goods_id'])? $item['goods_id'] : 0;
+            $goodsRefundStatus = isset($item['refund_status'])? $item['refund_status'] : 0;
+            // $goodsPlatAfterNo = isset($item['plat_after_no'])? $item['plat_after_no'] : 0;
+            $xdTotal = isset($item['xd_total'])? $item['xd_total'] : 0;
+            if(($goodsId == 0 && $goodsRefundStatus != 3) || (in_array($goodsRefundStatus,[1,2]) && $id == $goodsId)){
+                if(!OrderGoodsModel::where(['goods_id'=> $id,'order_no'=> $orderNo])->update(['refund_status'=>3,'update_time'=>time()])){
+                    DB::rollBack();
+                    $this->error = 2934;
+                    return false;
+                }
+
+                $refundTotal+= $xdTotal;
+                $goodsName = $goodsId? $item['goods_name'] : '全部';
+            }
+        }
+
+        // 统一退款处理
+        if($refundTotal<=0){
+            DB::rollBack();
+            $this->error = 2935;
+            return false;
+        }
+
+        // 订单售后状态
+        if(!$this->model->where(['id'=> $orderId,'mark'=>1])->update(['refund_status'=> 3,'update_time'=>time()])){
+            DB::rollBack();
+            $this->error = 2932;
+            return false;
+        }
+
+        // 退款进账
+        $updateData = ['balance'=> DB::raw("balance + {$refundTotal}"),'update_time'=>time()];
+        if(!MemberModel::where(['id'=> $orderUserId])->update($updateData)){
+            DB::rollBack();
+            $this->error = 2028;
+            return false;
+        }
+
+        // 账户明细
+        $userXd = isset($userInfo['balance'])? $userInfo['balance'] : 0;
+        $log = [
+            'user_id' => $orderUserId,
+            'source_id' => $orderId,
+            'source_order_no' => $orderNo,
+            'type' => 7,
+            'coin_type' => 2,
+            'user_type'=> 1,
+            'money' => $refundTotal,
+            'actual_money' => $refundTotal,
+            'balance' => $userXd,
+            'create_time' => time(),
+            'update_time' => time(),
+            'remark' => "商城订单售后退款",
+            'status' => 1,
+            'mark' => 1,
+        ];
+        if(!AccountLogModel::insertGetId($log)){
+            DB::rollBack();
+            $this->error = 2029;
+            return false;
+        }
+
+        // 退款消息
+        $dateTime = date('Y-m-d H:i:s');
+        if($goodsId){
+            $message = "您的订单在{$dateTime}(UTC+8)已成功售后:\n售后商品:{$goodsName}\n售后订单:{$orderNo}\n售后交易订单:{$platAfterNo}\n订单金额:{$payTotal}\n退款星豆:{$refundTotal} \n退款前:{$userXd} 星豆\n";
+        }else{
+            $message = "您的订单在{$dateTime}(UTC+8)已成功售后:\n售后商品:{$goodsName}\n售后订单:{$orderNo}\n订单金额:{$payTotal}\n退款星豆:{$refundTotal} \n退款前:{$userXd} 星豆\n";
+        }
+        MessageService::make()->pushMessage($userId, '每日积分返还星豆奖励', $message,3);
+
+        DB::commit();
+        $this->error = 2936;
+        return ['order_id'=>$orderId,'goods_id'=>$goodsId,'plat_after_no'=>$platAfterNo,'pay_total'=>$payTotal,'refund_total'=>$refundTotal];
+    }
+
     /**
     /**
      * 收货/已完成
      * 收货/已完成
      * @return bool
      * @return bool
@@ -1421,7 +1111,7 @@ class OrderService extends BaseService
     }
     }
 
 
     /**
     /**
-     * 更新订单状态
+     * 订单发货状态自动处理
      * @return array|false
      * @return array|false
      */
      */
     public function updateOrderStatus()
     public function updateOrderStatus()
@@ -1439,7 +1129,7 @@ class OrderService extends BaseService
         $pageSize = $catchNum>10 && $catchNum<=200? $catchNum : 100;
         $pageSize = $catchNum>10 && $catchNum<=200? $catchNum : 100;
         $orders = $this->model->whereIn('status',[2,3])
         $orders = $this->model->whereIn('status',[2,3])
             ->where(['mark'=>1])
             ->where(['mark'=>1])
-            ->select(['id','user_id',''])
+            ->select(['id','user_id','order_no','out_order_no','status'])
             ->orderBy('pay_time','asc')
             ->orderBy('pay_time','asc')
             ->offset(($page-1)*$pageSize)
             ->offset(($page-1)*$pageSize)
             ->limit($pageSize)
             ->limit($pageSize)
@@ -1496,4 +1186,84 @@ class OrderService extends BaseService
         RedisService::clear($cacheKey);
         RedisService::clear($cacheKey);
         return ['count'=> count($orders),'success'=> $success, 'delivery'=>$deliverCount];
         return ['count'=> count($orders),'success'=> $success, 'delivery'=>$deliverCount];
     }
     }
+
+    /**
+     * 售后订单自动处理
+     * @return array|false
+     */
+    public function updateOrderRefundStatus()
+    {
+        set_time_limit(0);
+        $cacheKey = "cache:orders:update_lock";
+        if(RedisService::get($cacheKey)){
+            $this->error = 3001;
+            return false;
+        }
+
+        $page = RedisService::get('caches:orders:update_page');
+        $page = $page? $page+1 : 1;
+        $catchNum = ConfigService::make()->getConfigByCode('update_order_num',100);
+        $pageSize = $catchNum>10 && $catchNum<=200? $catchNum : 100;
+        $orders = OrderGoodsModel::from('orders_goods as a')
+            ->leftJoin('orders as b','b.order_no','=','a.order_no')
+            ->whereIn('a.refund_status',[1,2])
+            ->where(['a.mark'=>1])
+            ->select(['b.id','b.user_id','b.order_no','b.out_order_no','a.refund_status','a.goods_id','a.plat_after_no','b.status','a.goods_id','a.sku_sn'])
+            ->orderBy('create_time','asc')
+            ->offset(($page-1)*$pageSize)
+            ->limit($pageSize)
+            ->get();
+        $orders = $orders? $orders->toArray() : [];
+        RedisService::set($cacheKey, $orders, rand(30,60));
+        $success = 0;
+        $refundCount = 0;
+        if($orders){
+            foreach ($orders as $item){
+                $orderId = isset($item['id'])? $item['id'] : 0;
+                $goodsId = isset($item['goods_id'])? $item['goods_id'] : 0;
+                $orderNo = isset($item['order_no'])? $item['order_no'] : '';
+                $outOrderNo = isset($item['out_order_no'])? $item['out_order_no'] : '';
+                $platAfterNo = isset($item['plat_after_no'])? $item['plat_after_no'] : '';
+
+                if(empty($orderNo) || empty($outOrderNo) || empty($platAfterNo)){
+                    continue;
+                }
+                $lockCacheKey = "caches:orders:refund:{$orderNo}_{$platAfterNo}";
+                if(RedisService::get($lockCacheKey.'_lock')){
+                    continue;
+                }
+
+                $data = [
+                    'out_order_no'=> $outOrderNo,
+                    'third_order_no'=> $orderNo,
+                ];
+                $result = SupplyService::make()->getApiData('getAfterStatus', $data);
+                if(empty($result)){
+                    RedisService::clear($lockCacheKey.'_lock');
+                    continue;
+                }
+
+                $orderStatus = isset($result['status'])? $result['status'] : 0;
+                $goodsSkuSn = isset($result['goods_sku']) && $result['goods_sku']? $result['goods_sku'] : '';
+                // 售后中
+                if(in_array($orderStatus,[2,4,5,6])){
+                    if(OrderGoodsModel::where(['plat_after_no'=> $platAfterNo])->update(['refund_status'=>$orderStatus,'update_time'=>time()])){
+                        $refundCount++;
+                    }
+                }
+                // 已完成
+                else if(in_array($orderStatus,[3]))
+                {
+                    if($this->refundConfirm(0,['order_id'=> $orderId,'goods_id'=>$goodsId,'plat_after_no'=>$platAfterNo])){
+                        $success++;
+                    }
+                }
+
+                RedisService::clear($lockCacheKey.'_lock');
+            }
+        }
+
+        RedisService::clear($cacheKey);
+        return ['count'=> count($orders),'success'=> $success, 'refund'=>$refundCount];
+    }
 }
 }

+ 5 - 1
resources/lang/zh-cn/api.php

@@ -208,11 +208,15 @@ return [
     '2930'=> '取消售后成功',
     '2930'=> '取消售后成功',
     '2931'=> '订单不可操作完成,请检查是否已售后处理完成',
     '2931'=> '订单不可操作完成,请检查是否已售后处理完成',
     '2932'=> '订单更新失败',
     '2932'=> '订单更新失败',
+    '2933'=> '无可售后处理的商品',
+    '2934'=> '售后商品处理失败',
+    '2935'=> '无售后退款金额',
+    '2936'=> '售后审核通过',
 
 
 
 
     '3001'=> '订单更新处理中',
     '3001'=> '订单更新处理中',
     '3002'=> '订单结算中',
     '3002'=> '订单结算中',
-    '3003'=> '订单结算用户不存在',
+    '3003'=> '订单用户不存在',
     '3004'=> '订单结算金额错误',
     '3004'=> '订单结算金额错误',
     '3005'=> '没有奖励可结算',
     '3005'=> '没有奖励可结算',
     '3006'=> '没到时间发放',
     '3006'=> '没到时间发放',