<?php
namespace App\Qingwuit\Services;
use App\Http\Resources\OrderAfterHomeCollection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
class OrderService extends BaseService
{
// 要删除的购物车数据
protected $cartId = [];
// 获取创建订单前处理订单
public function createOrderBefore()
{
$check = $this->base64Check();
if (!$check['status']) {
return $this->formatError($check['msg']);
}
$params = $check['data'];
$rs = $this->createOrderFormat($params);
return $rs['status'] ? $this->format($rs['data']) : $this->formatError($rs['msg']);
}
// 创建订单
public function createOrder()
{
$check = $this->base64Check();
if (!$check['status']) {
return $this->formatError($check['msg']);
}
$params = $check['data'];
$rs = $this->createOrderFormat($params);
if (!$rs['status']) {
return $this->formatError($rs['msg']);
}
// 优惠券的处理
if (!isset(request()->coupon_id)) {
return $this->formatError('coupon_id empty');
}
$couponId = explode(',', request()->coupon_id);
// 地址验证
$addressResp = $this->checkAddress();
if (!$addressResp['status']) {
return $this->formatError($addressResp['msg']);
}
$remark = request('remark', '');
$userId = $this->getUserId('users');
$makeRands = [];
if (count($rs['data']) > 0) {
foreach ($rs['data'] as $v) $makeRands[] = date('YmdHis') . substr($userId, 0, 1) . mt_rand(10000, 99999); // 生成订单号
}
// REDIS队列创建订单
if (env('APP_REDIS')) {
$this->getJob('CreateOrder', [
'rs' => $rs,
'address_resp' => $addressResp,
'coupon_id' => $couponId,
'userId' => $userId,
'make_rands' => $makeRands,
'remark' => $remark
])->onQueue('createOrder');
return $this->format(['order_id' => [], 'order_no' => $makeRands], 'Queue executing.');
}
return $this->createOrderData($rs, $addressResp, $couponId, $userId, $makeRands, $remark);
}
// 封装个创建订单的插入方法
public function createOrderData($rs, $address_resp, $coupon_id, $userId, $make_rands, $remark)
{
$address_info = $address_resp['data'];
// 实例化订单表
$order_model = $this->getService('Order', true);
$order_goods_model = $this->getService('OrderGoods', true);
$seckillService = $this->getService('Seckill');
$fullReductionService = $this->getService('FullReduction');
$collectiveService = $this->getService('Collective');
// 循环生成订单 多个商家则生成多个订单
try {
DB::beginTransaction();
$resp_data = [];
foreach ($rs['data'] as $k => $v) {
$order_data = [
'order_no' => $make_rands[$k], // 订单号
'user_id' => $userId, // 用户ID
'store_id' => $v['store_info']['id'], // 店铺ID
'order_name' => $v['goods_list'][0]['goods_name'], // 商品ID
'order_image' => $v['goods_list'][0]['goods_master_image'], // 商品图片
'receive_name' => $address_info['receive_name'], // 收件人姓名
'receive_tel' => $address_info['receive_tel'], // 收件人电话
'receive_area' => $address_info['area_info'], // 收件人地区
'receive_address' => $address_info['address'], // 详细地址
'coupon_id' => isset($coupon_id[$k]) ? intval(abs($coupon_id[$k])) : 0, // 优惠券ID
'remark' => $remark, // 备注
];
$order_info = $order_model->create($order_data); // 订单数据插入数据库
// 初始化其他费用
$total_price = 0; // 总金额
$order_price = 0; // 订单总金额
$total_weight = 0; // 总重量
$freight_money = 0; // 运费
$coupon_money = 0; // 优惠券 金额
// 循环将订单商品插入
foreach ($v['goods_list'] as $vo) {
$order_goods_data = [
'order_id' => $order_info->id, // 订单ID
'user_id' => $order_data['user_id'], // 用户ID
'store_id' => $order_data['store_id'], // 店铺ID
'sku_id' => $vo['sku_id'], // skuid
'goods_id' => $vo['id'], // 商品id
'goods_name' => $vo['goods_name'], // 商品名称
'goods_image' => $vo['goods_master_image'], // 商品图片
'sku_name' => $vo['sku_name'], // sku名称
'buy_num' => $vo['buy_num'], // 购买数量
'goods_price' => $vo['goods_price'], // 商品价格
'total_price' => $vo['total'], // 总价格
'total_weight' => $vo['total_weight'], // 总重量
];
// 秒杀
$seckillInfo = $seckillService->seckillInfoByGoodsId($order_goods_data['goods_id']);
if ($seckillInfo['status']) $coupon_money = bcadd($coupon_money, bcmul($order_goods_data['total_price'], $seckillInfo['data']['discount'] / 100, 2), 2);
$order_goods_model->create($order_goods_data); // 插入订单商品表
// 开始减去库存
$this->orderStock($order_goods_data['goods_id'], $order_goods_data['sku_id'], $order_goods_data['buy_num']);
// 将商品总总量加起来
$total_weight = bcadd($total_weight, $order_goods_data['total_weight'], 2);
// 将金额全部计算加载到订单里面
$order_price = bcadd($order_price, $order_goods_data['total_price'], 2);
}
// 获取优惠券的金额 并插入数据库
if ($order_data['coupon_id'] > 0) {
$cpli = $this->getService('CouponLog', true)->where('id', $order_data['coupon_id'])
->where('user_id', $order_data['user_id'])
->where('store_id', $order_data['store_id'])
->where('use_money', '<=', bcadd($order_price, $freight_money, 2))
->where('start_time', '<', now())->where('end_time', '>', now())
->where('status', 0)->first();
if (empty($cpli)) {
$order_data['coupon_id'] = 0;
} else {
$coupon_money = bcadd($coupon_money, $cpli['money'], 2);
$cpli->status = 1;
$cpli->order_id = $order_info->id;
$cpli->save();
}
}
// 满减
$full_reduction_resp = $fullReductionService->fullReductionInfoByStoreId($order_data['store_id'], bcadd($order_price, $freight_money, 2));
if ($full_reduction_resp['status']) {
$coupon_money = bcadd($coupon_money, $full_reduction_resp['data']['money'], 2);
}
// 判断是否是拼团 如果是拼团减去他的�
没有合适的资源?快使用搜索试试~ 我知道了~
青梧商城B2B2C-C++资源

共669个文件
php:372个
vue:146个
png:47个

需积分: 1 0 下载量 171 浏览量
2025-07-29
06:57:06
上传
评论
收藏 3.92MB ZIP 举报
温馨提示
B2B2C Laravel8+Vue3
资源推荐
资源详情
资源评论

















收起资源包目录





































































































共 669 条
- 1
- 2
- 3
- 4
- 5
- 6
- 7
资源评论


xyq2024
- 粉丝: 3963
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- Aestate-Python资源
- 网络营销和策划.pptx
- YKSwiftNetworking-Swift资源
- 三星中小企业网络安全解决方案.doc
- 通信原理教学.pptx
- 网络程序员工作计划样本.doc
- 我爱我家(主题网络)(20220208022735).pdf
- 公司通信调度系统技术规范及技术方案书.docx
- 网络营销与策划实践环节考核.doc
- 物联网简介幻灯片.ppt
- 华为网络认证工程师.docx
- 基于ARM的Buck-Boost双向DC-DC电源变换器:同步BUCK与BOOST电路级联的数字稳压技术
- 计算机科学与技术专业的知识体系与课程体系.pptx
- 网络推广协议范本最新.doc
- 2023年电子商务基础测试题库.doc
- 酒店住宿及消费管理系统数据库.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
