AuctionProductController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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\AuctionFrozen;
  8. use app\models\auction\AuctionOrder;
  9. use app\models\auction\AuctionProduct;
  10. use app\models\auction\AuctionTime;
  11. use app\models\user\User;
  12. use app\models\user\UserBill;
  13. use app\Request;
  14. use Monolog\Handler\Curl\Util;
  15. use think\facade\Cache;
  16. use crmeb\services\{
  17. CacheService,
  18. ExpressService,
  19. SystemConfigService
  20. };
  21. use crmeb\services\UtilService;
  22. use crmeb\repositories\OrderRepository;
  23. use think\facade\Db;
  24. class AuctionProductController
  25. {
  26. /**
  27. * 获取商品列表
  28. * @param Request $request
  29. * @return mixed
  30. * @throws \think\db\exception\DataNotFoundException
  31. * @throws \think\db\exception\DbException
  32. * @throws \think\db\exception\ModelNotFoundException
  33. */
  34. public function auction_product(Request $request)
  35. {
  36. $data = UtilService::getMore([
  37. [['page', 'd'], 0],
  38. [['limit', 'd'], 0],
  39. ['id'],
  40. ['name']
  41. ]);
  42. if (!$data['id']) return app('json')->fail('数据传入错误');
  43. return app('json')->successful(AuctionProduct::list($data, $request->uid()));
  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 purchase(Request $request)
  70. {
  71. $data = UtilService::getMore([
  72. ['product_id'],
  73. ]);
  74. if (!$data['product_id']) return app('json')->fail('数据传入错误');
  75. $product = AuctionProduct::where('id', $data['product_id'])->find();
  76. if ($product['is_show'] == 0) return app('json')->fail('商品未挂售');
  77. $auction = Auction::where('id', $product['auction_id'])->find();
  78. $user = $request->user();
  79. $time = strtotime(date('Y-m-d', time()));// 今天
  80. $today = strtotime(date('Y-m-d', strtotime('+1day')));// 明天
  81. $radd_time = strtotime($auction['radd_time']); // 抢购时间
  82. if ($user['is_new'] == 1 or ($user['green_time'] >= $time and $user['green_time'] <= $today)){
  83. // 新人或者绿色通道提前三分钟入场
  84. if ($radd_time - 180 > time()){
  85. return app('json')->fail('未到抢购时间');
  86. }
  87. }else if($radd_time > time()){
  88. return app('json')->fail('当前未到抢购时间');
  89. }
  90. $count = AuctionOrder::where('frequency',$auction['frequency'])->where('uid', $request->uid())->where('auction_id', '=', $auction['id'])->count();// 查找今天订单数量
  91. $orderCount = AuctionOrder::where([['uid', '=', $request->uid()]])->whereBetweenTime('create_time', $time, $today)->count(); // 查找今天抢购的订单数量;
  92. $config = SystemConfigService::get('auction_number');
  93. $max = SystemConfigService::get('maximum');
  94. if ($orderCount >= (int)$max) return app('json')->fail('今日抢购数量已达到最大'); // 查看当前是否已到最大
  95. if ($count >= (int)$config) return app('json')->fail('单场购买数量已到达最大'); // 查看当前是否已到最大
  96. if ($product['uid'] == $request->uid()) return app('json')->fail('无法购买自己商品');
  97. if ($product){
  98. AuctionOrder::beginTrans();
  99. // 查询商品是否以卖出
  100. $order = AuctionOrder::where('product_id', $data['product_id'])->where('status', '>', 0)->where('frequency', $auction['frequency'])->find();
  101. if ($order){
  102. return app('json')->fail('商品已卖出');
  103. }
  104. $order_id = getNewOrderId();
  105. if ($count >= 1){
  106. if ($user['anticipate'] < 200) return app('json')->fail('艺金券不足');
  107. $user['anticipate'] -= 200;
  108. User::where('uid', $user['uid'])->update(['anticipate' => $user['anticipate']]);
  109. UserBill::expend('冻结艺金券',$user['uid'], 'anticipate', 'fz_anticipate', 200, $user['spread_uid'], $user['anticipate'], '购买订单'.$order_id.'冻结艺金券');
  110. AuctionFrozen::create([
  111. 'order_id' => $order_id,
  112. 'uid' => $request->uid(),
  113. 'anticipate' => 200
  114. ]);
  115. }
  116. $res = AuctionOrder::create([
  117. 'uid' => $request->uid(),
  118. 'collection_id' => $product['uid'],// 商品拥有有人
  119. 'order_id' => $order_id,
  120. 'name' => $product['name'],
  121. 'product_id' => $product['id'],
  122. 'auction_id' => $auction['id'],
  123. 'image'=> $product['image'],
  124. 'price' => $product['hanging_price'],
  125. 'frequency' => $auction['frequency']
  126. ]);
  127. if ($res){
  128. AuctionOrder::commitTrans();
  129. return app('json')->successful('购买成功');
  130. }else{
  131. AuctionOrder::rollbackTrans();
  132. return app('json')->fail('购买失败');
  133. }
  134. }else{
  135. return app('json')->fail('购买商品不存在');
  136. }
  137. }
  138. /**
  139. * 获取用户竞拍订单
  140. * @param Request $request
  141. * @return mixed
  142. * @throws \think\db\exception\DataNotFoundException
  143. * @throws \think\db\exception\DbException
  144. * @throws \think\db\exception\ModelNotFoundException
  145. */
  146. public function user_auction_order(Request $request)
  147. {
  148. $data = UtilService::getMore([
  149. [['type', 'd'], 0],
  150. [['page', 'd'], 0],
  151. [['limit', 'd'], 0],
  152. ['order_id']
  153. ]);
  154. return app('json')->successful(AuctionOrder::userOrder($data,$request->uid()));
  155. }
  156. /**
  157. * 上传打款凭证
  158. * @param Request $request
  159. * @return mixed
  160. * @throws \think\db\exception\DataNotFoundException
  161. * @throws \think\db\exception\DbException
  162. * @throws \think\db\exception\ModelNotFoundException
  163. */
  164. public function up_image(Request $request)
  165. {
  166. $data = UtilService::getMore([
  167. ['image'],
  168. ['id']
  169. ]);
  170. if (!$data['image'] || !$data['id']) return app('json')->fail('请上传打款凭证');
  171. $order = AuctionOrder::where('id', $data['id'])->find();
  172. if (!$order) return app('json')->fail('订单不存在');
  173. if ($order['status'] != 1) return app('json')->fail('当前订单状态无法上传凭证');
  174. $order['upload_image'] = $data['image'];
  175. $order['status'] = 2;
  176. $order['voucher_time'] = time();
  177. if ($order->save()){
  178. return app('json')->successful('上传成功');
  179. }else{
  180. return app('json')->fail('上传失败');
  181. }
  182. }
  183. /**
  184. * 卖家显示订单
  185. * @param Request $request
  186. * @return void
  187. */
  188. public function seller(Request $request)
  189. {
  190. $data = UtilService::getMore([
  191. ['type', 0],
  192. [['page', 'd'], 0],
  193. [['limit', 'd'], 0],
  194. ['order_id']
  195. ]);
  196. return app('json')->successful(AuctionOrder::seller_list($data,$request->uid()));
  197. }
  198. /**
  199. * 确定订单
  200. * @param Request $request
  201. * @return void
  202. */
  203. public function adopt(Request $request){
  204. $data = UtilService::postMore([
  205. ['order_id']
  206. ]);
  207. if (!$data['order_id']) return app('json')->fail('数据传入错误');
  208. $order = AuctionOrder::where('order_id', $data['order_id'])->find();
  209. if ($order['status'] < 1) return app('json')->fail('该订单已失效');
  210. if ($order['status'] == 1) return app('json')->fail('未上传打款凭证');
  211. if ($order['status'] == 3) return app('json')->fail('该订单已完成');
  212. $order['status'] = 3;
  213. AuctionOrder::beginTrans();
  214. $res = $order->save();
  215. if ($res){
  216. $product = AuctionProduct::find($order['product_id']);
  217. if (!$product) return app('json')->fail('数据不存在');
  218. $uid = $product['uid']; // 所属人id
  219. $product['uid'] = $order['uid'];// 商品拥有人更新
  220. $product['add_time'] = time();
  221. $product['order'] = $data['order_id'];
  222. $res = $product->save();
  223. if ($res){
  224. if ($uid > 0){
  225. AuctionOrder::earn($uid,$order['price'] ,$product); // 卖家
  226. }
  227. }
  228. AuctionOrder::return($order['id']); // 买家
  229. AuctionOrder::commitTrans();
  230. return app('json')->successful('完成');
  231. }else{
  232. AuctionOrder::rollbackTrans();
  233. return app('json')->fail('失败');
  234. }
  235. }
  236. /**
  237. * 产品详情
  238. * @param Request $request
  239. * @return mixed
  240. * @throws \think\db\exception\DataNotFoundException
  241. * @throws \think\db\exception\DbException
  242. * @throws \think\db\exception\ModelNotFoundException
  243. */
  244. public function details(Request $request)
  245. {
  246. $data = UtilService::getMore([
  247. ['product_id']
  248. ]);
  249. if (!$data['product_id']) return app('json')->fail('数据传入错误');
  250. $details = AuctionProduct::where('id', $data['product_id'])->find();
  251. if (empty($details)) return app('json')->fail('商品不存在');
  252. $details['slider_image'] = is_string($details['slider_image']) ? json_decode($details['slider_image'], true) : [];
  253. $details['description'] = !empty($details['description']) ? html_entity_decode($details['description'], ENT_COMPAT) : '';
  254. $details['user_nickname'] = User::where('uid', $details['uid'])->find()['nickname'];
  255. $auction = Auction::where('id', $details['auction_id'])->find();
  256. $details['time'] = $auction['radd_time'].'-'.$auction['rend_time'];
  257. $details['times'] = strtotime($auction['radd_time']);
  258. $details = $details->toArray();
  259. return app('json')->successful($details);
  260. }
  261. /**商品以前属于人
  262. * @param Request $request
  263. * @return mixed
  264. */
  265. public function belong(Request $request)
  266. {
  267. $data = UtilService::getMore([
  268. ['product_id']
  269. ]);
  270. if (!$data['product_id']) return app('json')->fail('数据传入错误');
  271. $order = AuctionOrder::alias('a')
  272. ->field('u.nickname,u.avatar,a.create_time')
  273. ->leftJoin('user u', 'a.collection_id = u.uid')
  274. ->where('a.product_id', $data['product_id'])->where('a.status', 3)->select();
  275. if (empty($order->toArray())) $order = AuctionProduct::alias('a')
  276. ->field('u.nickname,u.avatar,a.create_time')
  277. ->leftJoin('user u', 'a.uid = u.uid')
  278. ->where('a.id', $data['product_id'])->select();
  279. $order = empty($order)? [] : $order->toArray();
  280. return app('json')->successful($order);
  281. }
  282. /**
  283. * 挂售详情
  284. * @param Request $request
  285. * @return mixed
  286. * @throws \think\db\exception\DataNotFoundException
  287. * @throws \think\db\exception\DbException
  288. * @throws \think\db\exception\ModelNotFoundException
  289. */
  290. public function gsxq(Request $request)
  291. {
  292. $data = UtilService::getMore([
  293. ['id']
  294. ]);
  295. $product = AuctionProduct::where('id', $data['id'])->find();
  296. if (!$product) return app('json')->fail('商品不存在');
  297. $data = AuctionProduct::bs($data['id']);
  298. return app('json')->successful($data);
  299. }
  300. /**
  301. * 挂售商品
  302. * @param Request $request
  303. * @return mixed
  304. * @throws \think\db\exception\DataNotFoundException
  305. * @throws \think\db\exception\DbException
  306. * @throws \think\db\exception\ModelNotFoundException
  307. */
  308. public function hanging_sale(Request $request)
  309. {
  310. $data = UtilService::postMore([
  311. ['id'],
  312. ]);
  313. $product = AuctionProduct::where('id', $data['id'])->where('uid', $request->uid())->find();
  314. $user = User::where('uid', $request->uid())->find();
  315. if (!$product) return app('json')->fail('商品不存在');
  316. if ($product['frozen'] == 1) return app('json')->fail('商品已冻结,无法挂售');
  317. if ($product['is_show'] == 1) return app('json')->fail('商品已挂售');
  318. $datas = AuctionProduct::bs($data['id']);// 获取挂售详情
  319. if ($user['anticipate'] < $datas['anticipate']) return app('json')->fail('预约券不足');
  320. $product['price'] = $product['hanging_price'];
  321. $product['hanging_price'] = $datas['hanging_price']; // 商品变为挂售价格
  322. $product['is_show'] = 1; // 上架
  323. $product['is_admin'] = 2;
  324. $user['anticipate'] -= $datas['anticipate']; // 扣除预约券
  325. $user['integral'] += $datas['give'];
  326. try {
  327. Db::startTrans();
  328. // 新增挂售时间段
  329. AuctionTime::create([
  330. 'uid' => $request->uid(),
  331. 'product_id' => $product['id'],
  332. 'auction_id' => $product['auction_id'],
  333. 'add_time' => strtotime($datas['gs_time'])
  334. ]);
  335. $product->save();
  336. $user->save();
  337. AuctionOrder::where([['uid', '=', $request->uid()], ['product_id', '=', $product['id']], ['is_gs', '=', 0]])->update(['is_gs' => 1]);// 修改订单挂售
  338. UserBill::expend('挂售扣除预约券', $user['uid'], 'anticipate', 'gs_anticipate', $datas['anticipate'], $user['spread_uid'], $user['anticipate'], '挂售扣除艺金券');
  339. UserBill::income('赠送趣豆', $user['uid'], 'integral', 'gs_integral', $datas['give'], $user['spread_uid'], $user['integral'], '挂售赠送趣豆');
  340. Db::commit();
  341. return app('json')->successful('挂售成功');
  342. } catch (\Exception $e) {
  343. Db::rollback();
  344. return app('json')->fail('挂售失败');
  345. }
  346. }
  347. /**
  348. * 取消挂售
  349. * @param Request $request
  350. * @return void
  351. * @throws \think\db\exception\DataNotFoundException
  352. * @throws \think\db\exception\DbException
  353. * @throws \think\db\exception\ModelNotFoundException
  354. */
  355. public function cancel(Request $request)
  356. {
  357. $data = UtilService::postMore([
  358. ['id']
  359. ]);
  360. $productModel = new AuctionProduct();
  361. $details = $productModel->find($data['id']);
  362. if (!$details) return app('json')->fail('商品不存在');
  363. $order = AuctionOrder::where([['product_id', '=', $details['id']], ['status', '<', 3], ['status', '>', 0]])->find();
  364. if ($order) return app('json')->fail('商品已出售,无法取消');
  365. $auction = Auction::where('id', $details['auction_id'])->find();
  366. $data['time'] = explode(',', $auction['site']);
  367. $date = date('w', time()); // 今天星期几
  368. $bs = 0;
  369. if (in_array(1, $data['time'])){
  370. if ($date >= 1 and $date <= 2){
  371. $bs = 3 - $date;
  372. }elseif ($date >= 3 and $date <= 4){
  373. $bs = 5 - $date;
  374. }elseif ($date >= 5 or $date == 0){
  375. if ($date == 5) $bs = 3;
  376. if ($date == 6) $bs = 2;
  377. if ($date == 0) $bs = 1;
  378. }
  379. }else if (in_array(2, $data['time'])){
  380. if ($date >= 2 and $date <= 3){
  381. $bs = 4 - $date;
  382. }elseif ($date >= 4 and $date <= 5){
  383. $bs = 6 - $date;
  384. }elseif ($date >= 6 or $date == 0 or $date == 1){
  385. if ($date == 6) $bs = 3;
  386. if ($date == 0) $bs = 2;
  387. if ($date == 1) $bs = 1;
  388. }
  389. }
  390. $user = User::where('uid', $request->uid())->find();
  391. $anticipate = round($details['price'] * $details['deduct']/100, 2);
  392. $details['hanging_price'] = $details['price']; // 价格变=回未挂售前;
  393. $details['is_show'] = 0; // 价格变回未挂售前;
  394. $user['anticipate'] += $anticipate*$bs;// 退回挂售扣除的预约预约券
  395. $user['integral'] -= round(($details['price'] * $details['give']/100)*$bs, 2);// 挂售赠送趣豆
  396. try {
  397. Db::startTrans();
  398. $details->save();
  399. $user->save();
  400. AuctionOrder::where('order_id', $details['order'])->save(['is_gs' => 0]);
  401. UserBill::income('取消挂售退回预约券', $request->uid(), 'anticipate', 'gsth_anticipate', $anticipate*$bs, 0, $user['anticipate'], '取消挂售退回预约券');
  402. UserBill::expend('取消挂售扣除趣豆', $request->uid(), 'integral', 'gsth_integral', round(($details['price'] * $details['give']/100)*$bs, 2), 0, $user['integral'], '取消挂售扣除赠送趣豆');
  403. AuctionTime::where([['uid', '=', $request->uid()], ['product_id', '=', $data['id']]])->delete(); // 删除上架时间
  404. Db::commit();
  405. return app('json')->successful('取消挂售成功');
  406. } catch (\Exception $e) {
  407. Db::rollback();
  408. return app('json')->fail('取消挂售失败');
  409. }
  410. }
  411. /**
  412. * 订单数量
  413. * @param Request $request
  414. * @return mixed
  415. */
  416. public function untreated(Request $request){
  417. $data = [
  418. 'user' => [
  419. 'paid' => AuctionOrder::where([['uid', '=', $request->uid()], ['status', '=', 1]])->count(), //待支付
  420. 'reviewed' => AuctionOrder::where([['uid', '=', $request->uid()], ['status', '=', 2]])->count(), // 待审核
  421. 'hanging' => AuctionOrder::where([['uid', '=', $request->uid()], ['status', '=', 3], ['is_gs', '=', 0]])->count(), // 待挂售
  422. ],
  423. 'seller' => [
  424. 'paid' => AuctionOrder::where([['collection_id', '=', $request->uid()], ['status', '=', 1]])->count(), //待支付
  425. 'reviewed' => AuctionOrder::where([['collection_id', '=', $request->uid()], ['status', '=', 2]])->count(), // 待审核
  426. 'hanging' => AuctionOrder::where([['uid', '=', $request->uid()], ['status', '=', 3], ['is_gs', '=', 1]])->count(), //挂售中
  427. ]
  428. ];
  429. return app('json')->successful($data);
  430. }
  431. }