<?php
namespace app\common\model;
use app\common\model\user\PointsLog as PointsLogModel;
class User extends BaseModel
{
protected $name = 'user';
private $gender = ['未知', '男', '女'];
public function grade()
{
$module = self::getCalledModule() ?: 'common';
return $this->belongsTo("app\\{$module}\\model\\user\\Grade");
}
public function address()
{
return $this->hasMany('UserAddress');
}
public function addressDefault()
{
return $this->belongsTo('UserAddress', 'address_id');
}
public function getGenderAttr($value)
{
return $this->gender[$value];
}
public static function detail($where, $with = ['address', 'addressDefault'])
{
$filter = ['is_delete' => 0];
if (is_array($where)) {
$filter = array_merge($filter, $where);
} else {
$filter['user_id'] = (int)$where;
}
return static::get($filter, $with);
}
public function setIncUserExpend($userId, $expendMoney)
{
return $this->where(['user_id' => $userId])->setInc('expend_money', $expendMoney);
}
public static function checkExistByGradeId($gradeId)
{
$model = new static;
return !!$model->where('grade_id', '=', (int)$gradeId)->value('user_id');
}
public function setIncPayMoney($money)
{
return $this->setInc('pay_money', $money);
}
public function onBatchIncExpendMoney($data)
{
foreach ($data as $userId => $expendMoney) {
$this->where(['user_id' => $userId])->setInc('expend_money', $expendMoney);
}
return true;
}
public function onBatchIncPoints($data)
{
foreach ($data as $userId => $expendMoney) {
$this->where(['user_id' => $userId])->setInc('points', $expendMoney);
}
return true;
}
public function setIncPoints($points, $describe)
{
PointsLogModel::add([
'user_id' => $this['user_id'],
'value' => $points,
'describe' => $describe,
]);
return $this->setInc('points', $points);
}
}