From fede05873d6909377eb50bf0a01a7a2428ed1cbf Mon Sep 17 00:00:00 2001 From: wanghongjun <1445693971@qq,com> Date: Fri, 1 Sep 2023 13:49:33 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=A1=E7=90=86=E7=BB=99=E4=BB=A3=E7=90=86?= =?UTF-8?q?=E4=B8=8A=E5=88=86=E3=80=81=E4=B8=8B=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/AdminTeam.php | 88 +++++++++++++++++++++++++++- app/model/AdminDownScoresRecords.php | 8 ++- app/model/AdminUpScoresRecords.php | 10 ++-- app/model/AgentUser.php | 19 ++++++ 4 files changed, 117 insertions(+), 8 deletions(-) diff --git a/app/controller/AdminTeam.php b/app/controller/AdminTeam.php index 012e089..646f8c9 100644 --- a/app/controller/AdminTeam.php +++ b/app/controller/AdminTeam.php @@ -4,11 +4,18 @@ declare (strict_types = 1); namespace app\controller; use app\BaseController; +use app\model\AdminDownScoresRecords; +use app\model\AdminUpScoresRecords; +use app\model\AgentRechargeRecords; use app\model\AgentUser; +use app\model\User; use app\model\User as UserModel; -use think\exception\ValidateException; +use think\facade\Db; use think\facade\Request; +use think\facade\Session; +use app\validate\Admin; use app\validate\User as UserValidate; +use think\exception\ValidateException; class AdminTeam extends BaseController { @@ -134,4 +141,83 @@ class AdminTeam extends BaseController return $this->renderSuccess('数据返回成功', ['list' => $list, 'total' => $total]); } + + /** + * 代理上分 + * @return array + */ + public function upScores() + { + $request = Request::param(); + + $connection = Db::connect(); + try { + $adminUser = Session::get('login_admin_user_data'); + + validate(Admin::class)->scene('scores')->check($request); + + $aid = $request['aid']; # 代理id + $quota = $request['quota']; # 额度 + + # 开启事务 + $connection->startTrans(); + + # 增加代理余额 + $agentBalance = AgentUser::incrBalance($aid,$quota); + # 代理上分记录 + AdminUpScoresRecords::createRecords($aid,$adminUser['id'],$quota,$agentBalance,2); + # 代理充值记录 + AgentRechargeRecords::createRecords($aid,$quota,$agentBalance,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 { + $adminUser = Session::get('login_admin_user_data'); + validate(Admin::class)->scene('scores')->check($request); + + $aid = $request['aid']; # 用户id + $quota = $request['quota']; # 额度 + + # 检测代理可提余额是否足够 + $inspectRes = AgentUser::inspectWithdrawalBalance($aid,$quota); + if (!$inspectRes['status']) throw new ValidateException($inspectRes['msg']); + + # 开启事务 + $connection->startTrans(); + + # 扣减代理可提余额 + $agentBalance = AgentUser::decrWithdrawalBalance($aid,$quota); + + # 代理下分记录 + AdminDownScoresRecords::createRecords($aid,10010,$quota,$agentBalance,2); + + $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/model/AdminDownScoresRecords.php b/app/model/AdminDownScoresRecords.php index 5bc9876..4c4d376 100644 --- a/app/model/AdminDownScoresRecords.php +++ b/app/model/AdminDownScoresRecords.php @@ -12,17 +12,19 @@ class AdminDownScoresRecords extends Model { /** * 创建下分记录 - * @param $aid + * @param $relation_id // 用户 | 代理id * @param $admin_id * @param $withdrawal_amount * @param $withdrawal_balance + * @param $relation_type // 1=用户 2=代理 * @return mixed */ - public static function createRecords($aid,$admin_id,$withdrawal_amount,$withdrawal_balance) + public static function createRecords($relation_id,$admin_id,$withdrawal_amount,$withdrawal_balance,$relation_type = 1) { $Records = new AdminDownScoresRecords(); - $Records->aid = $aid; + $Records->relation_id = $relation_id; + $Records->relation_type = $relation_type; $Records->admin_id = $admin_id; $Records->withdrawal_amount = $withdrawal_amount; $Records->withdrawal_balance = $withdrawal_balance; diff --git a/app/model/AdminUpScoresRecords.php b/app/model/AdminUpScoresRecords.php index abd6104..96300a7 100644 --- a/app/model/AdminUpScoresRecords.php +++ b/app/model/AdminUpScoresRecords.php @@ -12,17 +12,19 @@ class AdminUpScoresRecords extends Model { /** * 创建上分记录 - * @param $aid + * @param $relation_id * @param $admin_id * @param $balance * @param $residue_amount + * @param $relation_type * @return mixed */ - public static function createRecords($aid,$admin_id,$balance,$residue_amount) + public static function createRecords($relation_id,$admin_id,$balance,$residue_amount,$relation_type = 1) { - $Records = new AgentUpScoresRecords(); + $Records = new AdminUpScoresRecords(); - $Records->aid = $aid; + $Records->relation_id = $relation_id; + $Records->relation_type = $relation_type; $Records->admin_id = $admin_id; $Records->balance = $balance; $Records->residue_amount = $residue_amount; diff --git a/app/model/AgentUser.php b/app/model/AgentUser.php index e9e2543..1a1e0b1 100644 --- a/app/model/AgentUser.php +++ b/app/model/AgentUser.php @@ -142,6 +142,25 @@ class AgentUser extends Model return ['status' => 1]; } + /** + * 检测代理余额 + * @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 inspectWithdrawalBalance($aid,$balance) + { + $userModel = new AgentUser(); + $user = $userModel->find($aid); + if ($user->withdrawal_balance < $balance) { + return ['status' => 0, 'msg' => '代理可提余额不足']; + } + return ['status' => 1]; + } + /** * 生成密码 * @param $password