package cn.com.qmth.stmms.biz.report.service.impl;

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

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

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

import cn.com.qmth.stmms.biz.common.BaseQueryService;
import cn.com.qmth.stmms.biz.report.dao.ReportSubjectQuestionDao;
import cn.com.qmth.stmms.biz.report.model.ReportSubjectQuestion;
import cn.com.qmth.stmms.biz.report.query.ReportSubjectQuery;
import cn.com.qmth.stmms.biz.report.service.ReportSubjectQuestionService;

/**
 * 客、主观题分析 service 实现
 *
 * @author xuwenjin
 * @Date 2019-04-17
 */
@Service
public class ReportSubjectQuestionServiceImpl extends BaseQueryService<ReportSubjectQuestion>
        implements ReportSubjectQuestionService {

    @Autowired
    private ReportSubjectQuestionDao reportSubjectQuestionDao;

    @Override
    public ReportSubjectQuestion save(ReportSubjectQuestion reportSubjectQuestion) {
        return reportSubjectQuestionDao.save(reportSubjectQuestion);
    }

    @Transactional
    @Override
    public void deleteByExamId(Integer examId) {
        reportSubjectQuestionDao.deleteByExamId(examId);

    }

    @Override
    public List<ReportSubjectQuestion> findByQuery(final ReportSubjectQuery query) {
        query.setSort(new Sort(Direction.ASC, "paperType", "mainNumber", "subNumber"));
        Page<ReportSubjectQuestion> result = reportSubjectQuestionDao
                .findAll(new Specification<ReportSubjectQuestion>() {

                    @Override
                    public Predicate toPredicate(Root<ReportSubjectQuestion> root, CriteriaQuery<?> cQuery,
                            CriteriaBuilder cb) {
                        List<Predicate> predicates = new LinkedList<Predicate>();
                        if (query.getExamId() != null) {
                            predicates.add(cb.equal(root.get("examId"), query.getExamId()));
                        }
                        if (StringUtils.isNotBlank(query.getPaperType())) {
                            predicates.add(cb.equal(root.get("paperType"), query.getPaperType()));
                        }
                        if (StringUtils.isNotBlank(query.getSubjectCode())) {
                            predicates.add(cb.equal(root.get("subjectCode"), query.getSubjectCode()));
                        }
                        predicates.add(cb.equal(root.get("objective"), query.getObjective()));
                        return predicates.isEmpty() ? cb.conjunction()
                                : cb.and(predicates.toArray(new Predicate[predicates.size()]));
                    }
                }, query);
        return result.getContent();
    }

}
