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.ReportSubjectTeacherDao;
import cn.com.qmth.stmms.biz.report.model.ReportSubjectTeacher;
import cn.com.qmth.stmms.biz.report.query.ReportSubjectQuery;
import cn.com.qmth.stmms.biz.report.service.ReportSubjectTeacherService;
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-16 09:43
 */
@Service
public class ReportSubjectTeacherServiceImpl extends BaseQueryService<ReportSubjectTeacher>
        implements ReportSubjectTeacherService {

    @Autowired
    private ReportSubjectTeacherDao reportSubjectTeacherDao;

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

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

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

            @Override
            public Predicate toPredicate(Root<ReportSubjectTeacher> 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 ReportSubjectTeacher save(ReportSubjectTeacher r) {
        return reportSubjectTeacherDao.save(r);
    }

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