147 lines
5.8 KiB
PHP
147 lines
5.8 KiB
PHP
<?php
|
|
|
|
namespace app\api\logic;
|
|
|
|
use app\common\basics\Logic;
|
|
use app\common\model\localstore\LocalStoreGoods;
|
|
use app\common\model\localstore\LocalStoreGoodsCart;
|
|
use app\common\model\localstore\LocalStoreGoodsItem;
|
|
use think\Exception;
|
|
use think\facade\Db;
|
|
|
|
class LocalStoreGoodsCartLogic extends Logic
|
|
{
|
|
public static function addCart($post, $user_id)
|
|
{
|
|
Db::startTrans();
|
|
try {
|
|
// 验证商品
|
|
$goods = LocalStoreGoods::where(['id' => $post['lsg_id'], 'state' => 1, 'audit_status' => 1, 'delete_time' => 0])->find();
|
|
if (empty($goods)) {
|
|
throw new \Exception('商品信息错误');
|
|
}
|
|
|
|
$cart = LocalStoreGoodsCart::where(['user_id' => $user_id, 'lstore_id' => $post['lstore_id'], 'lsg_id' => $post['lsg_id'], 'item_id' => $post['item_id'], 'is_delete' => 0])->find();
|
|
// 验证商品库存
|
|
$item = LocalStoreGoodsItem::where(['lstore_id' => $post['lstore_id'], 'goods_id' => $post['lsg_id']])->field('stock')->find();
|
|
|
|
// 普通商品
|
|
if ($post['item_id'] == 0 && $post['goods_num'] > 999) {
|
|
throw new \Exception('商品库存不足');
|
|
}
|
|
|
|
if ($post['item_id'] > 0 && ($post['goods_num'] + $cart['goods_num']) > $item['stock']) {
|
|
throw new \Exception('商品库存不足');
|
|
}
|
|
|
|
if ($cart) {
|
|
// 如果存在于购物车,就增加数量
|
|
LocalStoreGoodsCart::where(['user_id' => $user_id, 'lstore_id' => $post['lstore_id'], 'lsg_id' => $post['lsg_id'], 'item_id' => $post['item_id'], 'is_delete' => 0])
|
|
->update([
|
|
'goods_num' => $post['goods_num'] + $cart['goods_num'],
|
|
]);
|
|
} else {
|
|
// 购物车创建商品
|
|
LocalStoreGoodsCart::create([
|
|
'user_id' => $user_id,
|
|
'lstore_id' => $post['lstore_id'],
|
|
'lsg_id' => $post['lsg_id'],
|
|
'item_id' => $post['item_id'],
|
|
'goods_num' => $post['goods_num'],
|
|
'create_time' => time()
|
|
]);
|
|
}
|
|
Db::commit();
|
|
return true;
|
|
} catch (Exception $e) {
|
|
Db::rollback();
|
|
self::$error = $e->getMessage();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// 获取对应的购物车商品
|
|
public static function getCartGoods($post, $user_id)
|
|
{
|
|
$data = [];
|
|
$cart = LocalStoreGoodsCart::alias('c')
|
|
->join('local_store_goods g', 'c.lsg_id = g.id')
|
|
->where(['c.user_id' => $user_id, 'c.lstore_id' => $post['lstore_id'], 'c.is_delete' => 0, 'g.state' => 1, 'g.audit_status' => 1, 'g.delete_time' => 0])
|
|
->field('c.id, c.item_id, c.goods_num, g.id as lsg_id, g.title, g.price')
|
|
->append(['pic', 'item'])
|
|
->select()
|
|
->toArray();
|
|
|
|
$price = 0;
|
|
foreach ($cart as &$v) {
|
|
$price += ($v['goods_num'] * $v['price']);
|
|
}
|
|
|
|
$data['total_amount'] = number_format($price, 2, '.', '');
|
|
$data['list'] = $cart;
|
|
|
|
return $data;
|
|
}
|
|
|
|
public static function cartNum($post, $user_id)
|
|
{
|
|
$num = LocalStoreGoodsCart::alias('c')
|
|
->join('local_store_goods g', 'c.lsg_id = g.id')
|
|
->where(['c.user_id' => $user_id, 'c.lstore_id' => $post['lstore_id'], 'c.is_delete' => 0, 'g.state' => 1, 'g.audit_status' => 1, 'g.delete_time' => 0])
|
|
->sum('c.goods_num');
|
|
return ['num' => $num ?? 0];
|
|
}
|
|
|
|
public static function modifyCart($post, $user_id)
|
|
{
|
|
Db::startTrans();
|
|
try {
|
|
if ($post['cart_id']) {
|
|
$cart = LocalStoreGoodsCart::getSelfCart($user_id, $post['cart_id']);
|
|
if (!empty($cart)) {
|
|
if ($cart['item_id'] == 0 && $post['goods_num'] > 999) {
|
|
// 单规格商品
|
|
throw new \Exception('商品数量超出限制');
|
|
} else if ($cart['item_id'] > 0) {
|
|
// 多规格商品
|
|
$item = LocalStoreGoodsItem::where('id', $cart['item_id'])->field('price, stock')->find();
|
|
if ($post['goods_num'] > $item['stock']) {
|
|
throw new \Exception('商品数量超出限制');
|
|
}
|
|
}
|
|
|
|
LocalStoreGoodsCart::where('id', $post['cart_id'])->update([
|
|
'goods_num' => $post['goods_num']
|
|
]);
|
|
|
|
$result = LocalStoreGoodsCart::alias('c')
|
|
->join('local_store_goods g', 'c.lsg_id = g.id')
|
|
->where(['c.user_id' => $user_id, 'c.lstore_id' => $cart['lstore_id'], 'c.is_delete' => 0, 'g.state' => 1, 'g.audit_status' => 1, 'g.delete_time' => 0])
|
|
->field('c.id, c.item_id, c.goods_num, g.id as lsg_id, g.title, g.price')
|
|
->append(['pic', 'item'])
|
|
->select()
|
|
->toArray();
|
|
|
|
$price = 0;
|
|
foreach ($result as &$v) {
|
|
$price += ($v['goods_num'] * $v['price']);
|
|
}
|
|
|
|
$price = number_format($price, 2, '.', '');
|
|
} else {
|
|
throw new \Exception('商品信息不存在');
|
|
}
|
|
} else {
|
|
throw new \Exception('ID不存在');
|
|
}
|
|
Db::commit();
|
|
return [
|
|
'total_amount' => $price,
|
|
];
|
|
} catch (Exception $e) {
|
|
Db::rollback();
|
|
self::$error = $e->getMessage();
|
|
return false;
|
|
}
|
|
}
|
|
} |