|
|
<?php
|
|
|
|
|
|
namespace app\integral\logic;
|
|
|
|
|
|
|
|
|
use app\integral\model\IntegralCart;
|
|
|
use app\integral\model\IntegralProduct;
|
|
|
|
|
|
class Cart extends Base
|
|
|
{
|
|
|
/**
|
|
|
* 商品详情页新加或更新兑换车
|
|
|
* @param int $product_id 产品ID
|
|
|
* @param int $product_sku 产品SKU
|
|
|
* @param int $number 变动数量
|
|
|
* @param int $is_select 是否选中 0--未选中 1--已选中
|
|
|
* @date 2022-11-15
|
|
|
*/
|
|
|
public function updateCartProduct($product_id, $product_sku, $number, $is_select = 0)
|
|
|
{
|
|
|
$cart_model = new IntegralCart();
|
|
|
$product_model = new IntegralProduct();
|
|
|
$product_service = new \app\integral\service\Product();
|
|
|
|
|
|
// 获取商品详情
|
|
|
$product = $product_model->getOneData([
|
|
|
['id', '=', $product_id]
|
|
|
], 'id,is_spec_open');
|
|
|
|
|
|
// 兑换车产品数量
|
|
|
$cart_number = $cart_model->getOneData([
|
|
|
['user_id', '=', $this->userId],
|
|
|
['product_id', '=', $product_id],
|
|
|
['product_sku', '=', $product_sku],
|
|
|
], 'number');
|
|
|
// 兑换车中没有该产品时数量设置为0
|
|
|
$cart_number = $cart_number ?? 0;
|
|
|
// 产品的总购买数量为本身的数量+新增数量
|
|
|
$buy_number = $cart_number + $number;
|
|
|
|
|
|
// 判断库存是否足够
|
|
|
$res = $product_service->isStockEnough($product_id, $product_sku, $buy_number);
|
|
|
if ($res['code'] != 0) {
|
|
|
return $res;
|
|
|
}
|
|
|
|
|
|
//加入兑换车
|
|
|
$data = [
|
|
|
'uid' => $this->mid,
|
|
|
'user_agent' => $this->userAgent,
|
|
|
'user_id' => $this->userId,
|
|
|
'product_id' => $product_id,
|
|
|
'is_product_spec_open' => $product['is_spec_open'],
|
|
|
'product_sku' => $product_sku,
|
|
|
'number' => $number,
|
|
|
'is_select' => $is_select
|
|
|
];
|
|
|
$res = $cart_model->updateCart($data);
|
|
|
if (!$res) {
|
|
|
return sendErrorArray(3001, '更新兑换车失败');
|
|
|
}
|
|
|
|
|
|
// 获取兑换车种类数量
|
|
|
$cart_type_number = $cart_model->getOneData([
|
|
|
['user_id', '=', $this->userId]
|
|
|
], 'count(id)');
|
|
|
|
|
|
return sendSuccessArray([
|
|
|
// 购物车种类数量
|
|
|
'cart_type_number' => $cart_type_number
|
|
|
], '加入兑换车成功');
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 更新购物车产品数量
|
|
|
* @param int $cart_id 购物车ID
|
|
|
* @param int $type 类型 1--增加 2--减少 3--直接输入数量
|
|
|
* @param int $number type=3时,数量
|
|
|
* @date 2022-11-15
|
|
|
*/
|
|
|
public function updateCartProductNumber($cart_id, $type, $number)
|
|
|
{
|
|
|
|
|
|
$cart_model = new IntegralCart();
|
|
|
$product_service = new \app\integral\service\Product();
|
|
|
$cart_service = new \app\integral\service\Cart();
|
|
|
|
|
|
// 获取兑换车详情
|
|
|
$cart = $cart_model->getOneData([
|
|
|
['id', '=', $cart_id]
|
|
|
]);
|
|
|
|
|
|
// 判断库存是否足够
|
|
|
$buy_number = '';
|
|
|
if ($type == 1) {
|
|
|
$buy_number = $cart['number'] + 1;
|
|
|
} else if ($type == 2) {
|
|
|
$buy_number = $cart['number'] - 1;
|
|
|
} else if ($type == 3) {
|
|
|
$buy_number = $number;
|
|
|
}
|
|
|
$res = $product_service->isStockEnough($cart['product_id'], $cart['product_sku'], $buy_number);
|
|
|
if ($res['code'] != 0) {
|
|
|
return $res;
|
|
|
}
|
|
|
|
|
|
// 更新兑换车数量
|
|
|
$data = [
|
|
|
'id' => $cart_id,
|
|
|
'number' => $buy_number
|
|
|
];
|
|
|
$res = $cart_model->dataUpdate($data);
|
|
|
if (!$res) {
|
|
|
return sendErrorArray(3001, '更新兑换车失败');
|
|
|
}
|
|
|
|
|
|
// 获取兑换车商品
|
|
|
list($effective_product_list, $invalid_product_list) = $cart_service->listUserCartEffectiveAndInvalid();
|
|
|
|
|
|
return sendSuccessArray([
|
|
|
// 有效产品列表
|
|
|
'effective_product_list' => $effective_product_list,
|
|
|
]);
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 更新兑换车产品选中状态
|
|
|
* @param int $cart_id 购物车ID
|
|
|
* @param int $is_select 0--未选中 1--已选中
|
|
|
* @date 2022-11-15
|
|
|
*/
|
|
|
public function updateCartProductSelectedStatus($cart_id, $is_select)
|
|
|
{
|
|
|
$cart_model = new IntegralCart();
|
|
|
$cart_service = new \app\integral\service\Cart();
|
|
|
|
|
|
// 更新购物车产品选中状态
|
|
|
$res = $cart_model->updateCartProductSelectedStatus($cart_id, $is_select);
|
|
|
if (!$res) {
|
|
|
return sendErrorArray(3001, '更新购物车失败');
|
|
|
}
|
|
|
|
|
|
// 获取兑换车商品
|
|
|
list($effective_product_list, $invalid_product_list) = $cart_service->listUserCartEffectiveAndInvalid();
|
|
|
|
|
|
return sendSuccessArray([
|
|
|
// 有效产品列表
|
|
|
'effective_product_list' => $effective_product_list,
|
|
|
]);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 更新兑换车产品选中状态
|
|
|
* @param int $is_select 0--未选中 1--已选中
|
|
|
* @date 2022-11-15
|
|
|
*/
|
|
|
public function updateAllCartProductSelectedStatus($is_select)
|
|
|
{
|
|
|
$cart_model = new IntegralCart();
|
|
|
$cart_service = new \app\integral\service\Cart();
|
|
|
|
|
|
$number = $cart_model->getNumber([
|
|
|
['user_id', '=', $this->userId]
|
|
|
]);
|
|
|
// 购物车有数据则更新
|
|
|
if (!empty($number)) {
|
|
|
$res = $cart_model->updateAllCartProductSelectedStatus($is_select);
|
|
|
|
|
|
if (!$res) {
|
|
|
return sendErrorArray(3001, '更新兑换车失败');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 获取兑换车商品
|
|
|
list($effective_product_list, $invalid_product_list) = $cart_service->listUserCartEffectiveAndInvalid();
|
|
|
|
|
|
return sendSuccessArray([
|
|
|
// 有效产品列表
|
|
|
'effective_product_list' => $effective_product_list,
|
|
|
]);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 清空失效宝贝
|
|
|
* @date 2022-11-15
|
|
|
*/
|
|
|
public function deleteCartInvalidProduct()
|
|
|
{
|
|
|
$cart_model = new IntegralCart();
|
|
|
|
|
|
$res = $cart_model->deleteInvalidCart($this->userId);
|
|
|
if (!$res) {
|
|
|
return sendErrorArray(3001, '清空失效宝贝失败');
|
|
|
}
|
|
|
|
|
|
return sendSuccessArray([], '清空失效宝贝成功');
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 删除选中商品
|
|
|
* @date 2022-11-15
|
|
|
*/
|
|
|
public function deleteCartSelectedProduct()
|
|
|
{
|
|
|
$cart_model = new IntegralCart();
|
|
|
$cart_service = new \app\integral\service\Cart();
|
|
|
|
|
|
$number = $cart_model->getNumber([
|
|
|
['user_id', '=', $this->userId],
|
|
|
['is_select', '=', 1]
|
|
|
]);
|
|
|
if(empty($number)){
|
|
|
return sendErrorArray(3001, '您必须至少选中1个商品');
|
|
|
}
|
|
|
|
|
|
$res = $cart_model->deleteCartSelectedProduct($this->userId);
|
|
|
if (!$res) {
|
|
|
return sendErrorArray(3002, '删除选中商品失败');
|
|
|
}
|
|
|
|
|
|
// 获取兑换车商品
|
|
|
list($effective_product_list, $invalid_product_list) = $cart_service->listUserCartEffectiveAndInvalid();
|
|
|
|
|
|
return sendSuccessArray([
|
|
|
// 有效产品列表
|
|
|
'effective_product_list' => $effective_product_list,
|
|
|
]);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取兑换车产品种类数量
|
|
|
* @date 2022-11-14
|
|
|
*/
|
|
|
public function listCartProduct()
|
|
|
{
|
|
|
$cart_service = new \app\integral\service\Cart();
|
|
|
|
|
|
// 判断兑换车失效商品并修改状态
|
|
|
$cart_service->updateInvalid($this->userId);
|
|
|
|
|
|
// 获取兑换车商品
|
|
|
list($effective_product_list, $invalid_product_list) = $cart_service->listUserCartEffectiveAndInvalid();
|
|
|
|
|
|
return sendSuccessArray([
|
|
|
// 有效产品列表
|
|
|
'effective_product_list' => $effective_product_list,
|
|
|
// 无效产品列表
|
|
|
'invalid_product_list' => $invalid_product_list
|
|
|
]);
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 获取兑换车产品种类数量
|
|
|
* @date 2022-11-14
|
|
|
*/
|
|
|
public function countCartTypeNumber()
|
|
|
{
|
|
|
$cart_model = new IntegralCart();
|
|
|
|
|
|
// 获取兑换车种类数量
|
|
|
$cart_type_number = $cart_model->getOneData([
|
|
|
['user_id', '=', $this->userId]
|
|
|
], 'count(id)');
|
|
|
|
|
|
return sendSuccessArray([
|
|
|
// 购物车种类数量
|
|
|
'cart_type_number' => $cart_type_number
|
|
|
]);
|
|
|
}
|
|
|
|
|
|
}
|