package net.tccn.user;

import lombok.Getter;
import lombok.Setter;
import net.tccn.base.JBean;
import org.redkale.persistence.Cacheable;
import org.redkale.persistence.Column;
import org.redkale.persistence.Entity;
import org.redkale.persistence.Id;
import org.redkale.util.Utility;

@Getter
@Setter
@Entity
@Cacheable(value = true, interval = 60 * 60 * 1000, direct = true)
public class MUser {
    @Id
    @Column(comment = "[用户ID]")
    private Integer userid;

    @Column(length = 64, comment = "[用户名]")
    private String username;

    @Column(length = 64, comment = "[加密后的密码]")
    private String pwd;

    @Column(length = 64, comment = "[会话ID]")
    private String sessionid;

    @Column(comment = "[状态] 1启用，0未启用")
    private short status;

    @Column(updatable = false, comment = "[创建时间]")
    private long createtime;

    @Column(comment = "[最后登录时间]")
    private long logintime;

    public static String md5IfNeed(String password) {
        if (password == null || password.length() == 0) {
            return "";
        }
        return Utility.md5Hex(password);
    }

    public JBean checkLogin(String pwd) {
        JBean jBean = JBean.by(0, "");

        if (this.pwd == null || this.pwd.isEmpty()) {
            jBean.set(-1, "密码错误");
        }
        if (!this.pwd.equalsIgnoreCase(md5IfNeed(pwd))) {
            jBean.set(-1, "密码错误");
        } else if (status != 1) {
            jBean.set(-1, "登陆失败，限制登陆。");
        }
        jBean.setBody(this);
        return jBean;
    }
}
