request->post(); $validate = \think\Loader::validate('Order'); if(!$validate->scene('add')->check($params)) $this->error($validate->getError()); $order_info = $productPopular->where('id', $params['order_id'])->find(); if(empty($order_info)) $this->error(__("参数有误,无可用产品")); $areaArr = ($params['type'] == 1)? explode(',', $params['area_id']): ''; $areaNum = ($params['type'] == 1)? count($areaArr): $params['num']; if(($order_info->num +$order_info->init_num+ $areaNum) > $order_info->stock) $this->error(__("库存不足")); $result = false; Db::startTrans(); try { $amount = $ledgerWalletModel::getWalletChaBao($this->auth->id); $totalPrice = bcmul($order_info['price'], $areaNum, 2); if(bccomp($totalPrice, $amount, 2) > 0) throw new Exception(__("余额不足请前往充值"), 15001); if($order_info->start_time > time()) throw new Exception(__("抢购未开始")); if($order_info->stock == 0 || time() >= $order_info->end_time) throw new Exception(__("抢购已结束")); //批量地区添加 1选择地区 2未选择地区 if($params['type'] == 1) $result = $productOrder::setPopularAreaOrder($areaArr, $params['order_id'], $order_info->price, $params['product_id'], $this->auth->id); else $result =$productOrder::setPopularNoAreaOrder($areaNum, $params['order_id'], $order_info->price, $params['product_id'], $this->auth->id); //余额记录 $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$totalPrice, $ledgerWalletModel::Popular, $this->auth->id); //直推收益: pv* ×10% if($order_info['pv'] > 0 && $this->auth->parent_id > 0 && $userModel::getUserRwaNum($this->auth->parent_id) > 0){ $pv = bcmul(($order_info['pv'] * $areaNum), getConfig('pv_rate'), 2); if($pv > 0) $ledgerWalletModel->changeWalletAccount($this->auth->parent_id, Asset::TOKEN, $pv, $ledgerWalletModel::Direct, $this->auth->id); //社区奖励 $pvs = bcmul(($order_info['pv'] * $areaNum), config('community_ratio'), 2); if($pvs > 0)$userModel::setCommunityRewards($this->auth->id, $pvs, Asset::TOKEN); } //更新Rwa持有数量 if($order_info->price > config('min_rwa_price')) $userModel::updateForRwaNum($this->auth->id, $this->auth->parent_id, $areaNum, '+'); //扣除库存 if(($order_info->stock - $areaNum) == 0 || time() >= $order_info->end_time) $order_info->status= $productPopular::STOP; $order_info->num += $areaNum; $order_info->save(); if (false === $result) $this->error(__('No rows were updated')); // 提交事务 Db::commit(); } catch (Exception $e) { // 回滚事务 Db::rollback(); $this->error($e->getMessage(), null, $e->getCode()); } $this->success('ok'); } /** * 提货订单 */ public function pickupOrder(UserArea $userArea, ProductOrder $productOrder, UserModel $userModel, LedgerWalletModel $ledgerWalletModel) { $params = $this->request->post(); $validate = \think\Loader::validate('Order'); if(!$validate->scene('pick')->check($params)) $this->error($validate->getError()); $order_info = $productOrder->where('id', $params['order_id'])->find(); if(empty($order_info)) $this->error( __("参数有误,无可用产品")); // 启动事务 Db::startTrans(); try { $freight= getConfig('logistics_freight'); $amount = $ledgerWalletModel::getWalletChaBao($this->auth->id); if(bccomp($freight, $amount, 2) > 0) throw new Exception(__("余额不足请前往充值"), 15001); // 生成订单 $userArea->create(['name'=>$params['name'], 'type_id'=>$userArea::TakeAdders, 'phone'=>$params['phone'],'address'=>$params['address'],'order_id'=>$params['order_id']]); //扣除Rwa数量 if($order_info->popular_price > config('min_rwa_price')) $userModel::updateForRwaNum($order_info->user_id, $userModel::getByParentId($order_info->user_id), 1, '-'); //扣除运费 $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$freight, $ledgerWalletModel::Freight, $this->auth->id); $order_info->status= $productOrder::Shipped; $order_info->save(); // 提交事务 Db::commit(); } catch (Exception $e) { // 回滚事务 Db::rollback(); $this->error( $e->getMessage()); } $this->success('ok'); } /** * 订单转让 * @return void */ public function transfer(ProductOrder $productOrder, ProductTransfer $productTransfer, ProductPopular $productPopular) { $params = $this->request->post(); $validate = \think\Loader::validate('Order'); if(!$validate->scene('tran')->check($params)) $this->error($validate->getError()); //启动事务 Db::startTrans(); try { $order_info = $productOrder->where('id', $params['order_id'])->where('status', $productOrder::Paid)->find(); if(empty($order_info)) throw new Exception(__("订单不存在")); $min_transfer = $productPopular::where('id', $order_info->order_id)->value('min_transfer'); if(bccomp($params['price'],$min_transfer, 2) < 0) throw new Exception(__("当前订单最低转让金额为").$min_transfer); //转让订单 $fee = getConfig('transfer_fee'); $feeAmount = bcmul($params['price'], $fee, 2) ; $productTransfer::setTransferOrder($this->auth->id, $order_info['product_id'], $order_info['area_id'], $feeAmount, $params); //修改 类型状态 $order_info->type_id = $productOrder::Transfer; $order_info->status = $productOrder::Transferred; $order_info->save(); Db::commit(); } catch (Exception $e) { Db::rollback(); $this->error($e->getMessage()); } $this->success('ok'); } /** * 转让订单购买 * @return void */ public function transferOrder(ProductOrder $productOrder, ProductTransfer $productTransfer, LedgerWalletModel $ledgerWalletModel, UserModel $userModel) { $params = $this->request->post(); $validate = \think\Loader::validate('Order'); if(!$validate->scene('out')->check($params)) $this->error($validate->getError()); //启动事务 Db::startTrans(); try { $order_info = $productTransfer->where('id', $params['order_id'])->where('status', $productTransfer::NORMAL)->find(); if(empty($order_info)) throw new Exception(__("订单不存在")); if($order_info['user_id'] == $this->auth->id) throw new Exception(__("不能购买自己寄售的商品")); $chabao = $ledgerWalletModel::getWalletChaBao($this->auth->id); if(bccomp($order_info['price'], $chabao, 2) > 0) throw new Exception(__("余额不足请前往充值"), 15001); //抢购订单 $popular_order = $productOrder->where('id', $order_info['order_id'])->find(); // 生成订单 $productOrder::setCreateOrder($params['order_id'], $order_info, $productOrder::Transfer, $this->auth->id, $order_info['user_id'], $popular_order->order_no, $order_info['fees'], $popular_order->popular_price); //扣除余额记录 $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$order_info['price'], $ledgerWalletModel::Payment, $order_info['user_id']); //扣除Rwa有效 if($popular_order->popular_price > config('min_rwa_price')) $userModel::updateForRwaNum($order_info['user_id'], $userModel::getByParentId($order_info['user_id']), 1, '-'); //增加转让人余额 $amount = bcsub($order_info['price'], $order_info['fees'], 2); $ledgerWalletModel->changeWalletAccount($order_info['user_id'], Asset::TOKEN, $amount, $ledgerWalletModel::Receive, $this->auth->id); //增加Rwa有效 if($popular_order->popular_price > config('min_rwa_price')) $userModel::updateForRwaNum($this->auth->id, $this->auth->parent_id, 1, '+'); //修改原订单状态 $popular_order->status=$productOrder::Closure; $popular_order->save(); //修改状态 $order_info->status = $productTransfer::STOP; $order_info->save(); Db::commit(); } catch (Exception $e) { Db::rollback(); $this->error($e->getMessage(), null, $e->getCode()); } $this->success('ok'); } //赠送 public function giveaway(ProductOrder $productOrder, UserModel $userModel, LedgerWalletModel $ledgerWalletModel) { $params = $this->request->post(); $validate = \think\Loader::validate('Order'); if(!$validate->scene('giv')->check($params)) $this->error($validate->getError()); // 启动事务 Db::startTrans(); try { $order_info = $productOrder->where('id', $params['order_id'])->where('status', $productOrder::Paid)->find(); if(empty($order_info)) throw new Exception(__("参数有误,无可用产品")); $user = $userModel->getByAddress($params['address']); if(empty($user)) throw new Exception(__("赠送用户不存在")); if($user['id'] == $order_info['user_id']) throw new Exception(__("赠送用户不能是自己")); $chabao = $ledgerWalletModel::getWalletChaBao($this->auth->id); $fees = bcmul($order_info['price'], getConfig('giveaway'), 2); if(bccomp($fees, $chabao, 2) > 0) throw new Exception(__("余额不足请前往充值"), 15001); //添加记录 $productOrder::setCreateOrder($params['order_id'], $order_info, $productOrder::Giveaway, $user['id'], $this->auth->id, $order_info->order_no, $fees, $order_info->popular_price); //对方Rwa+1 if($order_info->popular_price > config('min_rwa_price')) $userModel::updateForRwaNum($user['id'], $userModel::getByParentId($user['id']), 1, '+'); //扣除手续费 $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$fees, $ledgerWalletModel::Giveaway, $user['id']); //扣除Rwa有效-1 if($order_info->popular_price > config('min_rwa_price')) $userModel::updateForRwaNum($this->auth->id, $this->auth->parent_id, 1, '-'); //修改:类型状态 $order_info->type_id= $productOrder::Giveaway; $order_info->status = $productOrder::Closure; $order_info->save(); // 提交事务 Db::commit(); } catch (Exception $e) { // 回滚事务 Db::rollback(); $this->error( $e->getMessage(), null, $e->getCode()); } $this->success('ok'); } //取消转让 public function cancel(ProductOrder $productOrder, ProductTransfer $productTransfer) { $params = $this->request->post(); $validate = \think\Loader::validate('Order'); if(!$validate->scene('out')->check($params)) $this->error($validate->getError()); // 启动事务 Db::startTrans(); try { $order_info = $productOrder->where('id', $params['order_id'])->find(); if(empty($order_info)) throw new Exception(__("参数有误,无可用产品")); //转让列表取消 $productTransfer::where('order_id',$params['order_id'])->setField('status', $productTransfer::STOP); //新增记录 $productOrder::setCreateOrder($order_info['order_id'], $order_info, $productOrder::Popular, $order_info['user_id'], $order_info['from_user'], $order_info->order_no, 0, $order_info->popular_price); //修改:类型状态 $order_info->type_id= $productOrder::Transfer; $order_info->status = $productOrder::Cancelled; $order_info->save(); // 提交事务 Db::commit(); } catch (Exception $e) { // 回滚事务 Db::rollback(); $this->error( $e->getMessage()); } $this->success('ok'); } //查看快递信息 public function getTracking(UserArea $userArea) { $order_id = $this->request->post('order_id'); // 订单id if(empty($order_id)) $this->error(__("参数有误,无可用产品")); $tracking_no = $userArea->where('order_id', $order_id)->where('type_id', $userArea::TakeAdders)->value('tracking_no'); if(empty($tracking_no)) $this->error(__("暂无物流信息")); $this->success('ok', $tracking_no); } /** * 更新订单hash * @return void */ public function updateOrder() { $amount = $this->request->post('amount'); // 支付金额 $tx_hash = $this->request->post('tx_hash'); // 交易hash if (empty($amount)) $this->error(__('交易金额不能为空')); if (empty($tx_hash)) $this->error(__('交易Hash不能为空')); //用户id、用户地址、hash、金额、状态、时间 address Db::startTrans(); try { //更新订单支付状态为 待确认 OfflineRechargeRecordModel::create([ 'order_no' => $this->auth->id . substr((string)time(), -8),//会员ID+时间戳后8位 'user_id' => $this->auth->id, 'amount' => $amount, 'symbol' => Asset::USDT, 'status' => OfflineRechargeRecordModel::StatusConfirm, 'tx_hash' => $tx_hash, 'from_address' => $this->auth->address, 'to_address' => Env::get('rental.pay_address'), 'cha_bao' => getConfig('chabao_rate') * $amount ]); // 提交事务 Db::commit(); } catch (Exception $e) { // 回滚事务 Db::rollback(); $this->error( $e->getMessage()); } $this->success('ok'); } }