Просмотр исходного кода

Merge branch 'zhongqi-hbUpdate'

shish 5 месяцев назад
Родитель
Сommit
390cddd3b5

+ 21 - 5
app-hd/controllers/HbController.php

@@ -63,6 +63,7 @@ class HbController extends BaseController
         $where = [];
         $where['customId'] = $get['customId'];
         $where['status'] = 0;
+        $where['beginTime<='] = time(); // 生效时间小于等于当前时间
         $list = HbClass::getAllByCondition($where, 'id DESC');
         //已经过期的就不再显示,并且作废,多有处作废操作,搜索关键词 cancellation_hb
         if (!empty($list)) {
@@ -104,7 +105,8 @@ class HbController extends BaseController
 
         $duration = (int)$form->days;
         $count = $post['count'] ?? 1;
-        $beginTime = time();
+        $effectiveDate = $post['effectiveDate'];
+        $beginTime = $effectiveDate == '' ? time() : strtotime($effectiveDate);
         if ($duration <= 0) {
             $endTime = 4102416000;
         } else {
@@ -236,6 +238,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 +255,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 +270,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);
     }
 

+ 3 - 0
app-hd/controllers/OrderController.php

@@ -819,6 +819,9 @@ class OrderController extends BaseController
                         $hb->save();
                         util::fail('红包已失效');
                     }
+                    if ($hb->beginTime > time()) {
+                        util::fail('红包可使用期未到');
+                    }
                 } else {
                     Yii::error('没有找到红包,hbId: ', $hbId . ' ' . __METHOD__);
                     util::fail('红包无效');

+ 5 - 1
app-hd/models/hb/CreateHbForm.php

@@ -30,6 +30,9 @@ class CreateHbForm extends BaseForm
     /** @var string 备注 */
     public $remark;
 
+    /** @var string 生效时间 */
+    public $effectiveDate;
+
     /**
      * {@inheritdoc}
      */
@@ -51,7 +54,7 @@ class CreateHbForm extends BaseForm
             [['userId', 'customId'], 'integer', 'min' => 1, 'tooSmall' => '{attribute}参数错误'],
             [['days'], 'integer', 'min' => 0, 'max' => 3650, 'tooSmall' => '{attribute}必须大于等于0', 'tooBig' => '{attribute}过大'],
             [['count'], 'integer', 'min' => 1, 'max' => 1000, 'tooSmall' => '{attribute}必须大于等于1', 'tooBig' => '{attribute}过大'],
-            [['remark'], 'string'],
+            [['remark', 'effectiveDate'], 'string'],
             [['remark'], 'filter', 'filter' => 'trim'],
         ];
     }
@@ -69,6 +72,7 @@ class CreateHbForm extends BaseForm
             'minConsume' => '最低消费',
             'days' => '有效时长',
             'count' => '红包个数',
+            'effectiveDate' => '生效时间',
             'remark' => '备注信息',
         ];
     }

+ 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' => '生效天数',
         ];
     }
 }

+ 1 - 0
app-mall/controllers/HbController.php

@@ -39,6 +39,7 @@ class HbController extends BaseController
         $where = [];
         $where['customId'] = $this->customId;
         $where['status'] = 0;
+        $where['beginTime<='] = time();
         $list = HbClass::getAllByCondition($where, 'id DESC');
         //已经过期的就不再显示,并且作废,多有处作废操作,搜索关键词 cancellation_hb
         if (!empty($list)) {

+ 3 - 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
 {

+ 6 - 0
app-mall/controllers/OrderController.php

@@ -435,6 +435,9 @@ class OrderController extends BaseController
                         $hb->save();
                         util::fail('红包已失效');
                     }
+                    if ($hb->beginTime > time()) {
+                        util::fail('红包可使用期未到');
+                    }
                     if ($modifyPrice < $hb->minConsume) {
                         util::fail("不满足最低消费金额{$hb->minConsume}元");
                     }
@@ -753,6 +756,9 @@ class OrderController extends BaseController
                         $hb->save();
                         util::fail('红包已失效');
                     }
+                    if ($hb->beginTime > time()) {
+                        util::fail('红包可使用期未到');
+                    }
                     if ($modifyPrice < $hb->minConsume) {
                         util::fail("不满足最低消费金额{$hb->minConsume}元");
                     }

+ 0 - 1
biz-hd/hb/classes/HbClass.php

@@ -4,7 +4,6 @@ namespace bizHd\hb\classes;
 
 use bizHd\base\classes\BaseClass;
 use bizHd\hb\models\Hb;
-use common\components\util;
 use Yii;
 
 class HbClass extends BaseClass

+ 55 - 40
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;
 
         //如果客户有欠款、结账充值、售后充值、售后付款充值,不参与优惠
@@ -177,48 +177,63 @@ 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];
     }
 
 }