ActiveGoods.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\api\model\sharp;
  10. use app\common\library\helper;
  11. use app\common\model\sharp\ActiveGoods as ActiveGoodsModel;
  12. use app\api\model\Goods as GoodsModel;
  13. /**
  14. * 整点秒杀-活动会场与商品关联模型
  15. * Class ActiveGoods
  16. * @package app\api\model\sharp
  17. */
  18. class ActiveGoods extends ActiveGoodsModel
  19. {
  20. /**
  21. * 隐藏字段
  22. * @var array
  23. */
  24. protected $hidden = [
  25. 'wxapp_id',
  26. 'create_time',
  27. 'update_time',
  28. ];
  29. /**
  30. * 获取指定商品的活动详情
  31. * @param $activeTimeId
  32. * @param $sharpGoodsId
  33. * @return array|false|\PDOStatement|string|\think\Model
  34. */
  35. public static function getGoodsActive($activeTimeId, $sharpGoodsId)
  36. {
  37. return (new static)->with(['active', 'activeTime'])
  38. ->where('active_time_id', '=', $activeTimeId)
  39. ->where('sharp_goods_id', '=', $sharpGoodsId)
  40. ->find();
  41. }
  42. /**
  43. * 获取活动商品详情
  44. * @param $active
  45. * @param $sharpGoodsId
  46. * @param $isCheckStatus
  47. * @return GoodsModel|bool|\think\model\Collection
  48. * @throws \think\exception\DbException
  49. */
  50. public function getGoodsActiveDetail($active, $sharpGoodsId, $isCheckStatus = true)
  51. {
  52. // 获取商品详情
  53. $goods = $this->getGoodsDetail($sharpGoodsId);
  54. if (empty($goods)) return false;
  55. if ($isCheckStatus == true && ($goods['is_delete'] || !$goods['status'])) {
  56. $this->error = '很抱歉,秒杀商品不存在或已下架';
  57. return false;
  58. }
  59. // 活动商品的销量
  60. $goods['sales_actual'] = $active['sales_actual'];
  61. // 商品销售进度
  62. $goods['progress'] = $this->getProgress($active['sales_actual'], $goods['seckill_stock']);
  63. /* @var $goods \think\model\Collection */
  64. return $goods;
  65. }
  66. /**
  67. * 获取商品详情
  68. * @param $sharpGoodsId
  69. * @return GoodsModel|bool
  70. * @throws \think\exception\DbException
  71. */
  72. private function getGoodsDetail($sharpGoodsId)
  73. {
  74. // 获取秒杀商品详情
  75. $model = $this->getGoodsModel();
  76. $sharpGoods = $model::detail($sharpGoodsId, ['sku']);
  77. if (empty($sharpGoods)) {
  78. $this->error = '秒杀商品信息不存在';
  79. return false;
  80. }
  81. // 获取主商品详情
  82. $goods = GoodsModel::detail($sharpGoods['goods_id']);
  83. if (empty($goods)) return false;
  84. // 整理商品信息
  85. $goods['sharp_goods_id'] = $sharpGoods['sharp_goods_id'];
  86. $goods['deduct_stock_type'] = $sharpGoods['deduct_stock_type'];
  87. $goods['limit_num'] = $sharpGoods['limit_num'];
  88. $goods['seckill_stock'] = $sharpGoods['seckill_stock'];
  89. $goods['total_sales'] = $sharpGoods['total_sales'];
  90. $goods['status'] = $sharpGoods['status'];
  91. $goods['is_delete'] = $sharpGoods['is_delete'];
  92. // 商品sku信息
  93. $goods['sku'] = $this->getSharpSku($sharpGoods['sku'], $goods['sku']);
  94. /* @var \think\Collection $goods */
  95. return $goods->hidden(['category', 'sku']);
  96. }
  97. /**
  98. * 获取秒杀商品的sku信息
  99. * @param $sharpSku
  100. * @param $goodsSku
  101. * @return array
  102. */
  103. protected function getSharpSku($sharpSku, $goodsSku)
  104. {
  105. $sharpSku = helper::arrayColumn2Key($sharpSku, 'spec_sku_id');
  106. foreach ($goodsSku as &$item) {
  107. $sharpSkuItem = clone $sharpSku[$item['spec_sku_id']];
  108. $item['original_price'] = $item['goods_price'];
  109. $item['seckill_price'] = $sharpSkuItem['seckill_price'];
  110. $item['seckill_stock'] = $sharpSkuItem['seckill_stock'];
  111. }
  112. return $goodsSku;
  113. }
  114. }