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

import cn.com.qmth.stmms.biz.common.BaseQueryService;
import cn.com.qmth.stmms.biz.report.dao.ReportSubjectClassDao;
import cn.com.qmth.stmms.biz.report.model.ReportSubjectClass;
import cn.com.qmth.stmms.biz.report.query.ReportSubjectQuery;
import cn.com.qmth.stmms.biz.report.service.ReportSubjectClassService;
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 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实现
 *
 * @author xuwenjin
 * @date 2019-04-16
 */
@Service
public class ReportSubjectClassServiceImpl extends BaseQueryService<ReportSubjectClass>
        implements ReportSubjectClassService {

    @Autowired
    private ReportSubjectClassDao reportSubjectClassDao;

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

    @Transactional
    @Override
    public void deleteByExamIdAndSubjectCode(Integer examId, String subjectCode) {
        reportSubjectClassDao.deleteByExamIdAndSubjectCode(examId, subjectCode);
    }

    @Override
    public List<ReportSubjectClass> findByQuery(final ReportSubjectQuery query) {
        query.setSort(new Sort(Direction.ASC, "className"));
        Page<ReportSubjectClass> result = reportSubjectClassDao.findAll(new Specification<ReportSubjectClass>() {

            @Override
            public Predicate toPredicate(Root<ReportSubjectClass> 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.getSubjectCode())) {
                    predicates.add(cb.equal(root.get("subjectCode"), query.getSubjectCode()));
                }
                if (StringUtils.isNotBlank(query.getClassName())) {
                    predicates.add(cb.equal(root.get("className"), query.getClassName()));
                }
                return predicates.isEmpty() ?
                        cb.conjunction() :
                        cb.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        }, query);
        return result.getContent();
    }

    @Override
    public ReportSubjectClass save(ReportSubjectClass r) {
        return reportSubjectClassDao.save(r);
    }

    @Override
    public ReportSubjectClass findOne(Integer examId, String subjectCode, String className) {
        if (StringUtils.isBlank(className)) {
            return null;
        }
        ReportSubjectQuery query = new ReportSubjectQuery();
        query.setExamId(examId);
        query.setSubjectCode(subjectCode);
        query.setClassName(className);
        List<ReportSubjectClass> list = findByQuery(query);
        return list.isEmpty() ? null : list.get(0);
    }
}
