AuctionProductController.php 7.0 KB

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