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.
134 lines
4.2 KiB
134 lines
4.2 KiB
<?php
|
|
|
|
namespace App\Service\v3\Implementations;
|
|
|
|
use App\Constants\v3\ErrorCode;
|
|
use App\Exception\ErrorCodeException;
|
|
use App\Service\v3\Interfaces\GoodsServiceInterface;
|
|
use Hyperf\DbConnection\Db;
|
|
use App\Model\v3\Goods;
|
|
use App\Model\v3\GoodsBanner;
|
|
use App\Constants\v3\Store;
|
|
use App\Constants\v3\goods as goodsConstants;
|
|
use Hyperf\Redis\Redis;
|
|
use Hyperf\Utils\ApplicationContext;
|
|
|
|
class GoodsService implements GoodsServiceInterface
|
|
{
|
|
public function do($goodsId)
|
|
{
|
|
|
|
}
|
|
|
|
public function check(Goods $goods,$num = 1)
|
|
{
|
|
|
|
$redis = ApplicationContext::getContainer()->get(Redis::class);
|
|
$inventoryKey = 'goods_inventory_sold_1_'.$goods->id; // 拼接activity_type和goods_id
|
|
|
|
if (empty($goods)) {
|
|
return ErrorCode::GOODS_NOT_EXISTS;
|
|
}
|
|
|
|
// 商户歇业
|
|
if($goods->store->is_rest == 1){
|
|
return ErrorCode::STORE_REST;
|
|
}
|
|
// 商品下架或已删除
|
|
if($goods->on_sale == 0 || !is_null($goods->deleted_at)){
|
|
return ErrorCode::GOODS_ON_SALE_NO;
|
|
}
|
|
// 商品库存不足
|
|
// 获取冻结的库存
|
|
//$inventoryFrozen = (int)$redis->get($inventoryKey);
|
|
$inventoryFrozen = 0;
|
|
if($goods->is_infinite != 1 && $goods->inventory < ($num+$inventoryFrozen)){
|
|
return ErrorCode::GOODS_INVENTORY_ERROR;
|
|
}
|
|
|
|
// 是否超过限购数量
|
|
if ($goods->restrict_num != 0 && $goods->restrict_num < $num) {
|
|
return ErrorCode::GOODS_RESTRICT_LIMIT;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public function undo()
|
|
{
|
|
// TODO: Implement undo() method.
|
|
}
|
|
|
|
public function getBanner($goodsId)
|
|
{
|
|
$banner = GoodsBanner::query()->where('goods_id',$goodsId)->orderByDesc('type')->get();
|
|
return $banner;
|
|
}
|
|
|
|
public function detail($goodsId)
|
|
{
|
|
$res = Goods::query()->with('store')->where('id',$goodsId)->first();
|
|
return $res;
|
|
}
|
|
|
|
public function getByType($storeId,$typeId)
|
|
{
|
|
return Goods::query()->withoutGlobalScope('normal')->where(['store_id' => $storeId,'category_id' => $typeId])->orderByDesc('on_sale')->orderByDesc('created_at')->get()->toArray();
|
|
}
|
|
|
|
public function create($params)
|
|
{
|
|
$data =
|
|
[
|
|
'market_id' => $params['market_id'],
|
|
'store_id' => $params['store_id'],
|
|
'name' => $params['name'],
|
|
'category_id' => $params['market_id'],
|
|
'goods_unit' => $params['goods_unit'],
|
|
'price' => $params['price'],
|
|
'original_price' => $params['original_price'],
|
|
'inventory' => $params['inventory'],
|
|
'restrict_num' => $params['restrict_num'],
|
|
'start_num' => $params['start_num'],
|
|
'spec' => $params['spec'],
|
|
'tags' => $params['tags'],
|
|
'remark' => $params['remark'],
|
|
'on_sale' => $params['on_sale'],
|
|
'is_infinite' => $params['is_infinite']
|
|
];
|
|
return Goods::create($data);
|
|
}
|
|
|
|
public function update($params)
|
|
{
|
|
$goods = Goods::query()->where(
|
|
[
|
|
'id' => $params['id'],
|
|
'market_id' => $params['market_id'],
|
|
'store_id' => $params['store_id']
|
|
])
|
|
->update(
|
|
[
|
|
'name' => $params['name'],
|
|
'category_id' => $params['market_id'],
|
|
'goods_unit' => $params['goods_unit'],
|
|
'price' => $params['price'],
|
|
'original_price' => $params['original_price'],
|
|
'inventory' => $params['inventory'],
|
|
'restrict_num' => $params['restrict_num'],
|
|
'start_num' => $params['start_num'],
|
|
'spec' => $params['spec'],
|
|
'tags' => $params['tags'],
|
|
'remark' => $params['remark'],
|
|
'on_sale' => $params['on_sale'],
|
|
'is_infinite' => $params['is_infinite'],
|
|
]);
|
|
return $goods;
|
|
}
|
|
|
|
public function info($goodsId)
|
|
{
|
|
$res = Goods::query()->withoutGlobalScope('normal')->where('id',$goodsId)->first();
|
|
return $res;
|
|
}
|
|
}
|