| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use app\common\model\ProductTransfer;
- use app\common\model\ProductOrder;
- use app\common\model\ProductPopular;
- use app\common\model\ProductLists;
- use app\common\model\LedgerWalletModel;
- use app\common\model\UserBalanceLog;
- use app\common\model\UserArea;
- use app\common\model\UserModel;
- use app\common\model\OfflineRechargeRecordModel;
- use Exception;
- use fast\MembershipLevel;
- use think\Db;
- use think\Env;
- /**
- * 订单关联
- */
- class Order extends Api
- {
- /**
- * 创建订单
- */
- public function create(ProductPopular $productPopular, ProductOrder $productOrder, ProductLists $productLists)
- {
- $params = $this->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( __("参数有误,无可用产品"));
- $order_data['order_id'] = $params['order_id'];
- $order_data['product_id']= $params['product_id'];
- $order_data['price'] = $order_info['price'];
- $order_data['type_id'] = $productOrder::Popular;
- $order_data['area_id'] = $params['area_id'];
- $order_data['order_no'] = getOrderSN('R');
- $order_data['user_id'] = $this->auth->id;
- $order_data['status'] = $productOrder::Paid;
- $order_data['num'] = 1;
- // 启动事务
- Db::startTrans();
- try {
- $amount = bcsub($this->auth->balance, $this->auth->frozen_amount, 2);
- if(bccomp($order_info['price'], $amount, 2) > 0) throw new Exception(__("余额不足请前往充值"));
- if($order_info->stock == 0 || time() >= $order_info->end_time) throw new Exception(__("抢购已结束"));
- // 生成订单
- $order =$productOrder->create($order_data);
- //修改区域状态
- $productLists->where('id', $order_data['area_id'])->update(['status'=> $productLists::STOP]);
- //余额记录
- UserBalanceLog::changeWalletAccount($this->auth->id, UserBalanceLog::Popular, $order_info['price'],
- $this->auth->balance, bcsub($this->auth->balance, $order_info['price'], 6), $order->id, '-');
- //扣除库存
- if($order_info->stock == 1 || time() >= $order_info->end_time) $order_info->status= $productPopular::STOP;
- $order_info->num += 1;
- $order_info->stock-= 1;
- $order_info->save();
- // 提交事务
- Db::commit();
- } catch (Exception $e) {
- // 回滚事务
- Db::rollback();
- $this->error( $e->getMessage());
- }
- $this->success('订单创建成功');
- }
-
- /**
- * 提货订单
- */
- public function pickupOrder(UserArea $userArea, ProductOrder $productOrder, ProductLists $productLists)
- {
- $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( __("参数有误,无可用产品"));
- $order_data['name'] = $params['name'];
- $order_data['phone'] = $params['phone'];
- $order_data['address'] = $params['address'];
- $order_data['order_id'] = $params['order_id'];
- // 启动事务
- Db::startTrans();
- try {
- // 生成订单
- $userArea->create($order_data);
- $order_info->status= $productOrder::Shipped;
- $order_info->save();
- // 提交事务
- Db::commit();
- } catch (Exception $e) {
- // 回滚事务
- Db::rollback();
- $this->error( $e->getMessage());
- }
- $this->success('订单创建成功');
- }
- /**
- * 订单转让
- * @return void
- */
- public function transfer(ProductOrder $productOrder, ProductTransfer $productTransfer)
- {
- $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(__("订单不存在"));
- $fee = getConfig('transfer_fee');
- $feeAmount = bcmul($params['price'], $fee, 2) ;
- $order_data['user_id'] = $this->auth->id;
- $order_data['price'] = $params['price'];
- $order_data['product_id'] = $order_info['product_id'];
- $order_data['fees'] = $feeAmount;
- $order_data['area_id'] = $order_info['area_id'];
- $order_data['order_id'] = $params['order_id']; //订单ID
-
- // 生成订单
- $productTransfer->create($order_data);
-
- //修改状态
- $order_info->status = $productOrder::Transferred;
- $order_info->save();
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- $this->error('提交失败:' . $e->getMessage());
- }
- $this->success('订单创建成功');
- }
- /**
- * 转让订单购买
- * @return void
- */
- public function transferOrder(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 = $productTransfer->where('id', $params['order_id'])->where('status', $productTransfer::NORMAL)->find();
- if(empty($order_info)) throw new Exception(__("订单不存在"));
- if(bccomp($order_info['price'], $this->auth->balance, 2) > 0) throw new Exception(__("余额不足请前往充值"));
- $order_data['order_id'] = $params['order_id'];
- $order_data['product_id']= $order_info['product_id'];
- $order_data['type_id'] = $productOrder::Transfer;
- $order_data['status'] = $productOrder::Paid;
- $order_data['area_id'] = $order_info['area_id'];
- $order_data['order_no'] = getOrderSN('Z');
- $order_data['user_id'] = $this->auth->id;
- $order_data['price'] = $order_info['price'];
- $order_data['num'] = 1;
-
- // 生成订单
- $order = $productOrder->create($order_data);
- //扣除余额记录
- $balance = bcsub($this->auth->balance, $this->auth->frozen_amount, 2);
- UserBalanceLog::changeWalletAccount(
- $this->auth->id, UserBalanceLog::Payment,
- $order_info['price'], $balance,
- bcsub($balance, $order_info['price'], 2),
- $order->id, '-');
- //增加转让人余额
- $amount = bcsub($order_info['price'], $order_info['fees'], 2);
- $atBalance= UserModel::getUserAmount($order_info['user_id']);
- UserBalanceLog::changeWalletAccount(
- $order_info['user_id'], UserBalanceLog::Receive,
- $amount,
- $atBalance,
- bcadd($atBalance, $amount, 2),
- $order->id);
- //修改原订单状态
- $productOrder->where('id', $order_info['order_id'])->setField('status', $productOrder::Complete);
- //修改状态
- $order_info->status = $productTransfer::STOP;
- $order_info->save();
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- $this->success('订单创建成功');
- }
- //取消转让
- 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);
- $order_info->status= $productOrder::Paid;
- $order_info->save();
- // 提交事务
- Db::commit();
- } catch (Exception $e) {
- // 回滚事务
- Db::rollback();
- $this->error( $e->getMessage());
- }
- $this->success('订单创建成功');
- }
- /**
- * 更新订单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 {
- //更新订单支付状态为 待确认
- $order_update = OfflineRechargeRecordModel::create([
- 'user_id' => $this->auth->id,
- 'amount' => $amount,
- 'status' => OfflineRechargeRecordModel::StatusConfirm,
- 'tx_hash' => $tx_hash,
- 'from_address' => $this->auth->address,
- 'to_address' => Env::get('rental.pay_address')
- ]);
- // 提交事务
- Db::commit();
- } catch (Exception $e) {
- // 回滚事务
- Db::rollback();
- $this->error('提交失败:' . $e->getMessage());
- }
- $this->success('订单支付成功');
- }
- }
|