Order.php 10 KB

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