request->get(); $where = []; $where['t.userId'] = $this->userId; $status = $get['status'] ?? ''; if ($status != '') { $where['t.status'] = $get['status']; } $respond = HbClass::getHbList(['t.*', 's.merchantName', 's.shopName'], $where); // 各状态数量不受当前 status 筛选影响,始终按当前用户统计(单次聚合查询) $statusCounts = HbClass::getStatusCounts(['userId' => $this->userId]); $respond['all'] = $statusCounts['all']; $respond['unUse'] = $statusCounts['unUse']; $respond['used'] = $statusCounts['used']; $respond['expired'] = $statusCounts['expired']; if (!empty($respond['list'])) { $hbIds = array_column($respond['list'], 'id'); $scopeMap = HbScopeClass::getMapByHbIds($hbIds); foreach ($respond['list'] as &$v) { $v['customName'] = $v['custom']['name'] ?? ''; $v = HbScopeClass::attachScopeToHb($v, $scopeMap); $v['specialApplicableText'] = ((int)$v['specialApplicable'] === 1) ? '特价及活动商品适用' : '特价及活动商品不适用'; if ($v['status'] == 0 && $v['endTime'] < time()) { //发现红包已过期,需要作废,多有处作废操作,搜索关键词 cancellation_hb $id = $v['id']; HbClass::updateById($id, ['status' => -1]); $v['status'] = -1; // 本页懒过期后同步修正 Tab 数量,避免未使用偏多、已过期偏少 $respond['unUse']--; $respond['expired']++; } } } util::success($respond); } /** * 某门店下所有可用红包(附带适用范围) */ public function actionAvailableHb() { $where = []; $where['customId'] = $this->customId; $where['status'] = 0; $where['beginTime<='] = time(); // 若传 shopId 则限定门店 $shopId = (int)Yii::$app->request->get('shopId', 0); if ($shopId > 0) { $where['shopId'] = $shopId; } $list = HbClass::getAllByCondition($where, 'id DESC'); if (!empty($list)) { $hbIds = array_column($list, 'id'); $scopeMap = HbScopeClass::getMapByHbIds($hbIds); foreach ($list as $k => $v) { if ($v['status'] == 0 && $v['endTime'] < time()) { unset($list[$k]); $id = $v['id']; HbClass::updateById($id, ['status' => -1]); continue; } $list[$k] = HbScopeClass::attachScopeToHb($v, $scopeMap); } $list = array_values($list); } util::success($list); } /** * 检查是否有未提示的到账红包(首页弹框) * GET shopId 可选:个人首页不传则取任意一家有待提示红包的门店 */ public function actionCheckArrival() { $shopId = (int)Yii::$app->request->get('shopId', 0); $customId = (int)$this->customId; $userId = (int)$this->userId; if ($userId <= 0) { util::success(['has' => 0]); } // 通过 manage.notified=0 且 hb.status=0 查找 $hbWhere = [ 'userId' => $userId, 'status' => 0, 'beginTime<=' => time(), 'endTime>=' => time(), ]; if ($shopId > 0) { $hbWhere['shopId'] = $shopId; } if ($customId > 0) { $hbWhere['customId'] = $customId; } $list = HbClass::getAllByCondition($hbWhere, 'id DESC', 'id,shopId,customId,name,amount,minConsume,beginTime,endTime,getType'); if (empty($list)) { util::success(['has' => 0]); } $hbIds = array_column($list, 'id'); // 取全部流水(含已提示),按 hbId 索引 $manages = HbManageClass::getAllByCondition([ 'hbId' => ['in', $hbIds], ], null, 'hbId,notified', 'hbId'); $pending = []; foreach ($list as $hb) { $hid = (int)$hb['id']; $m = $manages[$hid] ?? null; if ($m === null) { // 历史无流水:补一条以便后续 mark,并视为未提示 try { HbManageClass::add([ 'hbId' => $hid, 'getType' => (int)$hb['getType'], 'getTargetId' => 0, 'createStaffId' => 0, 'createStaffName' => '系统', 'remark' => '', 'notified' => 0, ]); } catch (\Throwable $e) { } $pending[] = $hb; } elseif ((int)$m['notified'] === 0) { $pending[] = $hb; } } if (empty($pending)) { util::success(['has' => 0]); } $first = $pending[0]; util::success([ 'has' => 1, 'count' => count($pending), 'hb' => $first, 'hbIds' => array_column($pending, 'id'), ]); } /** * 标记红包已提示(弹框「立即查看」或「稍后再说」都调用) * POST hbIds[] 或 hbId */ public function actionMarkNotified() { $post = Yii::$app->request->post(); $hbIds = $post['hbIds'] ?? []; if (empty($hbIds) && !empty($post['hbId'])) { $hbIds = [$post['hbId']]; } if (!is_array($hbIds)) { $hbIds = [$hbIds]; } $hbIds = array_values(array_filter(array_map('intval', $hbIds))); if (empty($hbIds)) { util::complete(); } // 校验归属 $list = HbClass::getAllByCondition([ 'id' => ['in', $hbIds], 'userId' => $this->userId, ], null, 'id'); $okIds = array_column($list, 'id'); if (!empty($okIds)) { // updateByCondition 仅支持等值条件,IN 需走 updateByIds $manages = HbManageClass::getAllByCondition(['hbId' => ['in', $okIds]], null, 'id'); $manageIds = array_column($manages, 'id'); if (!empty($manageIds)) { HbManageClass::updateByIds($manageIds, [ 'notified' => 1, 'notifiedAt' => date('Y-m-d H:i:s'), ]); } } util::complete(); } }