Goods.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\common\model\sharp;
  10. use app\common\library\helper;
  11. use app\common\model\BaseModel;
  12. use app\common\service\Goods as GoodsService;
  13. /**
  14. * 整点秒杀-商品模型
  15. * Class Goods
  16. * @package app\common\model\sharp
  17. */
  18. class Goods extends BaseModel
  19. {
  20. protected $name = 'sharp_goods';
  21. protected $alias = 'sharp_goods';
  22. /**
  23. * 关联商品表
  24. * @return \think\model\relation\BelongsTo
  25. */
  26. public function goods()
  27. {
  28. $module = self::getCalledModule() ?: 'common';
  29. return $this->belongsTo("app\\{$module}\\model\\Goods");
  30. }
  31. /**
  32. * 关联商品规格表
  33. * @return \think\model\relation\HasMany
  34. */
  35. public function sku()
  36. {
  37. return $this->hasMany('GoodsSku', 'sharp_goods_id')
  38. ->order(['seckill_price' => 'asc', 'goods_sku_id' => 'asc']);
  39. }
  40. /**
  41. * 根据商品id集获取商品列表
  42. * @param array $goodsIds
  43. * @param array $param
  44. * @return false|\PDOStatement|string|\think\Collection
  45. * @throws \think\db\exception\DataNotFoundException
  46. * @throws \think\db\exception\ModelNotFoundException
  47. * @throws \think\exception\DbException
  48. */
  49. public function getListByIds($goodsIds, $param = [])
  50. {
  51. // 默认条件
  52. $param = array_merge([
  53. 'status' => null,
  54. 'limit' => 15,
  55. ], $param);
  56. // 筛选条件
  57. !is_null($param['status']) && $this->where('status', '=', (int)$param['status']);
  58. // 获取商品列表数据
  59. $list = $this->with(['sku'])
  60. ->where('sharp_goods_id', 'in', $goodsIds)
  61. ->where('is_delete', '=', 0)
  62. ->order(['sort' => 'asc'])
  63. ->paginate($param['limit'], false, [
  64. 'query' => \request()->request()
  65. ]);
  66. // 设置商品数据
  67. return $this->setGoodsListData($list, true);
  68. }
  69. /**
  70. * 秒杀商品详情
  71. * @param $sharpGoodsId
  72. * @param array $with
  73. * @return static|null
  74. * @throws \think\exception\DbException
  75. */
  76. public static function detail($sharpGoodsId, $with = [])
  77. {
  78. // 整理商品数据并返回
  79. $model = static::get($sharpGoodsId, $with);
  80. return $model->setGoodsListData($model, false);
  81. }
  82. /**
  83. * 商品多规格信息
  84. * @param $goods
  85. * @param $sharpGoodsSku
  86. * @return array|null
  87. */
  88. public function getSpecData($goods, $sharpGoodsSku)
  89. {
  90. $specData = GoodsService::getSpecData($goods);
  91. if ($goods['spec_type'] == 10) {
  92. return $specData;
  93. }
  94. $skuData = helper::arrayColumn2Key($sharpGoodsSku, 'spec_sku_id');
  95. foreach ($specData['spec_list'] as &$item) {
  96. if (isset($skuData[$item['spec_sku_id']])) {
  97. $item['form']['seckill_price'] = $skuData[$item['spec_sku_id']]['seckill_price'];
  98. $item['form']['original_price'] = $item['form']['goods_price'];
  99. $item['form']['seckill_stock'] = $skuData[$item['spec_sku_id']]['seckill_stock'];
  100. }
  101. }
  102. return $specData;
  103. }
  104. /**
  105. * 设置商品展示的数据
  106. * @param $data
  107. * @param bool $isMultiple
  108. * @param callable|null $callback
  109. * @return mixed
  110. * @throws \think\db\exception\DataNotFoundException
  111. * @throws \think\db\exception\ModelNotFoundException
  112. * @throws \think\exception\DbException
  113. */
  114. protected function setGoodsListData($data, $isMultiple = true, callable $callback = null)
  115. {
  116. // 设置原商品数据
  117. $data = GoodsService::setGoodsData($data, $isMultiple);
  118. if (!$isMultiple) $dataSource = [&$data]; else $dataSource = &$data;
  119. // 整理商品数据
  120. foreach ($dataSource as &$item) {
  121. // 商品名称
  122. $item['goods_name'] = $item['goods']['goods_name'];
  123. // 商品图片
  124. $item['goods_image'] = $item['goods']['goods_image'];
  125. // 秒杀商品sku信息
  126. $item['goods_sku'] = $this->getDefaultSharpSku($item['sku'], $item['goods']['sku']);
  127. // 回调函数
  128. is_callable($callback) && call_user_func($callback, $item);
  129. }
  130. return $data;
  131. }
  132. /**
  133. * 整理秒杀商品的默认sku信息 (用于商品列表)
  134. * @param $sharpSku
  135. * @param $goodsSku
  136. * @return mixed
  137. */
  138. private function getDefaultSharpSku($sharpSku, $goodsSku)
  139. {
  140. $sharpGoodsSku = $sharpSku[0];
  141. $goodsSkuItem = helper::getArrayItemByColumn($goodsSku, 'spec_sku_id', $sharpGoodsSku['spec_sku_id']);
  142. $sharpGoodsSku['original_price'] = $goodsSkuItem['goods_price'];
  143. $sharpGoodsSku['image_id'] = $goodsSkuItem['image_id'];
  144. $sharpGoodsSku['goods_no'] = $goodsSkuItem['goods_no'];
  145. $sharpGoodsSku['line_price'] = $goodsSkuItem['line_price'];
  146. $sharpGoodsSku['goods_weight'] = $goodsSkuItem['goods_weight'];
  147. return $sharpGoodsSku;
  148. }
  149. }