Browse Source

新刮奖中奖概率逻辑 及 用户添加编辑中奖概率

master
wanghongjun 2 years ago
parent
commit
c0755220b4
  1. 3
      app/command/Autousezone.php
  2. 13
      app/common.php
  3. 23
      app/controller/AdminAgentTeam.php
  4. 28
      app/controller/AdminUserTeam.php
  5. 2
      app/controller/AgentTeam.php
  6. 2
      app/controller/Index.php
  7. 2
      app/controller/Zone.php
  8. 105
      app/model/RechargeRecords.php
  9. 18
      app/model/User.php
  10. 3
      app/model/ZoneGoods.php
  11. 5
      app/validate/Agent.php
  12. 5
      app/validate/User.php

3
app/command/Autousezone.php

@ -3,6 +3,7 @@
namespace app\command;
use app\logic\Zone;
use app\logic\ZoneLogic;
use think\console\Command;
use think\console\Input;
use think\console\Output;
@ -27,7 +28,7 @@ class Autousezone extends Command
// 处理您的代码逻辑,使用获取到的选项值
// 执行脚本逻辑
$output->writeln(Zone::auto($user_id,$zone_goods_id,$limit));
$output->writeln(ZoneLogic::auto($user_id,$zone_goods_id,$limit));
}
}

13
app/common.php

@ -351,3 +351,16 @@ function get_new_apk_filename():string
return reset($files);
}
/**
* 返回随机浮点数 指定区间
* @param float $st_num
* @param float $end_num
* @param int $mul
* @return float
*/
function rand_float(float $st_num=0,float $end_num=1,int $mul=1000000):float
{
if ($st_num>$end_num) return false;
return mt_rand($st_num*$mul,$end_num*$mul)/$mul;
}

23
app/controller/AdminAgentTeam.php

@ -58,7 +58,7 @@ class AdminAgentTeam extends BaseController
}
# 查询用户列表
$field = 'id,aid,phone,avatar,balance,withdrawal_balance,status';
$field = 'id,aid,phone,avatar,balance,withdrawal_balance,status,rate';
$userRes = $UserModel->field($field)->where($where)->order('id desc')->paginate($limit);
$list = $userRes->items();
@ -73,6 +73,7 @@ class AdminAgentTeam extends BaseController
$rebate_ratio = $AgentInfo->where('aid',$item['id'])->value('rebate_ratio');
$item['rebate_ratio'] = $rebate_ratio ? round($rebate_ratio * 100,2) : '0';
$item['grade'] = empty($item['aid']) ? '1级' : 'id:' . $item['aid'];
$item['rate'] = round($item['rate'] * 100,2);
unset($item['aid']);
}
@ -174,12 +175,16 @@ class AdminAgentTeam extends BaseController
validate()->rule([
'aid|代理id' => 'require|number',
'password|密码' => 'min:6|max:20',
'rebate_ratio|返点占比' => 'float'
'rebate_ratio|返点占比' => 'float|between:0.01,100',
'rate|中奖比率' => 'float|between:0.01,100',
])->check($param);
$password = $param['password'] ?? '';
$rebate_ratio = (string) $param['rebate_ratio'] ?? '';
if (empty($password) && strlen($rebate_ratio) <= 0) {
throw new ValidateException('请填写修改的密码或返点占比');
$param['rebate_ratio'] = $param['rebate_ratio'] ?? '';
$rebate_ratio = (string) $param['rebate_ratio'];
$param['rate'] = $param['rate'] ?? '';
$rate = (string) $param['rate'];
if (empty($password) && strlen($rebate_ratio) <= 0 && strlen($rate) <= 0) {
throw new ValidateException('请填写至少一项修改');
}
# 开启事务
@ -205,6 +210,10 @@ class AdminAgentTeam extends BaseController
if ($validateAgentRebateRatio !== true) throw new ValidateException($validateAgentRebateRatio);
AgentInfo::updateRebateRatio($param['aid'],$rebate_ratio);
}
# 保存占比
if (!empty($rate)) {
User::saveRate($param['aid'],$rate);
}
$connection->commit();
return $this->renderSuccess('成功');
@ -277,7 +286,9 @@ class AdminAgentTeam extends BaseController
if (!$aid) throw new ValidateException('代理已存在');
AgentInfo::createInfo($aid,$param['rebate_ratio']);
if (isset($param['rebate_ratio'])) {
AgentInfo::createInfo($aid,$param['rebate_ratio']);
}
$connection->commit();
return $this->renderSuccess('添加成功');

28
app/controller/AdminUserTeam.php

@ -59,7 +59,7 @@ class AdminUserTeam extends BaseController
}
# 查询用户列表
$field = 'id,aid,phone,avatar,balance,withdrawal_balance,status';
$field = 'id,aid,phone,avatar,balance,withdrawal_balance,status,rate';
$userRes = $UserModel->field($field)->where($where)->order('id desc')->paginate($limit);
$list = $userRes->items();
@ -69,6 +69,7 @@ class AdminUserTeam extends BaseController
$item['avatar'] = get_image_url($item['avatar']);
$item['status'] = $item['status'] == 1 ? '正常' : '停用';
$item['rate'] = round($item['rate'] * 100,2);
}
return $this->renderSuccess('数据返回成功', ['list' => $list, 'total' => $total]);
@ -109,19 +110,36 @@ class AdminUserTeam extends BaseController
try {
validate(UserValidate::class)->scene('editUser')->check($param);
validate()->rule([
'user_id|用户id' => 'require|number',
'password|密码' => 'min:6|max:20',
'rate|中奖比率' => 'float|between:0.01,100',
])->check($param);
$password = $param['password'] ?? '';
$rate = $param['rate'] ?? '';
if (empty($password) && strlen($rate) <= 0) {
throw new ValidateException('请填写至少一项修改');
}
$UserModel = new User();
$result = $UserModel->retrieve($param);
$result = [];
if ($password) {
$result = $UserModel->retrieve($param);
if (!$result['status']) throw new ValidateException($result['msg']);
}
if (!$result['status']) throw new ValidateException($result['msg']);
if ($rate) User::saveRate($param['user_id'],$rate);
$event = new UserPasswordChange($param['user_id']);
Event::trigger(UserPasswordChange::class,$event);
return $this->renderSuccess($result['msg']);
return $this->renderSuccess('修改成功');
} catch (ValidateException $validateException) {
return $this->renderError($validateException->getMessage());
} catch (\Exception $e) {
return $this->renderError('操作失败');
}
}

2
app/controller/AgentTeam.php

@ -405,7 +405,7 @@ class AgentTeam extends BaseController
$agentData = $this->request->userInfo;
validate()->rule([
'user_id|用户id' => 'require|number',
'rebate_ratio|返点占比' => 'require|float'
'rebate_ratio|返点占比' => 'require|float|between:0.01,100'
])->check($param);
$user_id = $param['user_id'];

2
app/controller/Index.php

@ -152,7 +152,7 @@ class Index extends BaseController
$user_id = $param['user_id'] ?? 1;
$zone_goods_id = $param['zone_goods_id'] ?? 1;
$result = \app\logic\Zone::auto($user_id,$zone_goods_id,$limit);
$result = \app\logic\ZoneLogic::auto($user_id,$zone_goods_id,$limit);
if ($result != '完成') throw new ValidateException($result);
return $this->renderSuccess($result);

2
app/controller/Zone.php

@ -7,7 +7,7 @@ use app\middleware\CheckUser;
use app\model\ConsumptionRecords;
use app\model\Zone as ZoneModel;
use app\model\ZoneGoods;
use app\logic\Zone as ZoneLogic;
use app\logic\ZoneLogic;
use app\model\ZoneGoodsPlay;
use think\facade\Request;
use think\facade\Session;

105
app/model/RechargeRecords.php

@ -4,41 +4,122 @@ declare (strict_types = 1);
namespace app\model;
use think\Model;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
/**
* @mixin \think\Model
* 充值记录
*/
class RechargeRecords extends Model
{
public $tradeType = [1 => '代理上分', 2 => '管理员上分', 3 => '支付宝', 4 => '消费返点'];
/**
* 创建充值记录
* @param $user_id
* @param $recharge_amount
* @param $residue_amount
* @param $status
* @param $trade_type
* @param $trade_id
* @param int $status
* @param int $trade_type
* @param int $trade_id
* @return mixed
*/
public static function createRecords($user_id,$recharge_amount,$residue_amount,$status = 0,$trade_type = 1,$trade_id = 0)
public static function createRecords($user_id, $recharge_amount, $residue_amount, int $status = 0,int $trade_type = 1,int $trade_id = 0)
{
$RechargeRecords = new RechargeRecords();
$RechargeRecords->user_id = $user_id;
$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->trade_id = $trade_id;
$RechargeRecords->status = $status;
$RechargeRecords->residue_amount = $residue_amount;
$RechargeRecords->recharge_time = date("Y-m-d H:i:s", time());
$RechargeRecords->trade_type = $trade_type;
$RechargeRecords->trade_id = $trade_id;
$RechargeRecords->status = $status;
if ($status == 1) {
$RechargeRecords->pay_time = date("Y-m-d H:i:s",time());
$RechargeRecords->pay_time = date("Y-m-d H:i:s", time());
}
$RechargeRecords->save();
return $RechargeRecords->id;
}
/**
* 最近一次充值金额
* @param array $where
* @return array
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public static function lastRechargeAmount(array $where = []): array
{
$RechargeRecords = new RechargeRecords();
$rechargeRes = $RechargeRecords->where($where)
->where('status', 1)
->where('usage_status', 0)
->field('id,recharge_amount,usage_amount,recharge_time')
->order('recharge_time','asc')
->find();
$rechargeArr = [];
if ($rechargeRes) $rechargeArr = $rechargeRes->toArray();
return $rechargeArr;
}
/**
* 增加使用金额
* @param $where // 初始[ ['id','=',$id] ]
* @param $usageAmount // 增加金额
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public static function incrUsageAmount($where, $usageAmount)
{
// 充值记录
$RechargeRecords = self::lastRechargeAmount($where);
$id = $RechargeRecords['id']; // 已使用金额
$usage_amount = $RechargeRecords['usage_amount']; // 已使用金额
$recharge_timeWhere = [
['id', '>', $id],
['recharge_time', '>', $RechargeRecords['recharge_time']]
]; // 充值时间
$recharge_amount = $RechargeRecords['recharge_amount']; // 充值金额
// 当前使用金额
$target_usage_amount = $usageAmount + $usage_amount;
// 保存充值金额
$incr_usage_amount = $target_usage_amount;
// 超出此次充值记录使用金额
if ($target_usage_amount > $recharge_amount) {
$incr_usage_amount = $target_usage_amount - $recharge_amount;
// 剩余-增加-使用金额
$incr_next_usage_amount = $usageAmount - $incr_usage_amount;
# 递归增加使用金额
self::incrUsageAmount($recharge_timeWhere,$incr_next_usage_amount);
}
// 保存此次消费
if ($incr_usage_amount > 0) {
$updateArr = ['usage_amount' => $incr_usage_amount];
if ($recharge_amount == $incr_usage_amount) {
$updateArr['usage_status'] = 1;
}
self::update($updateArr,['id' => $id]);
}
}
}

18
app/model/User.php

@ -52,6 +52,9 @@ class User extends Model
$rand = rand(5,50);
$save['id'] = $lastId + $rand;
}
if (isset($data['rate']) && $data['rate'] > 0) {
$save['rate'] = round($data['rate'] / 100,4);
}
$User->save($save);
return $User->id;
@ -400,6 +403,21 @@ class User extends Model
return $UserData;
}
/**
* 编辑概率
* @param $user_id
* @param $rate
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public static function saveRate($user_id,$rate)
{
$User = User::find($user_id);
$User->rate = round($rate / 100,4);
$User->save();
}
/**
* 生成盐值
* @return string

3
app/model/ZoneGoods.php

@ -21,7 +21,8 @@ class ZoneGoods extends \think\Model
$item['image_path'] = $imageData['image_path'];
$item['image_url'] = $imageData['image_url'];
}
$item['max_awards_amount'] = PinyinNumber::getChinese($item['max_awards_amount'],'元');
$item['price'] = round($item['price']);
#$item['max_awards_amount'] = PinyinNumber::getChinese($item['max_awards_amount'],'元');
if ($is_manage) {
unset($item['status']);
}

5
app/validate/Agent.php

@ -21,7 +21,8 @@ class Agent extends Validate
'repassword|确认密码' => 'require|confirm:password',
'aid|代理id' => 'require|number',
'phone|手机号' => 'require|mobile',
'rebate_ratio|返点占比' => 'float'
'rebate_ratio|返点占比' => 'float|between:0.01,100',
'rate|比率' => 'float|between:0.01,100',
];
/**
@ -36,7 +37,7 @@ class Agent extends Validate
'scores' => ['user_id','quota'],
'edit' => ['aid','password','rebate_ratio'],
'del' => ['aid'],
'register' => ['phone','password','rebate_ratio'],
'register' => ['phone','password','rebate_ratio','rate'],
'modifyPassword' => ['password','repassword'],
'promotion' => ['user_id','rebate_ratio']
];

5
app/validate/User.php

@ -19,7 +19,8 @@ class User extends Validate
'password|密码' => 'require|min:6|max:20',
'repassword|确认密码' => 'require|confirm:password',
'phone|手机号' => 'require|mobile',
'user_id|用户id' => 'require|number'
'user_id|用户id' => 'require|number',
'rate|中奖比率' => 'float|between:0.01,100',
];
/**
@ -34,7 +35,7 @@ class User extends Validate
= [
'modifyPassword' => ['password', 'repassword'],
'editUser' => ['user_id', 'password'],
'register' => ['phone', 'password'],
'register' => ['phone', 'password','rate'],
'delUser' => ['user_id'],
];

Loading…
Cancel
Save