1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342 |
- <?php
- namespace app\common\Service;
- use app\common\Enum\GoodsEnum;
- use app\common\model\Order;
- use app\common\model\OrderGoods;
- use app\common\model\Address;
- use app\common\model\UserCoupon;
- use app\common\model\Goods;
- use app\common\model\Sku;
- use app\common\model\Freight;
- use think\Db;
- use think\Exception;
- use app\common\model\OrderAddress;
- use app\common\Enum\OrderEnum;
- use app\common\exception\BusinessException;
- use app\common\Service\Order\OrderActionService;
- use app\common\Enum\OrderActionEnum;
- use app\common\Service\SkuSpec;
- use app\common\Service\ParentOrderService;
- use app\common\Service\DiscountService;
- use app\common\Service\BodyProfileService;
- use app\common\model\OrderProfile;
- /**
- * 订单服务类
- * 封装订单创建相关逻辑
- *
- * 新增功能:
- * - createSingleGoodsOrders(): 创建单商品订单,一个商品一个订单
- *
- * 使用示例:
- *
- * // 创建单商品订单(一个商品一个订单)
- * $goodsList = [
- * ['goods_id' => 1, 'goods_sku_id' => 10, 'nums' => 2],
- * ['goods_id' => 2, 'goods_sku_id' => 20, 'nums' => 1],
- * ['goods_id' => 3, 'goods_sku_id' => 30, 'nums' => 3]
- * ];
- * $orders = OrderService::createSingleGoodsOrders($addressId, $userId, $goodsList, $userCouponId, $remark);
- * // 返回3个订单对象的数组,每个商品对应一个订单
- *
- * // 创建传统订单(多商品合并为一个订单)
- * $order = OrderService::createOrder($addressId, $userId, $goodsList, $userCouponId, $remark);
- * // 返回1个包含所有商品的订单对象
- */
- class OrderService
- {
- /**
- * 订单金额
- * @var array
- */
- protected static $orderPrice = [
- 'amount' => 0, //订单应付金额
- 'goods_price' => 0, //商品总费用
- 'goods_num' => 0, //商品数量
- 'discount_fee' => 0, //优惠金额
- 'coupon_discount_fee' => 0, //优惠券金额
- 'promo_discount_fee' => 0, //营销金额
- 'total_discount_amount' => 0, //总优惠
- 'order_amount' => 0, //订单金额
- 'pay_amount' => 0, //实际付款金额
- 'pay_original_amount' => 0, //原始支付金额
- 'pay_remain_amount' => 0, //剩余支付金额
- 'express_fee' => 0, //配送费用
- 'activity_discount_amount' => 0, //活动折扣金额(新增)
- 'goods_original_price' => 0, //商品原价总额(新增)
- 'order_snapshot' => null, //订单快照,记录折扣等详细信息
- ];
- /**
- * 初始化订单价格
- * @return array
- */
- protected static function initOrderPrice()
- {
- return [
- 'amount' => 0, //订单应付金额
- 'goods_price' => 0, //商品总费用
- 'goods_num' => 0, //商品数量
- 'discount_fee' => 0, //优惠金额
- 'coupon_discount_fee' => 0, //优惠券金额
- 'promo_discount_fee' => 0, //营销金额
- 'total_discount_amount' => 0, //总优惠
- 'order_amount' => 0, //订单金额
- 'pay_amount' => 0, //实际付款金额
- 'pay_original_amount' => 0, //原始支付金额
- 'pay_remain_amount' => 0, //剩余支付金额
- 'express_fee' => 0, //配送费用
- 'activity_discount_amount' => 0, //活动折扣金额(新增)
- 'goods_original_price' => 0, //商品原价总额(新增)
- 'order_snapshot' => null, //订单快照,记录折扣等详细信息
- ];
- }
- /**
- * 根据商品列表计算订单明细
- * @param array $orderInfo 订单基础信息
- * @param array $goods_list 商品列表
- * @param int $user_id 用户ID
- * @param int $area_id 地区ID
- * @param int $user_coupon_id 优惠券ID
- * @return array
- * @throws
- */
- protected static function computeGoods(&$orderInfo, $goodsList, $userId, $areaId, $userCouponId = 0)
- {
- // 初始化订单价格
- $orderPrice = self::initOrderPrice();
- foreach ($orderPrice as $key => $value) {
- $orderInfo[$key] = $value;
- }
- $orderItem = [];
- $shippingTemp = [];
- $userCoupon = null;
- $processedGoodsList = []; // 处理后的商品列表,避免与参数$goodsList冲突
- // 校验优惠券
- if ($userCouponId) {
- $userCouponModel = new UserCoupon();
- $userCoupon = $userCouponModel->checkUserOrUse($userCouponId, $userId);
- $orderInfo['user_coupon_id'] = $userCouponId;
- }
- // 提取所有商品ID和SKU ID,进行批量查询
- $goodsIds = array_column($goodsList, 'goods_id');
- $skuIds = [];
- foreach ($goodsList as $index => $item) {
- if (isset($item['goods_sku_id']) && $item['goods_sku_id'] > 0) {
- $skuIds[] = $item['goods_sku_id'];
- }
- }
- // 批量查询商品信息
- $goodsData = [];
- if (!empty($goodsIds)) {
- $goodsModel = new Goods();
- $goodsCollection = $goodsModel->with(['brand'])
- ->where('id', 'in', $goodsIds)
- ->where('status', GoodsEnum::STATUS_ON_SALE)
- ->select();
- foreach ($goodsCollection as $goods) {
- $goodsData[$goods->id] = $goods;
- }
- }
- // 批量查询SKU信息
- $skuData = [];
- $multiSpecSkuIds = []; // 用于存储多规格商品的SKU ID
- if (!empty($skuIds)) {
- $skuModel = new Sku();
- $skuCollection = $skuModel->where('id', 'in', $skuIds)->select();
- foreach ($skuCollection as $sku) {
- $skuData[$sku->id] = $sku;
- // 过滤出有规格值的SKU ID(spec_value_ids不为空)
- if (!empty($sku->spec_value_ids)) {
- $multiSpecSkuIds[] = $sku->id;
- }
- }
- }
- // 批量查询规格属性字符串(只查询多规格商品的SKU)
- $skuAttrData = [];
- if (!empty($multiSpecSkuIds)) {
- $skuAttrData = SkuSpec::getSkuAttrs($multiSpecSkuIds);
- }
- // 批量查询商品的折扣信息
- $goodsDiscountData = [];
- $orderSnapshotData = []; // 用于收集订单快照信息
- if (!empty($goodsIds)) {
- $goodsDiscountData = DiscountService::getBatchGoodsDiscountInfo($goodsIds);
- }
- // echo "<pre>";
- // print_r($goodsDiscountData);
- // echo "</pre>";
- // exit;
- // 验证并构建商品数据
- foreach ($goodsList as $item) {
- $goods_id = $item['goods_id'];
- $goods_sku_id = $item['goods_sku_id']; // 现在所有商品都应该有SKU ID
- $nums = $item['nums'];
- if ($nums <= 0) {
- throw new BusinessException("商品数量必须大于0");
- }
- // 检查商品是否存在
- if (!isset($goodsData[$goods_id])) {
- throw new BusinessException("商品已下架");
- }
- $goods = $goodsData[$goods_id];
- // 所有商品都必须有SKU(包括单规格商品的默认SKU)
- if (empty($skuData) || !isset($skuData[$goods_sku_id])) {
- throw new BusinessException("商品规格不存在");
- }
- $sku = $skuData[$goods_sku_id];
-
- // 验证SKU是否属于该商品
- if ($sku->goods_id != $goods_id) {
- throw new BusinessException("商品规格不匹配");
- }
-
- // 获取规格属性字符串(单规格商品的sku_attr为空)
- $sku_attr = $skuAttrData[$goods_sku_id] ?? '';
- // 构建商品对象,模拟购物车数据结构
- $goodsItem = (object)[
- 'goods_id' => $goods_id,
- 'goods_sku_id' => $goods_sku_id,
- 'nums' => $nums,
- 'goods' => $goods,
- 'sku' => $sku,
- 'sku_attr' => $sku_attr
- ];
- $processedGoodsList[] = $goodsItem;
- }
-
- // 计算商品价格和运费(统一使用SKU进行计算)
- foreach ($processedGoodsList as $item) {
- $goodsItemData = [];
-
- if (empty($item->goods) || empty($item->sku)) {
- throw new BusinessException("商品已下架");
- }
-
- // 库存验证(统一使用SKU库存)
- if ($item->sku->stocks < $item->nums) {
- throw new BusinessException("商品库存不足,请重新修改数量");
- }
-
- // 获取商品折扣信息
- $discountInfo = null;
- $actualPrice = $item->sku->price; // 默认使用原价
- $originalPrice = $item->sku->price;
-
- if (isset($goodsDiscountData[$item->goods_id])) {
- // 查找对应SKU的折扣信息
- foreach ($goodsDiscountData[$item->goods_id] as $discount) {
- if ($discount['sku_id'] == $item->sku->id) {
- $discountInfo = $discount;
- $actualPrice = $discount['discount_price']; // 使用折扣价格
- break;
- }
- }
- }
-
- // 计算金额
- $amount = bcmul($actualPrice, $item->nums, 2); // 使用实际价格计算
- $originalAmount = bcmul($originalPrice, $item->nums, 2); // 原价总额
-
- // 统一使用SKU数据进行计算
- $goodsItemData['image'] = !empty($item->sku->image) ? $item->sku->image : $item->goods->image;
- $goodsItemData['price'] = $actualPrice; // 使用实际价格(折扣价或原价)
- $goodsItemData['original_price'] = $originalPrice; // 原价
- $goodsItemData['sku_sn'] = $item->sku->sku_sn;
- $goodsItemData['discount_info'] = $discountInfo; // 折扣信息
-
- $goodsItemData['amount'] = $amount;
- $goodsItemData['original_amount'] = $originalAmount;
-
- // 订单应付金额
- $orderInfo['amount'] = bcadd($orderInfo['amount'], $amount, 2);
- // 商品总价
- $orderInfo['goods_price'] = bcadd($orderInfo['goods_original_price'], $originalAmount, 2);;
- // 商品原价总额
- $orderInfo['goods_original_price'] = bcadd($orderInfo['goods_original_price'], $originalAmount, 2);
- // 商品数量累计
- $orderInfo['goods_num'] += $item->nums;
-
- // 活动折扣金额累计
- if ($discountInfo) {
- $discountAmount = bcsub($originalAmount, $amount, 2);
- // 确保折扣金额不为负数,如果计算结果为负数则设为0
- $discountAmount = bccomp($discountAmount, '0', 2) >= 0 ? $discountAmount : '0';
- $orderInfo['activity_discount_amount'] = bcadd($orderInfo['activity_discount_amount'], $discountAmount, 2);
-
- // 收集折扣信息到订单快照
- $orderSnapshotData['discount_activities'][] = [
- 'goods_id' => $item->goods_id,
- 'goods_title' => $item->goods->title,
- 'sku_id' => $item->sku->id,
- 'sku_attr' => $item->sku_attr,
- 'activity_id' => $discountInfo['activity_id'],
- 'activity_name' => $discountInfo['activity_name'],
- 'original_price' => $originalPrice,
- 'discount_price' => $actualPrice,
- 'discount' => $discountInfo['discount'],
- 'discount_amount' => $discountAmount,
- 'quantity' => $item->nums,
- 'total_discount_amount' => $discountAmount,
- ];
- }
- $freight_id = $item->goods->express_template_id;
- // 计算邮费【合并运费模板】
- if (!isset($shippingTemp[$freight_id])) {
- $shippingTemp[$freight_id] = [
- 'nums' => $item->nums,
- 'weight' => $item->sku->weight,
- 'amount' => $amount
- ];
- } else {
- $shippingTemp[$freight_id] = [
- 'nums' => bcadd($shippingTemp[$freight_id]['nums'], $item->nums, 2),
- 'weight' => bcadd($shippingTemp[$freight_id]['weight'], $item->sku->weight, 2),
- 'amount' => bcadd($shippingTemp[$freight_id]['amount'], $amount, 2)
- ];
- }
-
- // 创建订单商品数据 (基于正确的表结构)
- $orderItemData = [
- 'user_id' => $userId,
- 'order_sn' => '', // 将在订单创建后补充
- 'goods_sn' => $item->goods->goods_sn ?: '', // 商品货号
- 'sku_sn' => $item->sku->sku_sn ?: '', // SKU编号
- 'goods_type' => $item->goods->type ?: 0, // 商品类型
- 'goods_id' => $item->goods->id,
- 'goods_sku_attr' => $item->sku_attr,
- 'goods_spec_value_ids' => $item->sku->spec_value_ids,
- 'goods_sku_id' => $item->sku->id,
- 'goods_title' => $item->goods->title,
- 'goods_market_price' => $item->sku->market_price ?: 0, // 市场价
- 'goods_original_price' => $originalPrice, // 商品原价
- 'goods_price' => $amount, // 实付金额(折扣价)
- 'goods_image' => $goodsItemData['image'],
- 'goods_weight' => $item->sku->weight ?: 0,
- 'nums' => $item->nums,
- 'sale_status' => 0, // 销售状态:0=待申请
- 'comment_status' => 0, // 评论状态:0=未评论
- 'status' => 1, // 状态
- 'supplier_id' => $item->goods->supplier_id ?: 0,
- 'inspection_type_id' => $item->goods->inspection_type_id ?: 0,
- ];
-
- // 如果有折扣信息,记录活动相关信息
- // if ($discountInfo) {
- // $orderItemData['activity_id'] = $discountInfo['activity_id'] ?? 0;
- // $orderItemData['activity_name'] = $discountInfo['activity_name'] ?? '';
- // $orderItemData['discount_amount'] = bcsub($originalAmount, $amount, 2);
- // }
-
- $orderItem[] = $orderItemData;
- }
-
- // 按运费模板计算
- foreach ($shippingTemp as $key => $item) {
- $shippingfee = Freight::calculate($key, $areaId, $item['nums'], $item['weight'], $item['amount']);
- $orderInfo['express_fee'] = bcadd($orderInfo['express_fee'], $shippingfee, 2);
- }
- // 订单金额(商品价格+运费)
- $orderInfo['order_amount'] = bcadd($orderInfo['goods_price'], $orderInfo['express_fee'], 2);
- // 订单应付金额(暂时等于订单金额,后续会减去优惠)
- $orderInfo['amount'] = $orderInfo['order_amount'];
- // if (!empty($userCoupon)) {
- // // 校验优惠券
- // $goods_ids = array_column($orderItem, 'goods_id');
- // $category_ids = array_column($orderItem, 'category_id');
- // $brand_ids = array_column($orderItem, 'brand_id');
- // $couponModel = new Coupon();
- // $coupon = $couponModel->getCoupon($userCoupon['coupon_id'])
- // ->checkCoupon()
- // ->checkOpen()
- // ->checkUseTime($userCoupon['createtime'])
- // ->checkConditionGoods($goods_ids, $userId, $category_ids, $brand_ids);
- // // 计算折扣金额,判断是使用不含运费,还是含运费的金额
- // $amount = !isset($config['shippingfeecoupon']) || $config['shippingfeecoupon'] == 0 ? $orderInfo['goods_price'] : $orderInfo['order_amount'];
- // list($new_money, $coupon_money) = $coupon->doBuy($amount);
- // // 判断优惠金额是否超出总价,超出则直接设定优惠金额为总价
- // $orderInfo['coupon_discount_fee'] = $coupon_money > $amount ? $amount : $coupon_money;
- // $orderInfo['discount_fee'] = $orderInfo['coupon_discount_fee'];
- // }
- // 汇总所有折扣金额
- $orderInfo['discount_fee'] = bcadd($orderInfo['activity_discount_amount'], $orderInfo['coupon_discount_fee'], 2);
- $orderInfo['promo_discount_fee'] = $orderInfo['activity_discount_amount']; // 营销金额 = 活动折扣金额
- $orderInfo['total_discount_amount'] = $orderInfo['discount_fee']; // 总优惠金额
-
- // 计算最终应付金额【订单金额减去折扣】
- $orderInfo['amount'] = max(0, bcsub($orderInfo['order_amount'], $orderInfo['discount_fee'], 2));
- $orderInfo['pay_amount'] = $orderInfo['amount']; // 实际付款金额等于应付金额
- // 保存订单快照信息
- if (!empty($orderSnapshotData)) {
- $orderSnapshotData['order_summary'] = [
- 'total_goods_original_price' => $orderInfo['goods_original_price'],
- 'total_goods_price' => $orderInfo['goods_price'],
- 'total_activity_discount' => $orderInfo['activity_discount_amount'],
- 'total_coupon_discount' => $orderInfo['coupon_discount_fee'],
- 'total_discount' => $orderInfo['discount_fee'],
- 'express_fee' => $orderInfo['express_fee'],
- 'final_amount' => $orderInfo['amount'],
- 'snapshot_time' => time(),
- ];
- $orderInfo['order_snapshot'] = json_encode($orderSnapshotData, JSON_UNESCAPED_UNICODE);
- }
- return [
- $orderItem,
- $processedGoodsList,
- $userCoupon
- ];
- }
-
- /**
- * 统一的创建订单方法
- * @param int $address_id 地址ID
- * @param int $user_id 用户ID
- * @param array $goods_list 标准化的商品列表
- * @param int $user_coupon_id 优惠券ID
- * @param string $memo 备注
- * @param array $cart_ids 购物车ID数组(如果是购物车模式需要清空)
- * @return Order
- * @throws BusinessException
- */
- public static function createOrder($addressId, $userId, $goodsList, $userCouponId = 0, $remark = '', $supplierId = 0, $profileId = 0)
- {
- $address = Address::get($addressId);
- if (!$address || $address['user_id'] != $userId) {
- throw new BusinessException("地址未找到");
- }
- if (empty($goodsList)) {
- throw new BusinessException("商品列表不能为空");
- }
- $orderTimeout = ShopConfigService::getConfigs('shop.order.order_timeout',false) ?? 3600;
- $orderSn = date("Ymdhis") . sprintf("%08d", $userId) . mt_rand(1000, 9999);
- // 订单主表信息 (基于新表结构)
- $orderInfo = array_merge(self::initOrderPrice(), [
- 'type' => 1, // 1:普通订单
- 'order_sn' => $orderSn,
- 'user_id' => $userId,
- 'profile_id' => $profileId, // 档案ID
- 'expire_time' => time() + $orderTimeout, // 过期时间
- 'order_status' => OrderEnum::STATUS_CREATE, // 待付款
- 'invoice_status' => 0, // 发票开具状态
- 'remark' => $remark, // 用户备注
- 'user_coupon_id' => $userCouponId ?: null,
- 'ip' => request()->ip(), // IP地址
- 'status' => 'normal',
- ]);
- $orderInfo['platform'] = request()->header('platform', 'H5');
- // 通过商品列表计算订单明细
- list($orderItem, $calculatedGoodsList, $userCoupon) = self::computeGoods($orderInfo, $goodsList, $userId, $address->area_id, $userCouponId);
-
- // 如果有活动折扣,记录活动信息
- if ($orderInfo['activity_discount_amount'] > 0) {
- $currentActivity = DiscountService::getCurrentActivity();
- if ($currentActivity) {
- $orderInfo['activity_type'] = 'discount';
- $orderInfo['activity_id'] = $currentActivity['id'];
- }
- }
-
- $orderInfo['pay_amount'] = max(0, bcsub($orderInfo['order_amount'], $orderInfo['discount_fee'], 2));
- $orderInfo['pay_original_amount'] = $orderInfo['pay_amount'];
- $orderInfo['pay_remain_amount'] = $orderInfo['pay_amount'];
- // echo "<pre>";
- // print_r($orderInfo);
- // echo "</pre>";
- // exit;
- // 创建订单
- $order = self::createOrderWithTransaction($orderInfo, $orderItem, $calculatedGoodsList, $userCoupon, $address, $profileId);
-
- return $order;
- }
- /**
- * 创建单商品订单 - 一个商品一个订单
- * @param int $addressId 地址ID
- * @param int $userId 用户ID
- * @param array $goodsList 商品列表(支持多商品但会拆分为多个订单)
- * @param int $userCouponId 优惠券ID(仅用于第一个订单)
- * @param string $remark 备注
- * @param int $supplierId 供应商ID
- * @param int $profileId 档案ID
- * @return array 返回创建的订单数组
- * @throws BusinessException
- */
- public static function createSingleGoodsOrders($addressId, $userId, $goodsList, $userCouponId = 0, $remark = '', $supplierId = 0, $profileId = 0)
- {
- // 验证地址
- $address = Address::get($addressId);
- if (!$address || $address['user_id'] != $userId) {
- throw new BusinessException("地址未找到");
- }
- if (empty($goodsList)) {
- throw new BusinessException("商品列表不能为空");
- }
- // 验证商品列表格式
- self::validateGoodsList($goodsList);
- $orderTimeout = ShopConfigService::getConfigs('shop.order.order_timeout', false) ?? 3600;
- $platform = request()->header('platform', 'H5');
- $ip = request()->ip();
- $orders = [];
- $firstOrder = true;
- // 为每个商品创建单独的订单
- foreach ($goodsList as $goodsItem) {
- // 构建单商品列表
- $singleGoodsList = [$goodsItem];
-
- // 生成订单号
- $orderSn = date("Ymdhis") . sprintf("%08d", $userId) . mt_rand(1000, 9999);
- // 订单主表信息
- $orderInfo = array_merge(self::initOrderPrice(), [
- 'type' => 1, // 1:普通订单
- 'order_sn' => $orderSn,
- 'user_id' => $userId,
- 'profile_id' => $profileId, // 档案ID
- 'expire_time' => time() + $orderTimeout, // 过期时间
- 'order_status' => OrderEnum::STATUS_CREATE, // 待付款
- 'invoice_status' => 0, // 发票开具状态
- 'remark' => $remark, // 用户备注
- 'user_coupon_id' => $firstOrder && $userCouponId > 0 ? $userCouponId : null, // 优惠券仅用于第一个订单
- 'ip' => $ip,
- 'status' => 'normal',
- 'platform' => $platform,
- ]);
- try {
- // 通过单商品列表计算订单明细
- list($orderItem, $calculatedGoodsList, $userCoupon) = self::computeGoods(
- $orderInfo,
- $singleGoodsList,
- $userId,
- $address->area_id,
- $firstOrder && $userCouponId > 0 ? $userCouponId : 0
- );
- // 如果有活动折扣,记录活动信息
- if ($orderInfo['activity_discount_amount'] > 0) {
- $currentActivity = DiscountService::getCurrentActivity();
- if ($currentActivity) {
- $orderInfo['activity_type'] = 'discount';
- $orderInfo['activity_id'] = $currentActivity['id'];
- }
- }
- $orderInfo['pay_amount'] = max(0, bcsub($orderInfo['order_amount'], $orderInfo['discount_fee'], 2));
- $orderInfo['pay_original_amount'] = $orderInfo['pay_amount'];
- $orderInfo['pay_remain_amount'] = $orderInfo['pay_amount'];
- // 创建订单
- $order = self::createOrderWithTransaction($orderInfo, $orderItem, $calculatedGoodsList, $userCoupon, $address, $profileId);
- $orders[] = $order;
- // 第一个订单创建完成后,后续订单不再使用优惠券
- $firstOrder = false;
- } catch (Exception $e) {
- // 如果某个订单创建失败,记录错误并继续创建其他订单
- \think\Log::error("创建单商品订单失败: " . $e->getMessage() . ", 商品ID: " . $goodsItem['goods_id']);
- throw new BusinessException("创建订单失败: " . $e->getMessage());
- }
- }
- if (empty($orders)) {
- throw new BusinessException("所有订单创建失败");
- }
- return $orders;
- }
- /**
- * 在事务中创建订单
- * @param array $orderInfo 订单信息
- * @param array $orderItem 订单商品列表
- * @param array $goodsList 商品列表
- * @param object $userCoupon 优惠券
- * @param object $address 地址信息
- * @param int $profileId 档案ID
- * @return Order
- * @throws BusinessException
- */
- protected static function createOrderWithTransaction($orderInfo, $orderItem, $goodsList, $userCoupon, $address, $profileId = 0)
- {
- $order = null;
- Db::startTrans();
- try {
- // 创建订单
- $order = Order::create($orderInfo, true);
- // 为每个订单商品添加订单ID和订单号
- foreach ($orderItem as &$item) {
- $item['order_id'] = $order->id;
- $item['order_sn'] = $order->order_sn;
- // 移除临时字段
- unset($item['category_id'], $item['brand_id']);
- }
- unset($item);
- // 创建订单地址信息
- $orderAddressData = [
- 'order_id' => $order->id,
- 'user_id' => $orderInfo['user_id'],
- 'consignee' => $address->receiver,
- 'mobile' => $address->mobile,
- 'province_name' => $address->province->name ?? '',
- 'city_name' => $address->city->name ?? '',
- 'district_name' => $address->area->name ?? '',
- 'address' => $address->address,
- 'province_id' => $address->province_id,
- 'city_id' => $address->city_id,
- 'district_id' => $address->area_id,
- ];
- OrderAddress::create($orderAddressData);
-
- // 减库存
- foreach ($goodsList as $index => $item) {
- if ($item->sku) {
- $item->sku->setDec('stocks', $item->nums);
- }
- $item->goods->setDec("stocks", $item->nums);
- }
-
- // 计算单个商品折扣后的价格 (基于新字段名)
- $saleamount = bcsub($order['amount'], $order['express_fee'], 2);
- $saleratio = $order['goods_price'] > 0 ? bcdiv($saleamount, $order['goods_price'], 10) : 1;
- $saleremains = $saleamount;
-
- foreach ($orderItem as $index => &$item) {
- if (!isset($orderItem[$index + 1])) {
- $saleprice = $saleremains;
- } else {
- $saleprice = $order['discount_fee'] == 0 ? bcmul($item['goods_original_price'], $item['nums'], 2) : bcmul(bcmul($item['goods_original_price'], $item['nums'], 2), $saleratio, 2);
- }
- $saleremains = bcsub($saleremains, $saleprice, 2);
- $item['goods_price'] = $saleprice;
- }
- unset($item);
- // 批量创建订单商品数据
- if (!empty($orderItem)) {
- (new OrderGoods())->saveAll($orderItem);
- }
-
- // 修改地址使用次数
- if ($address) {
- $address->setInc('used_nums');
- }
-
- // 优惠券已使用
- if (!empty($userCoupon)) {
- $userCoupon->save(['is_used' => 2]);
- }
-
- // 创建档案详情记录
- if ($profileId > 0) {
- self::createOrderProfileRecord($order, $profileId);
- }
-
- // 提交事务
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- throw new BusinessException($e->getMessage());
- }
-
- // 记录操作
- OrderActionService::recordUserAction(
- $orderInfo['order_sn'],
- OrderActionEnum::ACTION_CREATE,
- $orderInfo['user_id'],
- '创建订单',
- $orderInfo['user_id']
- );
-
- // 订单应付金额为0时直接结算
- if ($order['amount'] == 0) {
- // Order::settle($order->order_sn, 0);
- // $order = Order::get($order->id);
- return $order;
- }
-
- return $order;
- }
- /**
- * 创建订单档案详情记录
- * @param Order $order 订单对象
- * @param int $profileId 档案ID
- * @throws BusinessException
- */
- protected static function createOrderProfileRecord($order, $profileId)
- {
- try {
- // 获取档案详情
- $profileDetail = BodyProfileService::getProfileDetail($profileId, $order->user_id);
- if (!$profileDetail) {
- throw new BusinessException("档案不存在或无权限访问");
- }
- // 准备档案详情记录数据
- $orderProfileData = [
- 'order_id' => $order->id,
- 'order_sn' => $order->order_sn,
- 'profile_id' => $profileId,
- 'user_id' => $order->user_id,
-
- // 档案基础信息快照
- 'profile_name' => $profileDetail['profile_name'] ?? '',
- 'relation' => $profileDetail['relation'] ?? '',
- 'gender' => $profileDetail['gender'] ?? 0,
- 'age' => $profileDetail['age'] ?? 0,
- 'height' => $profileDetail['height'] ?? 0.00,
- 'weight' => $profileDetail['weight'] ?? 0.00,
- 'profile_photo' => $profileDetail['profile_photo'] ?? '',
- 'body_photos' => isset($profileDetail['body_photos_text']) ? json_encode($profileDetail['body_photos_text'], JSON_UNESCAPED_UNICODE) : '',
- 'is_own' => $profileDetail['is_own'] ?? 1,
- ];
-
- // 测量数据快照(最新一次测量数据)
- if (isset($profileDetail['measurements']) && !empty($profileDetail['measurements'])) {
- $measurement = $profileDetail['measurements'];
- $orderProfileData = array_merge($orderProfileData, [
- 'measurement_id' => $measurement['id'] ?? 0,
- 'chest' => $measurement['chest'] ?? 0.00,
- 'waist' => $measurement['waist'] ?? 0.00,
- 'hip' => $measurement['hip'] ?? 0.00,
- 'thigh' => $measurement['thigh'] ?? 0.00,
- 'calf' => $measurement['calf'] ?? 0.00,
- 'upper_arm' => $measurement['upper_arm'] ?? 0.00,
- 'forearm' => $measurement['forearm'] ?? 0.00,
- 'neck' => $measurement['neck'] ?? 0.00,
- 'shoulder_width' => $measurement['shoulder_width'] ?? 0.00,
- 'bust' => $measurement['bust'] ?? 0.00,
- 'underbust' => $measurement['underbust'] ?? 0.00,
- 'inseam' => $measurement['inseam'] ?? 0.00,
- 'outseam' => $measurement['outseam'] ?? 0.00,
- 'knee' => $measurement['knee'] ?? 0.00,
- 'arm_length' => $measurement['arm_length'] ?? 0.00,
- 'wrist' => $measurement['wrist'] ?? 0.00,
- 'pants_length' => $measurement['pants_length'] ?? 0.00,
- 'belly_belt' => $measurement['belly_belt'] ?? 0.00,
- 'leg_root' => $measurement['leg_root'] ?? 0.00,
- 'ankle' => $measurement['ankle'] ?? 0.00,
- 'waist_lower' => $measurement['waist_lower'] ?? 0.00,
- 'mid_waist' => $measurement['mid_waist'] ?? 0.00,
- 'shoe_size' => $measurement['shoe_size'] ?? '',
- 'measurement_date' => $measurement['measurement_date'] ?? 0,
- ]);
- }
- // 体型选择快照
- if (isset($profileDetail['body_types']) && !empty($profileDetail['body_types'])) {
- $orderProfileData['body_types'] = json_encode($profileDetail['body_types'], JSON_UNESCAPED_UNICODE);
- }
- // AI报告快照(最新一次报告)
- if (isset($profileDetail['latest_ai_report']) && !empty($profileDetail['latest_ai_report'])) {
- $aiReport = $profileDetail['latest_ai_report'];
- $orderProfileData = array_merge($orderProfileData, [
- 'ai_report_id' => $aiReport['id'] ?? 0,
- 'ai_report_type' => $aiReport['report_type'] ?? '',
- 'ai_report_content' => isset($aiReport['report_content']) ? json_encode($aiReport['report_content'], JSON_UNESCAPED_UNICODE) : '',
- ]);
- }
- // 计算BMI
- $height = $orderProfileData['height'] ?? 0;
- $weight = $orderProfileData['weight'] ?? 0;
- if ($height > 0 && $weight > 0) {
- $heightInMeters = $height / 100; // 转换为米
- $bmi = round($weight / ($heightInMeters * $heightInMeters), 2);
- $orderProfileData['bmi'] = $bmi;
-
- // BMI等级判断
- if ($bmi < 18.5) {
- $orderProfileData['bmi_level'] = 'underweight';
- } elseif ($bmi < 24) {
- $orderProfileData['bmi_level'] = 'normal';
- } elseif ($bmi < 28) {
- $orderProfileData['bmi_level'] = 'overweight';
- } else {
- $orderProfileData['bmi_level'] = 'obese';
- }
- }
- // 创建档案详情记录
- OrderProfile::create($orderProfileData);
-
- } catch (Exception $e) {
- throw new BusinessException("创建档案详情记录失败: " . $e->getMessage());
- }
- }
- /**
- * 验证商品规格参数
- * @param array $goods_list 商品列表
- * @throws BusinessException
- */
- public static function validateGoodsList($goods_list)
- {
- if (empty($goods_list) || !is_array($goods_list)) {
- throw new BusinessException("商品列表不能为空");
- }
- foreach ($goods_list as $item) {
- if (!isset($item['goods_id']) || !is_numeric($item['goods_id']) || $item['goods_id'] <= 0) {
- throw new BusinessException("商品ID无效");
- }
-
- if (!isset($item['nums']) || !is_numeric($item['nums']) || $item['nums'] <= 0) {
- throw new BusinessException("商品数量必须大于0");
- }
-
- if (isset($item['goods_sku_id']) && !is_numeric($item['goods_sku_id'])) {
- throw new BusinessException("商品规格ID无效");
- }
- }
- }
- /**
- * 创建父子订单 - 每个商品一个子订单,统一支付父订单
- * @param int $addressId 地址ID
- * @param int $userId 用户ID
- * @param array $goodsList 商品列表
- * @param int $userCouponId 优惠券ID
- * @param string $remark 备注
- * @param int $profileId 档案ID
- * @return array 返回父订单和子订单信息
- * @throws BusinessException
- */
- public static function createParentChildOrders($addressId, $userId, $goodsList, $userCouponId = 0, $remark = '', $profileId = 0)
- {
- return ParentOrderService::createParentChildOrders($addressId, $userId, $goodsList, $userCouponId, $remark, $profileId);
- }
- /**
- * 统一的订单计算方法(用于预览订单)
- * @param array $goods_list 标准化的商品列表
- * @param int $user_id 用户ID
- * @param int $area_id 地区ID
- * @param int $user_coupon_id 优惠券ID
- * @return array
- * @throws BusinessException
- */
- public static function calculateOrder($goodsList, $userId, $areaId = 0, $userCouponId = 0)
- {
- if (empty($goodsList)) {
- throw new BusinessException("商品列表不能为空");
- }
-
- // 验证商品列表格式
- self::validateGoodsList($goodsList);
- // 订单基础信息(用于计算,不包含订单号)
- $orderInfo = self::initOrderPrice();
- // 计算商品明细
- list($orderItem, $calculatedGoodsList, $userCoupon) = self::computeGoods($orderInfo, $goodsList, $userId, $areaId, $userCouponId);
- return [
- 'orderItem' => $orderItem,
- 'goodsList' => $calculatedGoodsList,
- 'orderInfo' => $orderInfo,
- 'userCoupon' => $userCoupon
- ];
- }
- /**
- * 订单列表
- *
- * @param $param
- * @return \think\Paginator
- */
- public static function getOrderList($userId = 0, $param =[],$status = [],$supplierId = 0)
- {
- $pageSize = 10;
- if (!empty($param['pageSize'])) {
- $pageSize = $param['pageSize'];
- }
- $orderModel = new Order();
- return $orderModel->with(['orderGoods'])
- ->where(function ($query) use ($param,$userId,$status) {
- if (!empty($userId)) {
- $query->where('user_id', $userId);
- }
-
- if (!empty($status)) {
- $query->whereIn('order_status', $status );
- }
-
- if (isset($param['keywords']) && $param['keywords'] != '') {
- $query->where('order_sn', 'in', function ($query) use ($param) {
- return $query->name('shop_order_goods')->where('order_sn|goods_title', 'like', '%' . $param['q'] . '%')->field('order_sn');
- });
- }
- if (!empty($supplierId)) {
- $query->where('order_sn', 'in', function ($query) use ($supplierId) {
- return $query->name('shop_order_goods')->where('supplier_id', $supplierId)->field('order_sn');
- });
- }
- })
- ->order('createtime desc')
- ->paginate($pageSize, false, ['query' => request()->get()]);
- }
- /**
- *
- * @ 订单信息
- * @param $orderId
- * @param $userId
- * @return array|false|\PDOStatement|string|Model
- */
- public static function getDetail($orderId = 0, $userId = 0)
- {
- $orderModel = new Order();
- return $orderModel->with(['orderGoods'])
- ->where('id', $orderId)
- ->where(function($query) use ($userId){
- if($userId > 0){
- $query->where('user_id', $userId);
- }
- })
- ->find();
- }
- public static function getDetailByOrderSn($orderSn)
- {
- $orderModel = new Order();
- return $orderModel->with(['orderGoods'])
- ->where('order_sn', $orderSn)
- ->find();
- }
- // 查询地址信息
- public static function getAddressInfo($orderId)
- {
- return OrderAddress::where('order_id', $orderId)->find();
- }
-
- /**
- * 判断订单是否失效
- * @param $order_sn
- * @return bool
- */
- public static function isExpired($orderSn)
- {
- $orderInfo = self::getByOrderSn($orderSn);
- //订单过期
- if (!$orderInfo['orderstate'] && !$orderInfo['paystate'] && time() > $orderInfo['expiretime']) {
- // 启动事务
- Db::startTrans();
- try {
- $orderInfo->save(['orderstate' => 2]);
- //库存恢复
- OrderGoods::setGoodsStocksInc($orderInfo->order_sn);
- // //恢复优惠券
- // UserCoupon::resetUserCoupon($orderInfo->user_coupon_id, $orderInfo->order_sn);
- // 提交事务
- Db::commit();
- } catch (\Exception $e) {
- // 回滚事务
- Db::rollback();
- }
- return true;
- }
- return false;
- }
- public static function getByOrderSn($orderSn)
- {
- return Order::where('order_sn', $orderSn)->find();
- }
- public static function getByOrderId($orderId)
- {
- return Order::where('id', $orderId)->find();
- }
- // 获取状态订单统计
- public static function getOrderStatusCount($userId = 0)
- {
- $info = [];
- $info['unpay'] = Order::where('user_id', $userId)->where('order_status',OrderEnum::STATUS_CREATE)->count();
- $info['unsend'] = Order::where('user_id', $userId)->where('order_status',OrderEnum::STATUS_INSPECTION_PASS)->count();
- $info['unrec'] = Order::where('user_id', $userId)->where('order_status',OrderEnum::STATUS_SHIP)->count();
- $info['uneva'] = Order::where('user_id', $userId)->where('order_status',OrderEnum::STATUS_CONFIRM)->count();
- $info['inspect'] = Order::where('user_id', $userId)
- ->whereIn('order_status',[
- OrderEnum::STATUS_PAY,
- OrderEnum::STATUS_INSPECTION,
- OrderEnum::STATUS_INSPECTION_FAIL])
- ->count();
-
- return $info;
- }
- // 更新订单状态
- public static function updateOrderStatus($orderId = 0, $userId = 0, $status = 0)
- {
-
- $order = self::getDetail($orderId, $userId);
- if (!$order) {
- throw new BusinessException('订单不存在!');
- }
- // 验证状态
- // if (!OrderEnum::isValidOrderStatus($status)) {
- // throw new BusinessException('状态不合法!');
- // }
- // 要处理每个状态对应的时间字段在枚举类中
- $timeField = OrderEnum::STATUS_TIME_MAP[$status];
- $updateData = [
- 'order_status' => $status,
- $timeField => time()
- ];
- Order::where('id', $orderId)->update($updateData);
- return $order;
- }
- /**
- * 获取供应商订单列表 - 关联查询方式
- * @param int $supplierId 供应商ID
- * @param int $userId 用户ID(可选)
- * @param array $param 查询参数
- * @param array $status 订单状态筛选
- * @return \think\Paginator
- */
- public static function getSupplierOrderList($supplierId, $userId = 0, $param = [], $status = [])
- {
- $pageSize = $param['page_size'] ?? 10;
-
- return Order::alias('o')
- ->join('shop_order_goods og', 'o.order_sn = og.order_sn', 'inner')
- ->with(['orderGoods' => function($query) use ($supplierId) {
- $query->where('supplier_id', $supplierId);
- }])
- ->where('og.supplier_id', $supplierId)
- ->where(function ($query) use ($param, $userId, $status) {
- if (!empty($userId)) {
- $query->where('o.user_id', $userId);
- }
-
- if (!empty($status)) {
- $query->whereIn('o.order_status', $status);
- }
-
- if (isset($param['keywords']) && $param['keywords'] != '') {
- $query->where(function($subQuery) use ($param) {
- $subQuery->where('o.order_sn', 'like', '%' . $param['keywords'] . '%')
- ->whereOr('og.goods_title', 'like', '%' . $param['keywords'] . '%');
- });
- }
- })
- ->field('o.*')
- ->group('o.id')
- ->order('o.createtime desc')
- ->paginate($pageSize, false, ['query' => request()->get()]);
- }
- /**
- * 获取供应商订单详情 - 支持订单ID和订单号两种方式
- * @param mixed $orderParam 订单ID或订单号
- * @param int $supplierId 供应商ID
- * @param int $userId 用户ID(可选,用于权限验证)
- * @return array|null
- */
- public static function getSupplierOrderDetail($orderParam, $supplierId, $userId = 0)
- {
- // 根据参数类型判断是订单ID还是订单号
- if (is_numeric($orderParam)) {
- // 数字类型,当作订单ID处理
- $query = Order::where('id', $orderParam);
- } else {
- // 字符串类型,当作订单号处理
- $query = Order::where('order_sn', $orderParam);
- }
-
- // 如果指定了用户ID,添加用户权限验证
- if (!empty($userId)) {
- $query->where('user_id', $userId);
- }
-
- $order = $query->find();
- if (!$order) {
- return null;
- }
-
- // 获取该供应商在此订单中的商品
- $orderGoods = OrderGoods::where('order_sn', $order->order_sn)
- ->where('supplier_id', $supplierId)
- ->select();
-
- // 如果该供应商在此订单中没有商品,返回null
- if (empty($orderGoods)) {
- return null;
- }
-
- $order->order_goods = $orderGoods;
- return $order;
- }
- /**
- * 获取供应商订单统计
- * @param int $supplierId 供应商ID
- * @param int $userId 用户ID(可选)
- * @return array
- */
- public static function getSupplierOrderStatusCount($supplierId, $userId = 0)
- {
- $baseQuery = function($status) use ($supplierId, $userId) {
- $query = Order::alias('o')
- ->join('shop_order_goods og', 'o.order_sn = og.order_sn', 'inner')
- ->where('og.supplier_id', $supplierId)
- ->where('o.order_status', $status);
-
- if (!empty($userId)) {
- $query->where('o.user_id', $userId);
- }
-
- return $query->count('DISTINCT o.id');
- };
-
- return [
- 'unpay' => $baseQuery(OrderEnum::STATUS_CREATE), // 待付款
- 'unsend' => $baseQuery(OrderEnum::STATUS_PAY), // 待发货
- 'unrec' => $baseQuery(OrderEnum::STATUS_SHIP), // 待收货
- 'uneva' => $baseQuery(OrderEnum::STATUS_CONFIRM), // 待评价
- ];
- }
- /**
- * 验证订单和订单商品是否存在
- * @param int $orderId 订单ID
- * @param int $orderGoodsId 订单商品ID
- * @return array|false 返回订单和订单商品信息,不存在返回false
- */
- public static function validateOrderAndOrderGoods($orderId, $orderGoodsId)
- {
- // 查询订单
- $order = Order::where('id', $orderId)->find();
- if (!$order) {
- return false;
- }
-
- // 查询订单商品
- $orderGoods = OrderGoods::where('id', $orderGoodsId)
- ->where('order_id', $orderId)
- ->find();
- if (!$orderGoods) {
- return false;
- }
-
- return [
- 'order' => $order,
- 'order_goods' => $orderGoods
- ];
- }
- /**
- * 更新订单商品的验收状态
- * @param int $orderGoodsId 订单商品ID
- * @param int $inspectStatus 验收状态 0:待验收 1:验收通过 2:验收不通过
- * @return bool
- */
- public static function updateOrderGoodsInspectStatus($orderGoodsId, $inspectStatus,$inspectUid = 0)
- {
- return OrderGoods::where('id', $orderGoodsId)->update([
- 'inspect_status' => $inspectStatus,
- 'inspect_time' => time(),
- 'inspect_id' => $inspectUid
- ]);
- }
- /**
- * 检查订单中所有商品的验收状态
- * @param int $orderId 订单ID
- * @return array 返回验收状态统计
- */
- public static function checkOrderInspectStatus($orderId)
- {
- $orderGoods = OrderGoods::where('order_id', $orderId)->select();
-
- $statusCount = [
- 'total' => count($orderGoods), // 总商品数
- 'pending' => 0, // 待验收
- 'passed' => 0, // 验收通过
- 'failed' => 0 // 验收不通过
- ];
-
- foreach ($orderGoods as $goods) {
- switch ($goods['inspect_status']) {
- case 0:
- $statusCount['pending']++;
- break;
- case 1:
- $statusCount['passed']++;
- break;
- case 2:
- $statusCount['failed']++;
- break;
- }
- }
-
- return $statusCount;
- }
- /**
- * 根据验收状态决定是否更新订单状态
- * @param int $orderId 订单ID
- * @param int $userId 用户ID
- * @return bool
- */
- public static function updateOrderStatusByInspectResult($orderId, $userId = 0)
- {
- $inspectStatus = self::checkOrderInspectStatus($orderId);
-
- // 如果所有商品都验收完成(没有待验收的商品)
- if ($inspectStatus['pending'] == 0) {
- // 如果所有商品都验收通过
- if ($inspectStatus['failed'] == 0 && $inspectStatus['passed'] > 0) {
- // 更新订单状态为验收通过
- return self::updateOrderStatus($orderId, $userId, OrderEnum::STATUS_INSPECTION_PASS);
- }
- // 如果所有商品都验收不通过
- elseif ($inspectStatus['failed'] > 0 && $inspectStatus['passed'] == 0) {
- // 更新订单状态为验收失败
- return self::updateOrderStatus($orderId, $userId, OrderEnum::STATUS_INSPECTION_FAIL);
- }
- // 如果部分商品验收通过,部分不通过
- elseif ($inspectStatus['failed'] > 0 && $inspectStatus['passed'] > 0) {
- // 混合状态,由业务人员手动处理,不自动更新订单状态
- return true;
- }
- }
-
- return true;
- }
- /**
- * 更新订单商品的发货状态
- * @param int $orderGoodsId 订单商品ID
- * @param string $expressName 快递公司
- * @param string $expressNo 快递单号
- * @param array $expressImage 快递图片
- * @return bool
- */
- public static function updateOrderGoodsDeliveryStatus($orderGoodsId, $expressName, $expressNo, $expressImage = [],$orderExpressId = 0)
- {
- $updateData = [
- 'express_name' => $expressName,
- 'express_no' => $expressNo,
- 'express_image' => is_array($expressImage) ? json_encode($expressImage) : $expressImage,
- 'delivery_status' => 1, // 已发货
- 'updatetime' => time(),
- 'order_express_id' =>$orderExpressId
- ];
-
- return OrderGoods::where('id', $orderGoodsId)->update($updateData);
- }
- /**
- * 检查订单中所有商品的发货状态
- * @param int $orderId 订单ID
- * @return array 返回发货状态统计
- */
- public static function checkOrderDeliveryStatus($orderId)
- {
- $orderGoods = OrderGoods::where('order_id', $orderId)->select();
-
- $statusCount = [
- 'total' => count($orderGoods), // 总商品数
- 'pending' => 0, // 待发货
- 'delivered' => 0, // 已发货
- ];
-
- foreach ($orderGoods as $goods) {
- if ($goods['delivery_status'] == 1) {
- $statusCount['delivered']++;
- } else {
- $statusCount['pending']++;
- }
- }
-
- return $statusCount;
- }
- /**
- * 根据发货状态决定是否更新订单状态
- * @param int $orderId 订单ID
- * @param int $userId 用户ID
- * @return bool
- */
- public static function updateOrderStatusByDeliveryResult($orderId, $userId = 0)
- {
- $deliveryStatus = self::checkOrderDeliveryStatus($orderId);
-
- // 如果所有商品都已发货(没有待发货的商品)
- if ($deliveryStatus['pending'] == 0 && $deliveryStatus['delivered'] > 0) {
- // 更新订单状态为已发货
- return self::updateOrderStatus($orderId, $userId, OrderEnum::STATUS_SHIP);
- }
-
- return true;
- }
- /**
- * 验证订单商品是否可以发货
- * @param int $orderId 订单ID
- * @param int $orderGoodsId 订单商品ID
- * @return array|false 返回订单和订单商品信息,验证失败返回false
- */
- public static function validateOrderGoodsForDelivery($orderId, $orderGoodsId)
- {
- // 验证订单和订单商品是否存在
- $orderData = self::validateOrderAndOrderGoods($orderId, $orderGoodsId);
- if (!$orderData) {
- return false;
- }
-
- $orderGoods = $orderData['order_goods'];
- // 检查订单商品的状态是都验货通过才可以发货
- // if ($orderGoods['inspect_status'] !== 1) {
- // return false;
- // }
- if (empty($orderGoods['inspect_time'])) {
- return false;
- }
- // 检查该订单商品是否已经发货
- if ($orderGoods['delivery_status'] == 1) {
- return false;
- }
-
- return $orderData;
- }
- }
|