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

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.apache.commons.lang3.StringUtils;

import net.sf.json.JSONObject;
import cn.com.qmth.stmms.common.enums.ImportType;

@Entity
@Table(name = "eb_import_query")
public class ImportQuery implements Serializable {

    private static final long serialVersionUID = -7983129784062675039L;

    public static final String DB_ITEM_JOINER = ",";

    public static final String SUCCESS = "success";

    public static final String TAG = "tag";

    public static final String FAILURE = "failure";

    @Id
    @GeneratedValue
    private Integer id;

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

    @Column(name = "user_id", nullable = false)
    private Integer userId;

    @Enumerated(EnumType.STRING)
    @Column(name = "type", length = 16, nullable = false)
    private ImportType type;

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

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "create_time")
    private Date createTime;

    public ImportQuery(Integer examId, Integer userId, ImportType type, String description) {
        this.examId = examId;
        this.userId = userId;
        this.type = type;
        this.description = description;
        this.createTime = new Date();
    }

    public ImportQuery() {
    }

    public ImportQuery(Integer examId, Integer userId, ImportType type, Set<Integer> successStudent,
            Set<String> failureStudent) {
        this.examId = examId;
        this.userId = userId;
        this.type = type;
        JSONObject json = new JSONObject();
        json.accumulate(SUCCESS, StringUtils.join(successStudent, DB_ITEM_JOINER));
        json.accumulate(FAILURE, StringUtils.join(failureStudent, DB_ITEM_JOINER));
        json.accumulate(TAG, "");
        this.description = json.toString();
        this.createTime = new Date();
    }

    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 Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public ImportType getType() {
        return type;
    }

    public void setType(ImportType type) {
        this.type = type;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public List<String> getStudentCodeList() {
        List<String> list = new ArrayList<String>();
        JSONObject json = JSONObject.fromObject(description);
        String failure = json.getString(FAILURE);
        String[] values = StringUtils.split(failure, DB_ITEM_JOINER);
        for (String studentCode : values) {
            list.add(studentCode);
        }
        return list;
    }

    public List<Integer> getStudentIdList() {
        List<Integer> list = new ArrayList<Integer>();
        JSONObject json = JSONObject.fromObject(description);
        String success = json.getString(SUCCESS);
        String[] values = StringUtils.split(success, DB_ITEM_JOINER);
        for (String studentId : values) {
            list.add(Integer.parseInt(studentId));
        }
        return list;
    }

    public List<Integer> getTagIdList() {
        List<Integer> list = new ArrayList<Integer>();
        JSONObject json = JSONObject.fromObject(description);
        String tag = json.getString(TAG);
        String[] values = StringUtils.split(tag, DB_ITEM_JOINER);
        for (String studentId : values) {
            list.add(Integer.parseInt(studentId));
        }
        return list;
    }

    public String getStudentIdString() {
        JSONObject json = JSONObject.fromObject(description);
        String success = json.getString(SUCCESS);
        return success;
    }

    public String buildDescription(Set<Integer> successStudent, Set<String> failureStudent) {
        JSONObject json = new JSONObject();
        json.accumulate(SUCCESS, StringUtils.join(successStudent, DB_ITEM_JOINER));
        json.accumulate(FAILURE, StringUtils.join(failureStudent, DB_ITEM_JOINER));
        return json.toString();

    }

    public void buildTag(List<Integer> ids) {
        JSONObject json = JSONObject.fromObject(description);
        json.remove(TAG);
        json.accumulate(TAG, StringUtils.join(ids, DB_ITEM_JOINER));
        this.description = json.toString();
    }
}
