multi-store/app/api/logic/order/CartLogic.php
2024-06-20 16:35:33 +08:00

114 lines
2.8 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 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): bool
{
if($params['store_id']<=0){
self::setError('门店ID不能为空');
return false;
}
Db::startTrans();
try {
$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,
'product_attr_unique' => '',
'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 true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @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 (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @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();
}
}