User.java 1.72 KB
package com.ecommerce.user.model;

import lombok.Data;
import javax.persistence.*;
import java.time.LocalDateTime;

@Data
@Entity
@Table(name = "users")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @Column(unique = true, nullable = false)
    private String username;
    
    @Column(unique = true, nullable = false)
    private String email;
    
    @Column(nullable = false)
    private String password;
    
    private String firstName;
    private String lastName;
    
    // 添加电话字段
    private String phone;
    
    // 添加最后登录时间字段
    private LocalDateTime lastLoginAt;
    
    @Column(nullable = false)
    private Boolean enabled = true;
    
    private LocalDateTime createdAt;
    private LocalDateTime updatedAt;
    
    @PrePersist
    protected void onCreate() {
        createdAt = LocalDateTime.now();
        updatedAt = LocalDateTime.now();
        enabled = true; // 确保新用户默认启用
    }
    
    @PreUpdate
    protected void onUpdate() {
        updatedAt = LocalDateTime.now();
    }
    
    // 为了方便,可以添加一个设置最后登录时间的方法
    public void updateLastLogin() {
        this.lastLoginAt = LocalDateTime.now();
    }
    
    // 添加缺失的方法来匹配 AuthService.java 中的调用
    public void setPasswordHash(String passwordHash) {
        this.password = passwordHash;
    }
    
    public String getPasswordHash() {
        return this.password;
    }
    
    public void setLastLoginAt(LocalDateTime lastLoginAt) {
        this.lastLoginAt = lastLoginAt;
    }
    
    // 确保有 getPhone() 和 setPhone() 方法
    // Lombok @Data 注解会自动生成这些方法
}