You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
205 lines
6.7 KiB
205 lines
6.7 KiB
<?php
|
|
|
|
namespace app\mall\model;
|
|
|
|
use think\facade\Db;
|
|
|
|
class MallCart extends Base
|
|
{
|
|
|
|
/**
|
|
* 更新购物车产品选中状态
|
|
* @param int $cart_id 购物车ID
|
|
* @param int $is_select 0--未选中 1--已选中
|
|
* @date 2022-08-25
|
|
*/
|
|
public function updateCartProductSelectedStatus($cart_id, $is_select)
|
|
{
|
|
$data = [
|
|
'id' => $cart_id,
|
|
'is_select' => $is_select
|
|
];
|
|
return $this->dataUpdate($data);
|
|
}
|
|
|
|
/**
|
|
* 获取用户购物车商品
|
|
* @param int $user_id 用户ID
|
|
* @date 2022-08-25
|
|
*/
|
|
public function listUserCart($user_id = '')
|
|
{
|
|
// 默认为当前用户ID
|
|
if (empty($user_id)) {
|
|
$user_id = $this->userId;
|
|
}
|
|
|
|
$field = 'id,product_id,is_product_spec_open,product_sku,number,is_effective,is_select';
|
|
$where = [
|
|
['user_id', '=', $user_id]
|
|
];
|
|
$order = 'id desc';
|
|
//获取购物车商品
|
|
$data_list = $this->with(['mallProduct', 'mallProductSku'])->field($field)->where($where)->order($order)->select()->each(function ($item) {
|
|
if ($item['is_product_spec_open_original'] == 0) {
|
|
$item['product_cover_img'] = $item['product_cover_img_original'];
|
|
$item['product_stock'] = $item['product_stock_original'];
|
|
$item['product_price'] = $item['product_price_original'];
|
|
$item['product_sku_name'] = '';
|
|
} else if ($item['is_product_spec_open_original'] == 1) {
|
|
$item['product_cover_img'] = $item['product_cover_img_sku'];
|
|
$item['product_stock'] = $item['product_stock_sku'];
|
|
$item['product_price'] = $item['product_price_sku'];
|
|
}
|
|
unset($item['mallProduct']);
|
|
unset($item['mallProductSku']);
|
|
unset($item['is_product_spec_open_original']);
|
|
unset($item['product_cover_img_original']);
|
|
unset($item['product_stock_original']);
|
|
unset($item['product_price_original']);
|
|
unset($item['product_cover_img_sku']);
|
|
unset($item['product_stock_sku']);
|
|
unset($item['product_price_sku']);
|
|
// unset($item['product_is_publish']);
|
|
// unset($item['product_delete_time']);
|
|
return $item;
|
|
});
|
|
|
|
// $data_list = $this->getAllCart([
|
|
// ['user_id', '=', $user_id]
|
|
// ], 'id,product_id,is_product_spec_open,product_sku,number,is_effective,is_select');
|
|
|
|
return $data_list;
|
|
}
|
|
|
|
/**
|
|
* 获取全部购物车商品
|
|
* @param array $where 查询条件
|
|
* @param string $field 查询字段
|
|
* @date 2022-10-13
|
|
*/
|
|
public function getAllCart($where, $field = '*')
|
|
{
|
|
return $this->with(['mallProduct', 'mallProductSku'])->field($field)->where($where)->order('id desc')
|
|
->select()->each(function ($item, $key) {
|
|
if ($item['is_product_spec_open_original'] == 0) {
|
|
$item['product_cover_img'] = $item['product_cover_img_original'];
|
|
$item['product_stock'] = $item['product_stock_original'];
|
|
$item['product_price'] = $item['product_price_original'];
|
|
$item['product_sku_name'] = '';
|
|
} else if ($item['is_product_spec_open_original'] == 1) {
|
|
$item['product_cover_img'] = $item['product_cover_img_sku'];
|
|
$item['product_stock'] = $item['product_stock_sku'];
|
|
$item['product_price'] = $item['product_price_sku'];
|
|
}
|
|
// 释放原始判断用字段
|
|
unset($item['is_product_spec_open_original']);
|
|
unset($item['product_cover_img_original']);
|
|
unset($item['product_stock_original']);
|
|
unset($item['product_price_original']);
|
|
unset($item['product_cover_img_sku']);
|
|
unset($item['product_stock_sku']);
|
|
unset($item['product_price_sku']);
|
|
// 释放关联表字段
|
|
unset($item['mallProduct']);
|
|
unset($item['mallProductSku']);
|
|
// return $item;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 清空购物车已选择商品
|
|
* @param int $user_id 用户ID
|
|
* @date 2022-10-21
|
|
*/
|
|
public function deleteChooseCart($user_id)
|
|
{
|
|
$where = [
|
|
['user_id', '=', $user_id],
|
|
['is_select', '=', 1],
|
|
['is_effective', '=', 1],
|
|
];
|
|
|
|
return $this->dataDestory($where);
|
|
}
|
|
|
|
/**
|
|
* 更新购物车
|
|
* @date 2021-03-01
|
|
*/
|
|
public function updateCart($data)
|
|
{
|
|
$where = [
|
|
['user_id', '=', $data['user_id']],
|
|
['product_id', '=', $data['product_id']],
|
|
['product_sku', '=', $data['product_sku']],
|
|
];
|
|
$id = $this->getOneData($where, 'id');
|
|
if (!empty($id)) {
|
|
$data['id'] = $id;
|
|
$data['number'] = Db::raw('number+' . $data['number']);
|
|
}
|
|
|
|
return $this->dataUpdate($data);
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* 清空购物车失效商品
|
|
* @date 2021-03-01
|
|
*/
|
|
public function deleteInvalidCart()
|
|
{
|
|
$where = [
|
|
['user_id', '=', $this->userId],
|
|
['is_effective', '=', 0],
|
|
];
|
|
return $this->dataDestory($where);
|
|
}
|
|
|
|
|
|
/**
|
|
* 一对一关联商品表
|
|
* @date 2022-10-13
|
|
*/
|
|
public function mallProduct()
|
|
{
|
|
return $this->hasOne('MallProduct', 'id', 'product_id')->removeOption('soft_delete')
|
|
->bind([
|
|
// 产品名称
|
|
'product_name' => 'name',
|
|
// 产品价钱
|
|
'product_price_original' => 'price',
|
|
// 产品库存
|
|
'product_stock_original' => 'stock',
|
|
// 产品封面图
|
|
'product_cover_img_original' => 'cover_img',
|
|
// 是否开启规格
|
|
'is_product_spec_open_original' => 'is_spec_open',
|
|
'freight_type',
|
|
'freight_id',
|
|
'freight_money',
|
|
'weight',
|
|
// 判断商品是否失效时用
|
|
'product_is_publish' => 'is_publish',
|
|
// 判断商品是否失效时用
|
|
'product_delete_time' => 'delete_time'
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 一对一关联商品SKU
|
|
* @date 2022-10-13
|
|
*/
|
|
public function mallProductSku()
|
|
{
|
|
return $this->hasOne('MallProductSku', 'sku', 'product_sku')->removeOption('soft_delete')
|
|
->bind([
|
|
'product_price_sku' => 'price',
|
|
'product_stock_sku' => 'stock',
|
|
'product_cover_img_sku' => 'cover_img',
|
|
'product_sku_name' => 'sku_name'
|
|
]);
|
|
}
|
|
} |