You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
3.3 KiB
109 lines
3.3 KiB
<?php
|
|
declare (strict_types = 1);
|
|
|
|
namespace app\model;
|
|
|
|
use think\Model;
|
|
use think\model\concern\SoftDelete;
|
|
|
|
/**
|
|
* @mixin \think\Model
|
|
*/
|
|
class AgentInfo extends Model
|
|
{
|
|
//
|
|
use SoftDelete;
|
|
protected $deleteTime = 'delete_time';
|
|
|
|
/**
|
|
* 升级为代理
|
|
* @param $aid
|
|
* @param $rebate_ratio
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public static function createInfo($aid,$rebate_ratio = 0)
|
|
{
|
|
$invite_code = self::returnInviteCode();
|
|
$model = new AgentInfo();
|
|
$save = [
|
|
'aid' => $aid,
|
|
'invite_code' => $invite_code,
|
|
'create_time' => date("Y-m-d H:i:s",time())
|
|
];
|
|
if (!empty($rebate_ratio)) {
|
|
$save['rebate_ratio'] = $rebate_ratio / 100;
|
|
}
|
|
$model->save($save);
|
|
}
|
|
|
|
/**
|
|
* 递归获取验证码(不重复)
|
|
* @return string
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public static function returnInviteCode()
|
|
{
|
|
$invite_code = generate_random_str(6);
|
|
$query = AgentInfo::withTrashed()->where('invite_code',$invite_code)->field('id')->find();
|
|
if ($query) {
|
|
return self::returnInviteCode();
|
|
}
|
|
return $invite_code;
|
|
}
|
|
|
|
|
|
/**
|
|
* 修改返点占比
|
|
* @param $aid
|
|
* @param $rebate_ratio
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public static function updateRebateRatio($aid,$rebate_ratio)
|
|
{
|
|
$AgentInfo = new AgentInfo();
|
|
$AgentInfoRes = $AgentInfo->where('aid',$aid)->find();
|
|
$AgentInfoRes->rebate_ratio = $rebate_ratio / 100;
|
|
$AgentInfoRes->update_time = date("Y-m-d H:i:s",time());
|
|
$AgentInfoRes->save();
|
|
self::syncUpdateRebateRatio($aid);
|
|
}
|
|
|
|
/**
|
|
* 代理修改占比,旗下代理全部清零
|
|
* @param $aid
|
|
* @return false|void
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
protected static function syncUpdateRebateRatio($aid)
|
|
{
|
|
$UserRes = User::where(['aid' => $aid,'identity' => 2])->field('id')->select()->toArray();
|
|
if (empty($UserRes)) return false;
|
|
foreach ($UserRes as $UserRow) {
|
|
# 修改为0
|
|
self::updateRebateRatio($UserRow['id'],0);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 代理被删除,旗下用户及代理变为 没有代理
|
|
* @param $oldAid // 被删除的aid
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public static function syncUpdateAid($oldAid)
|
|
{
|
|
$UserRes = User::where(['aid' => $oldAid, 'identity' => 2])->field('id')->select()->toArray();
|
|
$userIdArr = [];
|
|
foreach ($UserRes as $UserRow) $userIdArr[] = $UserRow['id'];
|
|
User::update(['aid' => 0],['id' => $userIdArr],['aid']);
|
|
}
|
|
}
|
|
|