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.ReportSubjectTeacherClassDao;
import cn.com.qmth.stmms.biz.report.model.ReportSubjectTeacherClass;
import cn.com.qmth.stmms.biz.report.query.ReportSubjectQuery;
import cn.com.qmth.stmms.biz.report.service.ReportSubjectTeacherClassService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
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-17
 */
@Service
public class ReportSubjectTeacherClassServiceImpl extends BaseQueryService<ReportSubjectTeacherClass>
        implements ReportSubjectTeacherClassService {

    @Autowired
    private ReportSubjectTeacherClassDao reportSubjectTeacherClassDao;

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

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

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

                    @Override
                    public Predicate toPredicate(Root<ReportSubjectTeacherClass> 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.getTeacherName())) {
                            predicates.add(cb.equal(root.get("teacherName"), query.getTeacherName()));
                        }
                        return predicates.isEmpty() ?
                                cb.conjunction() :
                                cb.and(predicates.toArray(new Predicate[predicates.size()]));
                    }
                });

        return result;
    }

    @Override
    public ReportSubjectTeacherClass save(ReportSubjectTeacherClass r) {
        return reportSubjectTeacherClassDao.save(r);
    }

    @Transactional
    @Override
    public void updateRelativeAvgScore(Integer examId) {
        reportSubjectTeacherClassDao.updateRelativeAvgScore(examId);
    }
}
