ManyController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace app\api\controller\many;
  3. use app\models\many\Many;
  4. use app\models\many\ManyDiscipline;
  5. use app\models\many\ManyGreen;
  6. use app\models\many\ManyOrder;
  7. use app\models\store\StoreOrder;
  8. use app\models\user\User;
  9. use app\models\user\UserBill;
  10. use app\Request;
  11. use crmeb\services\GroupDataService;
  12. use crmeb\services\QrcodeService;
  13. use crmeb\services\SystemConfigService;
  14. use crmeb\services\UtilService;
  15. use crmeb\services\upload\Upload;
  16. use think\facade\Db;
  17. /**
  18. * 账单类
  19. * Class UserBillController
  20. * @package app\api\controller\user
  21. */
  22. class ManyController
  23. {
  24. /**
  25. * 众筹列表
  26. * @param Request $request
  27. * @return mixed
  28. */
  29. public function many(Request $request)
  30. {
  31. $where = UtilService::getMore([
  32. ['page', 1],
  33. ['limit', 10],
  34. ['name']
  35. ]);
  36. $list = \app\models\many\Many::list($where);
  37. return app('json')->success($list);
  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 purchase(Request $request)
  48. {
  49. $data = UtilService::postMore([
  50. ['id'],
  51. ['price']
  52. ]);
  53. $many = Many::where('id', $data['id'])->find();
  54. $surplus = $this->surplus($data['id'], 1);
  55. $user = User::where('uid', $request->uid())->find();
  56. if (!$many) return app('json')->fail('场次不存在');
  57. if ($many['number'] >= $many['money']) return app('json')->fail('已完成无法投注');
  58. if ( $many['status'] == 0) return app('json')->fail('未开启');
  59. if ($many['end_time'] < time()) return app('json')->fail('已结束');
  60. if (($many['number']+$data['price']) > $many['money']) return app('json')->fail('还能最大投注'.($many['money']-$many['number']));
  61. if ($surplus < $data['price']) return app('json')->fail('超过最大可投注额度');
  62. Db::startTrans();
  63. if ($many['add_time'] > time()){
  64. $green = ManyGreen::where('uid', $user['uid'])->where('status', 0)->find();
  65. if (!$green) return app('json')->fail('你无法提前投注');
  66. $green['status'] = 1;
  67. }
  68. $integral = $user['white_integral'] + $user['purple_integral'];// 白积分加紫积分的总积分
  69. if ($integral < $data['price']) return app('json')->fail('积分额度不够');
  70. try {
  71. if ($user['white_integral'] < $data['price']){
  72. $white = $user['white_integral'];
  73. $user['white_integral'] = 0;
  74. $user['purple_integral'] = ($data['price'] - $white);
  75. UserBill::expend('扣除白积分', $user['uid'], 'white_integral', 'bet', $white, 0,0,'使用白积分,投注场次'.$many['name'].'成功');
  76. UserBill::expend('扣除紫积分', $user['uid'], 'purple_integral', 'bet', $data['price']-$white, 0,$user['purple_integral'],'使用紫积分,投注场次'.$many['name'].'成功');
  77. }else{
  78. $user['white_integral'] -= $data['price'];
  79. UserBill::expend('扣除白积分', $user['uid'], 'white_integral', 'bet', $data['price'], 0,$user['white_integral'],'使用白积分,投注场次'.$many['name'].'成功');
  80. }
  81. $many['number'] += $data['price'];
  82. if ($many['number'] >= $many['money']){
  83. $many['suc'] = 1;// 众筹成功
  84. $many['status'] = 0;// 众筹成功
  85. ManyDiscipline::create(['many_id' => $many['id'], 'stage' => $many['stage'], 'status' => 1]);// 成功记录
  86. if ($many['stage'] >= 4){
  87. // 期数如果大于等于4
  88. $stage = $many['stage'] - 3;
  89. ManyOrder::where('many_id', $many['id'])->where('stage', $stage)->update(['is_return' => 1]);// 成功后添加返还状态
  90. }
  91. }
  92. $user->save();
  93. ManyOrder::create([
  94. 'order_id' => StoreOrder::getNewOrderId(),
  95. 'many_id' => $many['id'],
  96. 'uid' => $user['uid'],
  97. 'stage' => $many['stage'],
  98. 'price' => $data['price'],
  99. ]);
  100. $many->save();
  101. if ($many['add_time'] > time()) $green->save();
  102. Db::commit();
  103. return app('json')->success('投注成功');
  104. } catch (\Exception $e) {
  105. Db::rollback();
  106. return app('json')->fail($e->getMessage());
  107. }
  108. }
  109. /**
  110. * 还有多少可投注
  111. * @param $id
  112. * @param $type
  113. * @return float|mixed
  114. * @throws \think\db\exception\DataNotFoundException
  115. * @throws \think\db\exception\DbException
  116. * @throws \think\db\exception\ModelNotFoundException
  117. */
  118. public function surplus($id, $type = 0)
  119. {
  120. if ($type > 0){
  121. $many = Many::where('id', $id)->find();
  122. if (!$many) return app('json')->fail('场次不存在');
  123. $many_order = ManyOrder::where('many_id', $id)->where('stage', $many['stage'])->sum('price');
  124. $price = ($many['upper_limit'] - $many_order); // 还可以投注额度
  125. return $price;
  126. }else{
  127. $many = Many::where('id', $id)->find();
  128. if (!$many) return app('json')->fail('场次不存在');
  129. $many_order = ManyOrder::where('many_id', $id)->where('stage', $many['stage'])->sum('price');
  130. $data['price'] = ($many['upper_limit'] - $many_order); // 还可以投注额度
  131. return app('json')->fail($data);
  132. }
  133. }
  134. }