Order.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\ProductTransfer;
  5. use app\common\model\ProductOrder;
  6. use app\common\model\ProductPopular;
  7. use app\common\model\ProductLists;
  8. use app\common\model\LedgerWalletModel;
  9. use app\common\model\UserArea;
  10. use app\common\model\UserModel;
  11. use app\common\model\OfflineRechargeRecordModel;
  12. use Exception;
  13. use fast\Asset;
  14. use think\Db;
  15. use think\Env;
  16. /**
  17. * 订单关联
  18. */
  19. class Order extends Api
  20. {
  21. /**
  22. * 创建订单
  23. */
  24. public function create(ProductPopular $productPopular, ProductOrder $productOrder, ProductLists $productLists, LedgerWalletModel $ledgerWalletModel)
  25. {
  26. $params = $this->request->post();
  27. $validate = \think\Loader::validate('Order');
  28. if(!$validate->scene('add')->check($params)) $this->error($validate->getError());
  29. $order_info = $productPopular->where('id', $params['order_id'])->find();
  30. if(empty($order_info)) $this->error( __("参数有误,无可用产品"));
  31. $order_data['order_id'] = $params['order_id'];
  32. $order_data['product_id']= $params['product_id'];
  33. $order_data['price'] = $order_info['price'];
  34. $order_data['type_id'] = $productOrder::Popular;
  35. $order_data['area_id'] = $params['area_id'];
  36. $order_data['order_no'] = getOrderSN('R');
  37. $order_data['user_id'] = $this->auth->id;
  38. $order_data['status'] = $productOrder::Paid;
  39. $order_data['num'] = 1;
  40. // 启动事务
  41. Db::startTrans();
  42. try {
  43. $amount = $ledgerWalletModel::getWalletChaBao($this->auth->id);
  44. if(bccomp($order_info['price'], $amount, 2) > 0) throw new Exception(__("余额不足请前往充值"));
  45. if($order_info->stock == 0 || time() >= $order_info->end_time) throw new Exception(__("抢购已结束"));
  46. // 生成订单
  47. $order =$productOrder->create($order_data);
  48. //修改区域状态
  49. $productLists->where('id', $order_data['area_id'])->update(['status'=> $productLists::STOP]);
  50. //余额记录
  51. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$order_info['price'], $ledgerWalletModel::Popular, $this->auth->id);
  52. //扣除库存
  53. if($order_info->stock == 1 || time() >= $order_info->end_time) $order_info->status= $productPopular::STOP;
  54. $order_info->num += 1;
  55. $order_info->stock-= 1;
  56. $order_info->save();
  57. // 提交事务
  58. Db::commit();
  59. } catch (Exception $e) {
  60. // 回滚事务
  61. Db::rollback();
  62. $this->error( $e->getMessage());
  63. }
  64. $this->success('ok');
  65. }
  66. /**
  67. * 提货订单
  68. */
  69. public function pickupOrder(UserArea $userArea, ProductOrder $productOrder)
  70. {
  71. $params = $this->request->post();
  72. $validate = \think\Loader::validate('Order');
  73. if(!$validate->scene('pick')->check($params)) $this->error($validate->getError());
  74. $order_info = $productOrder->where('id', $params['order_id'])->find();
  75. if(empty($order_info)) $this->error( __("参数有误,无可用产品"));
  76. $order_data['name'] = $params['name'];
  77. $order_data['phone'] = $params['phone'];
  78. $order_data['address'] = $params['address'];
  79. $order_data['order_id'] = $params['order_id'];
  80. // 启动事务
  81. Db::startTrans();
  82. try {
  83. // 生成订单
  84. $userArea->create($order_data);
  85. $order_info->status= $productOrder::Shipped;
  86. $order_info->save();
  87. // 提交事务
  88. Db::commit();
  89. } catch (Exception $e) {
  90. // 回滚事务
  91. Db::rollback();
  92. $this->error( $e->getMessage());
  93. }
  94. $this->success('ok');
  95. }
  96. /**
  97. * 订单转让
  98. * @return void
  99. */
  100. public function transfer(ProductOrder $productOrder, ProductTransfer $productTransfer)
  101. {
  102. $params = $this->request->post();
  103. $validate = \think\Loader::validate('Order');
  104. if(!$validate->scene('tran')->check($params)) $this->error($validate->getError());
  105. //启动事务
  106. Db::startTrans();
  107. try {
  108. $order_info = $productOrder->where('id', $params['order_id'])->where('status', $productOrder::Paid)->find();
  109. if(empty($order_info)) throw new Exception(__("订单不存在"));
  110. $fee = getConfig('transfer_fee');
  111. $feeAmount = bcmul($params['price'], $fee, 2) ;
  112. $order_data['user_id'] = $this->auth->id;
  113. $order_data['price'] = $params['price'];
  114. $order_data['product_id'] = $order_info['product_id'];
  115. $order_data['fees'] = $feeAmount;
  116. $order_data['area_id'] = $order_info['area_id'];
  117. $order_data['order_id'] = $params['order_id']; //订单ID
  118. // 生成订单
  119. $productTransfer->create($order_data);
  120. //修改状态
  121. $order_info->status = $productOrder::Transferred;
  122. $order_info->save();
  123. Db::commit();
  124. } catch (Exception $e) {
  125. Db::rollback();
  126. $this->error('提交失败:' . $e->getMessage());
  127. }
  128. $this->success('ok');
  129. }
  130. /**
  131. * 转让订单购买
  132. * @return void
  133. */
  134. public function transferOrder(ProductOrder $productOrder, ProductTransfer $productTransfer, LedgerWalletModel $ledgerWalletModel)
  135. {
  136. $params = $this->request->post();
  137. $validate = \think\Loader::validate('Order');
  138. if(!$validate->scene('out')->check($params)) $this->error($validate->getError());
  139. //启动事务
  140. Db::startTrans();
  141. try {
  142. $order_info = $productTransfer->where('id', $params['order_id'])->where('status', $productTransfer::NORMAL)->find();
  143. if(empty($order_info)) throw new Exception(__("订单不存在"));
  144. $chabao = $ledgerWalletModel::getWalletChaBao($this->auth->id);
  145. if(bccomp($order_info['price'], $chabao, 2) > 0) throw new Exception(__("余额不足请前往充值"));
  146. $order_data['order_id'] = $params['order_id'];
  147. $order_data['product_id']= $order_info['product_id'];
  148. $order_data['type_id'] = $productOrder::Transfer;
  149. $order_data['status'] = $productOrder::Paid;
  150. $order_data['area_id'] = $order_info['area_id'];
  151. $order_data['order_no'] = getOrderSN('Z');
  152. $order_data['user_id'] = $this->auth->id;
  153. $order_data['price'] = $order_info['price'];
  154. $order_data['num'] = 1;
  155. // 生成订单
  156. $order = $productOrder->create($order_data);
  157. //扣除余额记录
  158. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$order_info['price'], $ledgerWalletModel::Payment, $order_info['user_id']);
  159. //增加转让人余额
  160. $amount = bcsub($order_info['price'], $order_info['fees'], 2);
  161. $ledgerWalletModel->changeWalletAccount($order_info['user_id'], Asset::TOKEN, $amount, $ledgerWalletModel::Receive, $this->auth->id);
  162. //修改原订单状态
  163. $productOrder->where('id', $order_info['order_id'])->setField('status', $productOrder::Complete);
  164. //修改状态
  165. $order_info->status = $productTransfer::STOP;
  166. $order_info->save();
  167. Db::commit();
  168. } catch (Exception $e) {
  169. Db::rollback();
  170. $this->error($e->getMessage());
  171. }
  172. $this->success('ok');
  173. }
  174. //取消转让
  175. public function cancel(ProductOrder $productOrder, ProductTransfer $productTransfer)
  176. {
  177. $params = $this->request->post();
  178. $validate = \think\Loader::validate('Order');
  179. if(!$validate->scene('out')->check($params)) $this->error($validate->getError());
  180. // 启动事务
  181. Db::startTrans();
  182. try {
  183. $order_info = $productOrder->where('id', $params['order_id'])->find();
  184. if(empty($order_info)) throw new Exception( __("参数有误,无可用产品"));
  185. //转让列表取消
  186. $productTransfer::where('order_id',$params['order_id'])->setField('status', $productTransfer::STOP);
  187. $order_info->status= $productOrder::Paid;
  188. $order_info->save();
  189. // 提交事务
  190. Db::commit();
  191. } catch (Exception $e) {
  192. // 回滚事务
  193. Db::rollback();
  194. $this->error( $e->getMessage());
  195. }
  196. $this->success('ok');
  197. }
  198. /**
  199. * 更新订单hash
  200. * @return void
  201. */
  202. public function updateOrder()
  203. {
  204. $amount = $this->request->post('amount'); // 支付金额
  205. $tx_hash = $this->request->post('tx_hash'); // 交易hash
  206. if (empty($amount)) $this->error(__('交易金额不能为空'));
  207. if (empty($tx_hash)) $this->error(__('交易Hash不能为空'));
  208. //用户id、用户地址、hash、金额、状态、时间 address
  209. Db::startTrans();
  210. try {
  211. //更新订单支付状态为 待确认
  212. $order_update = OfflineRechargeRecordModel::create([
  213. 'user_id' => $this->auth->id,
  214. 'amount' => $amount,
  215. 'status' => OfflineRechargeRecordModel::StatusConfirm,
  216. 'tx_hash' => $tx_hash,
  217. 'from_address' => $this->auth->address,
  218. 'to_address' => Env::get('rental.pay_address')
  219. ]);
  220. // 提交事务
  221. Db::commit();
  222. } catch (Exception $e) {
  223. // 回滚事务
  224. Db::rollback();
  225. $this->error( $e->getMessage());
  226. }
  227. $this->success('ok');
  228. }
  229. }