Goods.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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 app\common\enum\goods\GoodsType;
  14. use think\model\relation\BelongsTo;
  15. use think\Paginator;
  16. use think\model\Collection;
  17. use app\common\library\helper;
  18. use app\store\model\GoodsCategoryRel as GoodsCategoryRelModel;
  19. use app\common\enum\goods\Status as GoodsStatusEnum;
  20. use app\common\model\chef\ChefAreas as RegionModel;
  21. use app\common\model\store\Setting as SettingModel;
  22. use app\api\service\User as UserService;
  23. use think\facade\Db;
  24. /**
  25. * 商品模型
  26. * Class Goods
  27. * @package app\common\model
  28. */
  29. class Goods extends BaseModel
  30. {
  31. // 定义表名
  32. protected $name = 'goods';
  33. // 定义主键
  34. protected $pk = 'goods_id';
  35. // 追加字段
  36. protected $append = ['goods_sales','province_text','city_text','region_text', 'category_name','cascader'];
  37. const PACKAGETYPE = 30;
  38. // const UNIT = [
  39. // 1 => '件',
  40. // 2 => '盒',
  41. // 3 => '包',
  42. // 4 => '份',
  43. // 5 => '箱',
  44. // 6 => '袋',
  45. // 7 => '斤',
  46. // 8 => '桶',
  47. // 9 => '瓶',
  48. // 10 => '罐',
  49. // 11 => '个',
  50. // ];
  51. /**
  52. * 省市区id集
  53. * @param $value
  54. * @param $data
  55. * @return array
  56. */
  57. public function getCascaderAttr($value, $data)
  58. {
  59. return [$data['province_id']??0, $data['city_id']??0, $data['region_id']??0];
  60. }
  61. /**
  62. * 计算显示销量 (初始销量 + 实际销量 + 门店销量)
  63. * @param $value
  64. * @param $data
  65. * @return mixed
  66. */
  67. public function getGoodsSalesAttr($value, $data)
  68. {
  69. return ($data['sales_initial']??0) + ($data['sales_actual']??0) + ($data['sales_shops']??0);
  70. }
  71. /**
  72. * Notes:产地:省份
  73. * Author: zhangs
  74. * DateTime: 2021/9/24 14:20
  75. * @param $value
  76. * @param $data
  77. * @return mixed
  78. */
  79. public function getProvinceTextAttr($value, $data)
  80. {
  81. if (empty($data['province_id'])) return '';
  82. return RegionModel::getNameById($data['province_id'] ?? 0);
  83. }
  84. /**
  85. * Notes:产地:城市
  86. * Author: zhangs
  87. * DateTime: 2021/9/24 14:21
  88. * @param $value
  89. * @param $data
  90. * @return mixed
  91. */
  92. public function getCityTextAttr($value, $data)
  93. {
  94. if (empty($data['city_id'])) return '';
  95. return RegionModel::getNameById($data['city_id'] ?? 0);
  96. }
  97. /**
  98. * Notes:产地:地区
  99. * Author: zhangs
  100. * DateTime: 2021/9/24 14:21
  101. * @param $value
  102. * @param $data
  103. * @return mixed
  104. */
  105. public function getRegionTextAttr($value, $data)
  106. {
  107. if (empty($data['region_id'])) return '';
  108. return RegionModel::getNameById($data['region_id'] ?? 0);
  109. }
  110. /**
  111. * Notes:商品分类名称
  112. * Author: zhangs
  113. * DateTime: 2021/9/24 16:41
  114. * @param $value
  115. * @param $data
  116. * @return string
  117. */
  118. public function getCategoryNameAttr($value, $data)
  119. {
  120. if(isset($data['goods_id'])){
  121. $category_ids = GoodsCategoryRel::where('goods_id', '=', $data['goods_id'])->column('category_id');
  122. if (empty($category_ids)) {
  123. return '';
  124. }
  125. $names = Category::whereIn('category_id', $category_ids)->column('name');
  126. return $names ? implode(',', $names) : '';
  127. }
  128. }
  129. /**
  130. * 商品详情:HTML实体转换回普通字符
  131. * @param $value
  132. * @return string
  133. */
  134. public function getContentAttr($value)
  135. {
  136. return htmlspecialchars_decode($value);
  137. }
  138. /**
  139. * 获取器:单独设置折扣的配置
  140. * @param $json
  141. * @return mixed
  142. */
  143. public function getAloneGradeEquityAttr($json)
  144. {
  145. return helper::jsonDecode($json);
  146. }
  147. /**
  148. * 修改器:单独设置折扣的配置
  149. * @param $data
  150. * @return mixed
  151. */
  152. public function setAloneGradeEquityAttr($data)
  153. {
  154. return helper::jsonEncode($data);
  155. }
  156. /**
  157. * 关联商品规格表
  158. * @return \think\model\relation\HasMany
  159. */
  160. public function skuList()
  161. {
  162. return $this->hasMany('GoodsSku')->order(['id' => 'asc']);
  163. }
  164. /**
  165. *
  166. * @return \think\model\relation\HasMany
  167. * @author: zjwhust
  168. * @Time: 2021/12/28 15:43
  169. */
  170. public function category()
  171. {
  172. return $this->hasOne('GoodsCategoryRel','goods_id','goods_id');
  173. }
  174. /**
  175. * 关联商品规格关系表
  176. * @return \think\model\relation\HasMany
  177. */
  178. public function specRel()
  179. {
  180. return $this->hasMany('GoodsSpecRel');
  181. }
  182. /**
  183. * 关联商品图片表
  184. * @return \think\model\relation\HasMany
  185. */
  186. public function images()
  187. {
  188. return $this->hasMany('GoodsImage')->order(['id']);
  189. }
  190. /**
  191. * 关联运费模板表
  192. * @return BelongsTo
  193. */
  194. public function delivery()
  195. {
  196. return $this->belongsTo('Delivery');
  197. }
  198. /**
  199. * 关联订单评价表
  200. * @return \think\model\relation\HasMany
  201. */
  202. public function commentData()
  203. {
  204. return $this->hasMany('Comment');
  205. }
  206. /**
  207. * 关联供应商表/商铺
  208. * @return BelongsTo
  209. */
  210. public function provider()
  211. {
  212. return $this->belongsTo(Provider::class, 'provider_id', 'provider_id');
  213. }
  214. /**
  215. * 获取商品列表
  216. * @param array $param 查询条件
  217. * @param int $listRows 分页数量
  218. * @param bool $showVip 会员商品列表
  219. * @return mixed
  220. * @throws \think\db\exception\DbException
  221. */
  222. public function getList(array $param = [], int $listRows = 15,bool $showVip = false)
  223. {
  224. // 筛选条件
  225. $query = $this->getQueryFilter($param);
  226. // 设置显示的销量 goods_sales
  227. // 购买数*1.5+加入购物车数+收藏数*0.8)之和排倒序
  228. $query->field(['(sales_initial + sales_actual) as online_sales','(sales_initial + sales_actual + sales_shops) as goods_sales','(shops_stock_total+stock_total) as goods_stocks','(collect_num*0.8+cart_num+(sales_initial+sales_actual)*1.5) as common_sort']);
  229. // 排序条件
  230. //$sort = $this->setQuerySort($param);
  231. $sort = $this->setQuerySortZq($param);
  232. //dd($sort);
  233. // 执行查询
  234. $list = $query->with(['images.file','provider'])
  235. ->alias($this->name);
  236. if ($showVip){
  237. $list = $list->whereExists('select id from yoshop_member_goods where goods_id=goods.goods_id and status=0');
  238. }
  239. $list = $list->field($this->getAliasFields($this->name, ['content']))
  240. ->where('is_delete', '=', 0)
  241. ->order($sort)
  242. ->paginate($listRows);
  243. // 整理列表数据并返回
  244. return $this->setGoodsListData($list);
  245. }
  246. /**
  247. * 获取商品列表
  248. * @param array $param 查询条件
  249. * @param int $listRows 分页数量
  250. * @param bool $showVip 会员商品列表
  251. * @param bool $enableExchange 我能兑换
  252. * @param string $userPonitsMoney 我能兑换
  253. * @return mixed
  254. * @throws \think\db\exception\DbException
  255. */
  256. public function getListFront(array $param = [], int $listRows = 15,bool $showVip = false,$enableExchange = false)
  257. {
  258. // 筛选条件
  259. $query = $this->getQueryFilter($param);
  260. // 设置显示的销量 goods_sales
  261. // 购买数*1.5+加入购物车数+收藏数*0.8)之和排倒序
  262. $query->field(['(sales_initial + sales_actual) as online_sales','(sales_initial + sales_actual + sales_shops) as goods_sales','(shops_stock_total+stock_total) as goods_stocks','(collect_num*0.8+cart_num+(sales_initial+sales_actual)*1.5) as common_sort']);
  263. // 排序条件
  264. //$sort = $this->setQuerySort($param);
  265. $sort = $this->setQuerySortZq($param);
  266. //dd($sort);
  267. // 执行查询
  268. $list = $query->with(['images.file','provider'])
  269. ->alias($this->name);
  270. if ($showVip){
  271. $list = $list->whereExists('select id from yoshop_member_goods where goods_id=goods.goods_id and status=0');
  272. }
  273. if ($enableExchange){
  274. $user = UserService::getCurrentLoginUser();
  275. $rate = SettingModel::getItem('points_rate')['points_2_money'];
  276. $pointsToMoney = bcdiv(strval($user['accumulate_points'] ?? '0'), $rate, 2);
  277. $list = $list->where('goods_price_min','<',$pointsToMoney);
  278. }
  279. $list = $list->field($this->getAliasFields($this->name, ['content']))
  280. ->where('is_delete', '=', 0)
  281. ->where('is_show', '=', 1)
  282. ->order($sort)
  283. ->paginate($listRows);
  284. // 整理列表数据并返回
  285. return $this->setGoodsListData($list);
  286. }
  287. /**
  288. * 检索排序条件
  289. * @param array $param
  290. * @return array|string[]
  291. */
  292. private function setQuerySort(array $param = [])
  293. {
  294. $params = $this->setQueryDefaultValue($param, [
  295. 'sortType' => 'all', // 排序类型
  296. 'sortPrice' => false, // 价格排序 (true高到低 false低到高)
  297. ]);
  298. // 排序规则
  299. $sort = [];
  300. if ($params['sortType'] === 'all') {
  301. $sort = ['common_sort' => 'desc'];
  302. } elseif ($params['sortType'] === 'sales') {
  303. $sort = ['goods_sales' => 'desc'];
  304. } elseif ($params['sortType'] === 'price') {
  305. $sort = $params['sortPrice'] ? ['goods_price_max' => 'desc'] : ['goods_price_min' => 'asc'];
  306. } elseif ($params['sortType'] == 'create_time') { // 按上架时间倒序
  307. $sort = ['create_time' => 'desc'];
  308. }
  309. return array_merge($sort, [$this->getPk() => 'desc']);
  310. }
  311. /**
  312. * 检索排序条件
  313. * @param array $param
  314. * @return array|string[]
  315. */
  316. private function setQuerySortZq(array $param = [])
  317. {
  318. $params = $this->setQueryDefaultValue($param, [
  319. 'sortStyle' => 'all', // 排序类型
  320. 'sortSort' => false, // 价格排序 (true高到低 false低到高)
  321. ]);
  322. // 排序规则
  323. $sort = [];
  324. $sortSort = ($params['sortSort'] == 'false' || !$params['sortSort'])?false:true;
  325. if ($params['sortStyle'] == 'all') {//综合
  326. $sort = $sortSort ?['goods_sales' => 'desc']:['goods_sales' => 'asc'];
  327. } elseif ($params['sortStyle'] == 'sales') {
  328. $sort = $sortSort ?['goods_sales' => 'desc']:['goods_sales' => 'asc'];
  329. } elseif ($params['sortStyle'] == 'price') {
  330. $sort = $sortSort ? ['goods_price_max' => 'desc'] : ['goods_price_min' => 'asc'];
  331. } elseif ($params['sortStyle'] == 'create_time') { // 按上架时间倒序
  332. $sort = $sortSort ? ['create_time' => 'desc'] : ['create_time' => 'asc'];
  333. }
  334. return array_merge($sort, [$this->getPk() => 'desc']);
  335. }
  336. /**
  337. * 检索查询条件
  338. * @param array $param
  339. * @return \think\db\BaseQuery
  340. */
  341. private function getQueryFilter(array $param)
  342. {
  343. // 商品列表获取条件
  344. $params = $this->setQueryDefaultValue($param, [
  345. 'listType' => 'all', // 列表模式 (全部:all 出售中:on_sale 已下架:off_sale 已售罄:sold_out)
  346. 'categoryId' => null, // 分类ID集
  347. 'goodsName' => null, // 商品名称
  348. 'goodsNo' => null, // 商品编码
  349. 'status' => 0, // 商品状态(0全部 10上架 20下架)
  350. 'have_stock' => 0, // 是否查找有库存商品
  351. 'stock_status' => -1, // 库存状态 0-预警 1-正常
  352. 'spec_type'=>0
  353. ]);
  354. // 实例化新查询对象
  355. $query = $this->getNewQuery();
  356. // 筛选条件
  357. $filter = [];
  358. // 列表模式
  359. if ($params['listType'] === 'on_sale') {
  360. $filter[] = ['status', '=', GoodsStatusEnum::ON_SALE]; // 出售中
  361. } elseif ($params['listType'] === 'off_sale') {
  362. $filter[] = ['status', '=', GoodsStatusEnum::OFF_SALE]; // 已下架
  363. } elseif ($params['listType'] === 'sold_out') {
  364. $filter[] = ['stock_total', '=', 0]; // 已售罄
  365. }
  366. if(!empty($params['not_goods_id_arr'])){
  367. $filter[] = ['goods.goods_id','not in',$params['not_goods_id_arr']];
  368. }
  369. if(!empty($params['spec_type'])){
  370. $filter[] = ['spec_type','=',$params['spec_type']];
  371. }
  372. if(isset($params['goods_id_arr'])){
  373. $filter[] = ['goods_id','in',$params['goods_id_arr']];
  374. }
  375. // 起止时间
  376. if (!empty($params['betweenTime'])) {
  377. $times = between_time($params['betweenTime']);
  378. $filter[] = ['goods.create_time', '>=', $times['start_time']];
  379. $filter[] = ['goods.create_time', '<', $times['end_time'] + 86400];
  380. }
  381. if (isset($params['stock_status']) && $params['stock_status'] != -1) { // 库存预警
  382. $filter[] = ['status', '=', GoodsStatusEnum::ON_SALE]; // 出售中
  383. $filterSku[] = ['stock_num', 'exp', Db::raw('< `alarm_stock_num`')];
  384. $goodsIds = GoodsSku::where($filterSku)->column('goods_id');
  385. if ($params['stock_status'] == 1) {
  386. if (!empty($goodsIds)) {
  387. $filter[] = ['goods_id', 'not in', $goodsIds];
  388. }
  389. } else {
  390. $filter[] = ['goods_id', 'in', $goodsIds];
  391. }
  392. }
  393. // 商品状态
  394. $params['status'] > 0 && $filter[] = ['status', '=', (int)$params['status']];
  395. $params['have_stock'] > 0 && $filter[] = ['stock_total', '>', 0]; // 有库存
  396. // 商品分类
  397. if ($params['categoryId'] > 0) {
  398. // 关联商品与分类关系记录表
  399. $GoodsCategoryRelName = (new GoodsCategoryRelModel)->getName();
  400. $query->join($GoodsCategoryRelName, "{$GoodsCategoryRelName}.goods_id = {$this->name}.goods_id");
  401. // 设置分类ID条件
  402. $query->where('goods_category_rel.category_id', '=', (int)$params['categoryId']);
  403. }
  404. // 商品名称
  405. !empty($params['goodsName']) && $filter[] = ['goods_name', 'like', "%{$params['goodsName']}%"];
  406. // 商品编码
  407. !empty($params['goodsNo']) && $filter[] = ['goods_no', 'like', "%{$params['goodsNo']}%"];
  408. //普通商品还是赠品
  409. //in_array($params['goods_type'],[10,20]) && $filter[] = ['goods_type','=',$params['goods_type']];
  410. if(isset($params['goods_type']) && ($params['goods_type'] == GoodsType::GIFT || $params['goods_type'] == GoodsType::NORMAL)){
  411. $filter[] = ['goods_type','=',$params['goods_type']];
  412. $filter[] = ['stock_total','>',0];
  413. }elseif(isset($params['goods_type']) && $params['goods_type'] == 90){
  414. //需要单规格的商品或赠品
  415. $filter[] = ['goods_type','<',GoodsType::MULTI];
  416. $filter[] = ['stock_total','>',0];
  417. }elseif(isset($params['goods_type']) && $params['goods_type'] == GoodsType::EXCHANGE){
  418. //积分兑换商品
  419. $filter[] = ['goods_type','=',GoodsType::EXCHANGE];
  420. $filter[] = ['stock_total','>',0];
  421. }else{
  422. if (isset($params['is_backend']) && $params['is_backend'] == 1){
  423. }else{
  424. $filter[] = ['goods_type','<>',GoodsType::GIFT];
  425. }
  426. }
  427. // // 是否显示在商品列表
  428. // $filter[] = ['display', '=', 1]; // 可以显示的
  429. // 实例化新查询对象
  430. return $query->where($filter);
  431. }
  432. /**
  433. * 设置商品展示的数据
  434. * @param Collection|Paginator $list 商品列表
  435. * @param callable|null $callback 回调函数
  436. * @return mixed
  437. */
  438. protected function setGoodsListData($list, callable $callback = null)
  439. {
  440. if ($list->isEmpty()) return $list;
  441. // 遍历商品列表整理数据
  442. foreach ($list as &$goods) {
  443. $goods = $this->setGoodsData($goods, $callback);
  444. }
  445. return $list;
  446. }
  447. /**
  448. * 设置商品展示的结算价格列表
  449. * @param Collection|Paginator $list 商品列表
  450. * @param callable|null $callback 回调函数
  451. * @return mixed
  452. */
  453. protected function setGoodsListDataFinance($list, callable $callback = null)
  454. {
  455. if ($list->isEmpty()) return $list;
  456. // 遍历商品列表整理数据
  457. foreach ($list as &$goods) {
  458. $goods = $this->setGoodsData($goods, $callback);
  459. $goodsArr = $goods->toArray();
  460. $clearing_price_arr = array_column($goodsArr['skues'],'clearing_price');
  461. //dd($clearing_price_arr);
  462. if (count($clearing_price_arr) == 0){
  463. $goods['clearing_price_range'] = '';
  464. } elseif (count($clearing_price_arr) == 1){
  465. $goods['clearing_price_range'] = (string)$clearing_price_arr[0];
  466. }else{
  467. $clearing_price_arr = array_unique($clearing_price_arr);
  468. asort($clearing_price_arr);
  469. $clearing_price_arr = array_values($clearing_price_arr);
  470. $goods['clearing_price_range'] = $clearing_price_arr[0].'~'.$clearing_price_arr[count($clearing_price_arr)-1];
  471. }
  472. $rate_arr = array_column($goodsArr['skues'],'platform_rate');
  473. if (count($rate_arr) == 0){
  474. $goods['platform_rate_range'] = '';
  475. } elseif (count($rate_arr) == 1){
  476. $goods['platform_rate_range'] = (string)$rate_arr[0];
  477. }else{
  478. $rate_arr = array_unique($rate_arr);
  479. asort($rate_arr);
  480. $rate_arr = array_values($rate_arr);
  481. $goods['platform_rate_range'] = $rate_arr[0].'~'.$rate_arr[count($rate_arr)-1];
  482. }
  483. }
  484. return $list;
  485. }
  486. /**
  487. * 整理商品数据
  488. * @param Collection|static $goodsInfo
  489. * @param callable|null $callback
  490. * @return mixed
  491. */
  492. protected function setGoodsData($goodsInfo, callable $callback = null)
  493. {
  494. // 商品图片列表
  495. $goodsInfo['goods_images'] = helper::getArrayColumn($goodsInfo['images'], 'file');
  496. $goodsImages = [];
  497. if (!empty($goodsInfo['goods_images'])) {
  498. foreach ($goodsInfo['goods_images'] as $item) {
  499. $item = $item->toArray();
  500. $item['preview_url'] = config('chef.sso_domain_text').$item['file_path'];
  501. $item['external_url'] = config('chef.sso_domain_text').$item['file_path'];
  502. $goodsImages[] = $item;
  503. }
  504. }
  505. $goodsInfo['goods_images'] = $goodsImages;
  506. // 商品主图
  507. $goodsInfo['goods_image'] = current($goodsInfo['goods_images'])['preview_url'];
  508. // 商品主图 生成海报用
  509. $goodsInfo['goods_image_text'] = current($goodsInfo['goods_images'])['preview_url'];
  510. // 商品销量(实际显示=初始虚拟销量+实际销量)
  511. $goodsInfo['goods_sales'] = $goodsInfo['sales_initial'] + $goodsInfo['sales_actual'];
  512. // 商品库存(总库存=线上库存+门店库存)
  513. $goodsInfo['goods_stocks'] = $goodsInfo['stock_total'] + $goodsInfo['shops_stock_total'];
  514. // 回调函数
  515. is_callable($callback) && call_user_func($callback, $goodsInfo);
  516. return $goodsInfo->hidden(array_merge($this->hidden, ['specRel', 'images']));
  517. }
  518. /**
  519. * 根据商品id集获取商品列表
  520. * @param array $goodsIds
  521. * @param null $status
  522. * @return array|mixed
  523. */
  524. public function getListByIds(array $goodsIds, $status = null)
  525. {
  526. if (empty($goodsIds)) {
  527. return [];
  528. }
  529. // 筛选条件
  530. $filter = [['goods_id', 'in', $goodsIds]];
  531. // 商品状态
  532. $status > 0 && $filter[] = ['status', '=', $status];
  533. // 获取商品列表数据
  534. $data = $this
  535. // ->withoutField(['content'])
  536. ->with(['images.file','category'])
  537. ->where($filter)
  538. ->where('is_delete', '=', 0)
  539. ->orderRaw('field(goods_id, ' . implode(',', $goodsIds) . ')')
  540. ->select();
  541. // 整理列表数据并返回
  542. return $this->setGoodsListData($data);
  543. }
  544. /**
  545. * 商品多规格信息
  546. * @param \think\Collection $specRel
  547. * @param \think\Collection $skuData
  548. * @return array
  549. */
  550. public function getManySpecData($specRel, $skuData)
  551. {
  552. // spec_attr
  553. $specAttrData = [];
  554. foreach ($specRel as $item) {
  555. if (!isset($specAttrData[$item['spec_id']])) {
  556. $specAttrData[$item['spec_id']] = [
  557. 'group_id' => $item['spec']['spec_id'],
  558. 'name' => $item['spec']['spec_name'],
  559. 'spec_items' => [],
  560. ];
  561. }
  562. $specAttrData[$item['spec_id']]['spec_items'][] = [
  563. 'item_id' => $item['spec_value_id'],
  564. 'spec_value' => $item['spec_value'],
  565. ];
  566. }
  567. // spec_list
  568. $specListData = [];
  569. foreach ($skuData as $item) {
  570. $image = (isset($item['images']) && !empty($item['images'])) ? $item['images'] : ['file_id' => 0, 'external_url' => ''];
  571. $specListData[] = [
  572. 'id' => $item['id'],
  573. 'goods_sku_id' => $item['goods_sku_id'],
  574. 'rows' => [],
  575. 'form' => [
  576. 'image_id' => $image['file_id'],
  577. 'image_path' => $image['external_url'],
  578. 'goods_sku_no' => $item['goods_sku_no'],
  579. 'goods_price' => $item['goods_price'],
  580. 'goods_weight' => $item['goods_weight'],
  581. 'line_price' => $item['line_price'],
  582. 'stock_num' => $item['stock_num'],
  583. ],
  584. ];
  585. }
  586. return ['spec_attr' => array_values($specAttrData), 'spec_list' => $specListData];
  587. }
  588. /**
  589. * 获取商品记录
  590. * @param int $goodsId
  591. * @param array $with
  592. * @return static
  593. */
  594. public static function detail(int $goodsId, array $with = [])
  595. {
  596. return static::get($goodsId, $with);
  597. }
  598. /**
  599. * 累积收藏数
  600. * @param int $goodsId 商品ID
  601. * @param int $num 递增的数量
  602. * @return mixed
  603. */
  604. public static function setIncByField(int $goodsId, $field = '', int $num = 1)
  605. {
  606. return (new static)->setInc([['goods_id', '=', $goodsId]], $field, $num);
  607. }
  608. /**
  609. * 累积收藏数
  610. * @param int $goodsId 商品ID
  611. * @param int $num 递减的数量
  612. * @return mixed
  613. */
  614. public static function setDecByField(int $goodsId, $field = '', int $num = 1)
  615. {
  616. return (new static)->setDec([['goods_id', '=', $goodsId]], $field, $num);
  617. }
  618. // /**
  619. // * 指定的商品规格信息
  620. // * @param static $goods 商品详情
  621. // * @param string $goodsSkuId
  622. // * @return array|bool
  623. // */
  624. // public static function getGoodsSku(self $goods, string $goodsSkuId)
  625. // {
  626. // // 获取指定的sku
  627. // $goodsSku = [];
  628. // foreach ($goods['skuList'] as $item) {
  629. // if ($item['goods_sku_id'] == $goodsSkuId) {
  630. // $goodsSku = $item;
  631. // break;
  632. // }
  633. // }
  634. // if (empty($goodsSku)) {
  635. // return false;
  636. // }
  637. // // 多规格文字内容
  638. // $goodsSku['goods_attr'] = '';
  639. // if ($goods['spec_type'] == 20) {
  640. // $specRelData = helper::arrayColumn2Key($goods['spec_rel'], 'spec_value_id');
  641. // $attrs = explode('_', $goodsSku['goods_sku_id']);
  642. // foreach ($attrs as $specValueId) {
  643. // $goodsSku['goods_attr'] .= $specRelData[$specValueId]['spec']['spec_name'] . ':'
  644. // . $specRelData[$specValueId]['spec_value'] . '; ';
  645. // }
  646. // }
  647. // return $goodsSku;
  648. // }
  649. public function getCostsList(array $param = [], int $listRows = 15)
  650. {
  651. // 筛选条件
  652. $query = $this->getQueryFilter($param);
  653. // 排序条件
  654. //$sort = $this->setQuerySort($param);
  655. // 执行查询
  656. $list = $query->with(['provider','skues'])
  657. ->alias($this->name)
  658. ->field($this->getAliasFields($this->name, ['content']))
  659. ->where('is_delete', '=', 0)
  660. ->paginate($listRows);
  661. // 整理列表数据并返回
  662. return $this->setGoodsListDataFinance($list);
  663. }
  664. /**
  665. * 关联供应商表/商铺
  666. * @return \think\model\relation\HasMany
  667. */
  668. public function skues()
  669. {
  670. return $this->hasMany(\app\store\model\GoodsSku::class, 'goods_id', 'goods_id')->field('id,goods_id,clearing_price,platform_rate,goods_props');
  671. }
  672. /**
  673. * 导出提现列表
  674. * @param array $param
  675. * @author: zq
  676. * @Time: 2021/10/15 13:51
  677. */
  678. public function listExport(array $param)
  679. {
  680. //$ids = $param['goods_id'];
  681. $data['header'] = ['商品序号', '商品编码', '商品名称', '供应商名称', '商品分类','售价', '结算价'];
  682. $data['filename'] = '财务成本管理列表导出';
  683. $data['data'] = [];
  684. $query = $this->getQueryFilter($param);
  685. $listI = $query->with(['provider','skues'])
  686. ->alias($this->name)
  687. ->field($this->getAliasFields($this->name, ['content']))
  688. ->where('is_delete', '=', 0)->select();
  689. //->whereIn('goods_id',$ids)
  690. //->paginate(500);
  691. // 整理列表数据并返回
  692. $list = $this->setGoodsListDataFinance($listI);
  693. /* $list = $listPage->toArray();
  694. dd($listPage);*/
  695. foreach ($list as $arr){
  696. $new_list['goods_id'] = $arr['goods_id'];
  697. $new_list['goods_no'] = $arr['goods_no'];
  698. $new_list['goods_name'] = $arr['goods_name'];
  699. $new_list['provider'] = $arr['provider']['provider_name'];
  700. $new_list['category_name'] = $arr['category_name'];
  701. $new_list['goods_prices'] = $arr['goods_price_min'].'~'.$arr['goods_price_max'];
  702. $new_list['clearing_price_range'] = $arr['clearing_price_range'];
  703. $data['data'][] = $new_list;
  704. }
  705. return $data;
  706. }
  707. /**
  708. * 获取商品列表
  709. * @param array $param 查询条件
  710. * @return mixed
  711. * @throws \think\db\exception\DbException
  712. */
  713. public function getListNoPage()
  714. {
  715. $param['sortType'] = 'create_time';
  716. $param['listType'] = 'all';
  717. $param['status'] = 10;
  718. // 筛选条件
  719. $query = $this->getQueryFilter($param);
  720. // 设置显示的销量 goods_sales
  721. // 购买数*1.5+加入购物车数+收藏数*0.8)之和排倒序
  722. $query->field(['(sales_initial + sales_actual + sales_shops) as goods_sales']);
  723. // 排序条件
  724. $sort = $this->setQuerySortZq($param);
  725. // 执行查询
  726. return $query->with(['images.file'])
  727. ->alias($this->name)
  728. ->field($this->getAliasFields($this->name, ['content']))
  729. ->where('status', '=', 10)
  730. ->where('is_delete', '=', 0)
  731. ->order($sort)
  732. ->select();
  733. /* return $list;
  734. dd($list->toArray());
  735. // 整理列表数据并返回
  736. return $this->setGoodsListData($list);*/
  737. }
  738. }