AuctionProductController.php 19 KB

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