Order.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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\ProductArea;
  8. use app\common\model\ProductLists;
  9. use app\common\model\LedgerWalletModel;
  10. use app\common\model\UserArea;
  11. use app\common\model\UserModel;
  12. use app\common\model\OfflineRechargeRecordModel;
  13. use Exception;
  14. use fast\Asset;
  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, ProductArea $productArea, LedgerWalletModel $ledgerWalletModel)
  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($order_info->num == $order_info->stock) $this->error(__("库存不足"));
  32. if(empty($order_info)) $this->error(__("参数有误,无可用产品"));
  33. $order_data['order_id'] = $params['order_id'];
  34. $order_data['product_id']= $params['product_id'];
  35. $order_data['price'] = $order_info['price'];
  36. $order_data['type_id'] = $productOrder::Popular;
  37. $order_data['area_id'] = $params['area_id'];
  38. $order_data['order_no'] = getOrderSN('R');
  39. $order_data['user_id'] = $this->auth->id;
  40. $order_data['status'] = $productOrder::Paid;
  41. $order_data['num'] = 1;
  42. // 启动事务
  43. Db::startTrans();
  44. try {
  45. $amount = $ledgerWalletModel::getWalletChaBao($this->auth->id);
  46. if(bccomp($order_info['price'], $amount, 2) > 0) throw new Exception(__("余额不足请前往充值"), 15001);
  47. if($order_info->stock == 0 || time() >= $order_info->end_time) throw new Exception(__("抢购已结束"));
  48. // 生成订单
  49. $productOrder->create($order_data);
  50. //修改区域状态
  51. $productArea->where('id', $order_data['area_id'])->setField('status', ProductLists::STOP);
  52. //余额记录
  53. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$order_info['price'], $ledgerWalletModel::Popular, $this->auth->id);
  54. //直推收益: pv* ×10%
  55. if($order_info['pv'] > 0 && $this->auth->parent_id > 0 && $productOrder::getEffectiveOrder($this->auth->parent_id) > 0){
  56. $pv = bcmul($order_info['pv'], getConfig('pv_rate'), 2);
  57. $ledgerWalletModel->changeWalletAccount($this->auth->parent_id, Asset::TOKEN, $pv, $ledgerWalletModel::Direct, $this->auth->id);
  58. }
  59. //扣除库存
  60. if($order_info->stock == 1 || time() >= $order_info->end_time) $order_info->status= $productPopular::STOP;
  61. $order_info->num += 1;
  62. $order_info->save();
  63. // 提交事务
  64. Db::commit();
  65. } catch (Exception $e) {
  66. // 回滚事务
  67. Db::rollback();
  68. $this->error($e->getMessage(), null, $e->getCode());
  69. }
  70. $this->success('ok');
  71. }
  72. /**
  73. * 提货订单
  74. */
  75. public function pickupOrder(UserArea $userArea, ProductOrder $productOrder)
  76. {
  77. $params = $this->request->post();
  78. $validate = \think\Loader::validate('Order');
  79. if(!$validate->scene('pick')->check($params)) $this->error($validate->getError());
  80. $order_info = $productOrder->where('id', $params['order_id'])->find();
  81. if(empty($order_info)) $this->error( __("参数有误,无可用产品"));
  82. $order_data['name'] = $params['name'];
  83. $order_data['phone'] = $params['phone'];
  84. $order_data['address'] = $params['address'];
  85. $order_data['order_id'] = $params['order_id'];
  86. // 启动事务
  87. Db::startTrans();
  88. try {
  89. // 生成订单
  90. $userArea->create($order_data);
  91. $order_info->status= $productOrder::Shipped;
  92. $order_info->save();
  93. // 提交事务
  94. Db::commit();
  95. } catch (Exception $e) {
  96. // 回滚事务
  97. Db::rollback();
  98. $this->error( $e->getMessage());
  99. }
  100. $this->success('ok');
  101. }
  102. /**
  103. * 订单转让
  104. * @return void
  105. */
  106. public function transfer(ProductOrder $productOrder, ProductTransfer $productTransfer)
  107. {
  108. $params = $this->request->post();
  109. $validate = \think\Loader::validate('Order');
  110. if(!$validate->scene('tran')->check($params)) $this->error($validate->getError());
  111. //启动事务
  112. Db::startTrans();
  113. try {
  114. $order_info = $productOrder->where('id', $params['order_id'])->where('status', $productOrder::Paid)->find();
  115. if(empty($order_info)) throw new Exception(__("订单不存在"));
  116. //转让订单
  117. $fee = getConfig('transfer_fee');
  118. $feeAmount = bcmul($params['price'], $fee, 2) ;
  119. $productTransfer::setTransferOrder($this->auth->id, $order_info['product_id'], $order_info['area_id'], $feeAmount, $params);
  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(__("余额不足请前往充值"), 15001);
  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['from_user'] = $order_info['user_id'];
  154. $order_data['price'] = $order_info['price'];
  155. $order_data['fees'] = $order_info['fees'];
  156. $order_data['num'] = 1;
  157. // 生成订单
  158. $productOrder->create($order_data);
  159. //扣除余额记录
  160. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$order_info['price'], $ledgerWalletModel::Payment, $order_info['user_id']);
  161. //增加转让人余额
  162. $amount = bcsub($order_info['price'], $order_info['fees'], 2);
  163. $ledgerWalletModel->changeWalletAccount($order_info['user_id'], Asset::TOKEN, $amount, $ledgerWalletModel::Receive, $this->auth->id);
  164. //修改原订单状态
  165. $productOrder->where('id', $order_info['order_id'])->setField('status', $productOrder::Closure);
  166. //修改状态
  167. $order_info->status = $productTransfer::STOP;
  168. $order_info->save();
  169. Db::commit();
  170. } catch (Exception $e) {
  171. Db::rollback();
  172. $this->error($e->getMessage(), null, $e->getCode());
  173. }
  174. $this->success('ok');
  175. }
  176. //赠送
  177. public function giveaway(ProductOrder $productOrder, UserModel $userModel, LedgerWalletModel $ledgerWalletModel)
  178. {
  179. $params = $this->request->post();
  180. $validate = \think\Loader::validate('Order');
  181. if(!$validate->scene('giv')->check($params)) $this->error($validate->getError());
  182. // 启动事务
  183. Db::startTrans();
  184. try {
  185. $order_info = $productOrder->where('id', $params['order_id'])->where('status', $productOrder::Paid)->find();
  186. if(empty($order_info)) throw new Exception(__("参数有误,无可用产品"));
  187. $user = $userModel->getByAddress($params['address']);
  188. if(empty($user)) throw new Exception(__("赠送用户不存在"));
  189. if($user['id'] == $this->auth->id) throw new Exception(__("赠送用户不能是自己"));
  190. $chabao = $ledgerWalletModel::getWalletChaBao($this->auth->id);
  191. $fees = bcmul($order_info['price'], getConfig('giveaway'), 2);
  192. if(bccomp($fees, $chabao, 2) > 0) throw new Exception(__("余额不足请前往充值"), 15001);
  193. //添加记录
  194. $order_data['order_id'] = $params['order_id'];
  195. $order_data['product_id']= $order_info['product_id'];
  196. $order_data['type_id'] = $productOrder::Giveaway;
  197. $order_data['status'] = $productOrder::Paid;
  198. $order_data['area_id'] = $order_info['area_id'];
  199. $order_data['order_no'] = getOrderSN('G');
  200. $order_data['user_id'] = $user['id'];
  201. $order_data['from_user'] = $this->auth->id;
  202. $order_data['price'] = $order_info['price'];
  203. $order_data['fees'] = $fees;
  204. $order_data['num'] = 1;
  205. $productOrder::create($order_data);
  206. //扣除手续费
  207. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$fees, $ledgerWalletModel::Giveaway, $user['id']);
  208. $order_info->status= $productOrder::Closure;
  209. $order_info->save();
  210. // 提交事务
  211. Db::commit();
  212. } catch (Exception $e) {
  213. // 回滚事务
  214. Db::rollback();
  215. $this->error( $e->getMessage(), null, $e->getCode());
  216. }
  217. $this->success('ok');
  218. }
  219. //取消转让
  220. public function cancel(ProductOrder $productOrder, ProductTransfer $productTransfer)
  221. {
  222. $params = $this->request->post();
  223. $validate = \think\Loader::validate('Order');
  224. if(!$validate->scene('out')->check($params)) $this->error($validate->getError());
  225. // 启动事务
  226. Db::startTrans();
  227. try {
  228. $order_info = $productOrder->where('id', $params['order_id'])->find();
  229. if(empty($order_info)) throw new Exception(__("参数有误,无可用产品"));
  230. //转让列表取消
  231. $productTransfer::where('order_id',$params['order_id'])->setField('status', $productTransfer::STOP);
  232. $order_info->status= $productOrder::Paid;
  233. $order_info->save();
  234. // 提交事务
  235. Db::commit();
  236. } catch (Exception $e) {
  237. // 回滚事务
  238. Db::rollback();
  239. $this->error( $e->getMessage());
  240. }
  241. $this->success('ok');
  242. }
  243. //查看快递信息
  244. public function getTracking(UserArea $userArea)
  245. {
  246. $order_id = $this->request->post('order_id'); // 订单id
  247. if(empty($order_id)) $this->error(__("参数有误,无可用产品"));
  248. $tracking_no = $userArea->where('order_id', $order_id)->value('tracking_no');
  249. if(empty($tracking_no)) $this->error(__("暂无物流信息"));
  250. $this->success('ok', $tracking_no);
  251. }
  252. /**
  253. * 更新订单hash
  254. * @return void
  255. */
  256. public function updateOrder()
  257. {
  258. $amount = $this->request->post('amount'); // 支付金额
  259. $tx_hash = $this->request->post('tx_hash'); // 交易hash
  260. if (empty($amount)) $this->error(__('交易金额不能为空'));
  261. if (empty($tx_hash)) $this->error(__('交易Hash不能为空'));
  262. //用户id、用户地址、hash、金额、状态、时间 address
  263. Db::startTrans();
  264. try {
  265. //更新订单支付状态为 待确认
  266. $order_update = OfflineRechargeRecordModel::create([
  267. 'user_id' => $this->auth->id,
  268. 'amount' => $amount,
  269. 'status' => OfflineRechargeRecordModel::StatusConfirm,
  270. 'tx_hash' => $tx_hash,
  271. 'from_address' => $this->auth->address,
  272. 'to_address' => Env::get('rental.pay_address'),
  273. 'cha_bao' => getConfig('chabao_rate') * $amount
  274. ]);
  275. // 提交事务
  276. Db::commit();
  277. } catch (Exception $e) {
  278. // 回滚事务
  279. Db::rollback();
  280. $this->error( $e->getMessage());
  281. }
  282. $this->success('ok');
  283. }
  284. }