mkm 40ec3e5ee0 feat: 修改了订单相关的API,优化了支付逻辑;
fix: 修复了用户地址、商品库存等错误;
refactor: 重构了登录逻辑,提高了代码可读性;
style: 调整了代码格式,使其更加规范;
test: 增加了订单支付的测试用例;
docs: 更新了相关文档;
build: 更新了依赖;
ops: 优化了服务器性能;
chore: 更新了.gitignore文件;
2024-08-27 11:56:48 +08:00

127 lines
3.4 KiB
PHP

<?php
namespace app\api\logic\order;
use app\common\model\order\Cart;
use app\common\logic\BaseLogic;
use app\common\model\store_product_log\StoreProductLog;
use support\exception\BusinessException;
use think\facade\Db;
/**
* 购物车表逻辑
* Class CartLogic
* @package app\admin\logic\order
*/
class CartLogic extends BaseLogic
{
/**
* @notes 添加购物车表
* @param array $params
* @return bool
* @author likeadmin
* @date 2024/04/24 10:37
*/
public static function add(array $params)
{
if ($params['store_id'] <= 0) {
throw new BusinessException('门店ID不能为空');
}
Db::startTrans();
try {
//check
$check = Cart::where([
'uid' => $params['uid'],
'store_id' => $params['store_id'],
'product_id' => $params['product_id'],
'is_pay' => 0
])->field('id')->find();
if ($check) {
Cart::where('id', $check['id'])->inc('cart_num', $params['cart_num'])
->update();
$cart['id'] = $check['id'];
} else {
$cart = Cart::create([
'uid' => $params['uid'],
'type' => $params['type'] ?? '',
'product_id' => $params['product_id'],
'store_id' => $params['store_id'] ?? 0,
'staff_id' => $params['staff_id'] ?? 0,
'cart_num' => $params['cart_num'],
'is_new' => $params['is_new'] ?? 0,
]);
}
StoreProductLog::create([
'type' => 'cart',
'uid' => $params['uid'],
'cart_id' => $cart['id'],
'store_id' => $params['store_id'] ?? 0,
'visit_num' => 1,
'product_id' => $params['product_id'],
'cart_num' => $params['cart_num'],
]);
Db::commit();
return $cart;
} catch (\Throwable $e) {
Db::rollback();
throw new BusinessException($e->getMessage());
}
}
/**
* @notes 编辑购物车表
* @param array $params
* @return bool
* @author likeadmin
* @date 2024/04/24 10:37
*/
public static function edit(array $params, $type = 'inc'): bool
{
Db::startTrans();
try {
Cart::where([
'uid' => $params['uid'],
'store_id' => $params['store_id'],
'product_id' => $params['product_id']
])
->update(['cart_num' => $params['cart_num']]);
Db::commit();
return true;
} catch (\Throwable $e) {
Db::rollback();
throw new BusinessException($e->getMessage());
}
}
/**
* @notes 删除购物车表
* @param array $params
* @return bool
* @author likeadmin
* @date 2024/04/24 10:37
*/
public static function delete(array $params): bool
{
return Cart::destroy($params['id']);
}
/**
* @notes 获取购物车表详情
* @param $params
* @return array
* @author likeadmin
* @date 2024/04/24 10:37
*/
public static function detail($params): array
{
return Cart::findOrEmpty($params['id'])->toArray();
}
}