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.ReportSubjectDao;
import cn.com.qmth.stmms.biz.report.model.ReportSubject;
import cn.com.qmth.stmms.biz.report.query.ReportSubjectQuery;
import cn.com.qmth.stmms.biz.report.service.ReportSubjectService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
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-12 18:10
 */
@Service
public class ReportSubjectServiceImpl extends BaseQueryService<ReportSubject> implements ReportSubjectService {

    @Autowired
    private ReportSubjectDao reportSubjectDao;

    @Override
    public ReportSubject save(ReportSubject reportSubject) {
        return reportSubjectDao.save(reportSubject);
    }

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

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

    @Override
    public ReportSubjectQuery findByQuery(final ReportSubjectQuery query) {
        checkQuery(query);
        Page<ReportSubject> result = reportSubjectDao.findAll(new Specification<ReportSubject>() {

            @Override
            public Predicate toPredicate(Root<ReportSubject> 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.getSubjectCodeIn())) {
                    String[] list = query.getSubjectCodeIn().split(",");
                    if (list.length > 0) {
                        Predicate[] sub = new Predicate[list.length];
                        for (int i = 0; i < list.length; i++) {
                            sub[i] = cb.equal(root.get("subjectCode"), list[i]);
                        }
                        predicates.add(cb.or(sub));
                    }
                }
                return predicates.isEmpty() ? cb.conjunction() : cb.and(predicates.toArray(new Predicate[predicates
                        .size()]));
            }
        }, query);
        fillResult(result, query);

        return query;
    }

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

}
