90 lines
2.5 KiB
PHP
90 lines
2.5 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace app\common\model;
|
||
|
|
|
||
|
|
use think\Db;
|
||
|
|
use think\Model;
|
||
|
|
|
||
|
|
|
||
|
|
class UserBondLog extends Model
|
||
|
|
{
|
||
|
|
|
||
|
|
// 开启自动写入时间戳字段
|
||
|
|
protected $autoWriteTimestamp = 'int';
|
||
|
|
// 定义时间戳字段名
|
||
|
|
protected $createTime = 'createtime';
|
||
|
|
protected $updateTime = '';
|
||
|
|
|
||
|
|
|
||
|
|
public static function init()
|
||
|
|
{
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
public function add_log($type,$intimacy,$user_info){
|
||
|
|
//查询每日牵绊数据
|
||
|
|
$UserBondConfig = new UserBondConfig();
|
||
|
|
$info = $UserBondConfig->where('type',$type)->find();
|
||
|
|
//查询今日以获取牵绊数据
|
||
|
|
$intimacy_day = $this->where([
|
||
|
|
'user_id'=>$user_info['id'],
|
||
|
|
'type'=>$type,
|
||
|
|
'createdate'=>date('Y-m-d')
|
||
|
|
])->sum('intimacy');
|
||
|
|
|
||
|
|
//可增加数量 = 上限-今日已获取-增加数量
|
||
|
|
$available_intimacy = $info['upper']-$intimacy_day-$intimacy;
|
||
|
|
//判断领取本次亲密度后是否超过上限
|
||
|
|
if($available_intimacy<=0){
|
||
|
|
//计算剩余可领取数量
|
||
|
|
$intimacy = $info['upper']-$intimacy_day;
|
||
|
|
if($intimacy<=0){
|
||
|
|
$result = [
|
||
|
|
'intimacy'=>0,
|
||
|
|
'msg'=>'今日已达到上限'
|
||
|
|
];
|
||
|
|
return $result;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
//增加牵绊
|
||
|
|
$this->save([
|
||
|
|
'user_id'=>$user_info['id'],
|
||
|
|
'type'=>$type,
|
||
|
|
'intimacy'=>$intimacy,
|
||
|
|
'createtime'=>time(),
|
||
|
|
'createdate'=>date('Y-m-d')
|
||
|
|
]);
|
||
|
|
//增加用户表亲密度值
|
||
|
|
|
||
|
|
$user_intimacy = $user_info['intimacy']+$intimacy;
|
||
|
|
$user_level = $user_info['level'];
|
||
|
|
//查询用户下一个等级
|
||
|
|
$next_level = UserLevel::where('level',$user_info['level']+1)->find();
|
||
|
|
//有下一个级别
|
||
|
|
$upgrade_level = false;
|
||
|
|
if(!empty($next_level)){
|
||
|
|
$next_level_intimacy = $next_level['intimacy'];
|
||
|
|
if($user_intimacy>=$next_level_intimacy){
|
||
|
|
$user_level = $user_info['level']+1;
|
||
|
|
$upgrade_level = true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
$user_model = new User();
|
||
|
|
$user_model->where('id',$user_info['id'])->update([
|
||
|
|
'intimacy'=>$user_intimacy,
|
||
|
|
'level'=>$user_level
|
||
|
|
]);
|
||
|
|
$result = [
|
||
|
|
'upgrade_level'=>$upgrade_level,
|
||
|
|
'intimacy'=>$intimacy,
|
||
|
|
'msg'=>'领取成功'
|
||
|
|
];
|
||
|
|
return $result;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
}
|