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.
80 lines
2.3 KiB
80 lines
2.3 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;
|
|
}
|
|
// 压redis库存
|
|
// redis记录当前商品的购买数量,压库存,下单失败、下单成功扣库存成功、订单取消的时候释放
|
|
if (!$redis->exists($inventoryKey)) {
|
|
$redis->set($inventoryKey, $num);
|
|
} else {
|
|
$redis->incrBy($inventoryKey, intval($num));
|
|
}
|
|
|
|
// 是否超过限购数量
|
|
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;
|
|
}
|
|
}
|