Order.php 15 KB

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