Goods.php 4.9 KB

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