findWhereBy(['uid' => $data['address_uid'], 'user_uid' => $userUid], ['name', 'phone', 'address', 'province', 'city', 'region']); if (empty($addressInfo)) { logger('错误的收货地址:' . $userUid, $data); return false; } $goodsInfo = make(OriginalRepository::class)->findWhereBy(['uid' => $data['uid'], 'status' => 2]); if (empty($goodsInfo)) { logger('商品已下架:' . $userUid, $data); return false; } if ($goodsInfo['stock'] < $data['number']) { logger('商品已经没有库存:' . $userUid, $data); return false; } $userInfo = make(UserRepository::class)->getUserInfoFromCache($userUid); if ($userInfo['is_vip'] == 'y') { $coins = $goodsInfo->member_coins * $data['number']; } else { $coins = $goodsInfo->coins * $data['number']; } $orderSn = make(CommonService::class)->createOrderSn('CJYW'); $orderData = [ 'order_sn' => $orderSn, 'buy_uid' => $userUid, 'sell_uid' => $goodsInfo->shops_uid, 'goods_uid' => $goodsInfo->uid, 'goods_name' => $goodsInfo->title, 'goods_cover' => $goodsInfo->cover, 'goods_amount' => $data['number'], 'original_amount' => $goodsInfo->original_cions, 'sell_amount' => $goodsInfo->coins, 'actual_amount' => $coins, 'clinch_amount' => $coins, 'order_status' => 1, 'pay_status' => 1, 'redund_status' => 1, 'message' => $data['message'], 'address' => json_encode($addressInfo->toArray(), true) ]; $res = $this->create($orderData); if ($res) { return $orderSn; } else { logger('创建订单失败:' . $userUid, $orderData); return false; } } /** * 购买商品 * * @param $orderSn * @param $userUid * @param $price * @return bool */ public function buyGoods($orderSn, $userUid) { try { $orderInfo = OriginalOrderModel::where(['order_sn' => $orderSn, 'buy_uid' => $userUid])->where('pay_status', 1)->first(); if (empty($orderInfo)) { logger('购买订单异常->'. $orderSn); return false; } Db::beginTransaction(); $res1 = UserModel::query()->where('code', $userUid)->decrement('coins', $orderInfo->clinch_amount); if (!$res1) { logger('购买订单失败1', $orderInfo); Db::rollBack(); return false; } $orderInfo->order_status = 2; $orderInfo->pay_status = 3; $orderInfo->pay_time = date('Y-m-d H:i:s'); if (!$orderInfo->save()) { logger('购买订单失败2'); Db::rollBack(); return false; } $userInfo = make(UserRepository::class)->getUserInfoFromCache($userUid); $chageData = [ 'user_uid' => $userUid, 'way' => ChangeLogConstant::WAY_2, 'currency_type' => ChangeLogConstant::CHANGE_COIN, 'type' => ChangeLogConstant::TYPE_7, 'amount' => $orderInfo->clinch_amount, 'before_amount' => $userInfo['coins'], 'after_amount' => $userInfo['coins'] - $orderInfo->clinch_amount, 'remark' => '购买商品->' . $orderSn, ]; $res3 = ChangeLogModel::create($chageData); if (!$res3) { logger('购买订单失败3'); Db::rollBack(); return false; } // 店铺加钱 $shopInfo = ShopsModel::where('uid', $orderInfo->sell_uid)->first(['frozen_amount', 'amount']); // 加冻结金额 $res4 = ShopsModel::where('uid', $orderInfo->sell_uid)->increment('frozen_amount', $orderInfo->clinch_amount); $goodInfo = GoodsModel::where('uid', $orderInfo->goods_uid)->first(); $goodInfo->stock -= $orderInfo->goods_amount; // 扣库存 $goodInfo->actual_number_orders += 1; // 加真实订单数 $res5 = $goodInfo->save(); if (!$res4 || !$res5) { logger('购买订单失败4'); Db::rollBack(); return false; } // 店铺帐变记录 $chageShopData = [ 'user_uid' => $orderInfo->sell_uid, 'way' => ChangeLogConstant::WAY_3, 'currency_type' => ChangeLogConstant::CHANGE_COIN, 'type' => ChangeLogConstant::TYPE_16, 'amount' => $orderInfo->clinch_amount, 'before_amount' => $shopInfo->frozen_amount, 'after_amount' => $shopInfo->frozen_amount + $orderInfo->clinch_amount, 'remark' => '店铺入账冻结金额变动->' . $orderSn, ]; $res6 = ChangeLogModel::create($chageShopData); if (!$res6) { logger('购买订单失败5'); Db::rollBack(); return false; } make(UserRepository::class)->delUserCache($userUid); Db::commit(); $sendData = [ 'routes' => 'api/tg/send/chat/message', 'content' => [ 'dispatcher' => $userUid, 'recipient' => $orderInfo->goods_uid, 'content' => '有用户购买商品(' . $orderInfo->goods_name . ')啦,请查看订单啦', ] ]; make(QueueService::class)->publicSendTgMegJob($sendData); return true; } catch (\Exception $exception) { Db::rollBack(); logger('购买异常:' . $exception->getMessage()); return false; } } /** * 确认收货 * * @param $data * @param $coins (用户确认收货会奖金币,自动收货则不会) * @return bool */ public function confirmReceipt($data, $coins = 0) { try { Db::beginTransaction(); $orderInfo = OriginalOrderModel::where(['order_sn' => $data['order_sn'], 'buy_uid' => $data['buy_uid'], 'order_status' => 3])->first(); if (empty($orderInfo)) { logger('确认的订单异常'. $data['order_sn']); Db::rollBack(); return false; } $orderInfo->order_status = 4; $orderInfo->receipt_time = date('Y-m-d H:i:s'); if (!$orderInfo->save()) { logger('订单状态修改异常', $data); Db::rollBack(); return false; } $shopInfo = ShopsModel::query()->where('uid', $orderInfo['sell_uid'])->first(); $shopOldFrozenAmount = $shopInfo->frozen_amount; $shopInfo->frozen_amount -= $orderInfo->clinch_amount; $shopInfo->amount += $orderInfo->clinch_amount; $shopInfo->credit_value += $data['comment_fraction'] ?? 0; $chageShopData = [ 'user_uid' => $orderInfo['sell_uid'], 'way' => ChangeLogConstant::WAY_4, 'currency_type' => ChangeLogConstant::CHANGE_COIN, 'type' => ChangeLogConstant::TYPE_17, 'amount' => $orderInfo->clinch_amount, 'before_amount' => $shopOldFrozenAmount, 'after_amount' => $shopOldFrozenAmount - $orderInfo->clinch_amount, 'remark' => '店铺入账解冻金额变动->' . $data['order_sn'], ]; $res = ChangeLogModel::create($chageShopData); if (!$shopInfo->save() || !$res) { logger('收货金额变动异常', $data); Db::rollBack(); return false; } $res1 = GoodsAssessModel::query()->create($data); if (!$res1) { logger('评论添加异常', $data); Db::rollBack(); return false; } $sellUserInfo = UserModel::query()->where('code', $orderInfo->sell_uid)->first(); $userAmount = ($orderInfo->clinch_amount / 10) * ($shopInfo->share_ratio / 100); // 店家账户金额 $userBeforeAmount = $sellUserInfo->balance; $sellUserInfo->balance += $userAmount; $userRes = $sellUserInfo->save(); $chageUserData = [ 'user_uid' => $orderInfo->sell_uid, 'way' => ChangeLogConstant::WAY_4, 'currency_type' => ChangeLogConstant::CHANGE_AMOUNT, 'type' => ChangeLogConstant::TYPE_15, 'amount' => $userAmount, 'before_amount' => $userBeforeAmount, 'after_amount' => $userBeforeAmount + $userAmount, 'remark' => '确认收货店家收款->收款金币' . $orderInfo->clinch_amount . '收款金额:' . $userAmount . '分成比例为:' . $shopInfo->share_ratio, ]; $userChageRes = ChangeLogModel::create($chageUserData); if (!$userRes || !$userChageRes) { logger('店家加钱失败', $data); Db::rollBack(); return false; } //评论+1 $res5 = GoodsModel::query()->where('uid', $data['goods_uid'])->increment('appraise'); if (!$res5) { logger('评论次数添加失败', $data); Db::rollBack(); return false; } // 如果有金币则奖励 if ($coins > 0) { $buyUserInfo = UserModel::query()->where('code', $data['buy_uid'])->first(); $oldCoins = $buyUserInfo->coins; $buyUserInfo->coins += $coins; $res2 = $buyUserInfo->save(); // $res2 = UserModel::query()->where('code', $data['buy_uid'])->increment('coins', $coins); // $userInfo = make(UserRepository::class)->getUserInfoFromCache($data['buy_uid']); $chageData = [ 'user_uid' => $data['buy_uid'], 'way' => ChangeLogConstant::WAY_1, 'type' => ChangeLogConstant::TYPE_8, 'currency_type' => ChangeLogConstant::CHANGE_COIN, 'amount' => $coins, 'before_amount' => $oldCoins, 'after_amount' => $oldCoins + $coins, 'remark' => '评论奖励', ]; $res3 = ChangeLogModel::create($chageData); if (!$res2 || !$res3) { logger('评论送金币异常', $data); Db::rollBack(); return false; } make(UserRepository::class)->delUserCache($data['buy_uid']); } Db::commit(); return true; } catch (\Exception $exception) { Db::rollBack(); logger('确认收货异常:' . $exception->getMessage()); return false; } } /** * 退款业务 * * @param array $orderInfo * @return bool */ public function refund(array $orderInfo) { try { Db::beginTransaction(); // 扣除冻结金额 $shopInfo = ShopsModel::query()->where('uid', $orderInfo['sell_uid'])->first(); $oldFrozenAmount = $shopInfo->frozen_amount; $shopInfo->frozen_amount -= $orderInfo['clinch_amount']; $res = $shopInfo->save(); $chageData = [ 'user_uid' => $orderInfo['sell_uid'], 'way' => ChangeLogConstant::WAY_4, 'type' => ChangeLogConstant::TYPE_13, 'currency_type' => ChangeLogConstant::CHANGE_COIN, 'amount' => $orderInfo['clinch_amount'], 'before_amount' => $oldFrozenAmount, 'after_amount' => $oldFrozenAmount - $orderInfo['clinch_amount'], 'remark' => '退款金额解冻给用户', ]; $res1 = ChangeLogModel::create($chageData); if (!$res || !$res1) { logger('退款解冻失败', $orderInfo); Db::rollBack(); return false; } $userInfo = UserModel::query()->where('code', $orderInfo['buy_uid'])->first(); $userCoins = $userInfo->coins; $userInfo->coins += $orderInfo['clinch_amount']; $res2 = $userInfo->save(); $userChageData = [ 'user_uid' => $orderInfo['buy_uid'], 'way' => ChangeLogConstant::WAY_1, 'type' => ChangeLogConstant::TYPE_14, 'currency_type' => ChangeLogConstant::CHANGE_COIN, 'amount' => $orderInfo['clinch_amount'], 'before_amount' => $userCoins, 'after_amount' => $userCoins + $orderInfo['clinch_amount'], 'remark' => '获取申请退款的金额', ]; $res3 = ChangeLogModel::create($userChageData); if (!$res2 || !$res3) { logger('退款解冻失败', $orderInfo); Db::rollBack(); return false; } Db::commit(); make(UserRepository::class)->delUserCache($orderInfo['buy_uid']); return true; } catch (\Exception $exception) { logger('退款解冻失败', [$exception->getMessage()]); Db::rollBack(); return false; } } }