package cn.com.qmth.stmms.biz.subjectheader.model;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.StringUtils;

import cn.com.qmth.stmms.biz.exam.model.ExamSubject;
import cn.com.qmth.stmms.common.annotation.ExcelField;

@Entity
@Table(name = "eb_subject_header")
public class SubjectHeader implements Serializable {

    public static final String LOGINNAME_SPLITE = "-";

    private static final long serialVersionUID = -4411296033041898963L;

    @Id
    @GeneratedValue
    private Integer id;

    @Column(name = "exam_id")
    private Integer examId;

    @Column(name = "subject_code")
    @ExcelField(title = "科目代码", align = 2, sort = 10)
    private String subjectCode;

    @Column(name = "login_name")
    @ExcelField(title = "登录名", align = 2, sort = 20)
    private String loginName;

    @Column(name = "password")
    @ExcelField(title = "密码", align = 2, sort = 30)
    private String password;

    @Column(name = "last_login_ip")
    private String lastLoginIp;

    @Column(name = "last_login_time")
    private Date lastLoginTime;

    @Transient
    private int number;

    @Transient
    private ExamSubject subject;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getExamId() {
        return examId;
    }

    public void setExamId(Integer examId) {
        this.examId = examId;
    }

    public String getSubjectCode() {
        return subjectCode;
    }

    public void setSubjectCode(String subjectCode) {
        this.subjectCode = subjectCode;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getLastLoginIp() {
        return lastLoginIp;
    }

    public void setLastLoginIp(String lastLoginIp) {
        this.lastLoginIp = lastLoginIp;
    }

    public Date getLastLoginTime() {
        return lastLoginTime;
    }

    public void setLastLoginTime(Date lastLoginTime) {
        this.lastLoginTime = lastLoginTime;
    }

    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public void buildLoginName(int number) {
        StringBuilder name = new StringBuilder();
        name.append(examId).append(LOGINNAME_SPLITE);
        name.append(subjectCode).append(LOGINNAME_SPLITE);
        name.append(number);
        setLoginName(name.toString());
    }

    public static SubjectHeader parseLoginName(String loginName) {
        SubjectHeader header = null;
        try {
            String[] values = StringUtils.split(loginName, LOGINNAME_SPLITE);
            header = new SubjectHeader();
            header.setExamId(Integer.valueOf(values[0]));
            header.setSubjectCode(values[1]);
            header.setNumber(Integer.valueOf(values[2]));
        } catch (Exception e) {
            header = null;
        }
        return header;
    }

    public ExamSubject getSubject() {
        return subject;
    }

    public void setSubject(ExamSubject subject) {
        this.subject = subject;
    }
}
