AuctionProductController.php 6.7 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\user\User;
  8. use app\models\user\UserBill;
  9. use app\Request;
  10. use Monolog\Handler\Curl\Util;
  11. use think\facade\Cache;
  12. use crmeb\services\{
  13. CacheService,
  14. ExpressService,
  15. SystemConfigService,
  16. };
  17. use crmeb\services\UtilService;
  18. use crmeb\repositories\OrderRepository;
  19. class AuctionProductController
  20. {
  21. /**
  22. * 获取商品列表
  23. * @param Request $request
  24. * @return mixed
  25. * @throws \think\db\exception\DataNotFoundException
  26. * @throws \think\db\exception\DbException
  27. * @throws \think\db\exception\ModelNotFoundException
  28. */
  29. public function auction_product(Request $request)
  30. {
  31. $data = UtilService::getMore([
  32. [['page', 'd'], 0],
  33. [['limit', 'd'], 0],
  34. ['id']
  35. ]);
  36. if (!$data['id']) return app('json')->fail('数据传入错误');
  37. return app('json')->successful(AuctionProduct::list($data, $request->uid()));
  38. }
  39. /**
  40. * 用户商品
  41. * @param Request $request
  42. * @return mixed
  43. * @throws \think\db\exception\DataNotFoundException
  44. * @throws \think\db\exception\DbException
  45. * @throws \think\db\exception\ModelNotFoundException
  46. */
  47. public function user_product(Request $request)
  48. {
  49. $data = UtilService::getMore([
  50. [['page', 'd'], 0],
  51. [['limit', 'd'], 0],
  52. ]);
  53. return app('json')->successful(AuctionProduct::user_product( $data,$request->uid()));
  54. }
  55. /**
  56. * 购买商品
  57. * @param Request $request
  58. * @return mixed
  59. * @throws \think\db\exception\DataNotFoundException
  60. * @throws \think\db\exception\DbException
  61. * @throws \think\db\exception\ModelNotFoundException
  62. */
  63. public function purchase(Request $request)
  64. {
  65. $data = UtilService::getMore([
  66. ['product_id'],
  67. ]);
  68. if (!$data['product_id']) return app('json')->fail('数据传入错误');
  69. $product = AuctionProduct::where('id', $data['product_id'])->find();
  70. if ($product['uid'] == $request->uid()) return app('json')->fail('无法购买自己商品');
  71. if ($product){
  72. AuctionOrder::beginTrans();
  73. // 查询商品是否以卖出
  74. $order = AuctionOrder::where('product_id', $data['product_id'])->whereBetweenTime('create_time', date('Y-m-d H:i:s', strtotime(date('Y-m-d'))), date('Y-m-d H:i:s', strtotime('+1 day')))->find();
  75. if ($order){
  76. return app('json')->fail('商品以卖出');
  77. }
  78. $res = AuctionOrder::create([
  79. 'uid' => $request->uid(),
  80. 'collection_id' => $product['uid'],// 商品用有人
  81. 'order_id' => getNewOrderId(),
  82. 'name' => $product['name'],
  83. 'product_id' => $product['id'],
  84. 'image'=> $product['image'],
  85. 'price' => $product['hanging_price'],
  86. ]);
  87. if ($res){
  88. AuctionOrder::commitTrans();
  89. return app('json')->successful('购买成功');
  90. }else{
  91. AuctionOrder::rollbackTrans();
  92. return app('json')->fail('购买失败');
  93. }
  94. }else{
  95. return app('json')->fail('购买商品不存在');
  96. }
  97. }
  98. /**
  99. * 获取用户竞拍订单
  100. * @param Request $request
  101. * @return mixed
  102. * @throws \think\db\exception\DataNotFoundException
  103. * @throws \think\db\exception\DbException
  104. * @throws \think\db\exception\ModelNotFoundException
  105. */
  106. public function user_auction_order(Request $request)
  107. {
  108. $data = UtilService::getMore([
  109. [['type', 'd'], 0],
  110. [['page', 'd'], 0],
  111. [['limit', 'd'], 0],
  112. ]);
  113. return app('json')->successful(AuctionOrder::userOrder($data,$request->uid()));
  114. }
  115. /**
  116. * 上传大框凭证
  117. * @param Request $request
  118. * @return mixed
  119. * @throws \think\db\exception\DataNotFoundException
  120. * @throws \think\db\exception\DbException
  121. * @throws \think\db\exception\ModelNotFoundException
  122. */
  123. public function up_image(Request $request)
  124. {
  125. $data = UtilService::getMore([
  126. ['image'],
  127. ['id']
  128. ]);
  129. if (!$data['image'] || !$data['id']) return app('json')->fail('数据传入错误');
  130. $order = AuctionOrder::where('id', $data['id'])->find();
  131. if (!$order) return app('json')->fail('订单不存在');
  132. if ($order['status'] != 1) return app('json')->fail('当前订单状态无法上传凭证');
  133. $order['upload_image'] = $data['image'];
  134. $order['status'] = 2;
  135. if ($order->save()){
  136. return app('json')->successful('上传成功');
  137. }else{
  138. return app('json')->fail('上传失败');
  139. }
  140. }
  141. /**
  142. * 卖家显示订单
  143. * @param Request $request
  144. * @return void
  145. */
  146. public function seller(Request $request)
  147. {
  148. $data = UtilService::getMore([
  149. ['type', 0]
  150. ]);
  151. return app('json')->successful(AuctionOrder::seller_list($data,$request->uid()));
  152. }
  153. /**
  154. * 确定订单
  155. * @param Request $request
  156. * @return void
  157. */
  158. public function adopt(Request $request){
  159. $data = UtilService::postMore([
  160. ['order_id']
  161. ]);
  162. if (!$data['order_id']) return app('json')->fail('数据传入错误');
  163. $order = AuctionOrder::where('order_id', $data['order_id'])->find();
  164. if ($order['status'] < 1) return app('json')->fail('该订单已失效');
  165. if ($order['status'] == 1) return app('json')->fail('未上传打款凭证');
  166. if ($order['status'] == 3) return app('json')->fail('该订单已完成');
  167. $order['status'] = 3;
  168. AuctionOrder::beginTrans();
  169. $res = $order->save();
  170. if ($res){
  171. $product = AuctionProduct::find($order['product_id']);
  172. if (!$product) return app('json')->fail('数据不存在');
  173. $uid = $product['uid']; // 所属人id
  174. $product['uid'] = $order['uid'];// 商品拥有人更新
  175. $res = $product->save();
  176. if ($res){
  177. if ($uid > 0){
  178. AuctionOrder::earn($uid,$order['price'] ,$product); // 卖家
  179. }
  180. }
  181. AuctionOrder::return($order['id']); // 买家
  182. AuctionOrder::commitTrans();
  183. return app('json')->successful('完成');
  184. }else{
  185. AuctionOrder::rollbackTrans();
  186. return app('json')->fail('失败');
  187. }
  188. }
  189. }