AuctionProductController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace app\api\controller\auction;
  3. use app\models\auction\Auction;
  4. use app\models\auction\AuctionBooking;
  5. use app\models\auction\AuctionOrder;
  6. use app\models\auction\AuctionProduct;
  7. use app\models\user\User;
  8. use app\models\user\UserBill;
  9. use app\Request;
  10. use crmeb\services\UtilService;
  11. use think\facade\Db;
  12. class AuctionProductController
  13. {
  14. /**
  15. * 购买随机返回一张订单
  16. * @param Request $request
  17. * @return void
  18. */
  19. public function purchase(Request $request)
  20. {
  21. $data = UtilService::postMore([
  22. ['id']
  23. ]);
  24. $auction = Auction::find($data['id']);
  25. if (!$auction) return app('json')->fail('场次不存在');
  26. if (AuctionOrder::where([['auction_id', '=', $data['id']], ['frequency', '=', $auction['frequency']], ['uid', '=', $request->uid()]])->count() >= 1) return app('json')->fail('当前场次已购买商品');
  27. $orderCount = AuctionOrder::where([['auction_id', '=', $data['id']], ['frequency', '=', $auction['frequency']]])->count(); // 查找出当前场次已派单多少
  28. $pd = AuctionBooking::where([['auction_id', '=', $data['id']], ['frequency', '=', $auction['frequency']]])->count(); // 当前预约人数
  29. $pds = ceil($pd * ($auction['dispatch']/100));
  30. if ($orderCount >= $pds) return app('json')->fail('商品已买完');
  31. $show = AuctionProduct::random($data['id'], $request->uid());
  32. if ($show == 'false') return app('json')->fail('购买失败');
  33. return app('json')->successful($show);
  34. }
  35. /**
  36. * 用户商品
  37. * @param Request $request
  38. * @return mixed
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\DbException
  41. * @throws \think\db\exception\ModelNotFoundException
  42. */
  43. public function user_product(Request $request)
  44. {
  45. $data = UtilService::getMore([
  46. [['page', 'd'], 0],
  47. [['limit', 'd'], 0],
  48. ]);
  49. return app('json')->successful(AuctionProduct::user_product( $data,$request->uid()));
  50. }
  51. /**
  52. * 用户订单
  53. * @param Request $request
  54. * @return mixed
  55. * @throws \think\db\exception\DataNotFoundException
  56. * @throws \think\db\exception\DbException
  57. * @throws \think\db\exception\ModelNotFoundException
  58. */
  59. public function user_auction_order(Request $request)
  60. {
  61. $data = UtilService::getMore([
  62. [['type', 'd'], 0],
  63. [['page', 'd'], 0],
  64. [['limit', 'd'], 0],
  65. ['order_id']
  66. ]);
  67. return app('json')->successful(AuctionOrder::userOrder($data,$request->uid()));
  68. }
  69. /**
  70. * 卖家显示订单
  71. * @param Request $request
  72. * @return void
  73. */
  74. public function seller(Request $request)
  75. {
  76. $data = UtilService::getMore([
  77. ['type', 0],
  78. [['page', 'd'], 0],
  79. [['limit', 'd'], 0],
  80. ['order_id']
  81. ]);
  82. return app('json')->successful(AuctionOrder::seller_list($data,$request->uid()));
  83. }
  84. /**
  85. * 支付订单
  86. * @param Request $request
  87. * @return void
  88. * @throws \think\db\exception\DataNotFoundException
  89. * @throws \think\db\exception\DbException
  90. * @throws \think\db\exception\ModelNotFoundException
  91. */
  92. public function zfpay(Request $request)
  93. {
  94. $data = UtilService::postMore([
  95. ['order_id']
  96. ]);
  97. $model = new User();
  98. $order = AuctionOrder::where('order_id', $data['order_id'])->find();
  99. if (!$order) return app('json')->fail('订单不存在');
  100. if ($order['status'] == 0 or $order['status'] == 2) return app('json')->fail('订单状态不对');
  101. $user = $model->where('uid', $request->uid())->find();
  102. if ($user['now_money'] < $order['actual_price']) return app('json')->fail('余额不足');
  103. try {
  104. Db::startTrans();
  105. AuctionOrder::pay($order, $request->uid());
  106. Db::commit();
  107. return app('json')->successful('支付成功');
  108. } catch (\Exception $e) {
  109. Db::rollback();
  110. return app('json')->fail('支付失败');
  111. }
  112. }
  113. /**
  114. * 挂售详情
  115. * @param Request $request
  116. * @return mixed
  117. * @throws \think\db\exception\DataNotFoundException
  118. * @throws \think\db\exception\DbException
  119. * @throws \think\db\exception\ModelNotFoundException
  120. */
  121. public function gsxq(Request $request)
  122. {
  123. $data = UtilService::postMore([
  124. ['id']
  125. ]);
  126. $product = AuctionProduct::where('id', $data['id'])->find();
  127. $data['aid_val'] = round($product['hanging_price'] * 0.0485, 2); // 需要的广告值
  128. $data['hanging_price'] = round((int)$product['hanging_price'] + $product['hanging_price'] * 0.06, 2); // 第二天溢价
  129. return app('json')->successful($data);
  130. }
  131. /**
  132. * 挂售商品
  133. * @param Request $request
  134. * @return mixed
  135. * @throws \think\db\exception\DataNotFoundException
  136. * @throws \think\db\exception\DbException
  137. * @throws \think\db\exception\ModelNotFoundException
  138. */
  139. public function hanging_sale(Request $request)
  140. {
  141. $data = UtilService::postMore([
  142. ['id']
  143. ]);
  144. $product = AuctionProduct::where('id', $data['id'])->find();
  145. if ($product['is_show'] == 1) return app('json')->fail('商品已挂售');
  146. $user = User::where('uid', $request->uid())->find();
  147. if (!$product) return app('json')->fail('商品不存在');
  148. $aid_val = round($product['hanging_price'] * 0.0485,2); // 需要的广告值
  149. $hanging_price = round((int)$product['hanging_price'] + $product['hanging_price'] * 0.06, 2); // 第二天溢价
  150. if ($user['aid_val'] < $aid_val) return app('json')->fail('挂售需要广告值不足');
  151. $product['hanging_price'] = $hanging_price;
  152. $product['is_show'] = 1;
  153. $user['aid_val'] -= $aid_val;
  154. try {
  155. Db::startTrans();
  156. $product->save();
  157. $user->save();
  158. UserBill::expend('挂售扣除广告值', $user['uid'], 'aid_val', 'sub_aid_val', $aid_val, $user['spread_uid'], $user['aid_val'], '挂售商品扣除广告值');
  159. Db::commit();
  160. return app('json')->successful('挂售成功');
  161. } catch (\Exception $e) {
  162. Db::rollback();
  163. return app('json')->fail('挂售失败');
  164. }
  165. }
  166. }