Order.php 13 KB

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