From de312d15975feb60c1cfb5034cbf2314163e4146 Mon Sep 17 00:00:00 2001 From: wanghongjun <1445693971@qq,com> Date: Thu, 31 Aug 2023 11:13:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=90=86=E4=B8=8A=E5=88=86=E4=B8=8B?= =?UTF-8?q?=E5=88=86=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/AgentTeam.php | 117 +++++++++++++++++++++++++++ app/logic/Zone.php | 2 +- app/model/AgentDownScoresRecords.php | 34 ++++++++ app/model/AgentUpScoresRecords.php | 34 ++++++++ app/model/AgentUser.php | 86 ++++++++++++++++++++ app/model/RechargeRecords.php | 37 +++++++++ app/model/User.php | 22 ++++- app/model/WithdrawalRecords.php | 36 +++++++++ app/validate/Agent.php | 32 ++++++++ route/app.php | 4 + 10 files changed, 401 insertions(+), 3 deletions(-) create mode 100644 app/controller/AgentTeam.php create mode 100644 app/model/AgentDownScoresRecords.php create mode 100644 app/model/AgentUpScoresRecords.php create mode 100644 app/model/RechargeRecords.php create mode 100644 app/model/WithdrawalRecords.php create mode 100644 app/validate/Agent.php diff --git a/app/controller/AgentTeam.php b/app/controller/AgentTeam.php new file mode 100644 index 0000000..8dfc3b6 --- /dev/null +++ b/app/controller/AgentTeam.php @@ -0,0 +1,117 @@ +scene('scores')->check($request); + + $user_id = $request['user_id']; # 用户id + $quota = $request['quota']; # 额度 + + # 检测代理余额是否足够 + $inspectRes = AgentUser::inspectBalance($agentUser['id'],$quota); + if (!$inspectRes['status']) throw new ValidateException($inspectRes['msg']); + + # 开启事务 + $connection->startTrans(); + + # 扣减代理余额 + $agentBalance = AgentUser::decrBalance($agentUser['id'],$quota); + + # 用户上分记录 + AgentUpScoresRecords::createRecords($agentUser['id'],$user_id,$quota,$agentBalance); + + # 用户上分 + $userBalance = User::incrBalance($user_id,$quota); + + # 用户充值记录-上分 + RechargeRecords::createRecords($user_id,$quota,$userBalance,1); + + $connection->commit(); + return $this->renderSuccess('上分成功'); + } catch (ValidateException $e) { + return $this->renderError($e->getMessage()); + } catch (\Exception $exception) { + $connection->rollback(); + return $this->renderError($exception->getMessage()); + } + } + + /** + * 下分 + * @return array|void + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function downScores() + { + $request = Request::param(); + + $connection = Db::connect(); + try { + $agentUser = Session::get('login_agent_user_data'); + $agentUser['id'] = 1000; + validate(Agent::class)->scene('scores')->check($request); + + $user_id = $request['user_id']; # 用户id + $quota = $request['quota']; # 额度 + + # 检测代理余额是否足够 + $inspectRes = User::inspectUserBalance($user_id,$quota); + if (!$inspectRes['status']) throw new ValidateException($inspectRes['msg']); + + # 开启事务 + $connection->startTrans(); + + # 增加代理余额 + $agentBalance = AgentUser::incrWithdrawalBalance($agentUser['id'],$quota); + + # 用户下分记录 + AgentDownScoresRecords::createRecords($agentUser['id'],$user_id,$quota,$agentBalance); + + # 用户下分 + $userBalance = User::decrWithdrawalBalance($user_id,$quota); + + # 用户提现记录-下分 + WithdrawalRecords::createRecords($user_id,$quota,$userBalance,1); + + $connection->commit(); + return $this->renderSuccess('下分成功'); + } catch (ValidateException $e) { + return $this->renderError($e->getMessage()); + } catch (\Exception $exception) { + $connection->rollback(); + return $this->renderError($exception->getMessage()); + } + } +} diff --git a/app/logic/Zone.php b/app/logic/Zone.php index 10faaf0..73de7de 100644 --- a/app/logic/Zone.php +++ b/app/logic/Zone.php @@ -113,7 +113,7 @@ class Zone if ($awards_amount > 0) { # 修改用户余额 - $balance = User::IncrWithdrawalBalance($user_id,$awards_amount); + $balance = User::incrWithdrawalBalance($user_id,$awards_amount); # 中奖做记录 AwardsRecords::createRecords($user_id,$c_r_id,$awards_amount,$balance); diff --git a/app/model/AgentDownScoresRecords.php b/app/model/AgentDownScoresRecords.php new file mode 100644 index 0000000..8d82b3c --- /dev/null +++ b/app/model/AgentDownScoresRecords.php @@ -0,0 +1,34 @@ +aid = $aid; + $Records->user_id = $user_id; + $Records->withdrawal_amount = $withdrawal_amount; + $Records->withdrawal_balance = $withdrawal_balance; + $Records->create_time = date("Y-m-d H:i:s",time()); + $Records->save(); + + return $Records->id; + } +} diff --git a/app/model/AgentUpScoresRecords.php b/app/model/AgentUpScoresRecords.php new file mode 100644 index 0000000..924e8a9 --- /dev/null +++ b/app/model/AgentUpScoresRecords.php @@ -0,0 +1,34 @@ +aid = $aid; + $Records->user_id = $user_id; + $Records->balance = $balance; + $Records->residue_amount = $residue_amount; + $Records->create_time = date("Y-m-d H:i:s",time()); + $Records->save(); + + return $Records->id; + } +} diff --git a/app/model/AgentUser.php b/app/model/AgentUser.php index bed2a38..6e37392 100644 --- a/app/model/AgentUser.php +++ b/app/model/AgentUser.php @@ -52,6 +52,92 @@ class AgentUser extends Model } } + /** + * 扣减余额(给用户上分) + * @param $aid + * @param $balance + * @return float|mixed + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public static function decrBalance($aid,$balance) + { + $user = self::find($aid); + $user->balance = round($user->balance - $balance,2); + $user->save(); + return $user->balance; + } + + /** + * 增加余额(充值、总后台上分) + * @param $aid + * @param $balance + * @return float|mixed + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public static function incrBalance($aid,$balance) + { + $user = self::find($aid); + $user->balance = round($user->balance + $balance,2); + $user->save(); + return $user->balance; + } + + /** + * 扣除可提余额 (提现) + * @param $aid + * @param $balance + * @return float|mixed + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public static function decrWithdrawalBalance($aid,$balance) + { + $user = self::find($aid); + $user->withdrawal_balance = round($user->withdrawal_balance - $balance,2); + $user->save(); + return $user->withdrawal_balance; + } + + /** + * 增加可提余额(给用户下分) + * @param $aid + * @param $balance + * @return float|mixed + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public static function incrWithdrawalBalance($aid,$balance) + { + $user = self::find($aid); + $user->withdrawal_balance = round($user->withdrawal_balance + $balance,2); + $user->save(); + return $user->withdrawal_balance; + } + + /** + * 检测代理余额 + * @param $aid + * @param $balance + * @return array|int[] + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public static function inspectBalance($aid,$balance) + { + $user = self::find($aid); + if ($user->balance < $balance) { + return ['status' => 0, 'msg' => '代理余额不足']; + } + return ['status' => 1]; + } + /** * 生成密码 * @param $password diff --git a/app/model/RechargeRecords.php b/app/model/RechargeRecords.php new file mode 100644 index 0000000..a7d857b --- /dev/null +++ b/app/model/RechargeRecords.php @@ -0,0 +1,37 @@ +user_id = $user_id; + $RechargeRecords->recharge_amount = $recharge_amount; + $RechargeRecords->residue_amount = $residue_amount; + $RechargeRecords->recharge_time = date("Y-m-d H:i:s",time()); + $RechargeRecords->trade_type = $trade_type; + $RechargeRecords->status = $status; + + $RechargeRecords->save(); + + return $RechargeRecords->id; + } +} diff --git a/app/model/User.php b/app/model/User.php index e50ec1b..4854775 100644 --- a/app/model/User.php +++ b/app/model/User.php @@ -187,7 +187,7 @@ class User extends Model * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ - public static function IncrBalance($user_id,$balance) + public static function incrBalance($user_id,$balance) { $user = self::find($user_id); $user->balance = round($user->balance + $balance,2); @@ -221,7 +221,7 @@ class User extends Model * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ - public static function IncrWithdrawalBalance($user_id,$balance) + public static function incrWithdrawalBalance($user_id,$balance) { $user = self::find($user_id); $user->withdrawal_balance = round($user->withdrawal_balance + $balance,2); @@ -229,6 +229,24 @@ class User extends Model return $user->withdrawal_balance; } + /** + * 检查用户余额是否足够 + * @param $user_id + * @param $balance + * @return array|int[] + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public static function inspectUserBalance($user_id,$balance) + { + $user = self::find($user_id); + if ($user->withdrawal_balance < $balance) { + return ['status' => 0, 'msg' => '用户可提余额不足']; + } + return ['status' => 1]; + } + /** * 生成盐值 * @return string diff --git a/app/model/WithdrawalRecords.php b/app/model/WithdrawalRecords.php new file mode 100644 index 0000000..f7b54be --- /dev/null +++ b/app/model/WithdrawalRecords.php @@ -0,0 +1,36 @@ +user_id = $user_id; + $Records->withdrawal_amount = $withdrawal_amount; + $Records->withdrawal_balance = $withdrawal_balance; + $Records->status = $status; + $Records->trade_type = $trade_type; + $Records->create_time = date("Y-m-d H:i:s",time()); + $Records->save(); + + return $Records->id; + } +} diff --git a/app/validate/Agent.php b/app/validate/Agent.php new file mode 100644 index 0000000..3bd65c2 --- /dev/null +++ b/app/validate/Agent.php @@ -0,0 +1,32 @@ + ['规则1','规则2'...] + * + * @var array + */ + protected $rule = [ + 'user_id|用户id' => 'require|number', + 'quota|额度' => 'require|number' + ]; + + /** + * 定义错误信息 + * 格式:'字段名.规则名' => '错误信息' + * + * @var array + */ + protected $message = []; + + protected $scene = [ + 'scores' => ['user_id','quota'] + ]; +} diff --git a/route/app.php b/route/app.php index a8422d8..5a6e33b 100644 --- a/route/app.php +++ b/route/app.php @@ -45,6 +45,10 @@ Route::group('zone',function(){ }); # 代理 +Route::group('agentTeam',function(){ + Route::get('upScores','agentTeam/upScores'); + Route::get('downScores','agentTeam/downScores'); +});