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.ReportSubjectGroupDao;
import cn.com.qmth.stmms.biz.report.model.ReportSubjectGroup;
import cn.com.qmth.stmms.biz.report.query.ReportSubjectQuery;
import cn.com.qmth.stmms.biz.report.service.ReportSubjectGroupService;
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-18
 */
@Service
public class ReportSubjectGroupServiceImpl extends BaseQueryService<ReportSubjectGroup>
        implements ReportSubjectGroupService {

    @Autowired
    private ReportSubjectGroupDao reportSubjectGroupDao;

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

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

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

            @Override
            public Predicate toPredicate(Root<ReportSubjectGroup> 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 (query.getObjective() != null) {
                    predicates.add(cb.equal(root.get("objective"), query.getObjective()));
                }
                if (query.getGroupNumber() > 0) {
                    predicates.add(cb.equal(root.get("groupNumber"), query.getGroupNumber()));
                }
                if (StringUtils.isNotBlank(query.getPaperType())) {
                    predicates.add(cb.equal(root.get("paperType"), query.getPaperType()));
                } else if (query.getNullPaperType() != null && query.getNullPaperType().booleanValue()) {
                    predicates.add(cb.isNull(root.get("paperType")));
                }
                return predicates.isEmpty() ?
                        cb.conjunction() :
                        cb.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        }, query);
        return result.getContent();
    }

    @Override
    public ReportSubjectGroup save(ReportSubjectGroup r) {
        return reportSubjectGroupDao.save(r);
    }

    @Override
    public ReportSubjectGroup findOne(Integer examId, String subjectCode, boolean objective, String paperType,
            Integer mainNumber) {
        ReportSubjectQuery query = new ReportSubjectQuery();
        query.setExamId(examId);
        query.setSubjectCode(subjectCode);
        query.setObjective(objective);
        query.setGroupNumber(mainNumber);
        if (objective) {
            query.setPaperType(paperType);
            if (paperType == null) {
                query.setNullPaperType(true);
            }
        }
        List<ReportSubjectGroup> list = findByQuery(query);
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public List<ReportSubjectGroup> findByExamIdAndSubjectCodeAndObjectiveAndPaperType(Integer examId,
            String subjectCode, boolean objective, String paperType) {
        return reportSubjectGroupDao
                .findByExamIdAndSubjectCodeAndObjectiveAndPaperType(examId, subjectCode, objective, paperType,
                        new Sort(Direction.ASC, "groupNumber"));
    }
}
