AuctionController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <?php
  2. namespace app\api\controller\auction;
  3. use app\models\auction\Auction;
  4. use app\models\auction\AuctionApply;
  5. use app\models\auction\AuctionBooking;
  6. use app\models\auction\AuctionGu;
  7. use app\models\auction\AuctionOrder;
  8. use app\models\auction\AuctionPay;
  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. use think\facade\Db;
  22. class AuctionController
  23. {
  24. /**
  25. * 场馆列表
  26. * @param Request $request
  27. * @return mixed
  28. */
  29. public function list(Request $request)
  30. {
  31. $data = UtilService::getMore([
  32. [['page', 'd'], 0],
  33. [['limit', 'd'], 0],
  34. ['advance'],
  35. ['auction_gu_id']
  36. ], $request);
  37. if (!$data['auction_gu_id']) return app('json')->fail('数据传入错误');
  38. $auctionModel = new \app\models\auction\Auction();
  39. return app('json')->successful($auctionModel->list($data, $request->uid()));
  40. }
  41. /**
  42. * 预约场馆
  43. * @param Request $request
  44. * @return void
  45. */
  46. public function subscribe(Request $request)
  47. {
  48. $data = UtilService::postMore([
  49. ['id']
  50. ]);
  51. if (!$data['id']) return app('json')->fail('数据传入错误');
  52. $auctionModel = new Auction();
  53. $auction = $auctionModel->find($data['id']);
  54. if (!$auction)return app('json')->fail('没有此数据');
  55. if (time() < strtotime($auction['add_time'])){
  56. return app('json')->fail('预约未开始');
  57. }
  58. if (time() > strtotime($auction['end_time'])){
  59. return app('json')->fail('预约时间已过');
  60. }
  61. if (AuctionBooking::where([['uid', '=', $request->uid()], ['auction_id' , '=', $auction['id']], ['frequency', '=', $auction['frequency']]])->find()){
  62. return app('json')->fail('当前场馆已预约');
  63. }
  64. $userModel = new User();
  65. $user = $userModel->find($request->uid());
  66. if ($user['anticipate'] < $auction['anticipate']) return app('json')->fail('预约卷不够');
  67. $user['anticipate'] = $user['anticipate'] - $auction['anticipate'];// 扣除预约卷
  68. User::rollbackTrans();
  69. $res = $user->save();
  70. if ($res){
  71. AuctionBooking::booking($user['uid'], $auction);
  72. UserBill::expend('预约场馆', $user['uid'], 'anticipate','reduce_anticipate', $auction['anticipate'], 0, $user['anticipate'], '预约扣除预约卷'); // 写入记录
  73. User::commitTrans();
  74. return app('json')->successful('预约成功');
  75. }else{
  76. User::rollbackTrans();
  77. return app('json')->fail('预约失败');
  78. }
  79. }
  80. /**
  81. * 进入场馆
  82. * @param Request $request
  83. * @return void
  84. */
  85. public function advance(Request $request)
  86. {
  87. $data = UtilService::getMore([
  88. ['id']
  89. ]);
  90. if (!$data['id']) return app('json')->fail('数据传入错误');
  91. $auction = Auction::find($data['id']);
  92. $booking = AuctionBooking::where([['auction_id', '=',$auction['id']], ['frequency', '=', $auction['frequency']]])->find();
  93. $user = $request->user();
  94. $time = strtotime(date('Y-m-d', time()));// 今天
  95. $today = strtotime(date('Y-m-d', strtotime('+1day')));// 明天
  96. if ($user['is_new'] == 1 or ($user['green_time'] >= $time and $user['green_time'] <= $today)){
  97. // 新人或者绿色通道提前三分钟入场
  98. if (strtotime($auction['radd_time']) -3000 > time()){
  99. return app('json')->fail('未到进入时间');
  100. }
  101. if (strtotime($auction['rend_time']) < time()){
  102. return app('json')->fail('进场时间已过');
  103. }
  104. }else{
  105. if (strtotime($auction['radd_time']) > time()){
  106. return app('json')->fail('未到进入时间');
  107. }
  108. if (strtotime($auction['rend_time']) < time()){
  109. return app('json')->fail('进场时间已过');
  110. }
  111. }
  112. if ($booking){
  113. return app('json')->successful('可进入');
  114. }else{
  115. return app('json')->fail('未预约');
  116. }
  117. }
  118. /**
  119. * 用户下级
  120. * @param Request $request
  121. * @return mixed
  122. */
  123. public function lower(Request $request){
  124. $data = UtilService::getMore([
  125. [['page', 'd'], 0],
  126. [['limit', 'd'], 0],
  127. ], $request);
  128. $user = User::where('spread_uid', $request->uid())->page($data['page'], $data['limit'])->field('uid,nickname,avatar')->order('uid DESC')->select();
  129. $user = empty($user) ? [] : $user->toArray();
  130. if ($user) {
  131. foreach ($user as $k => $v){
  132. $user[$k]['count'] = User::where('spread_uid',$v['uid'])->count(); // 粉丝人数
  133. $user[$k]['money'] = AuctionOrder::where('uid', $v['uid'])
  134. ->whereBetweenTime('create_time', date('Y-m-d 00:00:00'), date('Y-m-d 00:00:00', strtotime('+1 day')))
  135. ->where('status', 3)
  136. ->sum('price');
  137. }
  138. }
  139. $count = User::where('spread_uid', $request->uid())->count(); // 粉丝人数
  140. $active = User::where('spread_uid', $request->uid())->whereBetweenTime('last_time', date('Y-m-d H:i:s', strtotime('-1 week')), date('Y-m-d H:i:s'))->count(); // 活跃人数
  141. $money = AuctionOrder::where('uid', 'in', User::where('spread_uid', $request->uid())->column('uid'))
  142. ->where('status', 3)
  143. ->sum('price');
  144. $data = [
  145. 'user' => $user, // 下级用户
  146. 'count' => $count, // 下级用户人数
  147. 'active' => $active,// 活跃人数
  148. 'money' => $money // 总金额
  149. ];
  150. return app('json')->successful($data);
  151. }
  152. /**
  153. * 转预约卷给下级
  154. * @param Request $request
  155. * @return mixed
  156. * @throws \think\db\exception\DataNotFoundException
  157. * @throws \think\db\exception\DbException
  158. * @throws \think\db\exception\ModelNotFoundException
  159. */
  160. public function transfer_accounts(Request $request){
  161. $data = UtilService::getMore([
  162. ['uid'],
  163. ['anticipate']
  164. ], $request);
  165. if (!$data['uid'] or !$data['anticipate']) return app('json')->fail('数据传入错误');
  166. $user = User::find($request->uid());
  167. if ($user['anticipate'] < $data['anticipate']) return app('json')->fail('预约卷不够');
  168. $me = User::find($data['uid']);
  169. $user['anticipate'] = $user['anticipate'] - $data['anticipate'];// 扣除预约卷
  170. $me['anticipate'] = $me['anticipate'] + $data['anticipate'];// 增加预约卷
  171. try {
  172. Db::startTrans();
  173. UserBill::expend('预约卷减少',$user['uid'], 'anticipate', 'reduce_anticipate', $data['anticipate'], 0, $user['anticipate'], '转账给用户'.$me['nickname'].$data['anticipate'].'预约卷');
  174. UserBill::income('预约卷增加',$me['uid'], 'anticipate', 'add_anticipate', $data['anticipate'], 0, $me['anticipate'], $me['nickname'].'转账'.$data['anticipate'].'预约卷');
  175. $user->save();
  176. $me->save();
  177. Db::commit();
  178. return app('json')->successful('成功');
  179. } catch (\Exception $e) {
  180. Db::rollback();
  181. return app('json')->fail('失败');
  182. }
  183. }
  184. /**
  185. * 会馆
  186. * @param Request $request
  187. * @return mixed
  188. * @throws \think\db\exception\DataNotFoundException
  189. * @throws \think\db\exception\DbException
  190. * @throws \think\db\exception\ModelNotFoundException
  191. */
  192. public function auction_gu(Request $request)
  193. {
  194. $data = UtilService::getMore([
  195. [['page', 'd'], 0],
  196. [['limit', 'd'], 0],
  197. ], $request);
  198. $uid = getParent($request->uid());
  199. $uid[] = $request->uid();
  200. $list = AuctionGu::where('a.uid', 'in', $uid)
  201. ->alias('a')
  202. ->field('a.*,u.nickname,u.avatar')
  203. ->leftJoin('user u', 'a.uid = u.uid')
  204. ->page($data['page'], $data['limit'])
  205. ->select();
  206. $list = count($list) ? $list->toArray() : [];
  207. return app('json')->successful($list);
  208. }
  209. /**
  210. * 用户管理会馆
  211. * @param Request $request
  212. * @return mixed
  213. */
  214. public function user_gu(Request $request)
  215. {
  216. $data = UtilService::getMore([
  217. [['page', 'd'], 0],
  218. [['limit', 'd'], 0],
  219. ], $request);
  220. $list = AuctionGu::where('uid', $request->uid())->page($data['page'], $data['limit'])->select();
  221. $list = count($list) ? $list->toArray() : [];
  222. return app('json')->successful($list);
  223. }
  224. /**
  225. * 添加收款方式
  226. * @param Request $request
  227. * @return void
  228. */
  229. public function pay(Request $request)
  230. {
  231. $data = UtilService::postMore([
  232. ['payment'],
  233. ['image'],
  234. ['bank'],
  235. ['name'],
  236. ['type']
  237. ], $request);
  238. if (!$data['type']) return app('json')->fail('数据传入错误');
  239. $data['uid'] =$request->uid();
  240. $model = new AuctionPay();
  241. $pay = $model->where([['uid', '=', $request->uid()], ['type', '=', $data['type']]])->find();
  242. if (!empty($pay)){
  243. if ($data['type'] == 1 ){
  244. // 微信收款方式
  245. if (!$data['payment']) return app('json')->fail('微信账号不能为空');
  246. if (!$data['image']) return app('json')->fail('二维码不能为空');
  247. if (!$data['name']) return app('json')->fail('姓名不能为空');
  248. $pay['payment'] = $data['payment'];
  249. $pay['image'] = $data['image'];
  250. $pay['name'] = $data['name'];
  251. }elseif ($data['type'] == 2){
  252. // 支付宝收款方式
  253. if (!$data['payment']) return app('json')->fail('支付宝账号不能为空');
  254. if (!$data['name']) return app('json')->fail('姓名不能为空');
  255. $pay['payment'] = $data['payment'];
  256. $pay['name'] = $data['name'];
  257. }elseif ($data['type'] == 3){
  258. // 银行卡收款方式
  259. if (!$data['payment']) return app('json')->fail('银行卡号不能为空');
  260. if (!$data['name']) return app('json')->fail('姓名不能为空');
  261. if (!$data['bank']) return app('json')->fail('开户行');
  262. $pay['payment'] = $data['payment'];
  263. $pay['image'] = $data['image'];
  264. $pay['bank'] = $data['name'];
  265. }
  266. $res = $pay->save();
  267. if ($res) return app('json')->successful('修改成功');
  268. return app('json')->fail('修改失败');
  269. }else{
  270. if ($data['type'] == 1 ){
  271. // 微信收款方式
  272. if (!$data['payment']) return app('json')->fail('微信账号不能为空');
  273. if (!$data['image']) return app('json')->fail('二维码不能为空');
  274. if (!$data['name']) return app('json')->fail('姓名不能为空');
  275. }elseif ($data['type'] == 2){
  276. // 支付宝收款方式
  277. if (!$data['payment']) return app('json')->fail('支付宝账号不能为空');
  278. if (!$data['name']) return app('json')->fail('姓名不能为空');
  279. }elseif ($data['type'] == 3){
  280. // 银行卡收款方式
  281. if (!$data['payment']) return app('json')->fail('银行卡号不能为空');
  282. if (!$data['name']) return app('json')->fail('姓名不能为空');
  283. if (!$data['bank']) return app('json')->fail('开户行不能为空');
  284. }
  285. $res = $model->save($data);
  286. if ($res) return app('json')->successful('添加成功');
  287. return app('json')->fail('添加失败');
  288. }
  289. }
  290. /**
  291. * 收款方式详情
  292. * @param Request $request
  293. * @return mixed
  294. * @throws \think\db\exception\DataNotFoundException
  295. * @throws \think\db\exception\DbException
  296. * @throws \think\db\exception\ModelNotFoundException
  297. */
  298. public function pay_list(Request $request)
  299. {
  300. $model = new AuctionPay();
  301. $list = $model->where('uid', $request->uid())->select();
  302. $list = empty($list)? []: $list->toArray();
  303. $data = [];
  304. foreach ($list as $k => $v){
  305. if ($v['type'] == 1){
  306. $data['wx'] = $v;
  307. }elseif ($v['type'] == 2){
  308. $data['zfb'] = $v;
  309. }elseif ($v['type'] == 3){
  310. $data['bank'] = $v;
  311. }
  312. }
  313. return app('json')->successful($data);
  314. }
  315. /**
  316. * 馆长申请
  317. * @param Request $request
  318. * @return mixed
  319. * @throws \think\db\exception\DataNotFoundException
  320. * @throws \think\db\exception\DbException
  321. * @throws \think\db\exception\ModelNotFoundException
  322. */
  323. public function apply(Request $request)
  324. {
  325. if (!AuctionApply::where('status', '>', 0)->find($request->uid())){
  326. $res = AuctionApply::create([
  327. 'uid' => $request->uid()
  328. ]);
  329. if ($res)return app('json')->successful('申请成功');
  330. return app('json')->fail('申请失败');
  331. }
  332. return app('json')->fail('已申请,请等待管理员操作');
  333. }
  334. public function apply_status(Request $request)
  335. {
  336. $data = AuctionApply::where('uid', $request->uid())->find($request->uid());
  337. if (!$data){
  338. return app('json')->successful(['status' => 0, 'str' => '未提交申请']); // 未提交申请
  339. }
  340. $data = AuctionApply::where([['status', '=', 1], ['uid', '=', $request->uid()]])->find();
  341. if ($data){
  342. return app('json')->successful(['status' => 1, 'str' => '待审核']);// 待审核
  343. }
  344. $data = AuctionApply::where([['status', '=', 2], ['uid', '=', $request->uid()]])->find();
  345. if ($data){
  346. return app('json')->successful(['status' => 2 ,'str' => '已完成']);// 已完成
  347. }
  348. $data = AuctionApply::where([['status', '=', 0], ['uid', '=', $request->uid()]])->find();
  349. if ($data){
  350. return app('json')->successful(['status' => 3, 'str' => '失败']);// 失败
  351. }
  352. }
  353. }