package cn.com.qmth.stmms.ms.core.specification;

import cn.com.qmth.stmms.ms.commons.utils.specification.PagingAndSortingSpecification;
import cn.com.qmth.stmms.ms.core.domain.Student;
import cn.com.qmth.stmms.ms.core.domain.enums.TrialEnum;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class StudentSpecification extends PagingAndSortingSpecification {

    private String workId;
    private String areaCode;
    private String examNumber;
    private String studentName;
    private String uploadStatus;
    private Boolean isAbsent;
    private Long startNumber;//起始考号
    private Long endNumber;//终止考号
    private Boolean upload;//上传状态
    private String subject;//科目

    private String school;
    private String examRoom;

    public Boolean getAbsent() {
        return isAbsent;
    }

    public void setAbsent(Boolean absent) {
        isAbsent = absent;
    }

    public String getSubject() {
        return subject;
    }

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

    public Long getStartNumber() {
        return startNumber;
    }

    public void setStartNumber(Long startNumber) {
        this.startNumber = startNumber;
    }

    public Long getEndNumber() {
        return endNumber;
    }

    public void setEndNumber(Long endNumber) {
        this.endNumber = endNumber;
    }

    public Boolean getUpload() {
        return upload;
    }

    public void setUpload(Boolean upload) {
        this.upload = upload;
    }

    public String getWorkId() {
        return workId;
    }

    public void setWorkId(String workId) {
        this.workId = workId;
    }

    public String getAreaCode() {
        return areaCode;
    }

    public void setAreaCode(String areaCode) {
        this.areaCode = areaCode;
    }

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public String getExamNumber() {
        return examNumber;
    }

    public void setExamNumber(String examNumber) {
        this.examNumber = examNumber;
    }

    public Boolean getIsAbsent() {
        return isAbsent;
    }

    public void setIsAbsent(Boolean isAbsent) {
        this.isAbsent = isAbsent;
    }

    public String getUploadStatus() {
        return uploadStatus;
    }

    public void setUploadStatus(String uploadStatus) {
        this.uploadStatus = uploadStatus;
    }

    public String getSchool() {
        return school;
    }

    public void setSchool(String school) {
        this.school = school;
    }

    public String getExamRoom() {
        return examRoom;
    }

    public void setExamRoom(String examRoom) {
        this.examRoom = examRoom;
    }

    @Override
    public Specification<Student> getSpecification() {
        return (root, query, cb) -> {
            List<Predicate> predicates = new ArrayList<Predicate>();
            predicates.add(cb.equal(root.get("test"), String.valueOf(TrialEnum.DEFAULT.getId())));
            if (getStudentName() != null) {
                predicates.add(cb.like(root.get("name"), "%" + getStudentName() + "%"));
            }
//            if(getExamNumber() != null){
//                predicates.add(cb.like(root.get("examNumber"),"%"+getExamNumber()+"%"));
//            }
            if (getWorkId() != null) {
                predicates.add(cb.equal(root.get("workId"), getWorkId()));
            }
            if (getAreaCode() != null && getAreaCode() != "") {
                predicates.add(cb.equal(root.get("areaCode"), getAreaCode()));
            }
            if (getIsAbsent() != null) {
                predicates.add(cb.equal(root.get("isAbsent"), getIsAbsent()));
            }
//            if (getUploadStatus() != null) {
//                predicates.add(cb.like(root.get("uploadStatus"), "%" + getUploadStatus() + "%"));
//            }
            if (startNumber != null && endNumber != null) {
                predicates.add(cb.between(root.get("examNumber"), startNumber, endNumber));
            } else if (startNumber != null) {
                predicates.add(cb.equal(root.get("examNumber"), startNumber));
            } else if (endNumber != null) {
                predicates.add(cb.equal(root.get("examNumber"), endNumber));
            }
            if (getUpload() != null && !getUpload()) {
                if (Objects.nonNull(getSubject())) {
                    predicates.add(cb.like(root.get("uploadStatus"), "%" + getSubject() + ":0" + "%"));
                } else {
                    predicates.add(cb.equal(root.get("uploadStatus"), "SX:0,SC:0,SM:0"));
                }
            } else if (getUpload() != null && getUpload()) {
                if (Objects.nonNull(getSubject())) {
                    predicates.add(cb.like(root.get("uploadStatus"), "%" + getSubject() + ":1" + "%"));
                } else {
                    predicates.add(cb.equal(root.get("uploadStatus"), "SX:1,SC:1,SM:1"));
                }
            }
            if (getSchool() != null && getSchool() != "") {
                predicates.add(cb.equal(root.get("school"), getSchool()));
            }
            if (getExamRoom() != null && getExamRoom() != "") {
                predicates.add(cb.equal(root.get("examRoom"), getExamRoom()));
            }
            return cb.and(predicates.toArray(new Predicate[predicates.size()]));
        };
    }
}