StoreActivityServices.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\activity;
  12. use app\dao\activity\StoreActivityDao;
  13. use app\services\activity\seckill\StoreSeckillServices;
  14. use app\services\BaseServices;
  15. use app\services\product\product\StoreProductServices;
  16. use app\services\product\sku\StoreProductAttrValueServices;
  17. use crmeb\exceptions\AdminException;
  18. use crmeb\services\CacheService;
  19. class StoreActivityServices extends BaseServices
  20. {
  21. public function __construct(StoreActivityDao $dao)
  22. {
  23. $this->dao = $dao;
  24. }
  25. public function activityList($where)
  26. {
  27. [$page, $limit] = $this->getPageValue();
  28. $list = $this->dao->activityList($where, '*', $page, $limit, ['seckill' => function ($query) {
  29. $query->field('id,activity_id');
  30. }]);
  31. $seckillTimesData = sys_data('routine_seckill_time');
  32. $resultArray = [];
  33. foreach ($seckillTimesData as $timesData) {
  34. $id = $timesData['id'];
  35. $time = intval($timesData['time']);
  36. $continued = intval($timesData['continued']);
  37. // 格式化时间,加上 ":00"
  38. $startTime = sprintf("%02d:00", $time);
  39. $endTime = sprintf("%02d:00", $time + $continued);
  40. $resultArray[$id] = $startTime . '-' . $endTime;
  41. }
  42. $endIds = [];
  43. foreach ($list as &$item) {
  44. $item['start_day'] = date('Y-m-d H:i:s', $item['start_day']);
  45. if (bcadd($item['end_day'], '86400') < time()) {
  46. $endIds[] = $item['id'];
  47. $item['status'] = 0;
  48. }
  49. $item['end_day'] = date('Y-m-d 23:59:59', $item['end_day']);
  50. $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  51. $item['product_count'] = count($item['seckill']);
  52. foreach (explode(',', $item['time_ids']) as $timeId) {
  53. $item['times_list'][] = $resultArray[$timeId] ?? [];
  54. }
  55. }
  56. if (count($endIds)) {
  57. foreach ($endIds as $endId) {
  58. $this->activityStatus($endId, 0, 1);
  59. }
  60. }
  61. $count = $this->dao->activityCount($where);
  62. return compact('list', 'count');
  63. }
  64. public function activityInfo($id)
  65. {
  66. $info = $this->dao->get(['id' => $id]);
  67. if (!$info) throw new AdminException('数据不存在');
  68. $info = $info->toArray();
  69. /** @var StoreSeckillServices $seckillServices */
  70. $seckillServices = app()->make(StoreSeckillServices::class);
  71. $seckill = $seckillServices->getList(['activity_id' => $id, 'is_del' => 0], 0, 0);
  72. $info['section_time'] = [date('Y-m-d', $info['start_day']), date('Y-m-d', $info['end_day'])];
  73. $info['time_ids'] = array_map('intval', explode(',', $info['time_ids']));
  74. $productList = [];
  75. if ($seckill) {
  76. /** @var StoreProductServices $productServices */
  77. $productServices = app()->make(StoreProductServices::class);
  78. $productList = $productServices->searchList(['id' => array_column($seckill, 'product_id'), 'is_del' => 0]);
  79. $productList = $productList['list'] ?? [];
  80. $seckill = array_combine(array_column($seckill, 'product_id'), $seckill);
  81. //放入秒杀商品价格
  82. foreach ($productList as &$product) {
  83. $product['product_price'] = $product['price'];
  84. $seckillInfo = $seckill[$product['id']] ?? [];
  85. $attrValue = $product['attrs'] ?? [];
  86. if ($seckillInfo && $attrValue) {
  87. $product['status'] = $seckillInfo['status'];
  88. $seckillAttrValue = $seckillInfo['attrs'] ?? [];
  89. if ($seckillAttrValue) {
  90. $seckillAttrValue = array_combine(array_column($seckillAttrValue, 'suk'), $seckillAttrValue);
  91. foreach ($attrValue as &$value) {
  92. $value['status'] = 0;
  93. if (isset($seckillAttrValue[$value['suk']])) {
  94. $value['status'] = 1;
  95. }
  96. $value['product_price'] = $value['price'];
  97. if ($value['status']) {
  98. $value['quota'] = $seckillAttrValue[$value['suk']]['quota'] ?? 0;
  99. $value['quota_show'] = $seckillAttrValue[$value['suk']]['quota_show'] ?? 0;
  100. $value['price'] = $seckillAttrValue[$value['suk']]['price'] ?? 0;
  101. $value['cost'] = $seckillAttrValue[$value['suk']]['cost'] ?? 0;
  102. $value['ot_price'] = $seckillAttrValue[$value['suk']]['ot_price'] ?? 0;
  103. }
  104. }
  105. $product['attrs'] = $attrValue;
  106. }
  107. }
  108. }
  109. }
  110. $info['product_infos'] = $productList;
  111. return $info;
  112. }
  113. public function activityDel($id, $type)
  114. {
  115. if (!$id) throw new AdminException('缺少参数');
  116. $this->dao->update($id, ['is_del' => 1]);
  117. if ($type == 1) {
  118. /** @var StoreSeckillServices $storeSeckillServices */
  119. $storeSeckillServices = app()->make(StoreSeckillServices::class);
  120. $ids = $storeSeckillServices->getColumn(['activity_id' => $id], 'id');
  121. /** @var StoreProductAttrValueServices $storeProductAttrValueServices */
  122. $storeProductAttrValueServices = app()->make(StoreProductAttrValueServices::class);
  123. foreach ($ids as $sid) {
  124. $storeSeckillServices->update($sid, ['is_del' => 1]);
  125. $unique = $storeProductAttrValueServices->value(['product_id' => $id, 'type' => 1], 'unique');
  126. if ($unique) {
  127. CacheService::delete('seckill_' . $unique . '_1');
  128. }
  129. }
  130. }
  131. return true;
  132. }
  133. public function activityStatus($id, $status, $type)
  134. {
  135. if (!$id) throw new AdminException('缺少参数');
  136. $info = $this->dao->get(['id' => $id]);
  137. if (!$info) throw new AdminException('数据不存在');
  138. if (bcadd($info['end_day'], '86400') < time() && $status == 1) {
  139. throw new AdminException('活动已结束,无法操作');
  140. }
  141. $this->dao->update($id, ['status' => $status]);
  142. if ($type == 1) {
  143. /** @var StoreSeckillServices $storeSeckillServices */
  144. $storeSeckillServices = app()->make(StoreSeckillServices::class);
  145. $ids = $storeSeckillServices->getColumn(['activity_id' => $id], 'id');
  146. foreach ($ids as $sid) {
  147. $storeSeckillServices->update($sid, ['status' => $status]);
  148. }
  149. }
  150. return true;
  151. }
  152. }