HbController.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace mall\controllers;
  3. use Yii;
  4. use common\components\util;
  5. use bizHd\hb\classes\HbClass;
  6. use bizHd\hb\classes\HbManageClass;
  7. use bizHd\hb\classes\HbScopeClass;
  8. class HbController extends BaseController
  9. {
  10. /**
  11. * 我的红包列表
  12. * 返回分页列表,并附带各状态数量供顶部 Tab 展示:
  13. * all 全部、unUse 未使用、used 已使用、expired 已过期
  14. */
  15. public function actionList()
  16. {
  17. $get = Yii::$app->request->get();
  18. $where = [];
  19. $where['t.userId'] = $this->userId;
  20. $status = $get['status'] ?? '';
  21. if ($status != '') {
  22. $where['t.status'] = $get['status'];
  23. }
  24. $respond = HbClass::getHbList(['t.*', 's.merchantName', 's.shopName'], $where);
  25. // 各状态数量不受当前 status 筛选影响,始终按当前用户统计(单次聚合查询)
  26. $statusCounts = HbClass::getStatusCounts(['userId' => $this->userId]);
  27. $respond['all'] = $statusCounts['all'];
  28. $respond['unUse'] = $statusCounts['unUse'];
  29. $respond['used'] = $statusCounts['used'];
  30. $respond['expired'] = $statusCounts['expired'];
  31. if (!empty($respond['list'])) {
  32. $hbIds = array_column($respond['list'], 'id');
  33. $scopeMap = HbScopeClass::getMapByHbIds($hbIds);
  34. foreach ($respond['list'] as &$v) {
  35. $v['customName'] = $v['custom']['name'] ?? '';
  36. $v = HbScopeClass::attachScopeToHb($v, $scopeMap);
  37. $v['specialApplicableText'] = ((int)$v['specialApplicable'] === 1)
  38. ? '特价及活动商品适用'
  39. : '特价及活动商品不适用';
  40. if ($v['status'] == 0 && $v['endTime'] < time()) {
  41. //发现红包已过期,需要作废,多有处作废操作,搜索关键词 cancellation_hb
  42. $id = $v['id'];
  43. HbClass::updateById($id, ['status' => -1]);
  44. $v['status'] = -1;
  45. // 本页懒过期后同步修正 Tab 数量,避免未使用偏多、已过期偏少
  46. $respond['unUse']--;
  47. $respond['expired']++;
  48. }
  49. }
  50. }
  51. util::success($respond);
  52. }
  53. /**
  54. * 某门店下所有可用红包(附带适用范围)
  55. */
  56. public function actionAvailableHb()
  57. {
  58. $where = [];
  59. $where['customId'] = $this->customId;
  60. $where['status'] = 0;
  61. $where['beginTime<='] = time();
  62. // 若传 shopId 则限定门店
  63. $shopId = (int)Yii::$app->request->get('shopId', 0);
  64. if ($shopId > 0) {
  65. $where['shopId'] = $shopId;
  66. }
  67. $list = HbClass::getAllByCondition($where, 'id DESC');
  68. if (!empty($list)) {
  69. $hbIds = array_column($list, 'id');
  70. $scopeMap = HbScopeClass::getMapByHbIds($hbIds);
  71. foreach ($list as $k => $v) {
  72. if ($v['status'] == 0 && $v['endTime'] < time()) {
  73. unset($list[$k]);
  74. $id = $v['id'];
  75. HbClass::updateById($id, ['status' => -1]);
  76. continue;
  77. }
  78. $list[$k] = HbScopeClass::attachScopeToHb($v, $scopeMap);
  79. }
  80. $list = array_values($list);
  81. }
  82. util::success($list);
  83. }
  84. /**
  85. * 检查是否有未提示的到账红包(首页弹框)
  86. * GET shopId 可选:个人首页不传则取任意一家有待提示红包的门店
  87. */
  88. public function actionCheckArrival()
  89. {
  90. $shopId = (int)Yii::$app->request->get('shopId', 0);
  91. $customId = (int)$this->customId;
  92. $userId = (int)$this->userId;
  93. if ($userId <= 0) {
  94. util::success(['has' => 0]);
  95. }
  96. // 通过 manage.notified=0 且 hb.status=0 查找
  97. $hbWhere = [
  98. 'userId' => $userId,
  99. 'status' => 0,
  100. 'beginTime<=' => time(),
  101. 'endTime>=' => time(),
  102. ];
  103. if ($shopId > 0) {
  104. $hbWhere['shopId'] = $shopId;
  105. }
  106. if ($customId > 0) {
  107. $hbWhere['customId'] = $customId;
  108. }
  109. $list = HbClass::getAllByCondition($hbWhere, 'id DESC', 'id,shopId,customId,name,amount,minConsume,beginTime,endTime,getType');
  110. if (empty($list)) {
  111. util::success(['has' => 0]);
  112. }
  113. $hbIds = array_column($list, 'id');
  114. // 取全部流水(含已提示),按 hbId 索引
  115. $manages = HbManageClass::getAllByCondition([
  116. 'hbId' => ['in', $hbIds],
  117. ], null, 'hbId,notified', 'hbId');
  118. $pending = [];
  119. foreach ($list as $hb) {
  120. $hid = (int)$hb['id'];
  121. $m = $manages[$hid] ?? null;
  122. if ($m === null) {
  123. // 历史无流水:补一条以便后续 mark,并视为未提示
  124. try {
  125. HbManageClass::add([
  126. 'hbId' => $hid,
  127. 'getType' => (int)$hb['getType'],
  128. 'getTargetId' => 0,
  129. 'createStaffId' => 0,
  130. 'createStaffName' => '系统',
  131. 'remark' => '',
  132. 'notified' => 0,
  133. ]);
  134. } catch (\Throwable $e) {
  135. }
  136. $pending[] = $hb;
  137. } elseif ((int)$m['notified'] === 0) {
  138. $pending[] = $hb;
  139. }
  140. }
  141. if (empty($pending)) {
  142. util::success(['has' => 0]);
  143. }
  144. $first = $pending[0];
  145. util::success([
  146. 'has' => 1,
  147. 'count' => count($pending),
  148. 'hb' => $first,
  149. 'hbIds' => array_column($pending, 'id'),
  150. ]);
  151. }
  152. /**
  153. * 标记红包已提示(弹框「立即查看」或「稍后再说」都调用)
  154. * POST hbIds[] 或 hbId
  155. */
  156. public function actionMarkNotified()
  157. {
  158. $post = Yii::$app->request->post();
  159. $hbIds = $post['hbIds'] ?? [];
  160. if (empty($hbIds) && !empty($post['hbId'])) {
  161. $hbIds = [$post['hbId']];
  162. }
  163. if (!is_array($hbIds)) {
  164. $hbIds = [$hbIds];
  165. }
  166. $hbIds = array_values(array_filter(array_map('intval', $hbIds)));
  167. if (empty($hbIds)) {
  168. util::complete();
  169. }
  170. // 校验归属
  171. $list = HbClass::getAllByCondition([
  172. 'id' => ['in', $hbIds],
  173. 'userId' => $this->userId,
  174. ], null, 'id');
  175. $okIds = array_column($list, 'id');
  176. if (!empty($okIds)) {
  177. // updateByCondition 仅支持等值条件,IN 需走 updateByIds
  178. $manages = HbManageClass::getAllByCondition(['hbId' => ['in', $okIds]], null, 'id');
  179. $manageIds = array_column($manages, 'id');
  180. if (!empty($manageIds)) {
  181. HbManageClass::updateByIds($manageIds, [
  182. 'notified' => 1,
  183. 'notifiedAt' => date('Y-m-d H:i:s'),
  184. ]);
  185. }
  186. }
  187. util::complete();
  188. }
  189. }