ManyController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 ($data['price'] > $many['single']) return app('json')->fail('单次最大可以种树'.$many['single']);
  78. if ($surplus < $data['price']) return app('json')->fail('超过最大可种树额度');
  79. Db::startTrans();
  80. if ($many['add_time'] > time()){
  81. $green = ManyGreen::where('uid', $user['uid'])->where('status', 0)->find();
  82. if (!$green) return app('json')->fail('你无法提前投注');
  83. $green['status'] = 1;
  84. }
  85. $integral = $user['white_integral'];// 白积分加紫积分的总积分
  86. if ($integral < $data['price']) return app('json')->fail('肥料不够');
  87. try {
  88. $user['white_integral'] -= $data['price'];
  89. UserBill::expend('扣除肥料', $user['uid'], 'white_integral', 'bet_white_integral', $data['price'], 0,$user['white_integral'],'使用肥料,参与种树-》'.$many['name'].'期数-》'.$many['stage'].'-成功');
  90. $many['number'] += round($data['price'] * 3.33, 2);
  91. if ($many['number'] >= $many['money']){
  92. $many['number'] = $many['money'];
  93. $many['suc'] = 1;// 众筹成功
  94. $many['status'] = 0;// 众筹成功
  95. ManyDiscipline::create(['many_id' => $many['id'], 'stage' => $many['stage'], 'status' => 1]);// 成功记录
  96. if ($many['stage'] >= 4){
  97. // 期数如果大于等于4
  98. $stage = $many['stage'] - 3;
  99. ManyOrder::where('many_id', $many['id'])->where('stage', $stage)->update(['is_return' => 1]);// 成功后添加返还状态
  100. }
  101. }
  102. $user->save();
  103. ManyOrder::create([
  104. 'order_id' => StoreOrder::getNewOrderId(),
  105. 'many_id' => $many['id'],
  106. 'uid' => $user['uid'],
  107. 'stage' => $many['stage'],
  108. 'price' => $data['price'],
  109. ]);
  110. $many->save();
  111. if ($many['add_time'] > time()) $green->save();
  112. Db::commit();
  113. return app('json')->success('种树成功');
  114. } catch (\Exception $e) {
  115. Db::rollback();
  116. return app('json')->fail($e->getMessage());
  117. }
  118. }
  119. /**
  120. * 还有多少可投注
  121. * @param $id
  122. * @param $type
  123. * @return float|mixed
  124. * @throws \think\db\exception\DataNotFoundException
  125. * @throws \think\db\exception\DbException
  126. * @throws \think\db\exception\ModelNotFoundException
  127. */
  128. public function surplus($id, $uid,$type = 0)
  129. {
  130. if ($type > 0){
  131. $many = Many::where('id', $id)->find();
  132. if (!$many) return app('json')->fail('场次不存在');
  133. $many_order = ManyOrder::where('many_id', $id)->where('uid', $uid)->where('stage', $many['stage'])->sum('price');
  134. $price = ($many['upper_limit'] - $many_order); // 还可以投注额度
  135. return $price;
  136. }else{
  137. $many = Many::where('id', $id)->find();
  138. if (!$many) return app('json')->fail('场次不存在');
  139. $many_order = ManyOrder::where('many_id', $id)->where('uid', $uid)->where('stage', $many['stage'])->sum('price');
  140. $data['price'] = ($many['upper_limit'] - $many_order); // 还可以投注额度
  141. return app('json')->fail($data);
  142. }
  143. }
  144. }