Kaynağa Gözat

根据前端补充api: 1. 管理员手动标记生日已领取 2. 单个客户发信息通知(限生日是明天)

shizhongqi 2 ay önce
ebeveyn
işleme
5a70899a90

+ 14 - 0
app-hd/controllers/BirthdayGiftController.php

@@ -40,6 +40,12 @@ class BirthdayGiftController extends BaseController
 
     public function actionNotifyTomorrow()
     {
+        $get = Yii::$app->request->get();
+        $id = intval($get['id'] ?? 0);
+        if ($id > 0) {
+            BirthdayGiftClass::notifyOneTomorrow($this->shop, $id);
+            util::complete('通知成功');
+        }
         $result = BirthdayGiftClass::batchNotifyTomorrow($this->shop);
         if ($result['success'] > 0) {
             util::complete('已成功通知' . $result['success'] . '人' . ($result['fail'] > 0 ? ',失败' . $result['fail'] . '人' : ''));
@@ -57,6 +63,14 @@ class BirthdayGiftController extends BaseController
         util::complete('修改成功');
     }
 
+    public function actionCollect()
+    {
+        $get = Yii::$app->request->get();
+        $id = intval($get['id'] ?? 0);
+        BirthdayGiftClass::collectGiftByAdmin($this->shopId, $id);
+        util::complete('更新成功');
+    }
+
     public function actionPrint()
     {
         $get = Yii::$app->request->get();

+ 66 - 13
biz-hd/birthday/classes/BirthdayGiftClass.php

@@ -27,6 +27,7 @@ class BirthdayGiftClass extends BaseClass
     const STATUS_PICKING_UP = 3;     // 3领取中
     const STATUS_COLLECTED = 4;      // 4已领取
     const STATUS_EXPIRED = 5;        // 5超时放弃
+    const EXPIRE_SECONDS = 86400;    // 通知后24小时内回复有效
 
     public static function getStatusMap()
     {
@@ -395,6 +396,7 @@ class BirthdayGiftClass extends BaseClass
             'birthdayDisplay' => $gift->birthdayDisplay,
             'status' => $status,
             'statusName' => $statusMap[$status] ?? '',
+            'canNotify' => $status == self::STATUS_PENDING_NOTIFY && self::matchesBirthdayOffset($custom, 1),
             'pickupTime' => $gift->pickupTime ?? '',
             'collectTime' => $gift->collectTime ?? '',
             'notifyTime' => $gift->notifyTime ?? '',
@@ -411,7 +413,7 @@ class BirthdayGiftClass extends BaseClass
         $page = max(1, intval($params['page'] ?? 1));
         $pageSize = max(1, min(50, intval($params['pageSize'] ?? 20)));
 
-        list($customs, $levelMap) = self::getEligibleCustoms($shop->id, $shop->mainId);
+        list($customs,) = self::getEligibleCustoms($shop->id, $shop->mainId);
         $customMap = [];
         foreach ($customs as $c) {
             $customMap[$c->id] = $c;
@@ -662,23 +664,12 @@ class BirthdayGiftClass extends BaseClass
             if (intval($gift->status) != self::STATUS_PENDING_NOTIFY) {
                 continue;
             }
-            list($ok, $err) = self::sendNotifySms($shop, $gift, $custom);
+            list($ok, $err) = self::notifyGift($shop, $gift, $custom);
             if (!$ok) {
                 $fail++;
                 $messages[] = ($custom->name ?? '') . ':' . $err;
                 continue;
             }
-            $now = date('Y-m-d H:i:s');
-            $expire = date('Y-m-d H:i:s', time() + self::EXPIRE_SECONDS);
-            $gift->status = self::STATUS_PENDING_CLAIM;
-            $gift->notifyTime = $now;
-            $gift->expireTime = $expire;
-            $gift->smsSent = 1;
-            if (empty($gift->orderSn)) {
-                $gift->orderSn = 'BG' . date('ymdHis') . mt_rand(1000, 9999);
-            }
-            $gift->save();
-            self::scheduleExpireDelay($gift->id, strtotime($expire));
             $success++;
         }
         return [
@@ -688,6 +679,50 @@ class BirthdayGiftClass extends BaseClass
         ];
     }
 
+    public static function notifyGift($shop, $gift, $custom)
+    {
+        list($ok, $err) = self::sendNotifySms($shop, $gift, $custom);
+        if (!$ok) {
+            return [false, $err];
+        }
+        $now = date('Y-m-d H:i:s');
+        $expire = date('Y-m-d H:i:s', time() + self::EXPIRE_SECONDS);
+        $gift->status = self::STATUS_PENDING_CLAIM;
+        $gift->notifyTime = $now;
+        $gift->expireTime = $expire;
+        $gift->smsSent = 1;
+        if (empty($gift->orderSn)) {
+            $gift->orderSn = 'BG' . date('ymdHis') . mt_rand(1000, 9999);
+        }
+        $gift->save();
+        self::scheduleExpireDelay($gift->id, strtotime($expire));
+        return [true, ''];
+    }
+
+    public static function notifyOneTomorrow($shop, $giftId)
+    {
+        self::ensureYearRecords($shop);
+        $gift = self::getById($giftId, true);
+        if (empty($gift) || intval($gift->shopId) != intval($shop->id)) {
+            util::fail('记录不存在');
+        }
+        if (intval($gift->status) != self::STATUS_PENDING_NOTIFY) {
+            util::fail('当前状态不可通知');
+        }
+        $custom = CustomClass::getById($gift->customId, true);
+        if (empty($custom) || intval($custom->shopId) != intval($shop->id) || intval($custom->delStatus) != 0) {
+            util::fail('客户信息不存在');
+        }
+        if (!self::matchesBirthdayOffset($custom, 1)) {
+            util::fail('仅明日生日客户可发送通知');
+        }
+        list($ok, $err) = self::notifyGift($shop, $gift, $custom);
+        if (!$ok) {
+            util::fail($err ?: '通知失败');
+        }
+        return $gift;
+    }
+
     public static function getInfoByToken($shopId, $token)
     {
         $gift = self::getByCondition(['shopId' => $shopId, 'claimToken' => $token], true);
@@ -968,4 +1003,22 @@ class BirthdayGiftClass extends BaseClass
         self::printReceipt($gift);
         return true;
     }
+
+    public static function collectGiftByAdmin($shopId, $giftId)
+    {
+        $gift = self::getById($giftId, true);
+        if (empty($gift) || intval($gift->shopId) != intval($shopId)) {
+            util::fail('记录不存在');
+        }
+        if (intval($gift->status) == self::STATUS_COLLECTED) {
+            return $gift;
+        }
+        if (intval($gift->status) != self::STATUS_PICKING_UP) {
+            util::fail('仅领取中记录可更新为已领取');
+        }
+        $gift->status = self::STATUS_COLLECTED;
+        $gift->collectTime = date('Y-m-d H:i:s');
+        $gift->save();
+        return $gift;
+    }
 }