10 changed files with 401 additions and 3 deletions
@ -0,0 +1,117 @@ |
|||
<?php |
|||
declare (strict_types = 1); |
|||
|
|||
namespace app\controller; |
|||
|
|||
use app\BaseController; |
|||
use app\model\AgentDownScoresRecords; |
|||
use app\model\AgentUpScoresRecords; |
|||
use app\model\AgentUser; |
|||
use app\model\RechargeRecords; |
|||
use app\model\User; |
|||
use app\model\WithdrawalRecords; |
|||
use app\validate\Agent; |
|||
use think\exception\ValidateException; |
|||
use think\facade\Db; |
|||
use think\facade\Request; |
|||
use think\facade\Session; |
|||
|
|||
class AgentTeam extends BaseController |
|||
{ |
|||
|
|||
/** |
|||
* 上分 |
|||
* @return array |
|||
*/ |
|||
public function upScores() |
|||
{ |
|||
$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 = 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()); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,34 @@ |
|||
<?php |
|||
declare (strict_types = 1); |
|||
|
|||
namespace app\model; |
|||
|
|||
use think\Model; |
|||
|
|||
/** |
|||
* @mixin \think\Model |
|||
*/ |
|||
class AgentDownScoresRecords extends Model |
|||
{ |
|||
/** |
|||
* 创建下分记录 |
|||
* @param $aid |
|||
* @param $user_id |
|||
* @param $withdrawal_amount |
|||
* @param $withdrawal_balance |
|||
* @return mixed |
|||
*/ |
|||
public static function createRecords($aid,$user_id,$withdrawal_amount,$withdrawal_balance) |
|||
{ |
|||
$Records = new AgentDownScoresRecords(); |
|||
|
|||
$Records->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; |
|||
} |
|||
} |
|||
@ -0,0 +1,34 @@ |
|||
<?php |
|||
declare (strict_types = 1); |
|||
|
|||
namespace app\model; |
|||
|
|||
use think\Model; |
|||
|
|||
/** |
|||
* @mixin \think\Model |
|||
*/ |
|||
class AgentUpScoresRecords extends Model |
|||
{ |
|||
/** |
|||
* 创建上分记录 |
|||
* @param $aid |
|||
* @param $user_id |
|||
* @param $withdrawal_amount |
|||
* @param $withdrawal_balance |
|||
* @return mixed |
|||
*/ |
|||
public static function createRecords($aid,$user_id,$balance,$residue_amount) |
|||
{ |
|||
$Records = new AgentUpScoresRecords(); |
|||
|
|||
$Records->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; |
|||
} |
|||
} |
|||
@ -0,0 +1,37 @@ |
|||
<?php |
|||
declare (strict_types = 1); |
|||
|
|||
namespace app\model; |
|||
|
|||
use think\Model; |
|||
|
|||
/** |
|||
* @mixin \think\Model |
|||
*/ |
|||
class RechargeRecords extends Model |
|||
{ |
|||
/** |
|||
* 创建充值记录 |
|||
* @param $user_id |
|||
* @param $recharge_amount |
|||
* @param $residue_amount |
|||
* @param $status |
|||
* @param $trade_type |
|||
* @return mixed |
|||
*/ |
|||
public static function createRecords($user_id,$recharge_amount,$residue_amount,$status = 0,$trade_type = 1) |
|||
{ |
|||
$RechargeRecords = new RechargeRecords(); |
|||
|
|||
$RechargeRecords->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; |
|||
} |
|||
} |
|||
@ -0,0 +1,36 @@ |
|||
<?php |
|||
declare (strict_types = 1); |
|||
|
|||
namespace app\model; |
|||
|
|||
use think\Model; |
|||
|
|||
/** |
|||
* @mixin \think\Model |
|||
*/ |
|||
class WithdrawalRecords extends Model |
|||
{ |
|||
/** |
|||
* 用户提现记录 |
|||
* @param $user_id |
|||
* @param $withdrawal_amount |
|||
* @param $withdrawal_balance |
|||
* @param $status |
|||
* @param $trade_type |
|||
* @return mixed |
|||
*/ |
|||
public static function createRecords($user_id,$withdrawal_amount,$withdrawal_balance,$status = 0,$trade_type = 1) |
|||
{ |
|||
$Records = new WithdrawalRecords(); |
|||
|
|||
$Records->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; |
|||
} |
|||
} |
|||
@ -0,0 +1,32 @@ |
|||
<?php |
|||
declare (strict_types = 1); |
|||
|
|||
namespace app\validate; |
|||
|
|||
use think\Validate; |
|||
|
|||
class Agent extends Validate |
|||
{ |
|||
/** |
|||
* 定义验证规则 |
|||
* 格式:'字段名' => ['规则1','规则2'...] |
|||
* |
|||
* @var array |
|||
*/ |
|||
protected $rule = [ |
|||
'user_id|用户id' => 'require|number', |
|||
'quota|额度' => 'require|number' |
|||
]; |
|||
|
|||
/** |
|||
* 定义错误信息 |
|||
* 格式:'字段名.规则名' => '错误信息' |
|||
* |
|||
* @var array |
|||
*/ |
|||
protected $message = []; |
|||
|
|||
protected $scene = [ |
|||
'scores' => ['user_id','quota'] |
|||
]; |
|||
} |
|||
Loading…
Reference in new issue