car4s/application/common/model/user4s/Log.php

100 lines
2.5 KiB
PHP

<?php
namespace app\common\model\user4s;
use think\Model;
use traits\model\SoftDelete;
use app\common\model\user4s\User as UserModel;
class Log extends Model
{
use SoftDelete;
// 表名
protected $name = 'user4s_log';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = false;
protected $deleteTime = 'deletetime';
// 追加属性
protected $append = [
'state_text'
];
public function getStateList()
{
return ['0' => __('State 0'), '1' => __('State 1')];
}
public function getStateTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['state']) ? $data['state'] : '');
$list = $this->getStateList();
return isset($list[$value]) ? $list[$value] : '';
}
public function addLog($uid,$admin_id,$price=0,$integral=0,$description='')
{
if($price = 0 && $integral = 0){
return false;
}
$user = UserModel::get($uid);
if(!$user){
return false;
}
if($price > 0){
if($description == ''){
$description = '后台操作,扣除余额'.$price.'元';
}
$data = [
'user4s_id'=>$uid,
'admin_id'=>$admin_id,
'start'=>$user->balance,
'end'=>$user->balance - $price,
'description'=>$description,
'createtime'=>time(),
'state'=>0,
];
$this->isUpdate(false)->save($data);
}
if($integral > 0){
if($description == ''){
$description = '后台操作,扣除积分'.$integral;
}
$data = [
'user4s_id'=>$uid,
'admin_id'=>$admin_id,
'start'=>$user->integral,
'end'=>$user->integral - $integral,
'description'=>$description,
'createtime'=>time(),
'state'=>1,
];
$this->isUpdate(false)->save($data);
}
return true;
}
public function user()
{
return $this->belongsTo('User', 'user4s_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
public function admin()
{
return $this->belongsTo('app\common\model\Admin', 'admin_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
}