Order.php 14 KB

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