购物车

parent 8638708a
......@@ -37,9 +37,14 @@ class CartApiController extends Controller
'data',
]);
$result = CartService::addCart($data, $request->user->user_id);
$result = CartService::addCart($data, $request->user->id);
return !$result ? $this->setError('Add cart failed , please contact administrator'):$this->setSuccess('Add inquiry success');
}
//购物车列表
public function cartLists(){
}
}
......@@ -44,17 +44,17 @@ class CartService
$temp["raw_brand_name"]= $digikeyArr["raw_brand_name"];
}
$checkHas = CartModel::where(["user_id"=>$user_id,"goods_id"=>$goods_id])->first();
$checkHas = CartModel::where(["user_id"=>$user_id,"goods_id"=>$goods_id,"status"=>1])->first();
if ($checkHas){ //存在累计库存
$temp["goods_number"] = $skuInfo["stock"] > ($item['goods_number']+$checkHas["goods_number"]) ? $item['goods_number']+$checkHas["goods_number"] : $skuInfo["stock"];
$temp["goods_number"] = $skuInfo["stock"] > ($item['buy_number']+$checkHas["buy_number"]) ? $item['buy_number']+$checkHas["buy_number"] : $skuInfo["stock"];
$temp["update_time"] = time();
$flag = CartModel::where(["user_id"=>$user_id,"goods_id"=>$goods_id])->update($temp);
$flag = CartModel::where(["cart_id"=>$checkHas["cart_id"]])->update($temp);
if (!$flag){
return false;
}
}else{ //不存在插入购物车
$temp["goods_number"] = $skuInfo["stock"] > $item['goods_number'] ? $item['goods_number'] : $skuInfo["stock"];
$temp["goods_number"] = $skuInfo["stock"] > $item['buy_number'] ? $item['buy_number'] : $skuInfo["stock"];
$temp["create_time"] = time();
$temp["update_time"] = time();
......@@ -74,4 +74,62 @@ class CartService
}
}
//刷新购物车并且返回列表
public static function cartUpdateGetLists($user_id){
//当前用户所有可用的购物车数据
$query = CartModel::where(['user_id'=>$user_id,"status"=>1])
->orderBy('cart_id', 'desc');
$result = $query->get()->toArray();
if (!$result){
return false;
}
try{
$con = DB::connection();
$con->beginTransaction();
$redis = Redis::connection();
$goodsInfoArr = ThirdService::getGoodsInfo(array_column($result,"goods_id"));
foreach ($result as $item) {
$goods_id = $item['goods_id'];
$temp = [
'user_id' => $user_id,
'goods_id' => $item['goods_id'],
];
$skuInfo = $goodsInfoArr[$goods_id]; //sku库存
$digikeyInfo = $redis->hget("sku_raw_map",$goods_id); //digikey 编码
if ($digikeyInfo){
$digikeyArr = json_decode($digikeyInfo,true);
$temp["raw_goods_sn"]= $digikeyArr["raw_goods_id"];
$temp["raw_goods_packing"]= $digikeyArr["pack"];
$temp["raw_brand_name"]= $digikeyArr["raw_brand_name"];
}
$temp["buy_number"] = $skuInfo["stock"] > $item['buy_number'] ? $item['buy_number'] : $skuInfo["stock"];
if ($temp["buy_number"] == 0){
$temp["status"] = CartModel::STATUS_NO ;
}
$temp["update_time"] = time();
$flag = CartModel::where(["cart_id"=>$item["cart_id"]])->update($temp);
if (!$flag){
return false;
}
}
$con->commit();
return true;
}catch (\Exception $e){
$con->rollback();
throw new InvalidRequestException($e->getMessage().$e->getLine());
}
}
}
......@@ -10,6 +10,8 @@ class CartModel extends Model
protected $table = 'cart';
public $timestamps = false;
const STATUS_NO = -1; //状态禁用
const STATUS_YES = 1; //状态启动
//查询购物车数据
public static function getCartInfo($user_id)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment