Quellcode durchsuchen

红包规则变动后,相应代码所做修改

shizhongqi vor 5 Monaten
Ursprung
Commit
696c2f00ea

+ 18 - 4
app-hd/controllers/HbController.php

@@ -236,6 +236,16 @@ class HbController extends BaseController
         util::success($list);
     }
 
+    /**
+     * 充值送红包规则详情
+     */
+    public function actionHbRuleDetail()
+    {
+        $id = Yii::$app->request->get('id');
+        $rule = RechargeShbClass::getById($id);
+        util::success($rule);
+    }
+
     /**
      * 充值送红包规则创建
      */
@@ -243,8 +253,12 @@ class HbController extends BaseController
     {
         $data = Yii::$app->request->post();
 
-        $validatedRows = [];
-        foreach ($data as $item) {
+        $id = $data['id'];
+        $amount = $data['amount'];
+        $rules = $data['rules'];
+
+        $validatedRules = [];
+        foreach ($rules as $item) {
             if (!is_array($item)) {
                 util::fail('参数格式错误');
                 return;
@@ -254,10 +268,10 @@ class HbController extends BaseController
             if (!$form->validateForm()) {
                 return;
             }
-            $validatedRows[] = $form->attributes;
+            $validatedRules[] = $form->attributes;
         }
 
-        $hbRules = RechargeShbClass::setRules($this->shopId, $this->mainId, $validatedRows);
+        $hbRules = RechargeShbClass::setRules($this->shopId, $this->mainId, $id, $amount, $validatedRules);
         util::success($hbRules);
     }
 

+ 25 - 51
app-hd/models/hb/SendHbRuleItemForm.php

@@ -9,17 +9,11 @@ use hd\models\BaseForm;
  */
 class SendHbRuleItemForm extends BaseForm
 {
-    /** @var int|string 规则ID:新增传空字符串,更新传正整数 */
-    public $id;
-
-    /** @var int 充值金额 */
-    public $amount;
-
     /** @var int 红包金额 */
     public $hbAmount;
 
     /** @var int 红包张数 */
-    public $num;
+    public $hbNum;
 
     /** @var int 最低消费金额(0 表示不限制) */
     public $miniCost;
@@ -27,73 +21,53 @@ class SendHbRuleItemForm extends BaseForm
     /** @var int 有效天数(0 表示永不过期) */
     public $duration;
 
+    /** @var int 生效类型
+     * 1: 日期 2: 天数
+     */
+    public $effectiveType;
+
+    /** @var string 生效日期 */
+    public $effectiveDate;
+
+    /** @var int 生效天数 */
+    public $effectiveDays;
+
     /**
      * {@inheritdoc}
      */
     public function rules()
     {
         return [
-            [['id'], 'default', 'value' => ''],
-            [['num'], 'default', 'value' => 1],
             [['miniCost', 'duration'], 'default', 'value' => 0],
-
-            [['amount'], 'required', 'message' => '请填写充值金额'],
             [['hbAmount'], 'required', 'message' => '请填写红包金额'],
 
             // 注意:不要对字段做 intval 过滤,避免 "abc" 被转成 0 导致校验绕过
-            [['amount', 'hbAmount'], 'number', 'min' => 0.01, 'tooSmall' => '{attribute}必须大于等于0.01'],
-            [['num'], 'integer', 'message' => '{attribute}必须为正整数', 'min' => 1, 'tooSmall' => '{attribute}最小不能小于1'],
-            [['miniCost'], 'integer', 'message' => '{attribute}必须为正整数', 'min' => 0, 'tooSmall' => '{attribute}不能小于0'],
+            [['hbAmount'], 'number', 'min' => 0.01, 'tooSmall' => '{attribute}必须大于等于0.01'],
+            [['hbNum', 'miniCost'], 'integer', 'message' => '{attribute}必须为正整数', 'min' => 0, 'tooSmall' => '{attribute}不能小于0'],
             [['duration'], 'integer', 'min' => 0, 'max' => 3650, 'tooSmall' => '{attribute}不能小于0', 'tooBig' => '{attribute}过大'],
-
-            [['id'], 'validateId'],
+            [['effectiveType'], 'in', 'range' => [1, 2]],
+            [['effectiveDate'], 'default', 'value' => null],
+            [['effectiveDate'], 'filter', 'filter' => function($value) {
+                return $value === '0000-00-00' ? null : $value;
+            }],
+            [['effectiveDate'], 'datetime', 'format' => 'php:Y-m-d'],
+            [['effectiveDays'], 'integer', 'min' => 0, 'max' => 365, 'tooSmall' => '{attribute}不能小于0', 'tooBig' => '{attribute}过大'],
         ];
     }
 
-    /**
-         * 新增时 id 允许为空字符串;更新时必须为正整数。
-         *
-     * @param string $attribute
-     */
-    public function validateId($attribute)
-    {
-        $value = $this->$attribute;
-        if ($value === '' || $value === null) {
-            return;
-        }
-
-        if (is_int($value)) {
-            if ($value < 1) {
-                $this->addError($attribute, '规则ID参数错误');
-            }
-            return;
-        }
-
-        if (is_string($value) && ctype_digit($value)) {
-            $intVal = (int)$value;
-            if ($intVal < 1) {
-                $this->addError($attribute, '规则ID参数错误');
-                return;
-            }
-            $this->$attribute = $intVal;
-            return;
-        }
-
-        $this->addError($attribute, '规则ID参数错误');
-    }
-
     /**
      * {@inheritdoc}
      */
     public function attributeLabels()
     {
         return [
-            'id' => '规则ID',
-            'amount' => '充值金额',
             'hbAmount' => '红包金额',
-            'num' => '红包张数',
+            'hbNum' => '红包数量',
             'miniCost' => '最低消费金额',
             'duration' => '有效天数',
+            'effectiveType' => '生效类型',
+            'effectiveDate' => '生效日期',
+            'effectiveDays' => '生效天数',
         ];
     }
 }

+ 66 - 0
app-mall/controllers/NoticeController.php

@@ -19,6 +19,9 @@ use common\components\util;
 use common\components\dict;
 use biz\shop\classes\ShopExtClass;
 use common\components\lakala\Lakala;
+use bizHd\recharge\classes\RechargeShbClass;
+use bizHd\hb\classes\HbClass;
+use bizHd\hb\classes\HbManageClass;
 
 class NoticeController extends PublicController
 {
@@ -221,6 +224,69 @@ class NoticeController extends PublicController
                     Yii::$app->redis->executeCommand('SETEX', [$myKey, 864000, 'has']);
                     $recharge = RechargeClass::getByCondition(['orderSn' => $orderSn], true);
                     ShopExtClass::skRechargeReport($recharge);
+
+                    //充值送红包 -- 有多处,请搜索:充值送红包处理
+                    $shopId = $recharge->shopId;
+                    $amount = $recharge->amount;
+                    $maxHbRule = RechargeShbClass::getByCondition(['shopId' => $shopId, 'amount<=' => $amount], false, 'allHbAmount DESC');
+                    
+                    if (!empty($maxHbRule)) {
+                        $currentTime = time();
+                        
+                        $rulesJson = $maxHbRule['rules'] ?? '';
+                        $rulesArr = json_decode($rulesJson, true);
+                        
+                        if (is_array($rulesArr) && !empty($rulesArr)) {
+                            foreach ($rulesArr as $rule) {
+                                $hbAmount = $rule['hbAmount'];
+                                $hbNum = $rule['hbNum'];
+                                $miniCost = $rule['miniCost'];
+                                $duration = $rule['duration'];
+                                $effectiveType = $rule['effectiveType'] ?? 2;
+                                $effectiveDate = $rule['effectiveDate'] ?? '';
+                                $effectiveDays = $rule['effectiveDays'] ?? 0;
+
+                                if ($effectiveType == 1 && !empty($effectiveDate) && $effectiveDate != '0000-00-00') {
+                                    $beginTime = strtotime($effectiveDate);
+                                    if (!$beginTime) {
+                                        $beginTime = $currentTime;
+                                    }
+                                } else {
+                                    $beginTime = $currentTime + 86400 * $effectiveDays;
+                                }
+
+                                //给用户生成红包
+                                $data = [
+                                    'mainId' => $recharge->mainId,
+                                    'shopId' => $shopId,
+                                    'userId' => $recharge->userId,
+                                    'customId' => $recharge->customId,
+                                    'amount' => $hbAmount,
+                                    'minConsume' => $miniCost,
+                                    'beginTime' => $beginTime,
+                                    'endTime' => $duration == 0 ? 4102416000 : ($beginTime + 86400 * $duration),
+                                    'willTime' => $duration == 0 ? 4102416000 : ($beginTime + 86400 * $duration),
+                                    'duration' => $duration,
+                                    'getType' => 2, //取得方式 0新人领取 1门店发放 2充值赠送
+                                    'remark' => '充值赠送红包',
+                                ];
+                        
+                                $manageData = [
+                                    'getType' => 2,
+                                    'getTargetId' => 0,
+                                    'createStaffId' => 0,
+                                    'createStaffName' => '',
+                                    'remark' => $data['remark'] ?? '',
+                                ];
+                                for ($i = 0; $i < $hbNum; $i++) {
+                                    $hb = HbClass::add($data);
+                        
+                                    $manageData['hbId'] = $hb['id'];
+                                    HbManageClass::add($manageData);
+                                }
+                            }
+                        }
+                    }
                 }
             }
 

+ 56 - 41
biz-hd/recharge/classes/RechargeClass.php

@@ -154,7 +154,7 @@ class RechargeClass extends BaseClass
         }
 
         $shop = ShopClass::getById($shopId, true);
-        $rechargeWeal = $shop->rechargeWeal ?? 0;
+        $rechargeWeal = $shop->rechargeWeal ?? 0; //充值赠送: 0未设置 1设置了,充值送钱 2设置了,充值送红包
         $balanceGive = 0;
 
         //如果客户有欠款、结账充值、售后充值、售后付款充值,不参与优惠
@@ -175,50 +175,65 @@ class RechargeClass extends BaseClass
                     }
                 }
             } elseif ($rechargeWeal == 2) {
-                //充值送红包
+                //充值送红包 -- 有多处,请搜索:充值送红包处理
                 // 1.根据充值金额,查询符合条件的红包规则
-                $hbRules = RechargeShbClass::getAllByCondition(['shopId' => $shopId, 'amount<=' => $amount]);
+                $maxHbRule = RechargeShbClass::getByCondition(['shopId' => $shopId, 'amount<=' => $amount], false, 'allHbAmount DESC');
                 
-                // 2.遍历符合条件的红包规则,找出最大的红包金额(要结合红包数量来判断)
-                $maxHbAmount = 0;
-                $maxHbRule = null;
-                foreach ($hbRules as $hbRule) {
-                    $totalMoney = $hbRule['hbAmount'] * $hbRule['num'];
-                    if ($totalMoney > $maxHbAmount) {
-                        $maxHbAmount = $totalMoney;
-                        $maxHbRule = $hbRule;
-                    }
-                }
                 if (!empty($maxHbRule)) {
                     $currentTime = time();
-                    //给用户生成红包
-                    $data = [
-                        'mainId' => $mainId,
-                        'shopId' => $shopId,
-                        'userId' => $recharge->userId,
-                        'customId' => $customId,
-                        'amount' => $maxHbRule['hbAmount'],
-                        'minConsume' => $maxHbRule['miniCost'],
-                        'beginTime' => $currentTime,
-                        'endTime' => $maxHbRule['duration'] == 0 ? 4102416000 : ($currentTime + 86400 * $maxHbRule['duration']),
-                        'willTime' => $maxHbRule['duration'] == 0 ? 4102416000 : ($currentTime + 86400 * $maxHbRule['duration']),
-                        'duration' => $maxHbRule['duration'],
-                        'getType' => 2, //取得方式 0新人领取 1门店发放 2充值赠送
-                        'remark' => '充值赠送红包',
-                    ];
-            
-                    $manageData = [
-                        'getType' => 2,
-                        'getTargetId' => 0,
-                        'createStaffId' => 0,
-                        'createStaffName' => '',
-                        'remark' => $data['remark'] ?? '',
-                    ];
-                    for ($i = 0; $i < $maxHbRule['num']; $i++) {
-                        $hb = HbClass::add($data);
-            
-                        $manageData['hbId'] = $hb['id'];
-                        HbManageClass::add($manageData);
+                    
+                    $rulesJson = $maxHbRule['rules'] ?? '';
+                    $rulesArr = json_decode($rulesJson, true);
+                    
+                    if (is_array($rulesArr) && !empty($rulesArr)) {
+                        foreach ($rulesArr as $rule) {
+                            $hbAmount = $rule['hbAmount'];
+                            $hbNum = $rule['hbNum'];
+                            $miniCost = $rule['miniCost'];
+                            $duration = $rule['duration'];
+                            $effectiveType = $rule['effectiveType'] ?? 2;
+                            $effectiveDate = $rule['effectiveDate'] ?? '';
+                            $effectiveDays = $rule['effectiveDays'] ?? 0;
+
+                            if ($effectiveType == 1 && !empty($effectiveDate) && $effectiveDate != '0000-00-00') {
+                                $beginTime = strtotime($effectiveDate);
+                                if (!$beginTime) {
+                                    $beginTime = $currentTime;
+                                }
+                            } else {
+                                $beginTime = $currentTime + 86400 * $effectiveDays;
+                            }
+
+                            //给用户生成红包
+                            $data = [
+                                'mainId' => $mainId,
+                                'shopId' => $shopId,
+                                'userId' => $recharge->userId,
+                                'customId' => $customId,
+                                'amount' => $hbAmount,
+                                'minConsume' => $miniCost,
+                                'beginTime' => $beginTime,
+                                'endTime' => $duration == 0 ? 4102416000 : ($beginTime + 86400 * $duration),
+                                'willTime' => $duration == 0 ? 4102416000 : ($beginTime + 86400 * $duration),
+                                'duration' => $duration,
+                                'getType' => 2, //取得方式 0新人领取 1门店发放 2充值赠送
+                                'remark' => '充值赠送红包',
+                            ];
+                    
+                            $manageData = [
+                                'getType' => 2,
+                                'getTargetId' => 0,
+                                'createStaffId' => 0,
+                                'createStaffName' => '',
+                                'remark' => $data['remark'] ?? '',
+                            ];
+                            for ($i = 0; $i < $hbNum; $i++) {
+                                $hb = HbClass::add($data);
+                    
+                                $manageData['hbId'] = $hb['id'];
+                                HbManageClass::add($manageData);
+                            }
+                        }
                     }
                 }
             } else {

+ 37 - 32
biz-hd/recharge/classes/RechargeShbClass.php

@@ -14,50 +14,55 @@ class RechargeShbClass extends BaseClass
      * 设置充值送红包规则
      * @param int $shopId
      * @param int $mainId
-     * @param array $data
+     * @param int $id
+     * @param array $rulesData
      * @return array
      */
-    public static function setRules($shopId, $mainId, $data)
+    public static function setRules($shopId, $mainId, $id, $amount, $rulesData)
     {
-        if (empty($data)) {
+        if (empty($rulesData)) {
             return [];
         }
-
-        $updateRows = [];
         $createRows = [];
 
         $connection = Yii::$app->db;
         $transaction = $connection->beginTransaction();
         try {
-            foreach ($data as $item) {
-                if ($item['id'] == '') {
-                    $createRows[] = [
-                        'shopId' => $shopId,
-                        'mainId' => $mainId,
-                        'amount' => $item['amount'],
-                        'hbAmount' => $item['hbAmount'],
-                        'num' => $item['num'] ?? 1,
-                        'miniCost' => $item['miniCost'] ?? 0, //最低消费金额,0表示没有要求
-                        'duration' => $item['duration'] == 0 ? 0 :$item['duration'], //0表示永不过期
-                    ];
-                } else {
-                    $row = [
-                        'shopId' => $shopId,
-                        'mainId' => $mainId,
-                        'amount' => $item['amount'],
-                        'hbAmount' => $item['hbAmount'],
-                        'num' => $item['num'] ?? 1,
-                        'miniCost' => $item['miniCost'] ?? 0, //最低消费金额,0表示没有要求
-                        'duration' => $item['duration'] == 0 ? 0 :$item['duration'], //0表示永不过期
-                    ];
-                    self::updateById($item['id'], $row);
-                    $updateRows[] = $row;
-                }
+            $num = 0;
+            $allHbAmount = 0;
+            foreach ($rulesData as $item) {
+                $num += $item['hbNum'];
+                $allHbAmount += $item['hbAmount'] * $item['hbNum'];
+                $createRows[] = [
+                    'hbAmount' => $item['hbAmount'],
+                    'hbNum' => $item['hbNum'],
+                    'miniCost' => $item['miniCost'] ?? 0, //最低消费金额,0表示没有要求
+                    'duration' => $item['duration'] == 0 ? 0 : $item['duration'], //0表示永不过期
+                    'effectiveType' => $item['effectiveType'],
+                    'effectiveDate' =>  $item['effectiveType'] == 1 ? ($item['effectiveDate'] ?? date('Y-m-d')) : '0000-00-00',
+                    'effectiveDays' => $item['effectiveDays'] ?? 0
+                ];
             }
+            $createRowsStr = json_encode($createRows);
 
-            if (!empty($createRows)) {
-                self::batchAdd($createRows);
+            if ($id == '') {
+                self::add([
+                    'shopId' => $shopId,
+                    'mainId' => $mainId,
+                    'amount' => $amount,
+                    'num' => $num,
+                    'allHbAmount' => $allHbAmount,
+                    'rules' => $createRowsStr
+                ]);
+            } else {
+                self::updateById($id, [
+                    'amount' => $amount,
+                    'num' => $num,
+                    'allHbAmount' => $allHbAmount,
+                    'rules' => $createRowsStr
+                ]);
             }
+
             $transaction->commit();
         } catch (\Exception $e) {
             $transaction->rollBack();
@@ -65,7 +70,7 @@ class RechargeShbClass extends BaseClass
             throw $e;
         }
 
-        return ['createRows' => $createRows, 'updateRows' => $updateRows];
+        return ['createRows' => $createRows];
     }
 
 }