Order.php 15 KB

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