multi-store/app/api/logic/order/CartLogic.php

127 lines
3.4 KiB
PHP
Raw Normal View History

2024-06-01 17:22:46 +08:00
<?php
namespace app\api\logic\order;
use app\common\model\order\Cart;
use app\common\logic\BaseLogic;
2024-06-20 16:35:33 +08:00
use app\common\model\store_product_log\StoreProductLog;
use support\exception\BusinessException;
2024-06-01 17:22:46 +08:00
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
*/
2024-08-10 17:50:14 +08:00
public static function add(array $params)
2024-06-01 17:22:46 +08:00
{
2024-08-10 17:50:14 +08:00
if ($params['store_id'] <= 0) {
throw new BusinessException('门店ID不能为空');
}
2024-06-01 17:22:46 +08:00
Db::startTrans();
try {
2024-06-29 15:06:41 +08:00
//check
$check = Cart::where([
2024-06-01 17:22:46 +08:00
'uid' => $params['uid'],
2024-06-29 15:06:41 +08:00
'store_id' => $params['store_id'],
2024-06-01 17:22:46 +08:00
'product_id' => $params['product_id'],
2024-08-10 17:50:14 +08:00
'is_pay' => 0
2024-06-29 15:06:41 +08:00
])->field('id')->find();
2024-08-10 17:50:14 +08:00
if ($check) {
Cart::where('id', $check['id'])->inc('cart_num', $params['cart_num'])
2024-06-29 15:06:41 +08:00
->update();
$cart['id'] = $check['id'];
2024-08-10 17:50:14 +08:00
} else {
2024-06-29 15:06:41 +08:00
$cart = Cart::create([
'uid' => $params['uid'],
2024-08-10 17:50:14 +08:00
'type' => $params['type'] ?? '',
2024-06-29 15:06:41 +08:00
'product_id' => $params['product_id'],
2024-08-10 17:50:14 +08:00
'store_id' => $params['store_id'] ?? 0,
'staff_id' => $params['staff_id'] ?? 0,
2024-06-29 15:06:41 +08:00
'cart_num' => $params['cart_num'],
2024-08-10 17:50:14 +08:00
'is_new' => $params['is_new'] ?? 0,
2024-06-29 15:06:41 +08:00
]);
}
2024-06-20 16:35:33 +08:00
StoreProductLog::create([
2024-08-10 17:50:14 +08:00
'type' => 'cart',
2024-06-20 16:35:33 +08:00
'uid' => $params['uid'],
'cart_id' => $cart['id'],
2024-08-10 17:50:14 +08:00
'store_id' => $params['store_id'] ?? 0,
2024-06-20 16:35:33 +08:00
'visit_num' => 1,
'product_id' => $params['product_id'],
'cart_num' => $params['cart_num'],
]);
2024-06-01 17:22:46 +08:00
Db::commit();
2024-08-10 17:50:14 +08:00
return $cart;
} catch (\Throwable $e) {
2024-06-01 17:22:46 +08:00
Db::rollback();
throw new BusinessException($e->getMessage());
2024-06-01 17:22:46 +08:00
}
}
/**
* @notes 编辑购物车表
* @param array $params
* @return bool
* @author likeadmin
* @date 2024/04/24 10:37
*/
2024-08-10 17:50:14 +08:00
public static function edit(array $params, $type = 'inc'): bool
2024-06-01 17:22:46 +08:00
{
Db::startTrans();
try {
2024-08-10 17:50:14 +08:00
Cart::where([
'uid' => $params['uid'],
'store_id' => $params['store_id'],
'product_id' => $params['product_id']
])
->update(['cart_num' => $params['cart_num']]);
2024-06-01 17:22:46 +08:00
Db::commit();
return true;
} catch (\Throwable $e) {
2024-06-01 17:22:46 +08:00
Db::rollback();
throw new BusinessException($e->getMessage());
2024-06-01 17:22:46 +08:00
}
}
/**
* @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();
}
2024-08-10 17:50:14 +08:00
}