DiscountService.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. <?php
  2. namespace app\common\Service;
  3. use think\Db;
  4. use think\Model;
  5. use app\common\Enum\StatusEnum;
  6. use app\common\Enum\ActivityEnum;
  7. use app\common\Enum\GoodsEnum;
  8. /**
  9. * 折扣活动服务类
  10. *
  11. * 外层折扣字段说明:
  12. * - has_discount: 是否有折扣活动
  13. * - discount_price: 折扣价格(单规格商品直接使用,多规格商品显示最低价)
  14. * - discount: 折扣率(单规格商品直接使用,多规格商品显示最低折扣)
  15. * - discount_amount: 折扣金额(原价-折扣价,仅单规格商品)
  16. * - min_discount_price: 最低折扣价格
  17. * - max_discount_price: 最高折扣价格
  18. * - min_discount: 最低折扣率
  19. * - max_discount: 最高折扣率
  20. * - price_range: 价格区间文本(如:¥99 或 ¥99-199)
  21. */
  22. class DiscountService
  23. {
  24. /**
  25. * 查询当前时间段内的商品折扣信息
  26. * @param array $goodsIds 商品ID数组,为空则查询所有参与折扣的商品
  27. * @param int $limit 限制数量
  28. * @return array
  29. */
  30. public static function getCurrentDiscountGoods($goodsIds = [], $limit = 0)
  31. {
  32. // 第一步:查询当前时间有效的活动ID
  33. $activeActivityIds = self::getActiveActivityIds();
  34. if (empty($activeActivityIds)) {
  35. return [];
  36. }
  37. // 第二步:查询这些活动的SKU数据
  38. $query = Db::table('shop_activity_sku')
  39. ->alias('sku')
  40. ->join('shop_goods goods', 'sku.goods_id = goods.id')
  41. ->join('shop_goods_sku spec', 'sku.sku_id = spec.id', 'left')
  42. ->join(['shop_activity'=> 'activity'], 'sku.activity_id = activity.id')
  43. ->where('sku.activity_id', 'in', $activeActivityIds)
  44. ->where('goods.status', GoodsEnum::STATUS_ON_SALE) // 已上架的商品
  45. ->where('sku.stocks', '>', 0) // 有折扣库存的商品
  46. ->field([
  47. 'goods.id as goods_id',
  48. 'goods.title',
  49. 'goods.sub_title',
  50. 'goods.image',
  51. 'goods.price as original_price',
  52. 'goods.market_price',
  53. 'goods.stocks as goods_stocks',
  54. 'goods.sales',
  55. 'goods.spec_type',
  56. 'sku.id as activity_sku_id',
  57. 'sku.sku_id',
  58. 'sku.discount',
  59. 'sku.discount_price',
  60. 'sku.stocks as discount_stocks',
  61. 'sku.sale_quantity',
  62. 'activity.id as activity_id',
  63. 'activity.name as activity_name',
  64. 'activity.start_time',
  65. 'activity.end_time',
  66. 'spec.price as sku_price',
  67. 'spec.image as sku_image',
  68. // 'spec.specs as sku_specs'
  69. ]);
  70. // 如果指定了商品ID,则添加条件
  71. if (!empty($goodsIds)) {
  72. $query->where('sku.goods_id', 'in', $goodsIds);
  73. }
  74. // 如果设置了限制数量
  75. if ($limit > 0) {
  76. $query->limit($limit);
  77. }
  78. // 按折扣力度排序,折扣越大越靠前
  79. $query->order('sku.discount', 'asc');
  80. $result = $query->select();
  81. // 处理返回数据
  82. $processedData = [];
  83. foreach ($result as $item) {
  84. $goodsId = $item['goods_id'];
  85. $skuId = $item['sku_id'];
  86. // 如果商品还没有在结果中,初始化
  87. if (!isset($processedData[$goodsId])) {
  88. $processedData[$goodsId] = self::initGoodsDiscountStructure($item);
  89. }
  90. // 添加折扣信息
  91. $discountInfo = [
  92. 'activity_sku_id' => $item['activity_sku_id'],
  93. 'sku_id' => $skuId,
  94. 'discount' => $item['discount'],
  95. 'discount_price' => $item['discount_price'],
  96. 'discount_stocks' => $item['discount_stocks'],
  97. 'sale_quantity' => $item['sale_quantity']
  98. ];
  99. // 如果是多规格商品,添加规格信息
  100. if ($skuId > 0) {
  101. $discountInfo['sku_price'] = $item['sku_price'];
  102. $discountInfo['sku_image'] = $item['sku_image'];
  103. // $discountInfo['sku_specs'] = $item['sku_specs'];
  104. }
  105. $processedData[$goodsId]['discount_info'][] = $discountInfo;
  106. }
  107. // 处理外层折扣价格字段
  108. $processedData = self::processGoodsDiscountData($processedData);
  109. return array_values($processedData);
  110. }
  111. /**
  112. * 查询单个商品的折扣信息和对应的SKU折扣信息
  113. * @param int $goodsId 商品ID
  114. * @return array|null 商品折扣信息,包含所有SKU的折扣详情,如果没有折扣则返回null
  115. */
  116. public static function getGoodsDiscountDetail($goodsId)
  117. {
  118. // 第一步:查询当前时间有效的活动ID
  119. $activeActivityIds = self::getActiveActivityIds();
  120. if (empty($activeActivityIds)) {
  121. return null;
  122. }
  123. // 第二步:查询该商品的SKU折扣数据
  124. $result = Db::table('shop_activity_sku')
  125. ->alias('sku')
  126. ->join('shop_goods goods', 'sku.goods_id = goods.id')
  127. ->join('shop_goods_sku spec', 'sku.sku_id = spec.id', 'left')
  128. ->join(['shop_activity'=> 'activity'], 'sku.activity_id = activity.id')
  129. ->where('sku.goods_id', $goodsId)
  130. ->where('sku.activity_id', 'in', $activeActivityIds)
  131. ->where('goods.status', GoodsEnum::STATUS_ON_SALE) // 已上架的商品
  132. ->where('sku.stocks', '>', 0) // 有折扣库存的商品
  133. ->field([
  134. 'goods.id as goods_id',
  135. 'goods.title',
  136. 'goods.sub_title',
  137. 'goods.image',
  138. 'goods.price as original_price',
  139. 'goods.market_price',
  140. 'goods.stocks as goods_stocks',
  141. 'goods.sales',
  142. 'goods.spec_type',
  143. 'sku.id as activity_sku_id',
  144. 'sku.sku_id',
  145. 'sku.discount',
  146. 'sku.discount_price',
  147. 'sku.stocks as discount_stocks',
  148. 'sku.sale_quantity',
  149. 'activity.id as activity_id',
  150. 'activity.name as activity_name',
  151. 'activity.start_time',
  152. 'activity.end_time',
  153. 'spec.price as sku_price',
  154. 'spec.image as sku_image',
  155. // 'spec.specs as sku_specs'
  156. ])
  157. ->order('sku.discount', 'asc') // 按折扣力度排序,折扣越大越靠前
  158. ->select();
  159. if (empty($result)) {
  160. return null;
  161. }
  162. // 初始化商品折扣结构
  163. $goodsDiscountData = self::initGoodsDiscountStructure($result[0]);
  164. // 处理所有SKU的折扣信息
  165. foreach ($result as $item) {
  166. $skuId = $item['sku_id'];
  167. // 添加折扣信息
  168. $discountInfo = [
  169. 'activity_sku_id' => $item['activity_sku_id'],
  170. 'sku_id' => $skuId,
  171. 'discount' => $item['discount'],
  172. 'discount_price' => $item['discount_price'],
  173. 'discount_stocks' => $item['discount_stocks'],
  174. 'sale_quantity' => $item['sale_quantity']
  175. ];
  176. // 如果是多规格商品,添加规格信息
  177. if ($skuId > 0) {
  178. $discountInfo['sku_price'] = $item['sku_price'];
  179. $discountInfo['sku_image'] = $item['sku_image'];
  180. // $discountInfo['sku_specs'] = $item['sku_specs'];
  181. }
  182. $goodsDiscountData['discount_info'][] = $discountInfo;
  183. }
  184. // 处理外层折扣价格字段
  185. $discountPrices = [];
  186. $discounts = [];
  187. // 收集所有折扣价格和折扣率
  188. foreach ($goodsDiscountData['discount_info'] as $discount) {
  189. $discountPrices[] = $discount['discount_price'];
  190. $discounts[] = $discount['discount'];
  191. }
  192. if (!empty($discountPrices)) {
  193. // 计算最低和最高折扣价格
  194. $goodsDiscountData['min_discount_price'] = min($discountPrices);
  195. $goodsDiscountData['max_discount_price'] = max($discountPrices);
  196. $goodsDiscountData['min_discount'] = min($discounts);
  197. $goodsDiscountData['max_discount'] = max($discounts);
  198. // 单规格商品处理
  199. if ($goodsDiscountData['spec_type'] == 0) {
  200. $goodsDiscountData['discount_price'] = $goodsDiscountData['min_discount_price'];
  201. $goodsDiscountData['discount'] = $goodsDiscountData['min_discount'];
  202. $goodsDiscountData['discount_amount'] = round($goodsDiscountData['original_price'] - $goodsDiscountData['discount_price'], 2);
  203. $goodsDiscountData['price_range'] = '¥' . $goodsDiscountData['discount_price'];
  204. }
  205. // 多规格商品处理
  206. else {
  207. // 设置价格区间
  208. if ($goodsDiscountData['min_discount_price'] == $goodsDiscountData['max_discount_price']) {
  209. $goodsDiscountData['price_range'] = '¥' . $goodsDiscountData['min_discount_price'];
  210. } else {
  211. $goodsDiscountData['price_range'] = '¥' . $goodsDiscountData['min_discount_price'] . '-' . $goodsDiscountData['max_discount_price'];
  212. }
  213. // 对于多规格,使用最低价格作为主要显示价格
  214. $goodsDiscountData['discount_price'] = $goodsDiscountData['min_discount_price'];
  215. $goodsDiscountData['discount'] = $goodsDiscountData['min_discount'];
  216. }
  217. }
  218. $goodsDiscountData['remaining_time'] = self::formatRemainingTime($goodsDiscountData['end_time']);
  219. return $goodsDiscountData;
  220. }
  221. /**
  222. * 限时查询一个当前活动的信息和商品信息 活动信息直接加一个商品数组字段
  223. * @param mixed $activityId
  224. * @return array|bool|Model|string|\PDOStatement|null
  225. */
  226. public static function getCurrentActivity()
  227. {
  228. $currentTime = time();
  229. // 查询活动信息
  230. $activity = Db::table('shop_activity')
  231. // ->where('id', $activityId)
  232. ->where('start_time', '<=', $currentTime)
  233. ->where('end_time', '>=', $currentTime)
  234. ->where('activity_status', ActivityEnum::ACTIVITY_STATUS_ONGOING)
  235. ->where('status', StatusEnum::ENABLED)
  236. ->find();
  237. if (!$activity) {
  238. return null;
  239. }
  240. // 查询该活动下的商品信息
  241. $goods = Db::table('shop_activity_sku')
  242. ->alias('sku')
  243. ->join('shop_goods goods', 'sku.goods_id = goods.id')
  244. ->join('shop_goods_sku spec', 'sku.sku_id = spec.id', 'left')
  245. ->where('sku.activity_id', $activity['id'])
  246. ->where('goods.status', GoodsEnum::STATUS_ON_SALE)
  247. ->where('sku.stocks', '>', 0)
  248. ->field([
  249. 'goods.id as goods_id',
  250. 'goods.title',
  251. 'goods.sub_title',
  252. 'goods.image',
  253. 'goods.price as original_price',
  254. 'goods.market_price',
  255. 'goods.stocks as goods_stocks',
  256. 'goods.sales',
  257. 'goods.spec_type',
  258. 'sku.id as activity_sku_id',
  259. 'sku.sku_id',
  260. 'sku.discount',
  261. 'sku.discount_price',
  262. 'sku.stocks as discount_stocks',
  263. 'sku.sale_quantity',
  264. 'spec.price as sku_price',
  265. 'spec.image as sku_image'
  266. ])
  267. ->order('sku.discount', 'asc')
  268. ->select();
  269. // 处理商品数据
  270. $processedGoods = [];
  271. foreach ($goods as $item) {
  272. $goodsId = $item['goods_id'];
  273. if (!isset($processedGoods[$goodsId])) {
  274. $processedGoods[$goodsId] = self::initGoodsDiscountStructure($item);
  275. }
  276. $discountInfo = [
  277. 'activity_sku_id' => $item['activity_sku_id'],
  278. 'sku_id' => $item['sku_id'],
  279. 'discount' => $item['discount'],
  280. 'discount_price' => $item['discount_price'],
  281. 'discount_stocks' => $item['discount_stocks'],
  282. 'sale_quantity' => $item['sale_quantity']
  283. ];
  284. if ($item['sku_id'] > 0) {
  285. $discountInfo['sku_price'] = $item['sku_price'];
  286. $discountInfo['sku_image'] = $item['sku_image'];
  287. }
  288. $processedGoods[$goodsId]['discount_info'][] = $discountInfo;
  289. }
  290. // 处理外层折扣价格字段
  291. foreach ($processedGoods as &$goodsItem) {
  292. $discountPrices = [];
  293. $discounts = [];
  294. // 收集所有折扣价格和折扣率
  295. foreach ($goodsItem['discount_info'] as $discount) {
  296. $discountPrices[] = $discount['discount_price'];
  297. $discounts[] = $discount['discount'];
  298. }
  299. if (!empty($discountPrices)) {
  300. // 计算最低和最高折扣价格
  301. $goodsItem['min_discount_price'] = min($discountPrices);
  302. $goodsItem['max_discount_price'] = max($discountPrices);
  303. $goodsItem['min_discount'] = min($discounts);
  304. $goodsItem['max_discount'] = max($discounts);
  305. // 单规格商品处理
  306. if ($goodsItem['spec_type'] == 0) {
  307. $goodsItem['discount_price'] = $goodsItem['min_discount_price'];
  308. $goodsItem['discount'] = $goodsItem['min_discount'];
  309. $goodsItem['discount_amount'] = round($goodsItem['original_price'] - $goodsItem['discount_price'], 2);
  310. $goodsItem['price_range'] = '¥' . $goodsItem['discount_price'];
  311. }
  312. // 多规格商品处理
  313. else {
  314. // 设置价格区间
  315. if ($goodsItem['min_discount_price'] == $goodsItem['max_discount_price']) {
  316. $goodsItem['price_range'] = '¥' . $goodsItem['min_discount_price'];
  317. } else {
  318. $goodsItem['price_range'] = '¥' . $goodsItem['min_discount_price'] . '-' . $goodsItem['max_discount_price'];
  319. }
  320. // 对于多规格,使用最低价格作为主要显示价格
  321. $goodsItem['discount_price'] = $goodsItem['min_discount_price'];
  322. $goodsItem['discount'] = $goodsItem['min_discount'];
  323. }
  324. }
  325. }
  326. // 在活动信息中添加商品数组字段
  327. $activity['goods'] = array_values($processedGoods);
  328. return $activity;
  329. }
  330. // 限时zhe'kou一个当前活动的信息和商品信息 活动信息直接加一个商品数组字段
  331. public static function getCurrentActivityInfoAndPrize($activityId)
  332. {
  333. $activity = Db::table('shop_activity')->where('id', $activityId)->find();
  334. $prize = Db::table('shop_activity_sku_prize')->where('activity_id', $activityId)->find();
  335. return ['activity' => $activity, 'prize' => $prize];
  336. }
  337. /**
  338. * 根据商品ID和SKU ID获取折扣信息
  339. * @param int $goodsId 商品ID
  340. * @param int $skuId SKU ID,单规格商品传0
  341. * @return array|null
  342. */
  343. public static function getGoodsDiscountInfo($goodsId, $skuId = 0)
  344. {
  345. $currentTime = time();
  346. // 第一步:查询当前时间有效的活动ID
  347. $activeActivityIds = Db::table('shop_activity')
  348. ->where('start_time', '<=', $currentTime)
  349. ->where('end_time', '>=', $currentTime)
  350. ->where('activity_status', ActivityEnum::ACTIVITY_STATUS_ONGOING)
  351. ->where('status', 'normal')
  352. ->column('id');
  353. if (empty($activeActivityIds)) {
  354. return null;
  355. }
  356. // 第二步:查询指定商品的折扣信息
  357. $discountInfo = Db::table('shop_activity_sku_prize')
  358. ->alias('sku')
  359. ->join('shop_activity activity', 'sku.activity_id = activity.id')
  360. ->where('sku.goods_id', $goodsId)
  361. ->where('sku.sku_id', $skuId)
  362. ->where('sku.activity_id', 'in', $activeActivityIds)
  363. ->where('sku.status', 1)
  364. ->where('sku.stocks', '>', 0)
  365. ->field([
  366. 'sku.id as activity_sku_id',
  367. 'sku.discount',
  368. 'sku.discount_price',
  369. 'sku.stocks as discount_stocks',
  370. 'sku.sale_quantity',
  371. 'activity.id as activity_id',
  372. 'activity.name as activity_name',
  373. 'activity.start_time',
  374. 'activity.end_time'
  375. ])
  376. ->find();
  377. return $discountInfo ?: null;
  378. }
  379. /**
  380. * 批量获取商品的折扣信息
  381. * @param array $goodsIds 商品ID数组
  382. * @return array 键为商品ID,值为折扣信息数组
  383. */
  384. public static function getBatchGoodsDiscountInfo($goodsIds)
  385. {
  386. if (empty($goodsIds)) {
  387. return [];
  388. }
  389. $currentTime = time();
  390. // 第一步:查询当前时间有效的活动ID
  391. $activeActivityIds = Db::table('shop_activity')
  392. ->where('start_time', '<=', $currentTime)
  393. ->where('end_time', '>=', $currentTime)
  394. ->where('activity_status', ActivityEnum::ACTIVITY_STATUS_ONGOING)
  395. ->where('status', 'normal')
  396. ->column('id');
  397. if (empty($activeActivityIds)) {
  398. return [];
  399. }
  400. // 第二步:查询指定商品的折扣信息
  401. $result = Db::table('shop_activity_sku_prize')
  402. ->alias('sku')
  403. ->join('shop_activity activity', 'sku.activity_id = activity.id')
  404. ->where('sku.goods_id', 'in', $goodsIds)
  405. ->where('sku.activity_id', 'in', $activeActivityIds)
  406. ->where('sku.status', 1)
  407. ->where('sku.stocks', '>', 0)
  408. ->field([
  409. 'sku.goods_id',
  410. 'sku.sku_id',
  411. 'sku.id as activity_sku_id',
  412. 'sku.discount',
  413. 'sku.discount_price',
  414. 'sku.stocks as discount_stocks',
  415. 'sku.sale_quantity',
  416. 'activity.id as activity_id',
  417. 'activity.name as activity_name'
  418. ])
  419. ->order('sku.discount', 'asc')
  420. ->select();
  421. // 按商品ID分组
  422. $discountData = [];
  423. foreach ($result as $item) {
  424. $goodsId = $item['goods_id'];
  425. if (!isset($discountData[$goodsId])) {
  426. $discountData[$goodsId] = [];
  427. }
  428. $discountData[$goodsId][] = $item;
  429. }
  430. return $discountData;
  431. }
  432. /**
  433. * 检查商品是否参与当前折扣活动
  434. * @param int $goodsId 商品ID
  435. * @param int $skuId SKU ID
  436. * @return bool
  437. */
  438. public static function hasActiveDiscount($goodsId, $skuId = 0)
  439. {
  440. $discountInfo = self::getGoodsDiscountInfo($goodsId, $skuId);
  441. return !empty($discountInfo);
  442. }
  443. /**
  444. * 计算折扣后的价格
  445. * @param float $originalPrice 原价
  446. * @param float $discount 折扣(如9.0表示9折)
  447. * @return float
  448. */
  449. public static function calculateDiscountPrice($originalPrice, $discount)
  450. {
  451. return round($originalPrice * $discount / 10, 2);
  452. }
  453. /**
  454. * 获取活动剩余时间(秒)
  455. * @param int $endTime 活动结束时间戳
  456. * @return int
  457. */
  458. public static function getActivityRemainingTime($endTime)
  459. {
  460. return max(0, $endTime - time());
  461. }
  462. /**
  463. * 格式化活动剩余时间
  464. * @param int $endTime 活动结束时间戳
  465. * @return array
  466. */
  467. public static function formatRemainingTime($endTime)
  468. {
  469. $remaining = self::getActivityRemainingTime($endTime);
  470. if ($remaining <= 0) {
  471. return ['days' => 0, 'hours' => 0, 'minutes' => 0, 'seconds' => 0];
  472. }
  473. $days = floor($remaining / 86400);
  474. $hours = floor(($remaining % 86400) / 3600);
  475. $minutes = floor(($remaining % 3600) / 60);
  476. $seconds = $remaining % 60;
  477. return [
  478. 'days' => $days,
  479. 'hours' => $hours,
  480. 'minutes' => $minutes,
  481. 'seconds' => $seconds
  482. ];
  483. }
  484. /**
  485. * 处理商品的折扣价格信息(公共方法)
  486. * @param array $goodsData 商品数据数组
  487. * @return array 处理后的商品数据
  488. */
  489. private static function processGoodsDiscountData($goodsData)
  490. {
  491. foreach ($goodsData as &$goods) {
  492. $discountPrices = [];
  493. $discounts = [];
  494. // 收集所有折扣价格和折扣率
  495. foreach ($goods['discount_info'] as $discount) {
  496. $discountPrices[] = $discount['discount_price'];
  497. $discounts[] = $discount['discount'];
  498. }
  499. if (!empty($discountPrices)) {
  500. // 计算最低和最高折扣价格
  501. $goods['min_discount_price'] = min($discountPrices);
  502. $goods['max_discount_price'] = max($discountPrices);
  503. $goods['min_discount'] = min($discounts);
  504. $goods['max_discount'] = max($discounts);
  505. // 单规格商品处理
  506. if ($goods['spec_type'] == 0) {
  507. $goods['discount_price'] = $goods['min_discount_price'];
  508. $goods['discount'] = $goods['min_discount'];
  509. $goods['discount_amount'] = round($goods['original_price'] - $goods['discount_price'], 2);
  510. $goods['price_range'] = '¥' . $goods['discount_price'];
  511. }
  512. // 多规格商品处理
  513. else {
  514. // 设置价格区间
  515. if ($goods['min_discount_price'] == $goods['max_discount_price']) {
  516. $goods['price_range'] = '¥' . $goods['min_discount_price'];
  517. } else {
  518. $goods['price_range'] = '¥' . $goods['min_discount_price'] . '-' . $goods['max_discount_price'];
  519. }
  520. // 对于多规格,使用最低价格作为主要显示价格
  521. $goods['discount_price'] = $goods['min_discount_price'];
  522. $goods['discount'] = $goods['min_discount'];
  523. }
  524. }
  525. }
  526. return $goodsData;
  527. }
  528. /**
  529. * 获取当前有效的活动ID列表
  530. * @return array
  531. */
  532. private static function getActiveActivityIds()
  533. {
  534. $currentTime = time();
  535. return Db::table('shop_activity')
  536. ->where('start_time', '<=', $currentTime)
  537. ->where('end_time', '>=', $currentTime)
  538. ->where('activity_status', ActivityEnum::ACTIVITY_STATUS_ONGOING)
  539. ->where('status', StatusEnum::ENABLED)
  540. ->column('id');
  541. }
  542. /**
  543. * 初始化商品折扣数据结构
  544. * @param array $item 商品数据
  545. * @return array
  546. */
  547. private static function initGoodsDiscountStructure($item)
  548. {
  549. return [
  550. 'goods_id' => $item['goods_id'],
  551. 'title' => $item['title'],
  552. 'sub_title' => $item['sub_title'],
  553. 'image' => $item['image'],
  554. 'original_price' => $item['original_price'],
  555. 'market_price' => $item['market_price'],
  556. 'goods_stocks' => $item['goods_stocks'],
  557. 'sales' => $item['sales'],
  558. 'spec_type' => $item['spec_type'],
  559. 'activity_id' => $item['activity_id'] ?? null,
  560. 'activity_name' => $item['activity_name'] ?? null,
  561. 'start_time' => $item['start_time'] ?? null,
  562. 'end_time' => $item['end_time'] ?? null,
  563. 'has_discount' => true,
  564. 'discount_info' => [],
  565. // 初始化折扣价格相关字段
  566. 'min_discount_price' => null,
  567. 'max_discount_price' => null,
  568. 'min_discount' => null,
  569. 'max_discount' => null,
  570. 'discount_price' => null,
  571. 'discount' => null,
  572. 'discount_amount' => null,
  573. 'price_range' => ''
  574. ];
  575. }
  576. }