<?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 下载量 76 浏览量
2025-07-31
06:18:37
上传
评论
收藏 3.92MB ZIP 举报
温馨提示
B2B2C Laravel8+Vue3
资源推荐
资源详情
资源评论

















收起资源包目录





































































































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


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


最新资源
- 试议网络营销组合通路决策.pptx
- 死因监测网络直报.ppt
- 基于PLC饮料灌装生产流水线控制系统设计.doc
- AI人工智能技术的应用范围和案例.doc
- 现代通信技术与系统.doc
- 基于微课中的Photoshop课程教学可行性研究获奖科研报告论文.docx
- 零基础学Excel--Vba-应用实例.ppt
- 用MATLAB解决-条件平差和间接平差.ppt
- lilishop 商城 java商城-C++资源
- 2023年国网计算机职称考试辅导资料习题.doc
- 基于单片机的万年历实习报告.docx
- 解读防范电信诈骗网络诈骗学习课件.pptx
- 单片机教案(第6章存储器的扩展).doc
- 用Aspen-Plus模拟反胶束萃取大豆蛋白过程中毛油脱溶操作.pdf
- 医院信息系统安全.ppt
- 关于茶叶的网络营销策划方案.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



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