success($list); } /** * 众筹详情 * @return mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function details() { $where = UtilService::getMore([ ['id'], ]); $many = Many::find($where['id']); if (!$many) return app('json')->fail('没有该场次'); return app('json')->success($many->toArray()); } /** * 投注 * @param Request $request * @return mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function purchase(Request $request) { $data = UtilService::postMore([ ['id'], ['price'] ]); $many = Many::where('id', $data['id'])->find(); $surplus = $this->surplus($data['id'], $request->uid(),1); $user = User::where('uid', $request->uid())->find(); if (!$many) return app('json')->fail('场次不存在'); if ($many['number'] >= $many['money']) return app('json')->fail('已完成无法种树'); if ($many['status'] == 0) return app('json')->fail('未开启'); if ($many['end_time'] < time()) return app('json')->fail('已结束'); if (($many['number']+$data['price']) > $many['money']) return app('json')->fail('还能最大种树'.($many['money']-$many['number'])); if ($data['price'] > $many['single']) return app('json')->fail('单次最大可以种树'.$many['single']); if ($surplus < $data['price']) return app('json')->fail('超过最大可种树额度'); Db::startTrans(); if ($many['add_time'] > time()){ $green = ManyGreen::where('uid', $user['uid'])->where('status', 0)->find(); if (!$green) return app('json')->fail('你无法提前投注'); $green['status'] = 1; } $integral = $user['white_integral'];// 白积分加紫积分的总积分 if ($integral < $data['price']) return app('json')->fail('肥料不够'); try { $user['white_integral'] -= $data['price']; UserBill::expend('扣除肥料', $user['uid'], 'white_integral', 'bet_white_integral', $data['price'], 0,$user['white_integral'],'使用肥料,参与种树-》'.$many['name'].'期数-》'.$many['stage'].'-成功'); $many['number'] += round($data['price'] * 3.33, 2); if ($many['number'] >= $many['money']){ $many['number'] = $many['money']; $many['suc'] = 1;// 众筹成功 $many['status'] = 0;// 众筹成功 ManyDiscipline::create(['many_id' => $many['id'], 'stage' => $many['stage'], 'status' => 1]);// 成功记录 if ($many['stage'] >= 4){ // 期数如果大于等于4 $stage = $many['stage'] - 3; ManyOrder::where('many_id', $many['id'])->where('stage', $stage)->update(['is_return' => 1]);// 成功后添加返还状态 } } $user->save(); ManyOrder::create([ 'order_id' => StoreOrder::getNewOrderId(), 'many_id' => $many['id'], 'uid' => $user['uid'], 'stage' => $many['stage'], 'price' => $data['price'], ]); $many->save(); if ($many['add_time'] > time()) $green->save(); Db::commit(); return app('json')->success('种树成功'); } catch (\Exception $e) { Db::rollback(); return app('json')->fail($e->getMessage()); } } /** * 还有多少可投注 * @param $id * @param $type * @return float|mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function surplus($id, $uid,$type = 0) { if ($type > 0){ $many = Many::where('id', $id)->find(); if (!$many) return app('json')->fail('场次不存在'); $many_order = ManyOrder::where('many_id', $id)->where('uid', $uid)->where('stage', $many['stage'])->sum('price'); $price = ($many['upper_limit'] - $many_order); // 还可以投注额度 return $price; }else{ $many = Many::where('id', $id)->find(); if (!$many) return app('json')->fail('场次不存在'); $many_order = ManyOrder::where('many_id', $id)->where('uid', $uid)->where('stage', $many['stage'])->sum('price'); $data['price'] = ($many['upper_limit'] - $many_order); // 还可以投注额度 return app('json')->fail($data); } } }