Groupon.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace addons\shopro\controller\activity;
  3. use addons\shopro\controller\Common;
  4. use app\admin\model\shopro\activity\Groupon as GrouponModel;
  5. use app\admin\model\shopro\activity\GrouponLog as GrouponLogModel;
  6. use addons\shopro\service\goods\GoodsService;
  7. use addons\shopro\facade\Activity as ActivityFacade;
  8. class Groupon extends Common
  9. {
  10. protected $noNeedLogin = ['index', 'detail'];
  11. protected $noNeedRight = ['*'];
  12. // 商品详情,参团列表
  13. public function index()
  14. {
  15. $params = $this->request->param();
  16. $goods_id = $params['goods_id'] ?? 0;
  17. $activity_id = $params['activity_id'] ?? 0;
  18. $groupons = GrouponModel::with('leader')->ing()
  19. ->where('goods_id', $goods_id)
  20. // ->where('activity_id', $activity_id)
  21. ->order('id', 'asc')
  22. // ->paginate($this->request->param('list_rows', 10));
  23. ->autopage()->select();
  24. $this->success('获取成功', $groupons);
  25. }
  26. // 团详情
  27. public function detail()
  28. {
  29. $id = $this->request->param('id');
  30. $groupon = GrouponModel::with(['my', 'groupon_logs', 'activity' => function ($query) {
  31. $query->removeOption('soft_delete')->with(['activity_sku_prices']); // 关联团所属活动,并关联活动规格
  32. }])->where('id', $id)->find();
  33. if (!$groupon) {
  34. $this->error(__('No Results were found'));
  35. }
  36. session('goods-activity_id:' . $groupon->goods_id, $groupon->activity_id);
  37. $service = new GoodsService(function ($goods, $service) use ($groupon) {
  38. $goods->skus = $goods->skus;
  39. $goods->activity = $goods->activity;
  40. return $goods;
  41. });
  42. // 查询所有状态的商品,并且包含被删除的商品
  43. $goods = $service->activity($groupon->activity_id)->withTrashed()->where('id', $groupon->goods_id)->find();
  44. if (!$goods) {
  45. $this->error('活动商品不存在');
  46. }
  47. // 商品可能关联不出来活动,因为活动可能已经被删除,redis 也会被删除
  48. if (!$currentActivity = $goods->activity) {
  49. // 活动已经结束被删除
  50. if ($currentActivity = $groupon->activity) { // 尝试获取包含回收站在内的活动信息
  51. // 获取活动格式化之后的规格
  52. $skuPrices = ActivityFacade::recoverSkuPrices($goods, $currentActivity);
  53. $goods['new_sku_prices'] = $skuPrices;
  54. }
  55. }
  56. $goods = $goods->toArray();
  57. $goods['sku_prices'] = $goods['new_sku_prices'];
  58. unset($goods['new_sku_prices']);
  59. $groupon['goods'] = $goods;
  60. $groupon['activity_status'] = $currentActivity['status'];
  61. $this->success('获取成功', $groupon);
  62. }
  63. // 我的拼团
  64. public function myGroupons()
  65. {
  66. $user = auth_user();
  67. $params = $this->request->param();
  68. $type = $params['type'] ?? 'all';
  69. $grouponIds = GrouponLogModel::where('user_id', $user->id)->order('id', 'desc')->column('groupon_id');
  70. $groupons = GrouponModel::with(['my' => function ($query) {
  71. $query->with(['order', 'order_item']);
  72. }, 'groupon_logs', 'activity' => function ($query) {
  73. $query->removeOption('soft_delete')->with(['activity_sku_prices']); // 关联团所属活动,并关联活动规格
  74. }, 'goods' => function ($query) {
  75. $query->removeOption('soft_delete');
  76. }])->whereIn('id', $grouponIds);
  77. if ($type != 'all') {
  78. $type = $type == 'finish' ? ['finish', 'finish-fictitious'] : [$type];
  79. $groupons = $groupons->whereIn('status', $type);
  80. }
  81. if ($grouponIds) {
  82. $groupons = $groupons->orderRaw('field(id, ' . join(',', $grouponIds) . ')');
  83. }
  84. $groupons = $groupons->paginate(request()->param('list_rows', 10))->toArray();
  85. foreach ($groupons['data'] as &$groupon) {
  86. if ($groupon['goods'] && isset($groupon['my']['order_item'])) {
  87. $groupon['goods']['price'] = [$groupon['my']['order_item']['goods_price']];
  88. }
  89. if ($groupon['activity']) {
  90. $activity = $groupon['activity'];
  91. if ($activity['rules'] && isset($activity['rules']['sales_show_type']) && $activity['rules']['sales_show_type'] == 'real') {
  92. $sales = [];
  93. foreach ($activity['activity_sku_prices'] as $activitySkuPrice) {
  94. if ($activitySkuPrice['goods_id'] == $groupon['goods_id']) {
  95. $sales[] = $activitySkuPrice['sales'];
  96. }
  97. }
  98. if ($groupon['goods']) {
  99. $groupon['goods']['sales'] = array_sum($sales); // 这里计算的销量和 getSalesAttr 不一样,这里的没有排除下架的规格
  100. }
  101. }
  102. }
  103. }
  104. $this->success('获取成功', $groupons);
  105. }
  106. }