AuctionProductController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. ['pas']
  107. ]);
  108. if (empty($data['pas'])) return app('json')->fail('请填写支付密码');
  109. if ($request->user()['payment_pas'] != $data['pas']) return app('json')->fail('支付密码不正确');
  110. $model = new User();
  111. $order = AuctionOrder::where('order_id', $data['order_id'])->find();
  112. if (!$order) return app('json')->fail('订单不存在');
  113. if ($order['status'] == 0 or $order['status'] == 2) return app('json')->fail('订单状态不对');
  114. $user = $model->where('uid', $request->uid())->find();
  115. if ($user['now_money'] < $order['actual_price']) return app('json')->fail('余额不足');
  116. try {
  117. Db::startTrans();
  118. AuctionOrder::pay($order, $request->uid());
  119. Db::commit();
  120. return app('json')->successful('支付成功');
  121. } catch (\Exception $e) {
  122. Db::rollback();
  123. return app('json')->fail('支付失败');
  124. }
  125. }
  126. /**
  127. * 挂售详情
  128. * @param Request $request
  129. * @return mixed
  130. * @throws \think\db\exception\DataNotFoundException
  131. * @throws \think\db\exception\DbException
  132. * @throws \think\db\exception\ModelNotFoundException
  133. */
  134. public function gsxq(Request $request)
  135. {
  136. $data = UtilService::postMore([
  137. ['id']
  138. ]);
  139. $product = AuctionProduct::where('id', $data['id'])->find();
  140. $data['aid_val'] = round($product['hanging_price'] * 0.0485, 2); // 需要的广告值
  141. $data['hanging_price'] = round((int)$product['hanging_price'] + $product['hanging_price'] * 0.06, 2); // 第二天溢价
  142. return app('json')->successful($data);
  143. }
  144. /**
  145. * 挂售商品
  146. * @param Request $request
  147. * @return mixed
  148. * @throws \think\db\exception\DataNotFoundException
  149. * @throws \think\db\exception\DbException
  150. * @throws \think\db\exception\ModelNotFoundException
  151. */
  152. public function hanging_sale(Request $request)
  153. {
  154. $data = UtilService::postMore([
  155. ['id']
  156. ]);
  157. $product = AuctionProduct::where('id', $data['id'])->find();
  158. if ($product['is_show'] == 1) return app('json')->fail('商品已挂售');
  159. $user = User::where('uid', $request->uid())->find();
  160. if (!$product) return app('json')->fail('商品不存在');
  161. $aid_val = round($product['hanging_price'] * 0.0485,2); // 需要的广告值
  162. $hanging_price = round((int)$product['hanging_price'] + $product['hanging_price'] * 0.06, 2); // 第二天溢价
  163. if ($user['aid_val'] < $aid_val) return app('json')->fail('挂售需要广告值不足');
  164. $product['hanging_price'] = $hanging_price;
  165. $product['is_show'] = 1;
  166. $user['aid_val'] -= $aid_val;
  167. try {
  168. Db::startTrans();
  169. $product->save();
  170. $user->save();
  171. UserBill::expend('挂售扣除广告值', $user['uid'], 'aid_val', 'sub_aid_val', $aid_val, $user['spread_uid'], $user['aid_val'], '挂售商品扣除广告值');
  172. Db::commit();
  173. return app('json')->successful('挂售成功');
  174. } catch (\Exception $e) {
  175. Db::rollback();
  176. return app('json')->fail('挂售失败');
  177. }
  178. }
  179. /**
  180. * 商品详情
  181. * @param Request $request
  182. * @return mixed
  183. * @throws \think\db\exception\DataNotFoundException
  184. * @throws \think\db\exception\DbException
  185. * @throws \think\db\exception\ModelNotFoundException
  186. *
  187. */
  188. public function product(Request $request)
  189. {
  190. $data = UtilService::getMore([
  191. ['id']
  192. ]);
  193. $product = AuctionProduct::where('id', $data['id'])->find();
  194. if (!$product) return app('json')->fail('没有此商品');
  195. $product = $product->toArray();
  196. return app('json')->successful($product);
  197. }
  198. }