multi-store/app/store/controller/cart/CartController.php

120 lines
4.1 KiB
PHP
Raw Normal View History

<?php
namespace app\store\controller\cart;
use app\api\logic\order\CartLogic;
use app\api\validate\CartValidate;
2024-06-11 16:55:27 +08:00
use app\common\model\store_branch_product\StoreBranchProduct;
use app\store\lists\cart\CartList;
use app\common\model\order\Cart;
use app\common\model\store_product\StoreProduct;
use app\common\model\store_product_unit\StoreProductUnit;
use app\store\controller\BaseAdminController;
2024-06-14 15:53:47 +08:00
class CartController extends BaseAdminController
{
public function list()
{
return $this->dataLists(new CartList());
}
/**
* @notes 添加购物车
*/
public function create()
{
$params = (new CartValidate())->post()->goCheck('StoreAdd');
$adminInfo = $this->adminInfo;
$params['uid'] = $this->request->post('uid') ?? 0;
$params['staff_id'] = $adminInfo['admin_id'];
$params['store_id'] = $adminInfo['store_id'];
$result = Cart::where(['uid' => 0, 'staff_id' => $adminInfo['admin_id'], 'store_id' => $adminInfo['store_id'], 'product_id' => $params['product_id'], 'is_fail' => 0, 'is_pay' => 0])->find();
2024-06-20 18:02:20 +08:00
//判断起批发价
$branchProduct = StoreProduct::where(
[
'id' => $params['product_id'],
2024-06-20 18:02:20 +08:00
]
)->find();
if ($params['cart_num'] < $branchProduct['batch']) {
return $this->fail('起批发量低于最低值' . $branchProduct['batch']);
2024-06-20 18:02:20 +08:00
}
$count = Cart::where(['uid' => $params['uid'], 'is_pay' => 0])->count();
if ($count > 100) {
return $this->fail('购物车商品不能大于100个请先结算');
}
if (convertNumber($params['cart_num']) === false) {
$is_bulk = StoreProductUnit::where('id', $branchProduct['unit'])->value('is_bulk');
if ($is_bulk == 0) {
return $this->fail('非计量商品,不能有小数,请编辑购物车');
}
}
2024-06-11 16:55:27 +08:00
//数量下单判断
// $stock = StoreBranchProduct::where(
// ['product_id'=>$params['product_id'],
// 'store_id'=>$params['store_id']
// ])->value('stock')??0;
// if ($params['cart_num'] >$stock) {
// return $this->fail('库存数量不足');
// }
if ($result) {
$res = CartLogic::edit($params);
} else {
$res = CartLogic::add($params);
}
if ($res) {
return $this->success('添加成功');
} else {
return $this->fail(CartLogic::getError());
}
}
/**
* @notes 修改购物车
*/
public function change()
{
$params = (new CartValidate())->post()->goCheck('StoreChange');
$adminInfo = $this->adminInfo;
$params['cart_num']=bcadd($params['cart_num'],0,2);
if (convertNumber($params['cart_num']) === false) {
$branchProduct = StoreProduct::where(
[
'id' => $params['product_id'],
]
)->find();
$is_bulk = StoreProductUnit::where('id', $branchProduct['unit'])->value('is_bulk');
if ($is_bulk == 0) {
return $this->fail('非计量商品,不能有小数,请编辑购物车');
}
}
$params['uid'] = 0;
$params['staff_id'] = $adminInfo['admin_id'];
$params['store_id'] = $adminInfo['store_id'];
$res = CartLogic::edit($params, 'dec');
if ($res) {
return $this->success('修改成功');
} else {
return $this->fail(CartLogic::getError());
}
}
/**
* @notes 删除购物车
*/
public function delete()
{
$params = (new CartValidate())->post()->goCheck('delete');
$adminInfo = $this->adminInfo;
$params['uid'] = 0;
$params['staff_id'] = $adminInfo['admin_id'];
$params['store_id'] = $adminInfo['store_id'];
$res = CartLogic::delete($params);
if ($res) {
return $this->success('删除成功');
} else {
return $this->fail(CartLogic::getError());
}
}
}