dlh_test_server/app/api/logic/CartLogic.php

367 lines
14 KiB
PHP
Raw Normal View History

2024-08-03 10:04:14 +08:00
<?php
// +----------------------------------------------------------------------
// | likeshop开源商城系统
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | gitee下载:https://gitee.com/likeshop_gitee
// | github下载:https://github.com/likeshop-github
// | 访问官网:https://www.likeshop.cn
// | 访问社区:https://home.likeshop.cn
// | 访问手册:http://doc.likeshop.cn
// | 微信公众号:likeshop技术社区
// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
// | likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
// | 禁止对系统程序代码以任何目的,任何形式的再发布
// | likeshop团队版权所有并拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeshop.cn.team
// +----------------------------------------------------------------------
namespace app\api\logic;
use app\common\{
enum\GoodsEnum,
model\Cart,
basics\Logic,
model\goods\Goods,
enum\FootprintEnum
};
/**
* 购物车逻辑层
* Class CartLogic
* @package app\api\logic
*/
class CartLogic extends Logic
{
/**
* @notes 购物车列表
* @param $user_id
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author cjhao
* @date 2021/9/7 10:23
*/
public static function lists($user_id)
{
$carts = Cart::with(['goods', 'goods_item', 'shop'])
->where('user_id', $user_id)
->order('id desc')
->select()->toArray();
$goods_num = 0;
$total = 0;
$lists = [];
$shop_ids = array_unique(array_column($carts, 'shop_id'));
foreach ($shop_ids as $shop_id) {
$lists[$shop_id]['is_selected'] = 1;
$shop_info = $cart_lists = [];
foreach ($carts as $k => $cart) {
if ($shop_id != $cart['shop_id']) {
continue;
}
if (empty($shop_info)) {
$shop_info = [
'shop_id' => $cart['shop']['id'],
'shop_name' => $cart['shop']['name'],
'type' => $cart['shop']['type'],
'is_pay' => $cart['shop']['is_pay'],
];
}
$goods_total_amounts = 0;
$cart_msg = Cart::where('id', $cart['id'])->find();
$sub_price = 0; // 已选中 && 上架 && 未删除 && 规格信息不为空 && 商家支付功能开启
if ($cart['selected'] == 1 && $cart['goods']['status'] == 1 && $cart['goods']['del'] == 0
&& !empty($cart['goods_item']) && $cart['shop']['is_pay'])
{
$goods_num += $cart['goods_num'];
if($cart_msg['p_id']!=0){
if($cart_msg['w_discount'] > 0){
$goods_total_amounts = round($cart_msg['w_price'] * $cart_msg['basic_num']*$cart_msg['w_discount']/100 + $cart_msg['w_price']*$cart_msg['exc_num'],2);
$total += round($cart_msg['w_price'] * $cart_msg['basic_num']*$cart_msg['w_discount']/100 + $cart_msg['w_price']*$cart_msg['exc_num'],2);
}else{
$goods_total_amounts = round($cart_msg['w_price'] * $cart['goods_num'],2);
$total += round($cart_msg['w_price'] * $cart['goods_num'],2);
}
}elseif($cart['price_type'] == "jicai_wholesale"){
// var_dump(111);
$goods_total_amounts = round($cart['goods_item']['wholesale_pce'] * $cart['goods_num'],2);
$total += $cart['goods_item']['wholesale_pce'] * $cart['goods_num'];
}else{
// var_dump(222);
$goods_total_amounts = round($cart['goods_item']['price'] * $cart['goods_num'],2);
$total += $cart['goods_item']['price'] * $cart['goods_num'];
}
$sub_price = round($cart['goods_item']['price'] * $cart['goods_num'], 2);
} else {
2024-12-07 22:04:28 +08:00
$goods_total_amounts = round($cart['goods_item']['price'] * $cart['goods_num'],2);
2024-08-03 10:04:14 +08:00
// var_dump(333);
$cart['selected'] = 0;
}
// var_dump($goods_total_amounts);
// 设置商家选中状态; 满足条件(未选中的商品,上架,未删除,有规格信息,商家支付功能开启) 才影响商家选中状态
if(!$cart['selected']) {
if ($cart['goods']['status'] == 1 && $cart['goods']['del'] == 0
&& !empty($cart['goods_item']) && $cart['shop']['is_pay']) {
$lists[$shop_id]['is_selected'] = 0;
}
}
if($cart_msg['p_id']!=0){
$cart['goods_item']['price'] = $cart_msg['w_price'];
}
if($cart['price_type'] == "jicai_wholesale"){
$cart['goods_item']['price'] = $cart['goods_item']['wholesale_pce'];
}
$cart_lists[] = [
'cart_id' => $cart['id'],
'goods_id' => $cart['goods_id'],
'goods_name' => $cart['goods']['name'],
'image' => empty($cart['goods_item']['image']) ? $cart['goods']['image'] : $cart['goods_item']['image'],
'goods_num' => $cart['goods_num'],
'goods_status' => $cart['goods']['status'],
'goods_del' => $cart['goods']['del'],
'spec_value_str' => $cart['goods_item']['spec_value_str'] ?? '请重新选择规格',
'price' => $cart['goods_item']['price'],
'stock' => $cart['goods_item']['stock'],
'selected' => intval($cart['selected']),
'item_id' => $cart['item_id'],
'sub_price' => $sub_price,
'is_pay' => $cart['shop']['is_pay'],
'has_item' => empty($cart['goods_item']) ? 0 : 1, //是否有规格信息
'p_id' => $cart_msg['p_id'],
'w_price' => $cart_msg['w_price'],
'firend_invite_code' => $cart['firend_invite_code'],
'price_type' => $cart['price_type'],
'w_discount' => $cart['w_discount'],
'exc_num' => $cart['exc_num'],
'basic_num' => (int)$cart['basic_num'],
2024-12-13 01:03:36 +08:00
'goods_total_amounts' => round($goods_total_amounts,2)
// 'goods_total_amounts' => round(0.88*$goods_total_amounts,2)
2024-08-03 10:04:14 +08:00
];
}
$lists[$shop_id]['shop'] = $shop_info;
$lists[$shop_id]['cart'] = $cart_lists;
}
return [
'lists' => array_values($lists),
2024-12-13 01:03:36 +08:00
'total_amount' => round($total, 2),
// 'total_amount' => round(0.88*$total, 2),
2024-08-03 10:04:14 +08:00
'total_num' => $goods_num,
];
}
/**
* Notes: 添加
* @param $post
* @param $user_id
* @return bool
* @author 段誉(2021/5/10 19:03)
*/
public static function add($post, $user_id)
{
try {
$item_id = $post['item_id'];
$goods_num = $post['goods_num'];
$p_id = isset($post['p_id'])?$post['p_id']:0;
$w_price = isset($post['w_price'])?$post['w_price']:0;
$price_type = isset($post['price_type'])?$post['price_type']:0;
$basic_num = isset($post['basic_num'])?$post['basic_num']:0;
$exc_num = isset($post['exc_num'])?$post['exc_num']:0;
$w_discount = isset($post['w_discount'])?$post['w_discount']:0;
$firend_invite_code = isset($post['firend_invite_code'])?$post['firend_invite_code']:0;
$cart = Cart::where(['user_id' => $user_id, 'item_id' => $item_id])->find();
$cart_num = $post['goods_num'] + (isset($cart) ? $cart['goods_num'] : 0);
$goods = self::checkCartGoods($item_id, $cart_num);
if (false === $goods) {
throw new \Exception(self::getError() ?: '商品信息错误');
}
if ($cart) {
if($p_id!=0&&$w_price!=0&&$cart['w_price']!=$w_price){
Cart::create([
'user_id' => $user_id,
'goods_id' => $goods['id'],
'goods_num' => $goods_num,
'item_id' => $item_id,
'shop_id' => $goods['shop_id'],
'p_id' => $p_id,
'w_price' => $w_price,
'price_type'=>$price_type,
'basic_num'=>$basic_num,
'exc_num'=>$exc_num,
'w_discount'=>$w_discount
]);
}else{
//购物车内已有该商品
Cart::where(['id' => $cart['id'], 'shop_id' => $goods['shop_id']])
->update(['goods_num' => $goods_num + $cart['goods_num'],'p_id' => $p_id,
'w_price' => $w_price,'price_type'=>$price_type]);
}
} else {
//新增购物车记录
Cart::create([
'user_id' => $user_id,
'goods_id' => $goods['id'],
'goods_num' => $goods_num,
'item_id' => $item_id,
'shop_id' => $goods['shop_id'],
'p_id' => $p_id,
'w_price' => $w_price,
'firend_invite_code' =>$firend_invite_code,
'price_type'=>$price_type,
'basic_num'=>$basic_num,
'exc_num'=>$exc_num,
'w_discount'=>$w_discount
]);
}
// 记录访问足迹
event('Footprint', [
'type' => FootprintEnum::ADD_CART,
'user_id' => $user_id,
'foreign_id' => $goods['id']
]);
return true;
} catch (\Exception $e) {
self::$error = $e->getMessage();
return false;
}
}
/**
* Notes: 变动数量
* @param $cart_id
* @param $goods_num
* @author 段誉(2021/5/11 11:59)
* @return bool
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public static function change($cart_id, $goods_num)
{
$cart = Cart::find($cart_id);
$goods_num = ($goods_num <= 0) ? 1 : $goods_num;
if (false === self::checkCartGoods($cart['item_id'], $goods_num)) {
return false;
}
$result = Cart::where('id',$cart_id)->find();
if($result['basic_num'] > 0){
if($goods_num < $result['basic_num']){
self::$error = '批发价数量不能低于选择的批发数量';
return false;
}else{
$exc_num = $goods_num-$result['basic_num'];
Cart::update(['goods_num' => $goods_num,'exc_num'=>$exc_num], ['id' => $cart_id]);
}
}else{
Cart::update(['goods_num' => $goods_num], ['id' => $cart_id]);
}
return true;
}
/**
* Notes: 删除
* @param $cart_id
* @param $user_id
* @author 段誉(2021/5/11 12:02)
* @return bool
*/
public static function del($cart_id, $user_id)
{
return Cart::where(['id' => $cart_id, 'user_id' => $user_id])->delete();
}
/**
* Notes: 更改选中状态
* @param $post
* @param $user_id
* @author 段誉(2021/5/11 15:49)
* @return Cart
*/
public static function selected($post, $user_id)
{
return Cart::where(['user_id' => $user_id, 'id' => $post['cart_id']])
->update(['selected' => $post['selected']]);
}
/**
* Notes: 购物车数量
* @param $user_id
* @author 段誉(2021/5/11 12:07)
* @return array
*/
public static function cartNum($user_id)
{
$cart = new Cart();
$num = $cart->alias('c')
->join('goods g', 'g.id = c.goods_id')
->join('goods_item i', 'i.id = c.item_id')
->where(['g.status' => 1, 'g.del' => 0, 'c.user_id' => $user_id])
->sum('goods_num');
return ['num' => $num ?? 0];
}
/**
* Notes: 验证商品
* @param $item_id
* @param $goods_num
* @author 段誉(2021/5/11 11:59)
* @return bool
*/
public static function checkCartGoods($item_id, $goods_num)
{
$goodsModel = new Goods();
$goods = $goodsModel->alias('g')
->with('shop')
->field('g.id, g.status, g.del, g.shop_id, g.type,i.stock')
->join('goods_item i', 'i.goods_id = g.id')
->where('i.id', $item_id)
->find();
if (!$goods['shop']['is_pay']) {
self::$error = '该商家支付功能已关闭';
return false;
}
if (empty($goods) || $goods['status'] == 0 || $goods['del'] != 0) {
self::$error = '商品不存在或已下架';
return false;
}
if ($goods['stock'] < $goods_num) {
self::$error = '很抱歉,库存不足';
return false;
}
if ($goods['type'] == GoodsEnum::TYPE_VIRTUAL) {
self::$error = '虚拟商品不可加入购物车';
return false;
}
return $goods;
}
}