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.
 
 

98 lines
2.8 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 update($params)
{
$goods = Goods::query()->withoutGlobalScope('normal')->where('id',$params['id'])->first();
if (empty($goods)) {
throw new ErrorCodeException( ErrorCode::GOODS_NOT_EXISTS);
}
if(isset($params['name']) && !empty($params['name'])) {
$goods->name = $params['name'];
}
$goods->price = $params['price'];
$goods->on_sale = $params['on_sale'];
return $goods->save();
}
public function info($goodsId)
{
$res = Goods::query()->withoutGlobalScope('normal')->where('id',$goodsId)->first();
return $res;
}
}