likeshop/server/application/common/logic/OrderGoodsLogic.php
2025-07-27 21:42:34 +08:00

191 lines
6.3 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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\common\logic;
use app\api\logic\SeckillLogic;
use app\common\model\AfterSale;
use app\common\model\Order;
use app\common\model\OrderGoods;
use app\common\model\Pay;
use app\common\server\ConfigServer;
use think\Db;
use think\facade\Hook;
/**
* 订单商品逻辑
* Class OrderGoodsLogic
* @package app\common\logic
*/
class OrderGoodsLogic
{
//返回订单库存,销量
public static function backStock($order_goods, $pay_status)
{
$deduct_type = ConfigServer::get('trading', 'deduct_type', 1);
//支付后扣减
if ($deduct_type == 0 && $pay_status == Pay::UNPAID) {
return;
}
foreach ($order_goods as $good) {
//回退库存,回退规格库存,减少商品销量
Db::name('goods')
->where('id', $good['goods_id'])
->update([
// 'sales_sum' => Db::raw("sales_sum-" . $good['goods_num']),
'stock' => Db::raw('stock+' . $good['goods_num'])
]);
//补充规格表库存
Db::name('goods_item')
->where('id', $good['item_id'])
->setInc('stock', $good['goods_num']);
}
}
//下单扣除订单库存
public static function decStock($goods)
{
$seckill_data = SeckillLogic::getSeckillGoods();
$seckill = $seckill_data['seckill'];
$seckill_goods = $seckill_data['seckill_goods'];
$goods_ids = [];
foreach ($goods as $k1 => $good) {
$item_id = $good['item_id'];
//扣除库存,扣除规格库存,增加商品销量
Db::name('goods')
->where('id', $good['goods_id'])
->update([
'sales_sum' => Db::raw("sales_sum+" . $good['goods_num']),
'stock' => Db::raw('stock-' . $good['goods_num'])
]);
//扣除规格表库存
Db::name('goods_item')
->where('id', $item_id)
->setDec('stock', $good['goods_num']);
//秒杀商品增加销量
if (isset($seckill_goods[$item_id])){
$seckill_goods_id = $seckill_goods[$item_id]['seckill_goods_id'];
Db::name('seckill_goods')
->where('id', $seckill_goods_id)
->update([
'sales_sum' => Db::raw("sales_sum+" . $good['goods_num']),
'update_time' => time()
]);
}
$goods_ids[] = $good['goods_id'];
}
//下架商品总库存为0的商品
if (!empty($goods_ids)){
self::outGoods($goods_ids);
}
}
/**
* Notes: 下单后下架商品总库存为0的商品
* @param $goods_id
* @author 段誉(2021/3/19 16:19)
* @return bool
*/
public static function outGoods($goods_ids)
{
try{
$goods = Db::name('goods')
->field('id, stock')
->where('id', 'in', $goods_ids)
->select();
if (empty($goods)){
return true;
}
$need_handle_ids = [];
foreach ($goods as $good) {
if ($good['stock'] <= 0) {
$need_handle_ids[] = $good['id'];
}
}
if (empty($need_handle_ids)){
return true;
}
//下架订单商品中 商品总库存已为0的商品
Db::name('goods')->where('id', 'in', $need_handle_ids)->update(['status' => 0]);
// 下架或删除商品,更新商品收藏
Hook::listen('update_collect', ['goods_id' => $need_handle_ids]);
} catch (\Exception $e) {}
}
/**
* @notes 获取订单商品 退款运费
* @return integer|float
* @author lbzy
* @datetime 2023-06-26 09:20:21
*/
static function getRefundExpressMoney($id)
{
$orderGoods = OrderGoods::find($id);
$order = Order::find($orderGoods['order_id'] ?? 0);
// 不是待发货 不退运费
if (empty($order['id']) || $order['order_status'] != Order::STATUS_WAIT_DELIVERY) {
return 0;
}
$after_sales = AfterSale::where('order_id', $order['id'])->select()->toArray();
$aids = [];
foreach ($after_sales as $after_sale) {
if (! in_array($after_sale['order_goods_id'], $aids)) {
$aids[] = $after_sale['order_goods_id'];
}
}
$orderGoodsList = OrderGoods::where('order_id', $order['id'])->select()->toArray();
if (count($aids) != count($orderGoodsList)) {
if (in_array($id, $aids)) {
return 0;
} else {
// 没有记录,是否是最后一个订单商品
return count($aids) == (count($orderGoodsList) - 1) ? $order['shipping_price'] : 0;
}
}
// 有记录,是否是在最后申请的
return $aids[count($aids) - 1] == $id ? $order['shipping_price'] : 0;
}
}