Order.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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\LedgerFrozenChangeModel;
  8. use app\common\model\LedgerWalletModel;
  9. use app\common\model\UserArea;
  10. use app\common\model\UserModel;
  11. use app\common\model\LedgerTokenChangeModel;
  12. use app\common\model\OfflineRechargeRecordModel;
  13. use app\api\logic\OrderLogic;
  14. use app\api\logic\MarketLogic;
  15. use app\common\model\ProductMarket;
  16. use Exception;
  17. use fast\Asset;
  18. use think\Db;
  19. use think\Env;
  20. /**
  21. * 订单关联
  22. */
  23. class Order extends Api
  24. {
  25. /**
  26. * 创建订单
  27. */
  28. public function create(ProductPopular $productPopular, OrderLogic $orderLogic, LedgerWalletModel $ledgerWalletModel, UserModel $userModel)
  29. {
  30. $params = $this->request->post();
  31. $validate = \think\Loader::validate('Order');
  32. if(!$validate->scene('add')->check($params)) $this->error($validate->getError());
  33. $order_info = $productPopular->where('id', $params['order_id'])->find();
  34. if(empty($order_info)) $this->error(__("参数有误,无可用产品"));
  35. $areaArr = ($params['type'] == 1)? explode(',', $params['area_id']): '';
  36. $areaNum = ($params['type'] == 1)? count($areaArr): $params['num'];
  37. if(($order_info->num +$order_info->init_num+ $areaNum) > $order_info->stock) $this->error(__("库存不足"));
  38. $result = false;
  39. Db::startTrans();
  40. try {
  41. $amount = $ledgerWalletModel::getWalletChaBao($this->auth->id);
  42. $totalPrice = bcmul($order_info['price'], $areaNum, 2);
  43. if(bccomp($totalPrice, $amount, 2) > 0) throw new Exception(__("余额不足请前往充值"), 15001);
  44. if($order_info->start_time > time()) throw new Exception(__("抢购未开始"));
  45. if($order_info->stock == 0 || time() >= $order_info->end_time) throw new Exception(__("抢购已结束"));
  46. // //批量地区添加 1选择地区 2未选择地区
  47. $result = $orderLogic::createOrderByType($params['type'], $areaArr, $params['order_id'], $order_info->price, $params['product_id'], $this->auth->id, $areaNum);
  48. //余额记录
  49. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$totalPrice, LedgerTokenChangeModel::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, LedgerTokenChangeModel::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::getProductOrder($params['order_id'], productOrder::Paid, 'b.freight,b.is_freight');
  83. if(empty($order_info) || empty($order_info->is_freight)) $this->error( __("参数有误,无可用产品"));
  84. // 启动事务
  85. Db::startTrans();
  86. try {
  87. $amount = $ledgerWalletModel::getWalletTotalChaBao($this->auth->id);
  88. if(bccomp($order_info->freight, $amount, 2) > 0) throw new Exception(__("余额不足请前往充值"), 15001);
  89. // 生成订单
  90. $userArea->create(['name'=>$params['name'], 'type_id'=>$userArea::TakeAdders, 'phone'=>$params['phone'],'address'=>$params['address'],'order_id'=>$params['order_id']]);
  91. //扣除Rwa数量
  92. if($order_info->popular_price > config('min_rwa_price')) $userModel::updateForRwaNum($order_info->user_id, $userModel::getByParentId($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, MarketLogic $marketLogic)
  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::getProductOrder($params['order_id'], productOrder::Paid, 'b.is_transfer,b.min_transfer_fee,b.max_transfer_fee');
  117. if(empty($order_info) || empty($order_info->is_transfer)) throw new Exception(__("订单不存在"));
  118. if($params['price'] < $order_info->min_transfer_fee || $params['price'] > $order_info->max_transfer_fee) throw new Exception(__("当前订单转让金额为").$order_info->min_transfer_fee.'~'.$order_info->max_transfer_fee);
  119. //转让订单
  120. $marketLogic::createTransferOrder($params['price'], $order_info['product_id'], $order_info['area_id'], $order_info['order_no'], $this->auth->id, $params);
  121. //修改 类型状态
  122. $order_info->type_id = $productOrder::Transfer;
  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. if($order_info['user_id'] == $this->auth->id) throw new Exception(__("不能购买自己寄售的商品"));
  147. $chabao = $ledgerWalletModel::getWalletChaBao($this->auth->id);
  148. if(bccomp($order_info['price'], $chabao, 2) > 0) throw new Exception(__("余额不足请前往充值"), 15001);
  149. //抢购订单
  150. $popular_order = $productOrder->where('id', $order_info['order_id'])->find();
  151. // 生成订单
  152. $popularPrice = empty($popular_order->popular_price)? $order_info->price: $popular_order->popular_price;
  153. $productOrder::setCreateOrder($params['order_id'], $order_info, $productOrder::Transfer, $this->auth->id, $order_info['user_id'], $popular_order->order_no, $order_info['fees'], $popularPrice);
  154. //扣除余额记录
  155. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$order_info['price'], LedgerTokenChangeModel::Payment, $order_info['user_id']);
  156. //增加转让人余额
  157. $amount = bcsub($order_info['price'], $order_info['fees'], 2);
  158. $ledgerWalletModel->changeWalletAccount($order_info['user_id'], Asset::TOKEN, $amount, LedgerTokenChangeModel::Receive, $this->auth->id);
  159. if($popular_order->popular_price > config('min_rwa_price')) {
  160. //扣除转让人Rwa有效
  161. $userModel::updateForRwaNum($order_info['user_id'], $userModel::getByParentId($order_info['user_id']), 1, '-');
  162. //增加购买Rwa有效
  163. $userModel::updateForRwaNum($this->auth->id, $this->auth->parent_id, 1, '+');
  164. }
  165. //修改原订单状态
  166. $popular_order->status=$productOrder::Closure;
  167. $popular_order->save();
  168. //修改状态
  169. $order_info->status = $productTransfer::Stop;
  170. $order_info->save();
  171. Db::commit();
  172. } catch (Exception $e) {
  173. Db::rollback();
  174. $this->error($e->getMessage(), null, $e->getCode());
  175. }
  176. $this->success('ok');
  177. }
  178. //赠送
  179. public function giveaway(ProductOrder $productOrder, UserModel $userModel, LedgerWalletModel $ledgerWalletModel)
  180. {
  181. $params = $this->request->post();
  182. $validate = \think\Loader::validate('Order');
  183. if(!$validate->scene('giv')->check($params)) $this->error($validate->getError());
  184. // 启动事务
  185. Db::startTrans();
  186. try {
  187. $order_info = $productOrder::getProductOrder($params['order_id'], $productOrder::Paid, 'b.is_gift,b.gift_fee');
  188. if(empty($order_info) || empty($order_info->is_gift)) throw new Exception(__("参数有误,无可用产品"));
  189. $user = $userModel->getByAddress($params['address']);
  190. if(empty($user)) throw new Exception(__("赠送用户不存在"));
  191. if($user['id'] == $order_info['user_id']) throw new Exception(__("赠送用户不能是自己"));
  192. $chabao = $ledgerWalletModel::getWalletChaBao($this->auth->id);
  193. $fees = $order_info->gift_fee> 0? bcmul($order_info['price'], bcdiv($order_info->gift_fee, 100, 2), 2): 0;
  194. if(bccomp($fees, $chabao, 2) > 0) throw new Exception(__("余额不足请前往充值"), 15001);
  195. //添加记录
  196. $productOrder::setCreateOrder($params['order_id'], $order_info, $productOrder::Giveaway, $user['id'], $this->auth->id, $order_info->order_no, $fees, $order_info->popular_price);
  197. //对方Rwa+1
  198. if($order_info->popular_price > config('min_rwa_price')) $userModel::updateForRwaNum($user['id'], $userModel::getByParentId($user['id']), 1, '+');
  199. //扣除手续费
  200. $ledgerWalletModel->setChangeFrozen($this->auth->id, $fees, LedgerFrozenChangeModel::Giveaway, '-');
  201. //扣除Rwa有效-1
  202. if($order_info->popular_price > config('min_rwa_price')) $userModel::updateForRwaNum($this->auth->id, $this->auth->parent_id, 1, '-');
  203. //修改:类型状态
  204. $order_info->type_id= $productOrder::Giveaway;
  205. $order_info->status = $productOrder::Closure;
  206. $order_info->save();
  207. // 提交事务
  208. Db::commit();
  209. } catch (Exception $e) {
  210. // 回滚事务
  211. Db::rollback();
  212. $this->error( $e->getMessage(), null, $e->getCode());
  213. }
  214. $this->success('ok');
  215. }
  216. //取消转让
  217. public function cancel(ProductOrder $productOrder, MarketLogic $marketLogic)
  218. {
  219. $params = $this->request->post();
  220. $validate = \think\Loader::validate('Order');
  221. if(!$validate->scene('out')->check($params)) $this->error($validate->getError());
  222. // 启动事务
  223. Db::startTrans();
  224. try {
  225. $order_info = $productOrder->where('id', $params['order_id'])->find();
  226. if(empty($order_info)) throw new Exception(__("参数有误,无可用产品"));
  227. //取消转让订单
  228. $marketLogic::cancelTransferOrder($params['order_id'], $order_info['user_id'], $order_info);
  229. //修改:类型状态
  230. $order_info->type_id= $productOrder::Transfer;
  231. $order_info->status = $productOrder::Cancelled;
  232. $order_info->save();
  233. // 提交事务
  234. Db::commit();
  235. } catch (Exception $e) {
  236. // 回滚事务
  237. Db::rollback();
  238. $this->error( $e->getMessage());
  239. }
  240. $this->success('ok');
  241. }
  242. //查看快递信息
  243. public function getTracking(UserArea $userArea)
  244. {
  245. $order_id = $this->request->post('order_id'); // 订单id
  246. if(empty($order_id)) $this->error(__("参数有误,无可用产品"));
  247. $tracking_no = $userArea->where('order_id', $order_id)->where('type_id', $userArea::TakeAdders)->value('tracking_no');
  248. if(empty($tracking_no)) $this->error(__("暂无物流信息"));
  249. $this->success('ok', $tracking_no);
  250. }
  251. /**
  252. * 更新订单hash
  253. * @return void
  254. */
  255. public function updateOrder()
  256. {
  257. $amount = $this->request->post('amount'); // 支付金额
  258. $tx_hash = $this->request->post('tx_hash'); // 交易hash
  259. if (empty($amount)) $this->error(__('交易金额不能为空'));
  260. if (empty($tx_hash)) $this->error(__('交易Hash不能为空'));
  261. //用户id、用户地址、hash、金额、状态、时间 address
  262. Db::startTrans();
  263. try {
  264. //更新订单支付状态为 待确认
  265. OfflineRechargeRecordModel::create([
  266. 'order_no' => $this->auth->id . substr((string)time(), -8),//会员ID+时间戳后8位
  267. 'user_id' => $this->auth->id,
  268. 'amount' => $amount,
  269. 'symbol' => Asset::USDT,
  270. 'status' => OfflineRechargeRecordModel::StatusConfirm,
  271. 'tx_hash' => $tx_hash,
  272. 'from_address' => $this->auth->address,
  273. 'to_address' => Env::get('rental.pay_address'),
  274. 'cha_bao' => getConfig('chabao_rate') * $amount
  275. ]);
  276. // 提交事务
  277. Db::commit();
  278. } catch (Exception $e) {
  279. // 回滚事务
  280. Db::rollback();
  281. $this->error( $e->getMessage());
  282. }
  283. $this->success('ok');
  284. }
  285. }