package cn.com.qmth.stmms.biz.mark.service.Impl;

import cn.com.qmth.stmms.biz.common.BaseQueryService;
import cn.com.qmth.stmms.biz.mark.dao.ProblemHistoryDao;
import cn.com.qmth.stmms.biz.mark.model.ProblemHistory;
import cn.com.qmth.stmms.biz.mark.query.ProblemHistorySearchQuery;
import cn.com.qmth.stmms.biz.mark.service.ProblemHistoryService;
import cn.com.qmth.stmms.common.enums.HistoryStatus;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import java.util.LinkedList;
import java.util.List;

@Service
public class ProblemHistoryServiceImpl extends BaseQueryService<ProblemHistory> implements ProblemHistoryService {

    @Autowired
    private ProblemHistoryDao historyDao;

    @Override
    public ProblemHistory save(ProblemHistory p) {
        return historyDao.save(p);
    }

    @Override
    public long countByExamIdAndProblemId(Integer examId, Integer problemId) {
        return historyDao.countByExamIdAndProblemId(examId, problemId);
    }

    @Override
    public List<String> findProblemSubjectCode(Integer examId) {
        return historyDao.findProblemSubjectCodeAndHistoryStatus(examId, HistoryStatus.WAITING);
    }

    @Override
    public ProblemHistorySearchQuery findByQuery(final ProblemHistorySearchQuery query) {
        checkQuery(query);
        Page<ProblemHistory> result = historyDao.findAll(buildSpecification(query), query);
        fillResult(result, query);
        return query;
    }

    private Specification<ProblemHistory> buildSpecification(final ProblemHistorySearchQuery query) {
        return new Specification<ProblemHistory>() {

            @Override
            public Predicate toPredicate(Root<ProblemHistory> root, CriteriaQuery<?> cQuery, CriteriaBuilder cb) {
                List<Predicate> predicates = new LinkedList<Predicate>();
                if (query.getExamId() > 0) {
                    predicates.add(cb.equal(root.get("examId"), query.getExamId()));
                }
                if (StringUtils.isNotBlank(query.getSubjectCode())) {
                    predicates.add(cb.equal(root.get("subjectCode"), query.getSubjectCode()));
                }
                if (StringUtils.isNotBlank(query.getExamNumber())) {
                    predicates.add(cb.equal(root.get("examNumber"), query.getExamNumber()));
                }
                if (StringUtils.isNotBlank(query.getSecretNumber())) {
                    predicates.add(cb.equal(root.get("secretNumber"), query.getSecretNumber()));
                }
                if (query.getStudentId() != null) {
                    predicates.add(cb.equal(root.get("studentId"), query.getStudentId()));
                }
                if (query.getUserId() > 0) {
                    predicates.add(cb.equal(root.get("userId"), query.getUserId()));
                }
                if (query.getGroupNumber() > 0) {
                    predicates.add(cb.equal(root.get("groupNumber"), query.getGroupNumber()));
                }
                if (query.getStatus() != null) {
                    predicates.add(cb.equal(root.get("status").as(HistoryStatus.class), query.getStatus()));
                }
                if (query.getProblemId() != null) {
                    predicates.add(cb.equal(root.get("problemId"), query.getProblemId()));
                }
                if (StringUtils.isNotBlank(query.getSubjectCodeIn())) {
                    String[] list = query.getSubjectCodeIn().split(",");
                    if (list.length > 0) {
                        Predicate[] sub = new Predicate[list.length];
                        for (int i = 0; i < list.length; i++) {
                            sub[i] = cb.equal(root.get("subjectCode"), list[i]);
                        }
                        predicates.add(cb.or(sub));
                    }
                }
                return predicates.isEmpty() ? cb.conjunction() : cb.and(predicates.toArray(new Predicate[predicates
                        .size()]));
            }
        };
    }

}
