manghe/app/common/model/UserAddress.php
2025-05-04 16:54:54 +08:00

55 lines
1.4 KiB
PHP

<?php
declare(strict_types=1);
namespace app\common\model;
use think\Model;
class UserAddress extends Model
{
// 设置表名
protected $name = 'user_address';
// 自动时间戳
protected $autoWriteTimestamp = true;
// 设置字段信息
protected $schema = [
'id' => 'int',
'user_id' => 'int',
'receiver_name' => 'string',
'receiver_phone' => 'string',
'detailed_address' => 'string',
'is_default' => 'int',
'create_time' => 'datetime',
'update_time' => 'datetime',
'is_deleted' => 'int'
];
// 查询时默认排除已删除的记录
protected function base($query)
{
$query->where('is_deleted', 0);
}
// 获取用户的地址数量(未删除的)
public static function getUserAddressCount($userId)
{
return self::where('user_id', $userId)
->where('is_deleted', 0)
->count();
}
// 如果设置了默认地址,需要将其他地址设置为非默认
public static function setOtherAddressNotDefault($userId, $exceptId = null)
{
$query = self::where('user_id', $userId)
->where('is_default', 1);
if ($exceptId) {
$query->where('id', '<>', $exceptId);
}
return $query->update(['is_default' => 0]);
}
}