소스 검색

1. 会员等级折扣设置修改
2. 开单(或下单)接口的计算加入会员折扣

shizhongqi 1 개월 전
부모
커밋
f3ea283883
3개의 변경된 파일45개의 추가작업 그리고 8개의 파일을 삭제
  1. 12 2
      app-hd/controllers/MemberLevelController.php
  2. 25 5
      biz-hd/custom/classes/CustomClass.php
  3. 8 1
      biz-hd/order/services/OrderService.php

+ 12 - 2
app-hd/controllers/MemberLevelController.php

@@ -91,7 +91,17 @@ class MemberLevelController extends BaseController
                 $level = $v['level'] ?? 1;
                 $name = $v['name'] ?? '会员';
                 $amount = $v['amount'] ?? 0;
-                $discount = floatval($v['discount'] ?? 10);
+                $discount = $v['discount'] ?? 10;
+                if ($discount === '') {
+                    $discount = 10;
+                }
+                if (!is_numeric($discount)) {
+                    util::fail('请输入有效的折扣');
+                }
+                $discount = floatval($discount);
+                if ($discount <= 0 || $discount > 10) {
+                    util::fail('折扣必须大于0且不超过10');
+                }
                 $birthdayBenefit = mb_substr(trim($v['birthdayBenefit'] ?? ''), 0, 50);
                 $benefitDesc = mb_substr(trim($v['benefitDesc'] ?? ''), 0, 500);
                 $it = [
@@ -134,4 +144,4 @@ class MemberLevelController extends BaseController
         }
     }
 
-}
+}

+ 25 - 5
biz-hd/custom/classes/CustomClass.php

@@ -283,7 +283,7 @@ class CustomClass extends BaseClass
         $current = $levelData[$newLevel] ?? [];
         $before = $levelData[$oldLevel] ?? [];
         $beforeName = $before['name'] ?? '普通客户';
-        $discount = $current['discount'] ?? 1;
+        $discount = $current['discount'] ?? 10;
         $memberName = $current['name'] ?? '';
         $oldGrowth = $custom->growth;
 
@@ -980,8 +980,8 @@ class CustomClass extends BaseClass
     {
         $shopId = $data['shopId'];
         $mainId = $data['mainId'];
-        $customs = self::getAllByCondition(['shopId' => $shopId], null, 'id, name, hdId, userId, buyAmount, growth, member, memberName', null, true);
-        $levelDatas = MemberLevelClass::getAllByCondition(['mainId' => $mainId], 'amount DESC', 'id, name, level, amount');
+        $customs = self::getAllByCondition(['shopId' => $shopId], null, 'id, name, hdId, userId, buyAmount, growth, member, memberName, discount', null, true);
+        $levelDatas = MemberLevelClass::getAllByCondition(['mainId' => $mainId], 'amount DESC', 'id, name, level, amount, discount');
         if (!empty($customs) && !empty($levelDatas)) {
             foreach ($customs as $c) {
                 // $buyAmount = 0;
@@ -997,6 +997,7 @@ class CustomClass extends BaseClass
                 // 根据 growth 设置等级
                 $memberData = self::getCustomExpenseLevel($c->growth, $mainId, $levelDatas); // 设置等级多处需要同步修改的,请搜索:getCustomExpenseLevel
                 $newMember = intval($memberData['level']);
+                $newDiscount = floatval($memberData['discount'] ?? 10);
                 $customMember = $c->member;
                 if ($customMember != $newMember) {
                     $hdId = $c->hdId;
@@ -1027,8 +1028,27 @@ class CustomClass extends BaseClass
 
                     $c->member = $memberData['level'];
                     $c->memberName = $memberData['name'];
+                }
+                if ($c->memberName != $memberData['name']) {
+                    $c->memberName = $memberData['name'];
+                }
+                if (floatval($c->discount) != $newDiscount) {
+                    $c->discount = $newDiscount;
+                }
+                if ($c->isAttributeChanged('member') || $c->isAttributeChanged('memberName') || $c->isAttributeChanged('discount')) {
                     $c->save();
                 }
+                if (!empty($c->hdId)) {
+                    $hd = HdClass::getById($c->hdId, true);
+                    if (!empty($hd)) {
+                        $hd->member = $memberData['level'];
+                        $hd->memberName = $memberData['name'];
+                        $hd->discount = $newDiscount;
+                        if ($hd->isAttributeChanged('member') || $hd->isAttributeChanged('memberName') || $hd->isAttributeChanged('discount')) {
+                            $hd->save();
+                        }
+                    }
+                }
 
                 // 更新客户所对应花店的消费金额
                 // HdClass::updateByCondition(['shopId'=>$shopId, 'userId'=>$c->userId], ['expendAmount'=>$buyAmount]);
@@ -1041,7 +1061,7 @@ class CustomClass extends BaseClass
     public static function getCustomExpenseLevel($growth, $mainId, $levelDatas=[])
     {
         if (empty($levelDatas)) {
-            $levelDatas = MemberLevelClass::getAllByCondition(['mainId' => $mainId], 'amount DESC', 'id, name, level, amount');
+            $levelDatas = MemberLevelClass::getAllByCondition(['mainId' => $mainId], 'amount DESC', 'id, name, level, amount, discount');
         }
         foreach ($levelDatas as $data) {
             if ($growth >= $data['amount']) {
@@ -1049,6 +1069,6 @@ class CustomClass extends BaseClass
             }
         }
 
-        return ['level'=>0, 'name'=>''];
+        return ['level'=>0, 'name'=>'', 'discount'=>10];
     }
 }

+ 8 - 1
biz-hd/order/services/OrderService.php

@@ -242,6 +242,13 @@ class OrderService extends BaseService
         $totalPrice = bcsub($totalPrice, $hbAmount, 2);
 
         $modifyPrice = isset($data['modifyPrice']) && $data['modifyPrice'] > 0 ? $data['modifyPrice'] : 0;
+        $memberDiscount = $custom->discount ?? 10;
+        if (is_numeric($memberDiscount) && $memberDiscount > 0 && $memberDiscount < 10 && $modifyPrice > 0) {
+            $modifyPrice = bcdiv(bcmul($modifyPrice, $memberDiscount, 3), 10, 2);
+            if (bccomp($modifyPrice, '0', 2) <= 0) {
+                $modifyPrice = 0.01;
+            }
+        }
         //优惠
         if ($modifyPrice < $totalPrice) {
             $data['discountType'] = dict::getDict('discountType', 'discount');
@@ -542,4 +549,4 @@ class OrderService extends BaseService
         return ['price' => $discountPrice, 'discountType' => $discountType, 'discountAmount' => $discountAmount];
     }
 
-}
+}