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.ReportSubjectCollegeDao;
import cn.com.qmth.stmms.biz.report.model.ReportSubjectCollege;
import cn.com.qmth.stmms.biz.report.query.ReportSubjectQuery;
import cn.com.qmth.stmms.biz.report.service.ReportSubjectCollegeService;
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 ReportSubjectCollegeServiceImpl extends BaseQueryService<ReportSubjectCollege>
        implements ReportSubjectCollegeService {

    @Autowired
    private ReportSubjectCollegeDao reportSubjectCollegeDao;

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

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

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

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

    @Override
    public ReportSubjectCollege save(ReportSubjectCollege r) {
        return reportSubjectCollegeDao.save(r);
    }

}
