HomePageModuleClass.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. <?php
  2. namespace bizHd\homePageConfig\classes;
  3. use bizHd\goods\classes\GoodsClass;
  4. use bizHd\goods\classes\GoodsCategoryClass;
  5. use bizHd\goods\classes\GoodsUseCaseClass;
  6. use common\components\imgUtil;
  7. use common\components\util;
  8. use Yii;
  9. /**
  10. * 门店首页其余模块配置(金刚区/秒杀/团购/热门/上新/下拉商品)
  11. * 数据按 mainId 存 Redis,供 hdApp 配置、mallApp 读取展示。
  12. */
  13. class HomePageModuleClass
  14. {
  15. const REDIS_NAV_GRID = '_home_page_config_nav_grid';
  16. const REDIS_SECKILL = '_home_page_config_seckill';
  17. const REDIS_GROUP_BUY = '_home_page_config_group_buy';
  18. const REDIS_HOT = '_home_page_config_hot';
  19. const REDIS_NEW = '_home_page_config_new';
  20. const REDIS_PULL_GOODS = '_home_page_config_pull_goods';
  21. const NAV_OSS_ROOT = 'uploads_home_nav';
  22. const MAX_NAV_COUNT = 20;
  23. /** 关联:商品 */
  24. const LINK_GOODS = 2;
  25. /** 关联:分类 */
  26. const LINK_CATEGORY = 3;
  27. /** 关联:场景 */
  28. const LINK_USE_CASE = 4;
  29. /** 排序:按商品排序 */
  30. const SORT_PRODUCT = 1;
  31. /** 排序:按销量 */
  32. const SORT_SALES = 2;
  33. /** 排序:按上新时间 */
  34. const SORT_NEW = 3;
  35. /** 排列规则合法取值:1排1列/1排2列/1排3列 */
  36. const LAYOUT_COLS_OPTIONS = [1, 2, 3];
  37. /** 排列规则默认值 */
  38. const DEFAULT_LAYOUT_COLS = 3;
  39. /** 首页展示商品解析默认条数上限 */
  40. const DISPLAY_GOODS_LIMIT = 20;
  41. public static function redisKey($mainId, $suffix)
  42. {
  43. return intval($mainId) . $suffix;
  44. }
  45. public static function getJson($mainId, $suffix)
  46. {
  47. $raw = Yii::$app->redis->executeCommand('GET', [self::redisKey($mainId, $suffix)]);
  48. if (empty($raw)) {
  49. return [];
  50. }
  51. $data = json_decode($raw, true);
  52. return is_array($data) ? $data : [];
  53. }
  54. public static function setJson($mainId, $suffix, $payload)
  55. {
  56. Yii::$app->redis->executeCommand('SET', [
  57. self::redisKey($mainId, $suffix),
  58. json_encode($payload, JSON_UNESCAPED_UNICODE),
  59. ]);
  60. }
  61. public static function decodeList($raw)
  62. {
  63. if (is_string($raw)) {
  64. $decoded = json_decode($raw, true);
  65. return is_array($decoded) ? $decoded : [];
  66. }
  67. return is_array($raw) ? $raw : [];
  68. }
  69. // -------------------- 金刚区 --------------------
  70. public static function getNavGrid($mainId)
  71. {
  72. $mainId = intval($mainId);
  73. if ($mainId <= 0) {
  74. util::fail('无效门店');
  75. }
  76. $saved = self::getJson($mainId, self::REDIS_NAV_GRID);
  77. $cols = isset($saved['cols']) ? intval($saved['cols']) : 5;
  78. if (!in_array($cols, [4, 5], true)) {
  79. $cols = 5;
  80. }
  81. $list = [];
  82. foreach (self::decodeList($saved['list'] ?? []) as $item) {
  83. if (!is_array($item)) {
  84. continue;
  85. }
  86. $list[] = [
  87. 'id' => strval($item['id'] ?? ''),
  88. 'name' => strval($item['name'] ?? ''),
  89. 'icon' => strval($item['icon'] ?? ''),
  90. 'iconType' => intval($item['iconType'] ?? 1),
  91. 'enabled' => !empty($item['enabled']) ? 1 : 0,
  92. 'type' => intval($item['type'] ?? self::LINK_CATEGORY),
  93. 'value' => strval($item['value'] ?? ''),
  94. ];
  95. }
  96. return [
  97. 'enabled' => HomePageConfigClass::getModuleEnabled($mainId, 'navGrid'),
  98. 'cols' => $cols,
  99. 'list' => $list,
  100. ];
  101. }
  102. public static function saveNavGrid($mainId, $data)
  103. {
  104. $mainId = intval($mainId);
  105. if ($mainId <= 0) {
  106. util::fail('无效门店');
  107. }
  108. $enabled = !empty($data['enabled']) ? 1 : 0;
  109. $cols = isset($data['cols']) ? intval($data['cols']) : 5;
  110. if (!in_array($cols, [4, 5], true)) {
  111. util::fail('排列规则无效');
  112. }
  113. $rawList = self::decodeList($data['list'] ?? []);
  114. if (count($rawList) > self::MAX_NAV_COUNT) {
  115. util::fail('最多可配置' . self::MAX_NAV_COUNT . '个导航');
  116. }
  117. $list = [];
  118. foreach ($rawList as $index => $item) {
  119. if (!is_array($item)) {
  120. continue;
  121. }
  122. $name = trim(strval($item['name'] ?? ''));
  123. $icon = trim(strval($item['icon'] ?? ''));
  124. $type = intval($item['type'] ?? 0);
  125. $value = trim(strval($item['value'] ?? ''));
  126. $no = $index + 1;
  127. if ($name === '' || $icon === '' || $value === '') {
  128. util::fail("请完善第{$no}个导航项");
  129. }
  130. if (!in_array($type, [self::LINK_CATEGORY, self::LINK_USE_CASE], true)) {
  131. util::fail("第{$no}个导航关联类型无效");
  132. }
  133. $list[] = [
  134. 'id' => strval($item['id'] ?? ('nav_' . ($index + 1))),
  135. 'name' => mb_substr($name, 0, 20),
  136. 'icon' => $icon,
  137. 'iconType' => intval($item['iconType'] ?? 1) === 2 ? 2 : 1,
  138. 'enabled' => !empty($item['enabled']) ? 1 : 0,
  139. 'type' => $type,
  140. 'value' => $value,
  141. ];
  142. }
  143. self::setJson($mainId, self::REDIS_NAV_GRID, ['cols' => $cols, 'list' => $list]);
  144. HomePageConfigClass::updateModuleEnabled($mainId, 'navGrid', $enabled);
  145. return true;
  146. }
  147. // -------------------- 秒杀 --------------------
  148. public static function getSeckill($mainId, $refreshStock = true)
  149. {
  150. $mainId = intval($mainId);
  151. if ($mainId <= 0) {
  152. util::fail('无效门店');
  153. }
  154. $saved = self::getJson($mainId, self::REDIS_SECKILL);
  155. $data = self::normalizeActivityBase($saved);
  156. $data['enabled'] = HomePageConfigClass::getModuleEnabled($mainId, 'seckill');
  157. $data['goods'] = self::normalizeSeckillGoods($saved['goods'] ?? [], $mainId, $refreshStock);
  158. $data['status'] = self::calcActivityStatus($data['startTime'], $data['endTime'], $data['enabled']);
  159. return $data;
  160. }
  161. public static function saveSeckill($mainId, $data)
  162. {
  163. $mainId = intval($mainId);
  164. if ($mainId <= 0) {
  165. util::fail('无效门店');
  166. }
  167. $base = self::validateActivityBase($data);
  168. $goods = self::validateSeckillGoods($mainId, self::decodeList($data['goods'] ?? []));
  169. self::setJson($mainId, self::REDIS_SECKILL, array_merge($base, ['goods' => $goods]));
  170. HomePageConfigClass::updateModuleEnabled($mainId, 'seckill', !empty($data['enabled']) ? 1 : 0);
  171. return true;
  172. }
  173. /**
  174. * 校验并规范化秒杀商品;秒杀库存不得超过实际商品库存
  175. */
  176. public static function validateSeckillGoods($mainId, $rawList)
  177. {
  178. $list = [];
  179. foreach ($rawList as $index => $item) {
  180. if (!is_array($item)) {
  181. continue;
  182. }
  183. $no = $index + 1;
  184. $goodsId = intval($item['goodsId'] ?? 0);
  185. $price = floatval($item['price'] ?? 0);
  186. $stock = intval($item['stock'] ?? 0);
  187. $limit = intval($item['limit'] ?? 0);
  188. if ($goodsId <= 0) {
  189. util::fail("请选择第{$no}个秒杀商品");
  190. }
  191. if ($price <= 0) {
  192. util::fail("请填写第{$no}个秒杀价格");
  193. }
  194. if ($stock <= 0) {
  195. util::fail("请填写第{$no}个秒杀库存");
  196. }
  197. if ($limit <= 0) {
  198. util::fail("请填写第{$no}个单人限购");
  199. }
  200. $goods = GoodsClass::getById($goodsId, true);
  201. if (empty($goods) || intval($goods->mainId ?? 0) !== intval($mainId)) {
  202. util::fail("第{$no}个秒杀商品无效");
  203. }
  204. $realStock = intval($goods->stock ?? 0);
  205. // 秒杀库存不得超过实际库存
  206. if ($stock > $realStock) {
  207. util::fail("第{$no}个秒杀库存不能超过商品实际库存({$realStock})");
  208. }
  209. $status = !empty($item['status']) ? 1 : 0;
  210. // 实际库存已低于秒杀库存:自动下架该秒杀商品
  211. if ($realStock < $stock) {
  212. $status = 0;
  213. }
  214. $list[] = [
  215. 'goodsId' => $goodsId,
  216. 'price' => round($price, 2),
  217. 'stock' => $stock,
  218. 'limit' => $limit,
  219. 'status' => $status,
  220. 'name' => strval($goods->name ?? ($item['name'] ?? '')),
  221. 'cover' => strval($goods->shortCover ?? ($goods->cover ?? ($item['cover'] ?? ''))),
  222. 'originPrice' => floatval($goods->price ?? ($item['originPrice'] ?? 0)),
  223. ];
  224. }
  225. return $list;
  226. }
  227. /**
  228. * 读取时刷新秒杀商品状态:实际库存低于秒杀库存则下架,并回写 Redis
  229. */
  230. public static function normalizeSeckillGoods($rawList, $mainId, $refreshStock = true)
  231. {
  232. $list = [];
  233. $changed = false;
  234. foreach (self::decodeList($rawList) as $item) {
  235. if (!is_array($item) || empty($item['goodsId'])) {
  236. continue;
  237. }
  238. $row = [
  239. 'goodsId' => intval($item['goodsId']),
  240. 'price' => floatval($item['price'] ?? 0),
  241. 'stock' => intval($item['stock'] ?? 0),
  242. 'limit' => intval($item['limit'] ?? 0),
  243. 'status' => !empty($item['status']) ? 1 : 0,
  244. 'name' => strval($item['name'] ?? ''),
  245. 'cover' => strval($item['cover'] ?? ''),
  246. 'originPrice' => floatval($item['originPrice'] ?? 0),
  247. 'realStock' => intval($item['stock'] ?? 0),
  248. ];
  249. if ($refreshStock) {
  250. $goods = GoodsClass::getById($row['goodsId'], true);
  251. if (!empty($goods) && intval($goods->mainId ?? 0) === intval($mainId)) {
  252. $realStock = intval($goods->stock ?? 0);
  253. $row['realStock'] = $realStock;
  254. $row['name'] = strval($goods->name ?? $row['name']);
  255. $row['cover'] = strval($goods->shortCover ?? ($goods->cover ?? $row['cover']));
  256. $row['originPrice'] = floatval($goods->price ?? $row['originPrice']);
  257. if ($realStock < $row['stock'] && $row['status'] == 1) {
  258. $row['status'] = 0;
  259. $changed = true;
  260. }
  261. }
  262. }
  263. $list[] = $row;
  264. }
  265. if ($changed && $refreshStock) {
  266. $saved = self::getJson($mainId, self::REDIS_SECKILL);
  267. $persist = [];
  268. foreach ($list as $g) {
  269. $persist[] = [
  270. 'goodsId' => $g['goodsId'],
  271. 'price' => $g['price'],
  272. 'stock' => $g['stock'],
  273. 'limit' => $g['limit'],
  274. 'status' => $g['status'],
  275. 'name' => $g['name'],
  276. 'cover' => $g['cover'],
  277. 'originPrice' => $g['originPrice'],
  278. ];
  279. }
  280. $saved['goods'] = $persist;
  281. self::setJson($mainId, self::REDIS_SECKILL, $saved);
  282. }
  283. return $list;
  284. }
  285. // -------------------- 团购 --------------------
  286. public static function getGroupBuy($mainId, $refreshStock = true)
  287. {
  288. $mainId = intval($mainId);
  289. if ($mainId <= 0) {
  290. util::fail('无效门店');
  291. }
  292. $saved = self::getJson($mainId, self::REDIS_GROUP_BUY);
  293. $data = self::normalizeActivityBase($saved);
  294. $data['enabled'] = HomePageConfigClass::getModuleEnabled($mainId, 'groupBuy');
  295. $data['goods'] = self::normalizeGroupBuyGoods($saved['goods'] ?? [], $mainId, $refreshStock);
  296. $data['status'] = self::calcActivityStatus($data['startTime'], $data['endTime'], $data['enabled']);
  297. return $data;
  298. }
  299. public static function saveGroupBuy($mainId, $data)
  300. {
  301. $mainId = intval($mainId);
  302. if ($mainId <= 0) {
  303. util::fail('无效门店');
  304. }
  305. $base = self::validateActivityBase($data);
  306. $goods = self::validateGroupBuyGoods($mainId, self::decodeList($data['goods'] ?? []));
  307. self::setJson($mainId, self::REDIS_GROUP_BUY, array_merge($base, ['goods' => $goods]));
  308. HomePageConfigClass::updateModuleEnabled($mainId, 'groupBuy', !empty($data['enabled']) ? 1 : 0);
  309. return true;
  310. }
  311. public static function validateGroupBuyGoods($mainId, $rawList)
  312. {
  313. $list = [];
  314. foreach ($rawList as $index => $item) {
  315. if (!is_array($item)) {
  316. continue;
  317. }
  318. $no = $index + 1;
  319. $goodsId = intval($item['goodsId'] ?? 0);
  320. $price = floatval($item['price'] ?? 0);
  321. $stock = intval($item['stock'] ?? 0);
  322. $limit = intval($item['limit'] ?? 0);
  323. $groupSize = intval($item['groupSize'] ?? 3);
  324. if (!in_array($groupSize, [2, 3, 5], true)) {
  325. util::fail("第{$no}个成团人数无效");
  326. }
  327. if ($goodsId <= 0 || $price <= 0 || $stock <= 0 || $limit <= 0) {
  328. util::fail("请完善第{$no}个团购商品");
  329. }
  330. $goods = GoodsClass::getById($goodsId, true);
  331. if (empty($goods) || intval($goods->mainId ?? 0) !== intval($mainId)) {
  332. util::fail("第{$no}个团购商品无效");
  333. }
  334. $realStock = intval($goods->stock ?? 0);
  335. if ($stock > $realStock) {
  336. util::fail("第{$no}个团购库存不能超过商品实际库存({$realStock})");
  337. }
  338. $virtualGroup = !empty($item['virtualGroup']) ? 1 : 0;
  339. $virtualMinutes = intval($item['virtualMinutes'] ?? 0);
  340. if ($virtualGroup && $virtualMinutes <= 0) {
  341. util::fail("请填写第{$no}个虚拟成团时间");
  342. }
  343. $list[] = [
  344. 'goodsId' => $goodsId,
  345. 'price' => round($price, 2),
  346. 'stock' => $stock,
  347. 'limit' => $limit,
  348. 'groupSize' => $groupSize,
  349. 'virtualGroup' => $virtualGroup,
  350. 'virtualMinutes' => $virtualMinutes,
  351. 'autoRefund' => !empty($item['autoRefund']) ? 1 : 0,
  352. 'status' => ($realStock < $stock) ? 0 : (!empty($item['status']) ? 1 : 0),
  353. 'name' => strval($goods->name ?? ($item['name'] ?? '')),
  354. 'cover' => strval($goods->shortCover ?? ($goods->cover ?? ($item['cover'] ?? ''))),
  355. 'originPrice' => floatval($goods->price ?? ($item['originPrice'] ?? 0)),
  356. ];
  357. }
  358. return $list;
  359. }
  360. public static function normalizeGroupBuyGoods($rawList, $mainId, $refreshStock = true)
  361. {
  362. $list = [];
  363. $changed = false;
  364. foreach (self::decodeList($rawList) as $item) {
  365. if (!is_array($item) || empty($item['goodsId'])) {
  366. continue;
  367. }
  368. $row = [
  369. 'goodsId' => intval($item['goodsId']),
  370. 'price' => floatval($item['price'] ?? 0),
  371. 'stock' => intval($item['stock'] ?? 0),
  372. 'limit' => intval($item['limit'] ?? 0),
  373. 'groupSize' => intval($item['groupSize'] ?? 3),
  374. 'virtualGroup' => !empty($item['virtualGroup']) ? 1 : 0,
  375. 'virtualMinutes' => intval($item['virtualMinutes'] ?? 0),
  376. 'autoRefund' => !empty($item['autoRefund']) ? 1 : 0,
  377. 'status' => !empty($item['status']) ? 1 : 0,
  378. 'name' => strval($item['name'] ?? ''),
  379. 'cover' => strval($item['cover'] ?? ''),
  380. 'originPrice' => floatval($item['originPrice'] ?? 0),
  381. 'realStock' => intval($item['stock'] ?? 0),
  382. ];
  383. if ($refreshStock) {
  384. $goods = GoodsClass::getById($row['goodsId'], true);
  385. if (!empty($goods) && intval($goods->mainId ?? 0) === intval($mainId)) {
  386. $realStock = intval($goods->stock ?? 0);
  387. $row['realStock'] = $realStock;
  388. $row['name'] = strval($goods->name ?? $row['name']);
  389. $row['cover'] = strval($goods->shortCover ?? ($goods->cover ?? $row['cover']));
  390. $row['originPrice'] = floatval($goods->price ?? $row['originPrice']);
  391. if ($realStock < $row['stock'] && $row['status'] == 1) {
  392. $row['status'] = 0;
  393. $changed = true;
  394. }
  395. }
  396. }
  397. $list[] = $row;
  398. }
  399. if ($changed && $refreshStock) {
  400. $saved = self::getJson($mainId, self::REDIS_GROUP_BUY);
  401. $persist = [];
  402. foreach ($list as $g) {
  403. unset($g['realStock']);
  404. $persist[] = $g;
  405. }
  406. $saved['goods'] = $persist;
  407. self::setJson($mainId, self::REDIS_GROUP_BUY, $saved);
  408. }
  409. return $list;
  410. }
  411. // -------------------- 热门/上新/下拉 --------------------
  412. public static function getGoodsSection($mainId, $moduleKey)
  413. {
  414. $mainId = intval($mainId);
  415. $suffix = self::sectionSuffix($moduleKey);
  416. $saved = self::getJson($mainId, $suffix);
  417. $defaults = [
  418. 'hot' => '热门推荐',
  419. 'new' => '今日上新',
  420. 'pullGoods' => '更多商品',
  421. ];
  422. return [
  423. 'enabled' => HomePageConfigClass::getModuleEnabled($mainId, $moduleKey),
  424. 'name' => strval($saved['name'] ?? ($defaults[$moduleKey] ?? '')),
  425. 'type' => intval($saved['type'] ?? self::LINK_GOODS),
  426. 'value' => strval($saved['value'] ?? ''),
  427. 'sort' => intval($saved['sort'] ?? self::SORT_PRODUCT),
  428. 'layoutCols' => self::normalizeLayoutCols($saved['layoutCols'] ?? null),
  429. ];
  430. }
  431. public static function saveGoodsSection($mainId, $moduleKey, $data)
  432. {
  433. $mainId = intval($mainId);
  434. $suffix = self::sectionSuffix($moduleKey);
  435. $name = trim(strval($data['name'] ?? ''));
  436. $type = intval($data['type'] ?? 0);
  437. $value = trim(strval($data['value'] ?? ''));
  438. $sort = intval($data['sort'] ?? self::SORT_PRODUCT);
  439. $layoutCols = intval($data['layoutCols'] ?? self::DEFAULT_LAYOUT_COLS);
  440. if ($name === '') {
  441. util::fail('请输入首页展示名称');
  442. }
  443. if (mb_strlen($name) > 20) {
  444. util::fail('展示名称不能超过20字');
  445. }
  446. if (!in_array($type, [self::LINK_GOODS, self::LINK_CATEGORY, self::LINK_USE_CASE], true)) {
  447. util::fail('关联类型无效');
  448. }
  449. if ($value === '') {
  450. util::fail('请选择关联内容');
  451. }
  452. if (!in_array($sort, [self::SORT_PRODUCT, self::SORT_SALES, self::SORT_NEW], true)) {
  453. util::fail('排序规则无效');
  454. }
  455. if (!in_array($layoutCols, self::LAYOUT_COLS_OPTIONS, true)) {
  456. util::fail('排列规则无效');
  457. }
  458. self::setJson($mainId, $suffix, [
  459. 'name' => $name,
  460. 'type' => $type,
  461. 'value' => $value,
  462. 'sort' => $sort,
  463. 'layoutCols' => $layoutCols,
  464. ]);
  465. HomePageConfigClass::updateModuleEnabled($mainId, $moduleKey, !empty($data['enabled']) ? 1 : 0);
  466. return true;
  467. }
  468. /**
  469. * 规范化排列规则取值,非法/缺省时回退默认值
  470. */
  471. public static function normalizeLayoutCols($cols)
  472. {
  473. $cols = intval($cols);
  474. return in_array($cols, self::LAYOUT_COLS_OPTIONS, true) ? $cols : self::DEFAULT_LAYOUT_COLS;
  475. }
  476. /**
  477. * 将热门推荐/今日上新/下拉商品的关联配置(type+value)解析为真实商品列表,用于首页展示
  478. * type=2 商品:value 为商品ID逗号拼接,按选择顺序展示
  479. * type=3 分类:value 为分类ID逗号拼接,取分类下全部商品
  480. * type=4 场景:value 为场景ID逗号拼接,取场景下全部商品
  481. * sort=1 按上面解析出的原始顺序;sort=2 按销量(虚拟+实际)降序;sort=3 按上架时间降序
  482. *
  483. * @param int $mainId
  484. * @param int $type
  485. * @param string $value
  486. * @param int $sort
  487. * @param int $limit
  488. * @return array [{id,name,price,stock,cover,coverUrl,sold}]
  489. */
  490. public static function resolveDisplayGoods($mainId, $type, $value, $sort, $limit = self::DISPLAY_GOODS_LIMIT)
  491. {
  492. $mainId = intval($mainId);
  493. $type = intval($type);
  494. $value = trim(strval($value));
  495. if ($mainId <= 0 || $value === '') {
  496. return [];
  497. }
  498. $ids = array_values(array_unique(array_filter(array_map('intval', explode(',', $value)))));
  499. if (empty($ids)) {
  500. return [];
  501. }
  502. $goodsIds = [];
  503. if ($type === self::LINK_GOODS) {
  504. // 商品类型:保留用户选择顺序
  505. $goodsIds = $ids;
  506. } elseif ($type === self::LINK_CATEGORY) {
  507. $rows = GoodsCategoryClass::getAllByCondition(
  508. ['cId' => ['in', $ids], 'delStatus' => 0],
  509. null,
  510. 'gId'
  511. );
  512. $goodsIds = array_values(array_unique(array_map('intval', array_column($rows, 'gId'))));
  513. } elseif ($type === self::LINK_USE_CASE) {
  514. $rows = GoodsUseCaseClass::getAllByCondition(
  515. ['useCaseId' => ['in', $ids]],
  516. null,
  517. 'goodsId'
  518. );
  519. $goodsIds = array_values(array_unique(array_map('intval', array_column($rows, 'goodsId'))));
  520. }
  521. if (empty($goodsIds)) {
  522. return [];
  523. }
  524. // 注意:Base::getByIds 传入 $order 会走到未定义的 order() 方法而报错,这里统一取回后在 PHP 侧排序
  525. $rows = GoodsClass::getByIds($goodsIds, null, null, 'id,name,price,stock,cover,sold,actualSold,status,delStatus,masterId,mainId,createTime');
  526. // 仅取当前门店、未删除、上架中的主规格商品
  527. $rows = array_values(array_filter($rows, function ($row) use ($mainId) {
  528. return intval($row['mainId'] ?? 0) === $mainId
  529. && intval($row['delStatus'] ?? 0) === 0
  530. && intval($row['status'] ?? 0) === 1
  531. && intval($row['masterId'] ?? 0) === 0;
  532. }));
  533. if ($sort == self::SORT_SALES) {
  534. usort($rows, function ($a, $b) {
  535. $soldA = floatval($a['actualSold'] ?? 0) + floatval($a['sold'] ?? 0);
  536. $soldB = floatval($b['actualSold'] ?? 0) + floatval($b['sold'] ?? 0);
  537. return $soldB <=> $soldA;
  538. });
  539. } elseif ($sort == self::SORT_NEW) {
  540. usort($rows, function ($a, $b) {
  541. return strtotime($b['createTime'] ?? '') <=> strtotime($a['createTime'] ?? '');
  542. });
  543. } elseif ($type === self::LINK_GOODS) {
  544. // 商品类型按用户选择顺序重新排列;分类/场景按商品排序时无自定义顺序可依,保持数据库默认返回顺序
  545. $indexBy = [];
  546. foreach ($rows as $row) {
  547. $indexBy[intval($row['id'])] = $row;
  548. }
  549. $ordered = [];
  550. foreach ($goodsIds as $gid) {
  551. if (isset($indexBy[$gid])) {
  552. $ordered[] = $indexBy[$gid];
  553. }
  554. }
  555. $rows = $ordered;
  556. }
  557. if ($limit > 0 && count($rows) > $limit) {
  558. $rows = array_slice($rows, 0, $limit);
  559. }
  560. $list = [];
  561. foreach ($rows as $row) {
  562. $cover = strval($row['cover'] ?? '');
  563. $coverUrl = $cover !== '' ? imgUtil::groupImg($cover) . '?x-oss-process=image/resize,m_fill,h_700,w_700' : '';
  564. $list[] = [
  565. 'id' => intval($row['id']),
  566. 'name' => strval($row['name'] ?? ''),
  567. 'price' => floatval($row['price'] ?? 0),
  568. 'stock' => intval($row['stock'] ?? 0),
  569. 'cover' => $cover,
  570. 'coverUrl' => $coverUrl,
  571. 'sold' => intval(bcadd($row['actualSold'] ?? 0, $row['sold'] ?? 0)),
  572. ];
  573. }
  574. return $list;
  575. }
  576. public static function sectionSuffix($moduleKey)
  577. {
  578. $map = [
  579. 'hot' => self::REDIS_HOT,
  580. 'new' => self::REDIS_NEW,
  581. 'pullGoods' => self::REDIS_PULL_GOODS,
  582. ];
  583. if (!isset($map[$moduleKey])) {
  584. util::fail('无效模块');
  585. }
  586. return $map[$moduleKey];
  587. }
  588. // -------------------- 活动公共 --------------------
  589. public static function normalizeActivityBase($saved)
  590. {
  591. return [
  592. 'title' => strval($saved['title'] ?? ''),
  593. 'subtitle' => strval($saved['subtitle'] ?? ''),
  594. 'showCountdown' => !empty($saved['showCountdown']) ? 1 : 0,
  595. 'expandHome' => !empty($saved['expandHome']) ? 1 : 0,
  596. 'startTime' => intval($saved['startTime'] ?? 0),
  597. 'endTime' => intval($saved['endTime'] ?? 0),
  598. 'desc' => strval($saved['desc'] ?? ''),
  599. 'layoutCols' => self::normalizeLayoutCols($saved['layoutCols'] ?? null),
  600. ];
  601. }
  602. public static function validateActivityBase($data)
  603. {
  604. $title = trim(strval($data['title'] ?? ''));
  605. $subtitle = trim(strval($data['subtitle'] ?? ''));
  606. $desc = trim(strval($data['desc'] ?? ''));
  607. $startTime = intval($data['startTime'] ?? 0);
  608. $endTime = intval($data['endTime'] ?? 0);
  609. $layoutCols = intval($data['layoutCols'] ?? self::DEFAULT_LAYOUT_COLS);
  610. if ($title === '') {
  611. util::fail('请输入活动标题');
  612. }
  613. if (mb_strlen($title) > 10) {
  614. util::fail('活动标题不能超过10字');
  615. }
  616. if ($subtitle === '') {
  617. util::fail('请输入活动副标题');
  618. }
  619. if (mb_strlen($subtitle) > 20) {
  620. util::fail('活动副标题不能超过20字');
  621. }
  622. if ($startTime <= 0 || $endTime <= 0) {
  623. util::fail('请选择活动时间');
  624. }
  625. if ($endTime <= $startTime) {
  626. util::fail('结束时间必须大于开始时间');
  627. }
  628. if ($desc === '') {
  629. util::fail('请输入活动说明');
  630. }
  631. if (mb_strlen($desc) > 500) {
  632. util::fail('活动说明不能超过500字');
  633. }
  634. if (!in_array($layoutCols, self::LAYOUT_COLS_OPTIONS, true)) {
  635. util::fail('排列规则无效');
  636. }
  637. return [
  638. 'title' => $title,
  639. 'subtitle' => $subtitle,
  640. 'showCountdown' => !empty($data['showCountdown']) ? 1 : 0,
  641. 'expandHome' => !empty($data['expandHome']) ? 1 : 0,
  642. 'startTime' => $startTime,
  643. 'endTime' => $endTime,
  644. 'desc' => $desc,
  645. 'layoutCols' => $layoutCols,
  646. ];
  647. }
  648. /**
  649. * 活动状态:0未开始 1进行中 2已结束;模块关闭视为已结束展示用
  650. */
  651. public static function calcActivityStatus($startTime, $endTime, $enabled)
  652. {
  653. if (empty($enabled)) {
  654. return 2;
  655. }
  656. $now = time();
  657. $startTime = intval($startTime);
  658. $endTime = intval($endTime);
  659. if ($startTime <= 0 || $endTime <= 0) {
  660. return 0;
  661. }
  662. if ($now < $startTime) {
  663. return 0;
  664. }
  665. if ($now > $endTime) {
  666. return 2;
  667. }
  668. return 1;
  669. }
  670. }