From e5336610ead2d9f6592d1d67ff4d939eb8911a69 Mon Sep 17 00:00:00 2001
From: liuchaofu <1873441552@qq.com>
Date: Mon, 17 Jun 2024 21:33:42 +0800
Subject: [PATCH] =?UTF-8?q?vip=E7=AD=89=E7=BA=A7=E4=B8=BA1=E6=97=B6?=
 =?UTF-8?q?=E5=8A=A0=E5=85=A5=E5=86=BB=E7=BB=93=E9=87=91=E9=A2=9D=E9=80=BB?=
 =?UTF-8?q?=E8=BE=91?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 app/api/logic/order/OrderLogic.php  |   7 +-
 app/common/logic/PayNotifyLogic.php |   4 +
 app/common/logic/VipLogic.php       | 183 ++++++++++++++++++++++++++++
 3 files changed, 191 insertions(+), 3 deletions(-)
 create mode 100644 app/common/logic/VipLogic.php

diff --git a/app/api/logic/order/OrderLogic.php b/app/api/logic/order/OrderLogic.php
index 78dc82275..1728473f1 100644
--- a/app/api/logic/order/OrderLogic.php
+++ b/app/api/logic/order/OrderLogic.php
@@ -99,12 +99,13 @@ class OrderLogic extends BaseLogic
                 $cart_select[$k]['old_cart_id'] = $v['id'];
                 $cart_select[$k]['cart_num'] = $v['cart_num'];
                 $cart_select[$k]['verify_code'] = $params['verify_code'] ?? '';
-                
+                //vip1待返回金额
+
+                $cart_select[$k]['vip_frozen_price'] = bcsub($cart_select[$k]['pay_price'],$cart_select[$k]['store_price'],2);
+//                d($cart_select[$k]['pay_price'],$cart_select[$k]['store_price'],$cart_select[$k]['vip_price'] );
                 $cartInfo = $cart_select[$k];
                 $cartInfo['name'] = $find['store_name'];
                 $cartInfo['image'] = $find['image'];
-
-                $cartInfo['vip_price'] = 0; //$cart_select[$k]['total'] - $vipPrice ?? 0;
                 $cart_select[$k]['cart_info'] = json_encode($cartInfo);
                 $cart_select[$k]['branch_product_id'] = $find['branch_product_id'];
                 //理论上每笔都是拆分了
diff --git a/app/common/logic/PayNotifyLogic.php b/app/common/logic/PayNotifyLogic.php
index 9a4e21e80..5b2b3ab5e 100644
--- a/app/common/logic/PayNotifyLogic.php
+++ b/app/common/logic/PayNotifyLogic.php
@@ -113,6 +113,10 @@ class PayNotifyLogic extends BaseLogic
 
         $capitalFlowDao = new CapitalFlowLogic($user);
         $capitalFlowDao->userExpense('user_order_purchase_pay', 'order', $order['id'], $order['pay_price']);
+        if($user['user_ship'] ==  1){
+            VipLogic::dealVipAmount($order,PayEnum::PURCHASE_FUNDS);
+        }
+
         // self::afterPay($order);
         // Redis::send('push-platform-print', ['id' => $order['id']], 60);
         // PushService::push('store_merchant_' . $order['store_id'], $order['store_id'], ['type' => 'store_merchant', 'msg' => '您有一笔新的订单']);
diff --git a/app/common/logic/VipLogic.php b/app/common/logic/VipLogic.php
new file mode 100644
index 000000000..d47b95051
--- /dev/null
+++ b/app/common/logic/VipLogic.php
@@ -0,0 +1,183 @@
+<?php
+
+
+namespace app\common\logic;
+
+use app\common\enum\OrderEnum;
+use app\common\model\store_order\StoreOrder;
+use app\common\model\store_order_cart_info\StoreOrderCartInfo;
+use app\common\model\system_store\SystemStore;
+use app\common\model\user\User;
+use app\common\model\user\UserAddress;
+use app\common\model\user_sign\UserSign;
+use think\facade\Db;
+
+/**
+ * vip结算逻辑
+ * Class VipLogic
+ * @package app\common\logic
+ */
+class VipLogic  extends BaseLogic
+{
+
+
+    public static function dealVipAmount($order,$pay_type =1, $transaction_id = 0)
+    {
+        $detail = StoreOrderCartInfo::where('oid',$order['id'])->select()->toArray();
+        $total_vip = 0;
+        foreach ($detail as $value){
+            $total_vip +=$value['cart_info']['vip_frozen_price'];
+        }
+        $data=[
+            'order_id' => $order['id'],
+            'transaction_id' => $transaction_id??0,
+            'order_sn' =>$order['order_id'],
+            'user_id' => $order['uid'],
+//            'financial_type' => $financialType,
+            'number' => $total_vip,
+            'pay_type' => $pay_type??1,
+            'status' => 0,
+            'store_id' => $order['store_id'],
+            'staff_id' => $order['staff_id'],
+            'create_time'=>time()
+        ];
+        Db::name('vip_flow')->insert($data);
+        //todo 限制执行
+//        self::afterPay($order,$transaction_id,$total_vip);
+        return true;
+    }
+    
+    
+    
+    
+    
+    
+
+    public static function afterPay($order, $transaction_id = 0,$Vipmoney=0)
+    {
+        $financeLogic = new StoreFinanceFlowLogic();
+        $financeLogic->order = $order;
+        $financeLogic->user = ['uid' => $order['uid']];
+       
+        $financeLogic->in($transaction_id, $Vipmoney, OrderEnum::USER_ORDER_PAY);  //用户订单支付
+        $count_frees = 0;
+    
+        //平台手续费
+        $fees = bcdiv(bcmul($Vipmoney, '0.02', 2), 1, 2);
+        $count_frees = bcadd($count_frees, $fees, 2);
+        if ($fees > 0) {
+            $financeLogic->in($transaction_id, $fees, OrderEnum::ORDER_HANDLING_FEES, $order['store_id'], 0, 0, $order['pay_type']);  //平台手续费
+            $financeLogic->out($transaction_id, $fees, OrderEnum::ORDER_HANDLING_FEES, $order['store_id'], $order['staff_id'], 0, $order['pay_type']); //商户平台手续费支出
+        }
+        // $frozen = bcsub($order->profit, $fees, 2);
+        //缴纳齐全了就加商户没有就加到平台
+        $money_limt = SystemStore::where('id', $order['store_id'])->field('paid_deposit,security_deposit')->find();
+        $deposit = bcsub($money_limt['security_deposit'], $money_limt['paid_deposit'], 2); //保证金剩余额度
+        $store_profit = bcdiv(bcmul($Vipmoney, '0.05', 2), 1, 2);
+        $count_frees = bcadd($count_frees, $store_profit, 2);
+        if ($deposit > 0) {
+            if ($deposit > $store_profit) {
+                if ($store_profit > 0) {
+                    $financeLogic->out($transaction_id, $store_profit, OrderEnum::ORDER_MARGIN, $order['store_id'], $order['staff_id'], 0, $order['pay_type']);
+                }
+            } else {
+                $money = bcsub($store_profit, $deposit, 2);
+                if ($deposit > 0) {
+                    $financeLogic->out($transaction_id, $deposit, OrderEnum::ORDER_MARGIN, $order['store_id'], $order['staff_id'], 0, $order['pay_type']);
+                }
+                if ($money) {
+                    SystemStore::where('id', $order['store_id'])->inc('store_money', $money)->update();
+                    $financeLogic->in($transaction_id, $money, OrderEnum::MERCHANT_ORDER_OBTAINS, $order['store_id'], 0, 0, $order['pay_type']);  //平台手续费
+                }
+            }
+        } else {
+            if ($store_profit > 0) {
+                SystemStore::where('id', $order['store_id'])->inc('store_money', $store_profit)->update();
+                $financeLogic->in($transaction_id, $store_profit, OrderEnum::MERCHANT_ORDER_OBTAINS, $order['store_id'], 0, 0, $order['pay_type']);  //平台手续费
+            }
+        }
+        
+        if ($order['is_vip'] >= 1) {
+            if ($order['spread_uid'] > 0) {
+                $financeLogic->other_arr['vip_uid'] = $order['spread_uid'];
+                $fees = bcdiv(bcmul($Vipmoney, '0.08', 2), 1, 2);
+                $count_frees = bcadd($count_frees, $fees, 2);
+                if ($fees > 0) {
+                    User::where('id', $order['spread_uid'])->inc('now_money', $fees)->update();
+                    $financeLogic->in($transaction_id, $fees, OrderEnum::VIP_ORDER_OBTAINS, $order['store_id'], 0, 0, $order['pay_type']);  //vip订单获得
+                    $financeLogic->out($transaction_id, $fees, OrderEnum::VIP_ORDER_OBTAINS, $order['store_id'], $order['staff_id'], 0, $order['pay_type']);
+                }
+            }
+            $fees = bcdiv(bcmul($Vipmoney, '0.01', 2), 1, 2);
+            $count_frees = bcadd($count_frees, bcmul($fees, 3, 2), 2);
+            $village_uid = 0;
+            $brigade_uid = 0;
+            //查询用户对应的村长和队长
+            if ($order['uid'] > 0) {
+                $address = UserAddress::where(['uid' => $order['uid'], 'is_default' => 1])->find();
+                if ($address) {
+                    $arr1 = UserAddress::where(['village' => $address['village'], 'is_default' => 1])->column('uid');
+                    if ($arr1) {
+                        $village_uid = User::where('id', 'in', $arr1)->where('user_ship', 2)->value('id');
+                        if($village_uid){
+                            User::where('id', $village_uid)->inc('integral', $fees)->update();
+                        }
+                    }
+                    $arr2 = UserAddress::where(['village' => $address['village'], 'brigade' => $address['brigade'], 'is_default' => 1])->column('uid');
+                    if ($arr2) {
+                        $brigade_uid = User::where('id', 'in', $arr1)->where('user_ship', 3)->value('id');
+                        if($brigade_uid){
+                            User::where('id', $brigade_uid)->inc('integral', $fees)->update();
+                        }
+                    }
+                }
+            }
+            if ($fees > 0) {
+                //村长获得
+                $sing=[];
+                $user_sing=new UserSign();
+
+                $sing[]=[
+                    'uid'=>$village_uid,
+                    'title'=>'村长订单获得兑换券',
+                    'store_id'=>$order['store_id'],
+                    'number'=>$fees,
+                ];
+                $sing[]=[
+                    'uid'=>$brigade_uid,
+                    'title'=>'队长订单获得兑换券',
+                    'store_id'=>$order['store_id'],
+                    'number'=>$fees,
+                ];
+                // if ($village_uid > 0) {
+                // $financeLogic->other_arr['vip_uid'] = $village_uid;
+                // }
+                // $financeLogic->in($transaction_id, $fees, OrderEnum::VILLAGE_ORDER_OBTAINS, $order['store_id'], 0, 0, $order['pay_type']);
+                // $financeLogic->out($transaction_id, $fees, OrderEnum::VILLAGE_ORDER_OBTAINS, $order['store_id'], $order['staff_id'], 0, $order['pay_type']);
+                //队长获得
+                // if ($brigade_uid > 0) {
+                //     $financeLogic->other_arr['vip_uid'] = $brigade_uid;
+                // }
+                // $financeLogic->in($transaction_id, $fees, OrderEnum::BRIGADE_ORDER_OBTAINS, $order['store_id'], 0, 0, $order['pay_type']);
+                // $financeLogic->out($transaction_id, $fees, OrderEnum::BRIGADE_ORDER_OBTAINS, $order['store_id'], $order['staff_id'], 0, $order['pay_type']);
+                $user_sing->saveAll($sing);
+                //其他获得
+                $financeLogic->other_arr['vip_uid'] = 0;
+                $financeLogic->in($transaction_id, $fees, OrderEnum::OTHER_ORDER_OBTAINS, $order['store_id'], 0, 0, $order['pay_type']);
+                $financeLogic->out($transaction_id, $fees, OrderEnum::OTHER_ORDER_OBTAINS, $order['store_id'], $order['staff_id'], 0, $order['pay_type']);
+            }
+        }
+        
+        $fees=bcsub($Vipmoney, $count_frees, 2);
+        //供应链订单获得
+        if ($fees > 0) {
+            $financeLogic->in($transaction_id,$fees , OrderEnum::SUPPLIER_ORDER_OBTAINS, $order['store_id'], 0, 0, $order['pay_type']);
+            $financeLogic->out($transaction_id, $fees, OrderEnum::SUPPLIER_ORDER_OBTAINS, $order['store_id'], $order['staff_id'], 0, $order['pay_type']);
+
+        }
+        $financeLogic->save();
+
+    }
+    
+
+}
\ No newline at end of file