Order.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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\UserPathModel;
  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 = bcsub($params['price'] , bcmul($params['price'], $fee, 2), 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. // 生成订单
  153. $order = $productOrder->create($order_data);
  154. //扣除余额记录
  155. $balance = bcsub($this->auth->balance, $this->auth->frozen_amount, 2);
  156. UserBalanceLog::changeWalletAccount(
  157. $this->auth->id, UserBalanceLog::Payment,
  158. $order_info['price'], $balance,
  159. bcsub($balance, $order_info['price'], 2),
  160. $order->id, '-');
  161. //增加转让人余额
  162. $amount = bcsub($order_info['price'], $order_info['fees'], 2);
  163. $atBalance= UserModel::getUserAmount($order_info['user_id']);
  164. UserBalanceLog::changeWalletAccount(
  165. $order_data['user_id'], UserBalanceLog::Receive,
  166. $amount,
  167. $atBalance,
  168. bcadd($atBalance, $amount, 2),
  169. $order->id);
  170. //修改原订单状态
  171. $productOrder->where('id', $params['order_id'])->setField('status', $productOrder::Complete);
  172. //修改状态
  173. $order_info->status = $productTransfer::STOP;
  174. $order_info->save();
  175. Db::commit();
  176. } catch (Exception $e) {
  177. Db::rollback();
  178. $this->error($e->getMessage());
  179. }
  180. $this->success('订单创建成功');
  181. }
  182. //取消转让
  183. public function cancel(ProductOrder $productOrder, ProductTransfer $productTransfer)
  184. {
  185. $params = $this->request->post();
  186. $validate = \think\Loader::validate('Order');
  187. if(!$validate->scene('out')->check($params)) $this->error($validate->getError());
  188. // 启动事务
  189. Db::startTrans();
  190. try {
  191. $order_info = $productOrder->where('id', $params['order_id'])->find();
  192. if(empty($order_info)) throw new Exception( __("参数有误,无可用产品"));
  193. //转让列表取消
  194. $productTransfer::where('order_id',$params['order_id'])->setField('status', $productTransfer::STOP);
  195. $order_info->status= $productOrder::Paid;
  196. $order_info->save();
  197. // 提交事务
  198. Db::commit();
  199. } catch (Exception $e) {
  200. // 回滚事务
  201. Db::rollback();
  202. $this->error( $e->getMessage());
  203. }
  204. $this->success('订单创建成功');
  205. }
  206. }