Goods.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\model;
  13. use think\Paginator;
  14. use think\model\Collection;
  15. use think\model\relation\HasOne;
  16. use cores\BaseModel;
  17. use app\store\model\GoodsCategoryRel as GoodsCategoryRelModel;
  18. use app\common\enum\goods\Status as GoodsStatusEnum;
  19. use app\common\library\helper;
  20. /**
  21. * 商品模型
  22. * Class Goods
  23. * @package app\common\model
  24. */
  25. class Goods extends BaseModel
  26. {
  27. // 定义表名
  28. protected $name = 'goods';
  29. // 定义主键
  30. protected $pk = 'goods_id';
  31. // 追加字段
  32. protected $append = ['goods_sales'];
  33. /**
  34. * 关联模型:主图视频文件
  35. * @return HasOne
  36. */
  37. public function video(): HasOne
  38. {
  39. return $this->hasOne('UploadFile', 'file_id', 'video_id');
  40. }
  41. /**
  42. * 关联模型:主图视频封面图片文件
  43. * @return HasOne
  44. */
  45. public function videoCover(): HasOne
  46. {
  47. return $this->hasOne('UploadFile', 'file_id', 'video_cover_id');
  48. }
  49. /**
  50. * 计算显示销量 (初始销量 + 实际销量)
  51. * @param $value
  52. * @param $data
  53. * @return mixed
  54. */
  55. public function getGoodsSalesAttr($value, $data)
  56. {
  57. if (isset($data['sales_initial'])){
  58. return $data['sales_initial'] + $data['sales_actual'];
  59. }
  60. return 0;
  61. }
  62. /**
  63. * 商品详情:HTML实体转换回普通字符
  64. * @param $value
  65. * @return string
  66. */
  67. public function getContentAttr($value)
  68. {
  69. return htmlspecialchars_decode($value);
  70. }
  71. /**
  72. * 获取器:单独设置折扣的配置
  73. * @param $json
  74. * @return mixed
  75. */
  76. public function getAloneGradeEquityAttr($json)
  77. {
  78. return helper::jsonDecode($json);
  79. }
  80. /**
  81. * 修改器:单独设置折扣的配置
  82. * @param $data
  83. * @return mixed
  84. */
  85. public function setAloneGradeEquityAttr($data)
  86. {
  87. return helper::jsonEncode($data);
  88. }
  89. /**
  90. * 关联商品规格表
  91. * @return \think\model\relation\HasMany
  92. */
  93. public function skuList()
  94. {
  95. return $this->hasMany('GoodsSku')->order(['id' => 'asc']);
  96. }
  97. /**
  98. * 关联商品规格关系表
  99. * @return \think\model\relation\HasMany
  100. */
  101. public function specRel()
  102. {
  103. return $this->hasMany('GoodsSpecRel');
  104. }
  105. /**
  106. * 关联商品图片表
  107. * @return \think\model\relation\HasMany
  108. */
  109. public function images()
  110. {
  111. return $this->hasMany('GoodsImage')->order(['id']);
  112. }
  113. /**
  114. * 关联运费模板表
  115. * @return \think\model\relation\BelongsTo
  116. */
  117. public function delivery()
  118. {
  119. return $this->BelongsTo('Delivery');
  120. }
  121. /**
  122. * 关联订单评价表
  123. * @return \think\model\relation\HasMany
  124. */
  125. public function commentData()
  126. {
  127. return $this->hasMany('Comment');
  128. }
  129. /**
  130. * 获取商品列表
  131. * @param array $param 查询条件
  132. * @param int $listRows 分页数量
  133. * @return mixed
  134. * @throws \think\db\exception\DbException
  135. */
  136. public function getList(array $param = [], int $listRows = 15)
  137. {
  138. // 筛选条件
  139. $query = $this->getQueryFilter($param);
  140. // 设置显示的销量 goods_sales
  141. $query->field(['(sales_initial + sales_actual) as goods_sales']);
  142. // 排序条件
  143. $sort = $this->setQuerySort($param);
  144. // 执行查询
  145. $list = $query->with(['images.file'])
  146. ->alias($this->name)
  147. ->field($this->getAliasFields($this->name, ['content']))
  148. ->where('is_delete', '=', 0)
  149. ->order($sort)
  150. ->paginate($listRows);
  151. // 整理列表数据并返回
  152. return $this->setGoodsListData($list);
  153. }
  154. /**
  155. * 检索排序条件
  156. * @param array $param
  157. * @return array|string[]
  158. */
  159. private function setQuerySort(array $param = [])
  160. {
  161. $params = $this->setQueryDefaultValue($param, [
  162. 'sortType' => 'all', // 排序类型
  163. 'sortPrice' => false, // 价格排序 (true高到低 false低到高)
  164. ]);
  165. // 排序规则
  166. $sort = [];
  167. if ($params['sortType'] === 'all') {
  168. $sort = ['sort' => 'asc'];
  169. } elseif ($params['sortType'] === 'sales') {
  170. $sort = ['goods_sales' => 'desc'];
  171. } elseif ($params['sortType'] === 'price') {
  172. $sort = $params['sortPrice'] ? ['goods_price_max' => 'desc'] : ['goods_price_min' => 'asc'];
  173. }
  174. return array_merge($sort, [$this->getPk() => 'desc']);
  175. }
  176. /**
  177. * 检索查询条件
  178. * @param array $param
  179. * @return \think\db\BaseQuery
  180. */
  181. private function getQueryFilter(array $param)
  182. {
  183. // 商品列表获取条件
  184. $params = $this->setQueryDefaultValue($param, [
  185. 'listType' => 'all', // 列表模式 (全部:all 出售中:on_sale 已下架:off_sale 已售罄:sold_out)
  186. 'categoryId' => null, // 分类ID集
  187. 'goodsName' => null, // 商品名称
  188. 'goodsNo' => null, // 商品编码
  189. 'status' => 0, // 商品状态(0全部 10上架 20下架)
  190. ]);
  191. // 实例化新查询对象
  192. $query = $this->getNewQuery();
  193. // 筛选条件
  194. $filter = [];
  195. // 列表模式
  196. if ($params['listType'] === 'on_sale') {
  197. $filter[] = ['status', '=', GoodsStatusEnum::ON_SALE]; // 出售中
  198. } elseif ($params['listType'] === 'off_sale') {
  199. $filter[] = ['status', '=', GoodsStatusEnum::OFF_SALE]; // 已下架
  200. } elseif ($params['listType'] === 'sold_out') {
  201. $filter[] = ['stock_total', '=', 0]; // 已售罄
  202. }
  203. // 商品状态
  204. $params['status'] > 0 && $filter[] = ['status', '=', (int)$params['status']];
  205. // 商品分类
  206. if ($params['categoryId'] > 0) {
  207. // 关联商品与分类关系记录表
  208. $GoodsCategoryRelName = (new GoodsCategoryRelModel)->getName();
  209. $query->join($GoodsCategoryRelName, "{$GoodsCategoryRelName}.goods_id = {$this->name}.goods_id");
  210. // 设置分类ID条件
  211. $query->where('goods_category_rel.category_id', '=', (int)$params['categoryId']);
  212. }
  213. // 商品名称
  214. !empty($params['goodsName']) && $filter[] = ['goods_name', 'like', "%{$params['goodsName']}%"];
  215. // 商品编码
  216. !empty($params['goodsNo']) && $filter[] = ['goods_no', 'like', "%{$params['goodsNo']}%"];
  217. // 实例化新查询对象
  218. return $query->where($filter);
  219. }
  220. /**
  221. * 设置商品展示的数据
  222. * @param Collection|Paginator $list 商品列表
  223. * @param callable|null $callback 回调函数
  224. * @return mixed
  225. */
  226. protected function setGoodsListData($list, callable $callback = null)
  227. {
  228. if ($list->isEmpty()) return $list;
  229. // 遍历商品列表整理数据
  230. foreach ($list as &$goods) {
  231. $goods = $this->setGoodsData($goods, $callback);
  232. }
  233. return $list;
  234. }
  235. /**
  236. * 整理商品数据
  237. * @param Collection|static $goodsInfo
  238. * @param callable|null $callback
  239. * @return mixed
  240. */
  241. protected function setGoodsData($goodsInfo, callable $callback = null)
  242. {
  243. // 商品图片列表
  244. $goodsInfo['goods_images'] = helper::getArrayColumn($goodsInfo['images'], 'file');
  245. // 商品主图
  246. $goodsInfo['goods_image'] = current($goodsInfo['goods_images'])['preview_url'];
  247. // 商品主图缩略图
  248. $goodsInfo['goods_image_thumbnail'] = config('app.app_google_gcs_host') . '/' . current($goodsInfo['goods_images'])['file_path'];
  249. // 商品销量(实际显示=初始虚拟销量+实际销量)
  250. $goodsInfo['goods_sales'] = $goodsInfo['sales_initial'] + $goodsInfo['sales_actual'];
  251. // 回调函数
  252. is_callable($callback) && call_user_func($callback, $goodsInfo);
  253. return $goodsInfo->hidden(array_merge($this->hidden, ['specRel', 'images']));
  254. }
  255. /**
  256. * 根据商品id集获取商品列表
  257. * @param array $goodsIds
  258. * @param null $status
  259. * @return array|mixed
  260. */
  261. public function getListByIds(array $goodsIds, $status = null)
  262. {
  263. // 筛选条件
  264. $filter = [['goods_id', 'in', $goodsIds]];
  265. // 商品状态
  266. $status > 0 && $filter[] = ['status', '=', $status];
  267. // 获取商品列表数据
  268. $data = $this->withoutField(['content'])
  269. ->with(['images.file'])
  270. ->where($filter)
  271. ->where('is_delete', '=', 0)
  272. ->orderRaw('field(goods_id, ' . implode(',', $goodsIds) . ')')
  273. ->select();
  274. // 整理列表数据并返回
  275. return $this->setGoodsListData($data);
  276. }
  277. /**
  278. * 商品多规格信息
  279. * @param \think\Collection $specRel
  280. * @param \think\Collection $skuData
  281. * @return array
  282. */
  283. public function getManySpecData($specRel, $skuData)
  284. {
  285. // spec_attr
  286. $specAttrData = [];
  287. foreach ($specRel as $item) {
  288. if (!isset($specAttrData[$item['spec_id']])) {
  289. $specAttrData[$item['spec_id']] = [
  290. 'group_id' => $item['spec']['spec_id'],
  291. 'name' => $item['spec']['spec_name'],
  292. 'spec_items' => [],
  293. ];
  294. }
  295. $specAttrData[$item['spec_id']]['spec_items'][] = [
  296. 'item_id' => $item['spec_value_id'],
  297. 'spec_value' => $item['spec_value'],
  298. ];
  299. }
  300. // spec_list
  301. $specListData = [];
  302. foreach ($skuData as $item) {
  303. $image = (isset($item['images']) && !empty($item['images'])) ? $item['images'] : ['file_id' => 0, 'external_url' => ''];
  304. $specListData[] = [
  305. 'id' => $item['id'],
  306. 'goods_sku_id' => $item['goods_sku_id'],
  307. 'rows' => [],
  308. 'form' => [
  309. 'image_id' => $image['file_id'],
  310. 'image_path' => $image['external_url'],
  311. 'goods_sku_no' => $item['goods_sku_no'],
  312. 'goods_price' => $item['goods_price'],
  313. 'goods_weight' => $item['goods_weight'],
  314. 'line_price' => $item['line_price'],
  315. 'stock_num' => $item['stock_num'],
  316. ],
  317. ];
  318. }
  319. return ['spec_attr' => array_values($specAttrData), 'spec_list' => $specListData];
  320. }
  321. /**
  322. * 获取商品记录
  323. * @param int $goodsId
  324. * @param array $with
  325. * @return static
  326. */
  327. public static function detail(int $goodsId, array $with = [])
  328. {
  329. return static::get($goodsId, $with);
  330. }
  331. }