Order.php 15 KB

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