AuctionProductController.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. namespace app\api\controller\auction;
  3. use app\admin\model\system\SystemConfig;
  4. use app\common\model\Config;
  5. use app\models\auction\Auction;
  6. use app\models\auction\AuctionBooking;
  7. use app\models\auction\AuctionOrder;
  8. use app\models\auction\AuctionProduct;
  9. use app\models\user\User;
  10. use app\models\user\UserBill;
  11. use app\Request;
  12. use Monolog\Handler\Curl\Util;
  13. use think\facade\Cache;
  14. use crmeb\services\{
  15. CacheService,
  16. ExpressService,
  17. SystemConfigService,
  18. };
  19. use crmeb\services\UtilService;
  20. use crmeb\repositories\OrderRepository;
  21. class AuctionProductController
  22. {
  23. /**
  24. * 获取商品列表
  25. * @param Request $request
  26. * @return mixed
  27. * @throws \think\db\exception\DataNotFoundException
  28. * @throws \think\db\exception\DbException
  29. * @throws \think\db\exception\ModelNotFoundException
  30. */
  31. public function auction_product(Request $request)
  32. {
  33. $data = UtilService::getMore([
  34. [['page', 'd'], 0],
  35. [['limit', 'd'], 0],
  36. ['id'],
  37. ['name']
  38. ]);
  39. if (!$data['id']) return app('json')->fail('数据传入错误');
  40. return app('json')->successful(AuctionProduct::list($data, $request->uid()));
  41. }
  42. /**
  43. * 用户商品
  44. * @param Request $request
  45. * @return mixed
  46. * @throws \think\db\exception\DataNotFoundException
  47. * @throws \think\db\exception\DbException
  48. * @throws \think\db\exception\ModelNotFoundException
  49. */
  50. public function user_product(Request $request)
  51. {
  52. $data = UtilService::getMore([
  53. [['page', 'd'], 0],
  54. [['limit', 'd'], 0],
  55. ]);
  56. return app('json')->successful(AuctionProduct::user_product( $data,$request->uid()));
  57. }
  58. /**
  59. * 购买商品
  60. * @param Request $request
  61. * @return mixed
  62. * @throws \think\db\exception\DataNotFoundException
  63. * @throws \think\db\exception\DbException
  64. * @throws \think\db\exception\ModelNotFoundException
  65. */
  66. public function purchase(Request $request)
  67. {
  68. $data = UtilService::getMore([
  69. ['product_id'],
  70. ]);
  71. if (!$data['product_id']) return app('json')->fail('数据传入错误');
  72. $product = AuctionProduct::where('id', $data['product_id'])->find();
  73. $product_ids = AuctionProduct::where('auction_id', $product['auction_id'])->column('id');
  74. $auction = Auction::where('id', $product['auction_id'])->find();
  75. $count = AuctionOrder::where('frequency',$auction['frequency'])->where('product_id', 'in', $product_ids)->count();
  76. // $config = SystemConfig::where('menu_name', 'auction_number')->find();
  77. // if ($count >= (int)$config['value']) return app('json')->fail('单场购买数量已到达最大');
  78. if ($product['uid'] == $request->uid()) return app('json')->fail('无法购买自己商品');
  79. if ($product){
  80. AuctionOrder::beginTrans();
  81. // 查询商品是否以卖出
  82. $order = AuctionOrder::where('product_id', $data['product_id'])->where('status', '>', 0)->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();
  83. if ($order){
  84. return app('json')->fail('商品已卖出');
  85. }
  86. $res = AuctionOrder::create([
  87. 'uid' => $request->uid(),
  88. 'collection_id' => $product['uid'],// 商品拥有有人
  89. 'order_id' => getNewOrderId(),
  90. 'name' => $product['name'],
  91. 'product_id' => $product['id'],
  92. 'image'=> $product['image'],
  93. 'price' => $product['hanging_price'],
  94. 'frequency' => $auction['frequency']
  95. ]);
  96. if ($res){
  97. AuctionOrder::commitTrans();
  98. return app('json')->successful('购买成功');
  99. }else{
  100. AuctionOrder::rollbackTrans();
  101. return app('json')->fail('购买失败');
  102. }
  103. }else{
  104. return app('json')->fail('购买商品不存在');
  105. }
  106. }
  107. /**
  108. * 获取用户竞拍订单
  109. * @param Request $request
  110. * @return mixed
  111. * @throws \think\db\exception\DataNotFoundException
  112. * @throws \think\db\exception\DbException
  113. * @throws \think\db\exception\ModelNotFoundException
  114. */
  115. public function user_auction_order(Request $request)
  116. {
  117. $data = UtilService::getMore([
  118. [['type', 'd'], 0],
  119. [['page', 'd'], 0],
  120. [['limit', 'd'], 0],
  121. ['order_id']
  122. ]);
  123. return app('json')->successful(AuctionOrder::userOrder($data,$request->uid()));
  124. }
  125. /**
  126. * 上传打款凭证
  127. * @param Request $request
  128. * @return mixed
  129. * @throws \think\db\exception\DataNotFoundException
  130. * @throws \think\db\exception\DbException
  131. * @throws \think\db\exception\ModelNotFoundException
  132. */
  133. public function up_image(Request $request)
  134. {
  135. $data = UtilService::getMore([
  136. ['image'],
  137. ['id']
  138. ]);
  139. if (!$data['image'] || !$data['id']) return app('json')->fail('数据传入错误');
  140. $order = AuctionOrder::where('id', $data['id'])->find();
  141. if (!$order) return app('json')->fail('订单不存在');
  142. if ($order['status'] != 1) return app('json')->fail('当前订单状态无法上传凭证');
  143. $order['upload_image'] = $data['image'];
  144. $order['status'] = 2;
  145. if ($order->save()){
  146. return app('json')->successful('上传成功');
  147. }else{
  148. return app('json')->fail('上传失败');
  149. }
  150. }
  151. /**
  152. * 卖家显示订单
  153. * @param Request $request
  154. * @return void
  155. */
  156. public function seller(Request $request)
  157. {
  158. $data = UtilService::getMore([
  159. ['type', 0],
  160. [['page', 'd'], 0],
  161. [['limit', 'd'], 0],
  162. ['order_id']
  163. ]);
  164. return app('json')->successful(AuctionOrder::seller_list($data,$request->uid()));
  165. }
  166. /**
  167. * 确定订单
  168. * @param Request $request
  169. * @return void
  170. */
  171. public function adopt(Request $request){
  172. $data = UtilService::postMore([
  173. ['order_id']
  174. ]);
  175. if (!$data['order_id']) return app('json')->fail('数据传入错误');
  176. $order = AuctionOrder::where('order_id', $data['order_id'])->find();
  177. if ($order['status'] < 1) return app('json')->fail('该订单已失效');
  178. if ($order['status'] == 1) return app('json')->fail('未上传打款凭证');
  179. if ($order['status'] == 3) return app('json')->fail('该订单已完成');
  180. $order['status'] = 3;
  181. AuctionOrder::beginTrans();
  182. $res = $order->save();
  183. if ($res){
  184. $product = AuctionProduct::find($order['product_id']);
  185. if (!$product) return app('json')->fail('数据不存在');
  186. $uid = $product['uid']; // 所属人id
  187. $product['uid'] = $order['uid'];// 商品拥有人更新
  188. $res = $product->save();
  189. if ($res){
  190. if ($uid > 0){
  191. AuctionOrder::earn($uid,$order['price'] ,$product); // 卖家
  192. }
  193. }
  194. AuctionOrder::return($order['id']); // 买家
  195. AuctionOrder::commitTrans();
  196. return app('json')->successful('完成');
  197. }else{
  198. AuctionOrder::rollbackTrans();
  199. return app('json')->fail('失败');
  200. }
  201. }
  202. /**
  203. * 产品详情
  204. * @param Request $request
  205. * @return mixed
  206. * @throws \think\db\exception\DataNotFoundException
  207. * @throws \think\db\exception\DbException
  208. * @throws \think\db\exception\ModelNotFoundException
  209. */
  210. public function details(Request $request)
  211. {
  212. $data = UtilService::getMore([
  213. ['product_id']
  214. ]);
  215. if (!$data['product_id']) return app('json')->fail('数据传入错误');
  216. $details = AuctionProduct::where('id', $data['product_id'])->find();
  217. if (empty($details)) return app('json')->fail('商品不存在');
  218. $details['slider_image'] = is_string($details['slider_image']) ? json_decode($details['slider_image'], true) : [];
  219. $details['description'] = !empty($details['description']) ? html_entity_decode($details['description'], ENT_COMPAT) : [];
  220. $details['user_nickname'] = User::where('uid', $details['auction_id'])->find()['nickname'];
  221. $auction = Auction::where('id', $details['auction_id'])->find();
  222. $details['time'] = $auction['radd_time'].'-'.$auction['rend_time'];
  223. $details = $details->toArray();
  224. return app('json')->successful($details);
  225. }
  226. /**商品以前属于人
  227. * @param Request $request
  228. * @return mixed
  229. */
  230. public function belong(Request $request)
  231. {
  232. $data = UtilService::getMore([
  233. ['product_id']
  234. ]);
  235. if (!$data['product_id']) return app('json')->fail('数据传入错误');
  236. $order = AuctionOrder::alias('a')
  237. ->field('u.nickname,u.avatar,a.create_time')
  238. ->leftJoin('user u', 'a.collection_id = u.uid')
  239. ->where('a.product_id', $data['product_id'])->where('a.status', 3)->select();
  240. $order = empty($order)? [] : $order->toArray();
  241. return app('json')->successful($order);
  242. }
  243. }