123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- <?php
- namespace app\api\controller\many;
- use app\models\many\AuctionPay;
- use app\models\many\Many;
- use app\models\many\ManyDiscipline;
- use app\models\many\ManyGreen;
- use app\models\many\ManyOrder;
- use app\models\store\StoreOrder;
- use app\models\user\User;
- use app\models\user\UserBill;
- use app\Request;
- use crmeb\services\GroupDataService;
- use crmeb\services\QrcodeService;
- use crmeb\services\SystemConfigService;
- use crmeb\services\UtilService;
- use crmeb\services\upload\Upload;
- use think\facade\Db;
- use think\facade\Validate;
- /**
- * 账单类
- * Class UserBillController
- * @package app\api\controller\user
- */
- class ManyController
- {
- /**
- * 众筹列表
- * @param Request $request
- * @return mixed
- */
- public function many(Request $request)
- {
- $where = UtilService::getMore([
- ['page', 1],
- ['limit', 10],
- ['name']
- ]);
- $list = \app\models\many\Many::list($where);
- return app('json')->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']
- ]);
- Db::startTrans();
- $many = Many::where('id', $data['id'])->lock(true)->find();
- $surplus = $this->surplus($data['id'], $request->uid(),1);
- $user = User::where('uid', $request->uid())->find();
- if ($user['blacklist'] < 1) return app('json')->fail('种植失败');
- if ($data['price'] < $many['lower_limit']) return app('json')->fail('最小种植数'.$many['lower_limit']);
- 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('超过最大可种植额度');
- if ($many['add_time'] > time()) return app('json')->fail('你无法提前种植');
- // 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'] + $user['purple_integral'];// 白积分加紫积分的总积分
- if ($integral < $data['price']) return app('json')->fail('积分额度不够');
- try {
- if ($user['white_integral'] < $data['price']){
- $white = $user['white_integral'];
- if ($white > 0){
- User::where('uid', $request->uid())->dec('white_integral',$white)->update();
- UserBill::expend('扣除红积分', $user['uid'], 'white_integral', 'bet_white_integral', $white, 0,0,'使用红积分,参与种植-》'.$many['name'].'期数-》'.$many['stage'].'-成功');
- }
- $user['purple_integral'] -= $data['price'] - $white;
- User::where('uid', $request->uid())->dec('purple_integral', $data['price']-$white)->update();
- UserBill::expend('扣除阳光积分', $user['uid'], 'purple_integral', 'bet_purple_integral', $data['price']-$white, 0,$user['purple_integral'],'使用阳光积分,参与种植-》'.$many['name'].'期数-》'.$many['stage'].'-成功');
- }else{
- $user['white_integral'] -= $data['price'];
- User::where('uid', $request->uid())->dec('white_integral', $data['price'])->update();
- UserBill::expend('扣除红积分', $user['uid'], 'white_integral', 'bet_white_integral', $data['price'], 0,$user['white_integral'],'使用红积分,参与种植-》'.$many['name'].'期数-》'.$many['stage'].'-成功');
- }
- $number = $many['number'] + $data['price'];
- if ($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]);// 成功后添加返还状态
- }
- }
- ManyOrder::create([
- 'order_id' => StoreOrder::getNewOrderId(),
- 'many_id' => $many['id'],
- 'uid' => $user['uid'],
- 'stage' => $many['stage'],
- 'price' => $data['price'],
- ]);
- Many::where('id', $data['id'])->inc('number', $data['price'])->update();
- $many->save();
- // if ($many['add_time'] > time()) $green->save();
- event('Many', ['many' => $many, 'data' => $data, 'uid' => $request->uid()]);
- 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);
- }
- }
- /**
- * 添加收款方式
- * @param Request $request
- * @return void
- */
- public function pay(Request $request)
- {
- $data = UtilService::postMore([
- ['payment'],
- ['image'],
- ['bank'],
- ['name'],
- ['type'],
- ['phone'],
- ['bank_name']
- ], $request);
- if (!$data['type']) return app('json')->fail('数据传入错误');
- $data['uid'] =$request->uid();
- $model = new AuctionPay();
- $pay = $model->where([['uid', '=', $request->uid()], ['type', '=', $data['type']]])->find();
- $res = Validate::rule([
- 'phone' => 'mobile'
- ]);
- $res->message([
- 'phone.mobile' => '请填写正确手机格式'
- ]);
- if (!$res->check($data)){
- return app('json')->fail($res->getError());
- }
- if (!empty($pay)){
- if ($data['type'] == 1 ){
- // 微信收款方式
- if (!$data['payment']) return app('json')->fail('微信账号不能为空');
- if (!$data['image']) return app('json')->fail('二维码不能为空');
- if (!$data['name']) return app('json')->fail('姓名不能为空');
- $pay['payment'] = $data['payment'];
- $pay['image'] = $data['image'];
- $pay['name'] = $data['name'];
- $pay['phone'] = $data['phone'];
- }elseif ($data['type'] == 2){
- // 支付宝收款方式
- if (!$data['payment']) return app('json')->fail('支付宝账号不能为空');
- if (!$data['name']) return app('json')->fail('姓名不能为空');
- $pay['payment'] = $data['payment'];
- $pay['name'] = $data['name'];
- $pay['phone'] = $data['phone'];
- }elseif ($data['type'] == 3){
- // 银行卡收款方式
- if (!$data['payment']) return app('json')->fail('银行卡号不能为空');
- if (!$data['name']) return app('json')->fail('姓名不能为空');
- if (!$data['bank']) return app('json')->fail('开户行不能为空');
- if (!$data['bank_name']) return app('json')->fail('开户支行不能为空');
- if (!$data['phone']) return app('json')->fail('请填写手机号');
- $pay['payment'] = $data['payment'];
- $pay['bank'] = $data['bank'];
- $pay['bank_name'] = $data['bank_name'];
- $pay['phone'] = $data['phone'];
- $pay['name'] = $data['name'];
- }
- $res = $pay->save();
- if ($res) return app('json')->successful('修改成功');
- return app('json')->fail('修改失败');
- }else{
- if ($data['type'] == 1 ){
- // 微信收款方式
- if (!$data['payment']) return app('json')->fail('微信账号不能为空');
- if (!$data['image']) return app('json')->fail('二维码不能为空');
- if (!$data['name']) return app('json')->fail('姓名不能为空');
- if (!$data['phone']) return app('json')->fail('请填写手机号');
- }elseif ($data['type'] == 2){
- // 支付宝收款方式
- if (!$data['payment']) return app('json')->fail('支付宝账号不能为空');
- if (!$data['name']) return app('json')->fail('姓名不能为空');
- if (!$data['phone']) return app('json')->fail('请填写手机号');
- }elseif ($data['type'] == 3){
- // 银行卡收款方式
- if (!$data['payment']) return app('json')->fail('银行卡号不能为空');
- if (!$data['name']) return app('json')->fail('姓名不能为空');
- if (!$data['bank']) return app('json')->fail('开户行不能为空');
- if (!$data['bank_name']) return app('json')->fail('开户支行不能为空');
- if (!$data['phone']) return app('json')->fail('请填写手机号');
- }
- $res = $model->save($data);
- if ($res) return app('json')->successful('添加成功');
- return app('json')->fail('添加失败');
- }
- }
- /**
- * 收款方式详情
- * @param Request $request
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function pay_list(Request $request)
- {
- $model = new AuctionPay();
- $list = $model->where('uid', $request->uid())->select();
- $list = empty($list)? []: $list->toArray();
- $data['wx'] = [];
- $data['zfb'] = [];
- $data['bank'] = [];
- foreach ($list as $k => $v){
- if ($v['type'] == 1){
- $data['wx'] = $v;
- }elseif ($v['type'] == 2){
- $data['zfb'] = $v;
- }elseif ($v['type'] == 3){
- $data['bank'] = $v;
- }
- }
- return app('json')->successful($data);
- }
- }
|