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.
208 lines
6.4 KiB
208 lines
6.4 KiB
<?php
|
|
|
|
namespace app\integral\model;
|
|
|
|
use think\facade\Db;
|
|
|
|
class IntegralCart extends Base
|
|
{
|
|
|
|
/**
|
|
* API-获取兑换车商品
|
|
* @param array $where 查询条件
|
|
* @param string $field
|
|
* @date 2022-11-17
|
|
*/
|
|
public function listCartProduct($where = [], $field = '*')
|
|
{
|
|
$order = 'id desc';
|
|
|
|
return $this->with(['integralProduct', 'integralProductSku'])->field($field)->where($where)->order($order)
|
|
->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_integral'] = $item['product_integral_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'];
|
|
$item['product_integral'] = $item['product_integral_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_integral_original']);
|
|
unset($item['product_cover_img_sku']);
|
|
unset($item['product_stock_sku']);
|
|
unset($item['product_price_sku']);
|
|
unset($item['product_integral_sku']);
|
|
|
|
// 释放关联表字段
|
|
unset($item['integralProduct']);
|
|
unset($item['integralProductSku']);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* API-获取用户兑换车商品
|
|
* @param int $user_id 用户ID
|
|
* @param string $field
|
|
* @date 2022-11-17
|
|
*/
|
|
public function listUserCart($user_id = '', $field = '')
|
|
{
|
|
// 默认为当前用户ID
|
|
if (empty($user_id)) {
|
|
$user_id = $this->userId;
|
|
}
|
|
|
|
$where = [
|
|
['user_id', '=', $user_id]
|
|
];
|
|
|
|
if (empty($field)) {
|
|
$field = 'id,product_id,is_product_spec_open,product_sku,number,is_select,is_effective';
|
|
}
|
|
|
|
return $this->listCartProduct($where, $field);
|
|
}
|
|
|
|
/**
|
|
* 更新兑换车
|
|
* @param array $data
|
|
* @date 2022-11-15
|
|
*/
|
|
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);
|
|
}
|
|
|
|
/**
|
|
* 更新购物车产品选中状态
|
|
* @param int $cart_id 购物车ID
|
|
* @param int $is_select 0--未选中 1--已选中
|
|
* @date 2022-11-15
|
|
*/
|
|
public function updateCartProductSelectedStatus($cart_id, $is_select)
|
|
{
|
|
$data = [
|
|
'id' => $cart_id,
|
|
'is_select' => $is_select
|
|
];
|
|
return $this->dataUpdate($data);
|
|
}
|
|
|
|
/**
|
|
* 更新购物车产品选中状态
|
|
* @param int $cart_id 购物车ID
|
|
* @param int $is_select 0--未选中 1--已选中
|
|
* @date 2022-11-15
|
|
*/
|
|
public function updateAllCartProductSelectedStatus($is_select)
|
|
{
|
|
$data = [
|
|
'is_select' => $is_select,
|
|
'update_time' => time()
|
|
];
|
|
return $this->where([
|
|
['user_id', '=', $this->userId]
|
|
])->update($data);
|
|
}
|
|
|
|
/**
|
|
* 清空兑换车已选择商品
|
|
* @date 2021-06-01
|
|
*/
|
|
public function deleteCartSelectedProduct($user_id)
|
|
{
|
|
$where = [
|
|
['user_id', '=', $user_id],
|
|
['is_select', '=', 1],
|
|
['is_effective', '=', 1],
|
|
];
|
|
|
|
return $this->dataDestory($where);
|
|
}
|
|
|
|
/**
|
|
* 清空兑换车失效商品
|
|
* @date 2022-11-15
|
|
*/
|
|
public function deleteInvalidCart($user_id)
|
|
{
|
|
$where = [
|
|
['user_id', '=', $user_id],
|
|
['is_effective', '=', 0],
|
|
];
|
|
|
|
return $this->dataDestory($where);
|
|
}
|
|
|
|
|
|
/**
|
|
* API-一对一关联商品表
|
|
* @date 2021-06-01
|
|
*/
|
|
public function integralProduct()
|
|
{
|
|
return $this->hasOne('IntegralProduct', 'id', 'product_id')->removeOption('soft_delete')
|
|
->bind([
|
|
// 产品类型 1--实物 2--虚拟
|
|
'product_type' => 'type',
|
|
// 产品名称
|
|
'product_name' => 'name',
|
|
// 产品价钱
|
|
'product_price_original' => 'price',
|
|
// 产品积分
|
|
'product_integral_original' => 'integral',
|
|
// 产品库存
|
|
'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'
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* API-一对一关联商品SKU
|
|
* @date 2021-06-01
|
|
*/
|
|
public function integralProductSku()
|
|
{
|
|
return $this->hasOne('IntegralProductSku', 'sku', 'product_sku')->removeOption('soft_delete')
|
|
->bind([
|
|
'product_price_sku' => 'price',
|
|
'product_integral_sku' => 'integral',
|
|
'product_stock_sku' => 'stock',
|
|
'product_cover_img_sku' => 'cover_img',
|
|
'product_sku_name' => 'sku_name'
|
|
]);
|
|
}
|
|
} |