| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- <?php
- namespace mall\controllers;
- use Yii;
- use common\components\util;
- use bizHd\hb\classes\HbClass;
- use bizHd\hb\classes\HbManageClass;
- use bizHd\hb\classes\HbScopeClass;
- use bizMall\hd\classes\HdClass;
- class HbController extends BaseController
- {
- /**
- * 我的红包列表
- * 返回分页列表,并附带各状态数量供顶部 Tab 展示:
- * all 全部、unUse 未使用、used 已使用、expired 已过期
- * 每条红包额外附带 hdId(xhHd.id),供「去使用」跳转对应店铺商品列表。
- */
- public function actionList()
- {
- $get = Yii::$app->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);
- // 按 shopId 批量查当前用户在该店的 xhHd.id,避免跨店跳转商品列表时缺少门店上下文
- $shopHdMap = $this->buildShopHdMap(array_column($respond['list'], 'shopId'));
- foreach ($respond['list'] as &$v) {
- $v['customName'] = $v['custom']['name'] ?? '';
- $v = HbScopeClass::attachScopeToHb($v, $scopeMap);
- $v['specialApplicableText'] = ((int)$v['specialApplicable'] === 1)
- ? '特价及活动商品适用'
- : '特价及活动商品不适用';
- $shopId = (int)($v['shopId'] ?? 0);
- $v['hdId'] = $shopHdMap[$shopId] ?? 0;
- 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']++;
- }
- }
- unset($v);
- }
- util::success($respond);
- }
- /**
- * 按 shopId 批量解析当前用户的花店客户关系 id(xhHd.id)。
- * 「去使用」跳转商品列表必须带 hdId,否则商城接口无法正确绑定客户价与购物车店铺维度。
- *
- * @param array $shopIds 红包所属门店 id 列表
- * @return array shopId => hdId
- */
- private function buildShopHdMap($shopIds)
- {
- $shopIds = array_values(array_unique(array_filter(array_map('intval', $shopIds ?: []))));
- if (empty($shopIds) || $this->userId <= 0) {
- return [];
- }
- $hdList = HdClass::getAllByCondition(
- [
- 'userId' => $this->userId,
- 'shopId' => ['in', $shopIds],
- 'delStatus' => 0,
- ],
- null,
- 'id,shopId'
- );
- $map = [];
- foreach ($hdList ?: [] as $hd) {
- $sid = (int)($hd['shopId'] ?? 0);
- if ($sid > 0 && !isset($map[$sid])) {
- $map[$sid] = (int)$hd['id'];
- }
- }
- return $map;
- }
- /**
- * 某门店下所有可用红包(附带适用范围)
- */
- 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();
- }
- }
|