AuctionProductController.php 6.4 KB

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