ManyController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. * @return mixed
  42. * @throws \think\db\exception\DataNotFoundException
  43. * @throws \think\db\exception\DbException
  44. * @throws \think\db\exception\ModelNotFoundException
  45. */
  46. public function details()
  47. {
  48. $where = UtilService::getMore([
  49. ['id'],
  50. ]);
  51. $many = Many::find($where['id']);
  52. if (!$many) return app('json')->fail('没有该场次');
  53. return app('json')->success($many->toArray());
  54. }
  55. /**
  56. * 投注
  57. * @param Request $request
  58. * @return mixed
  59. * @throws \think\db\exception\DataNotFoundException
  60. * @throws \think\db\exception\DbException
  61. * @throws \think\db\exception\ModelNotFoundException
  62. */
  63. public function purchase(Request $request)
  64. {
  65. $data = UtilService::postMore([
  66. ['id'],
  67. ['price']
  68. ]);
  69. $many = Many::where('id', $data['id'])->find();
  70. $surplus = $this->surplus($data['id'], $request->uid(),1);
  71. $user = User::where('uid', $request->uid())->find();
  72. if (!$many) return app('json')->fail('场次不存在');
  73. if ($many['number'] >= $many['money']) return app('json')->fail('已完成无法投注');
  74. if ($many['status'] == 0) return app('json')->fail('未开启');
  75. if ($many['end_time'] < time()) return app('json')->fail('已结束');
  76. if (($many['number']+$data['price']) > $many['money']) return app('json')->fail('还能最大投注'.($many['money']-$many['number']));
  77. if ($surplus < $data['price']) return app('json')->fail('超过最大可投注额度');
  78. Db::startTrans();
  79. if ($many['add_time'] > time()){
  80. $green = ManyGreen::where('uid', $user['uid'])->where('status', 0)->find();
  81. if (!$green) return app('json')->fail('你无法提前投注');
  82. $green['status'] = 1;
  83. }
  84. $integral = $user['white_integral'];// 白积分加紫积分的总积分
  85. if ($integral < $data['price']) return app('json')->fail('肥料额度不够');
  86. try {
  87. $user['white_integral'] -= $data['price'];
  88. UserBill::expend('扣除肥料', $user['uid'], 'white_integral', 'bet_white_integral', $data['price'], 0,$user['white_integral'],'使用肥料,参与种树-》'.$many['name'].'期数-》'.$many['stage'].'-成功');
  89. $many['number'] += $data['price'];
  90. if ($many['number'] >= $many['money']){
  91. $many['suc'] = 1;// 众筹成功
  92. $many['status'] = 0;// 众筹成功
  93. ManyDiscipline::create(['many_id' => $many['id'], 'stage' => $many['stage'], 'status' => 1]);// 成功记录
  94. if ($many['stage'] >= 4){
  95. // 期数如果大于等于4
  96. $stage = $many['stage'] - 3;
  97. ManyOrder::where('many_id', $many['id'])->where('stage', $stage)->update(['is_return' => 1]);// 成功后添加返还状态
  98. }
  99. }
  100. $user->save();
  101. ManyOrder::create([
  102. 'order_id' => StoreOrder::getNewOrderId(),
  103. 'many_id' => $many['id'],
  104. 'uid' => $user['uid'],
  105. 'stage' => $many['stage'],
  106. 'price' => $data['price'],
  107. ]);
  108. $many->save();
  109. if ($many['add_time'] > time()) $green->save();
  110. Db::commit();
  111. return app('json')->success('投注成功');
  112. } catch (\Exception $e) {
  113. Db::rollback();
  114. return app('json')->fail($e->getMessage());
  115. }
  116. }
  117. /**
  118. * 还有多少可投注
  119. * @param $id
  120. * @param $type
  121. * @return float|mixed
  122. * @throws \think\db\exception\DataNotFoundException
  123. * @throws \think\db\exception\DbException
  124. * @throws \think\db\exception\ModelNotFoundException
  125. */
  126. public function surplus($id, $uid,$type = 0)
  127. {
  128. if ($type > 0){
  129. $many = Many::where('id', $id)->find();
  130. if (!$many) return app('json')->fail('场次不存在');
  131. $many_order = ManyOrder::where('many_id', $id)->where('uid', $uid)->where('stage', $many['stage'])->sum('price');
  132. $price = ($many['upper_limit'] - $many_order); // 还可以投注额度
  133. return $price;
  134. }else{
  135. $many = Many::where('id', $id)->find();
  136. if (!$many) return app('json')->fail('场次不存在');
  137. $many_order = ManyOrder::where('many_id', $id)->where('uid', $uid)->where('stage', $many['stage'])->sum('price');
  138. $data['price'] = ($many['upper_limit'] - $many_order); // 还可以投注额度
  139. return app('json')->fail($data);
  140. }
  141. }
  142. }