package cn.com.qmth.stmms.biz.exam.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.common.annotation.ExcelField;

@Entity
@Table(name = "eb_marker")
public class Marker implements Serializable {

    public static final String LOGINNAME_SPLITE = "-";

    private static final long serialVersionUID = -5728058071677581938L;

    @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 = 30)
    private String loginName;

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

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

    @Column(name = "enable")
    private boolean enable;

    @Column(name = "common")
    private boolean common;

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

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

    @Column(name = "mode")
    private String mode;

    @Transient
    @ExcelField(title = "科目(导入时可为空)", align = 2, sort = 20)
    private String subjectName;

    @Transient
    private int number;

    @Transient
    private ExamSubject subject;

    @Transient
    private long markedCount;

    @Transient
    private long currentCount;

    @Column(name = "top_count")
    private Integer topCount;

    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 boolean isEnable() {
        return enable;
    }

    public void setEnable(boolean enable) {
        this.enable = enable;
    }

    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);
        if (!isCommon()) {
            name.append(subjectCode).append(LOGINNAME_SPLITE);
        }
        name.append(number);
        setLoginName(name.toString());
    }

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

    public ExamSubject getSubject() {
        return subject;
    }

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

    public long getMarkedCount() {
        return markedCount;
    }

    public void setMarkedCount(long markedCount) {
        this.markedCount = markedCount;
    }

    public long getCurrentCount() {
        return currentCount;
    }

    public void setCurrentCount(long currentCount) {
        this.currentCount = currentCount;
    }

    public String getSubjectName() {
        return subjectName;
    }

    public void setSubjectName(String subjectName) {
        this.subjectName = subjectName;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isCommon() {
        return common;
    }

    public void setCommon(boolean common) {
        this.common = common;
    }

    public String getMode() {
        return mode;
    }

    public void setMode(String mode) {
        this.mode = mode;
    }

    public Integer getTopCount() {
        return topCount;
    }

    public void setTopCount(Integer topCount) {
        this.topCount = topCount;
    }
}
