Order.php 10 KB

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