|
|
<?php
namespace App\Service;
use Hyperf\Di\Annotation\Inject;use Hyperf\DbConnection\Db;use App\Model\Goods;use App\Model\Combination;use App\Model\ShopCar;use App\Constants\LogLabel;use App\Commons\Log;use Hyperf\Utils\ApplicationContext;use App\TaskWorker\SSDBTask;use App\Constants\SsdbKeysPrefix;
class ShopCarService implements ShopCarServiceInterface{ public function addShopCar($params) { //获取ssdb购买记录
$ssdb = ApplicationContext::getContainer()->get(SSDBTask::class); $record_exists = $ssdb->exec('get',SsdbKeysPrefix::PURCHASE_RECORD. date('Ymd') .'_'.$params['user_id'].'_'.$params['good_id']); if($record_exists) { $error = [ 'error' => '特价商品每日只能购买一次' ]; return $error; } //一个订单只能添加一个特价商品
if($params['money'] == 0.01){ $goods_exists = ShopCar::where([ ['money','=',0.01], ['user_id','=',$params['user_id']], ['good_id','!=',$params['good_id']] ]) ->exists(); if($goods_exists){ $error = [ 'error' => '一个订单只能添加一个特价商品' ]; return $error; } } //获取主表商品表信息
$goods = Goods::where([ ['id','=',$params['good_id']], ]) ->select('is_max','restrict_num','box_money','inventory','money') ->first();
//获取购物车该商品购买数量
$num = ShopCar::where([ ['user_id', $params['user_id']], ['good_id', $params['good_id']], ]) ->sum('num'); //限购检验
if($goods->restrict_num > 0 && $goods->restrict_num <= $num) { $error = [ 'error' => '超过商品限购数量' ]; return $error; } //获取规格表商品信息
if($params['combination_id'] > 0) { $combination = Combination::where([ ['id', '=', $params['combination_id']], ]) ->select('wm_money', 'number') ->first(); $inventory = $combination->number; $money = $combination->wm_money; }else{ $inventory = $goods->inventory; $money = $goods->money; } //库存校验 is_max 无限库存
if($goods->is_max != Goods::INVENTORY_NOLIMIT && ($num + $params['num']) > $inventory) { $error = [ 'error' => '库存不足' ]; return $error; } //更新购物车
$exists_sql = ShopCar::where([ ['user_id', '=', $params['user_id']], ['good_id', '=', $params['good_id']], ['market_id','=',$params['market_id']], ['combination_id','=', $params['combination_id']], ]); if($params['combination_id'] > 0) { $exists_sql->where('combination_id',$params['combination_id']); } $exists = $exists_sql->exists(); if($exists) { $update = ShopCar::where([ ['user_id', '=', $params['user_id']], ['good_id', '=', $params['good_id']], ['market_id','=',$params['market_id']], ]); if($params['combination_id'] > 0) { $update->where('combination_id',$params['combination_id']); } $update->increment('num', $params['num']); }else{ $son_id = empty($params['son_id']) ? 0 : $params['son_id']; $dr_id = empty($params['dr_id']) ? 0 : $params['dr_id']; $combination_id = empty($params['combination_id']) ? 0 : $params['combination_id']; $qg_name = empty($params['qg_name']) ? ' ' : $params['qg_name']; $qg_logo = empty($params['qg_logo']) ? ' ' :$params['qg_logo']; ShopCar::insert( [ 'market_id' => $params['market_id'], 'good_id' => $params['good_id'], 'store_id' => $params['store_id'], 'user_id' => $params['user_id'], 'combination_id' => $combination_id, 'num' => $params['num'], 'spec' => $params['spec'], 'son_id' => $son_id, 'dr_id' => $dr_id, 'qg_name' => $qg_name, 'qg_logo' => $qg_logo, 'money' => $money, 'box_money' => $goods->box_money, ] ); } return true; // if($params['goods_id'] == 1561){
// return false;
// }else{
// return '加入购物车成功';
// }
}
public function updateShopCar($params) { if($params['num'] <= 0) { ShopCar::where('id',$params['id'])->delete(); }else { $shop_car = ShopCar::where('id',$params['id']) ->select('good_id','user_id') ->first(); //获取主表商品表信息
$goods = Goods::where([ ['id','=',$shop_car['good_id']], ]) ->select('is_max','restrict_num','box_money','inventory','money') ->first(); //获取购物车该商品购买数量
$num = ShopCar::where([ ['user_id', $shop_car['user_id']], ['good_id', $shop_car['good_id']], ]) ->sum('num'); //限购检验
if($goods->restrict_num > 0 && $goods->restrict_num <= $num) { $error = [ 'error' => '超过商品限购数量' ]; return $error; } if ($shop_car['combination_id'] > 0) { $combination = Combination::where([ ['id', '=', $shop_car['combination_id']], ]) ->select('wm_money', 'number') ->first(); $inventory = $combination->number; }else{ $inventory = $goods->inventory; } //库存校验 is_max 无限库存
if($goods->is_max != Goods::INVENTORY_NOLIMIT && $params['num'] > $inventory) { $error = [ 'error' => '库存不足' ]; return $error; } ShopCar::where('id',$params['id']) ->update(['num' => $params['num']]); return true; } // if($params['good_id'] == 1561){
// return false;
// }else{
// return '更新购物车成功';
// }
}}
|