You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
yanzong/app/api/service/Goods.php

171 lines
5.4 KiB

<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\api\service;
use app\api\model\Goods as GoodsModel;
use app\api\model\Setting as SettingModel;
use app\api\model\GoodsSku as GoodsSkuModel;
use app\common\enum\Setting as SettingEnum;
use app\common\service\Goods as GoodsService;
use app\api\model\sharp\Active as SharpActiveModel;
use app\api\model\sharp\ActiveTime as ActiveTimeModel;
/**
* 商品服务类
* Class Goods
* @package app\api\service
*/
class Goods extends GoodsService
{
/**
* 获取商品的指定的某个SKU信息
* @param int $goodsId
* @param string $goodsSkuId
* @return GoodsSkuModel|array|null
*/
public static function getSkuInfo(int $goodsId, string $goodsSkuId)
{
$detail = GoodsSkuModel::detail($goodsId, $goodsSkuId);
if (!empty($detail['image'])) {
$detail['goods_image'] = $detail['image']['preview_url'];
}
return $detail;
}
/**
* 推荐的商品列表
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function recommended(): array
{
// 获取商品推荐数据
$setting = $this->getRecommendedSetting();
// 获取商品数据
$model = new GoodsModel;
if ($setting['params']['source'] === 'choice') {
// 数据来源:手动
if (empty($setting['params']['goodsIds'])) return [];
$goodsList = $model->getListByIdsFromApi($setting['params']['goodsIds']);
} else {
// 数据来源:自动
$goodsList = $model->getList([
'status' => 10,
'categoryId' => $setting['params']['auto']['category'],
'sortType' => $setting['params']['auto']['goodsSort'],
], $setting['params']['auto']['showNum']);
}
if (empty($goodsList) && $goodsList->isEmpty()) {
return [];
}
return $this->formatGoodsList($goodsList);
}
//大牌正品列表
public function brandList(): array
{
$model = new GoodsModel;
$list = $model->where([
'is_delete' => 0,
'status' => 10,
'is_brand' => 1,
'store_id' => $_GET['store_id']
])->select()->toArray();
return $list;
}
//新品首发
public function newList(): array
{
$model = new GoodsModel;
$list = $model->where([
'is_delete' => 0,
'status' => 10,
'is_new' => 1,
'store_id' => $_GET['store_id']
])->select()->toArray();
return $list;
}
//同城信息
public function cityInfo(): array
{
$model = new GoodsModel;
$list = $model->where([
'is_delete' => 0,
'status' => 10,
'is_brand' => 1,
'store_id' => $_GET['store_id']
])->select()->toArray();
return $list;
}
//抢购会场列表
public function panicList()
{
$model = new SharpActiveModel;
$list = $model->alias('a')
->join('sharpActiveTime', 'a.active_id = sharpActiveTime.active_id', 'left')->where([
'a.is_delete' => 0,
'a.status' => 1,
'a.store_id' => $_GET['store_id'],
'sharpActiveTime.status' => 1,
])->select()->toArray();
foreach ($list as &$v) {
$v['active_date'] = date('Y-m-d H:i:s', $v['active_date']);
}
return $list;
}
/**
* 格式化商品列表
* @param $goodsList
* @return array
*/
private function formatGoodsList($goodsList): array
{
$data = [];
foreach ($goodsList as $goods) {
$data[] = [
'goods_id' => $goods['goods_id'],
'goods_name' => $goods['goods_name'],
'selling_point' => $goods['selling_point'],
'goods_image' => $goods['goods_image'],
'goods_price_min' => $goods['goods_price_min'],
'goods_price_max' => $goods['goods_price_max'],
'line_price_min' => $goods['line_price_min'],
'line_price_max' => $goods['line_price_max'],
'goods_sales' => $goods['goods_sales'],
];
}
return $data;
}
/**
* 获取商品推荐设置
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
private function getRecommendedSetting(): array
{
return SettingModel::getItem(SettingEnum::RECOMMENDED);
}
}