package cn.com.qmth.stmms.biz.exam.service.impl;

import java.util.LinkedList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

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 cn.com.qmth.stmms.biz.common.BaseQueryService;
import cn.com.qmth.stmms.biz.exam.dao.ScoreRateDao;
import cn.com.qmth.stmms.biz.exam.model.ScoreRate;
import cn.com.qmth.stmms.biz.exam.model.ScoreRatePK;
import cn.com.qmth.stmms.biz.exam.service.ScoreRateService;
import cn.com.qmth.stmms.biz.exam.service.query.ScoreRateSearchQuery;

@Service
public class ScoreRateServiceImpl extends BaseQueryService<ScoreRate> implements ScoreRateService {

    @Autowired
    private ScoreRateDao scoreRateDao;

    @Transactional
    @Override
    public ScoreRate save(ScoreRate scoreRate) {
        return scoreRateDao.save(scoreRate);
    }

    @Override
    public ScoreRate findOne(int examId, String subjectCode,String campusName, boolean objective, int mainNumber, int subNumber) {
    	ScoreRatePK pk = new ScoreRatePK();
        pk.setExamId(examId);
        pk.setSubjectCode(subjectCode);
        pk.setCampusName(campusName);
        pk.setMainNumber(mainNumber);
        pk.setSubNumber(subNumber);
        pk.setObjective(objective);
        return scoreRateDao.findOne(pk);
    }

    @Override
    public ScoreRateSearchQuery findByQuery(ScoreRateSearchQuery query) {
        checkQuery(query);
        query.setSort(new Sort(Direction.ASC, "pk.subjectCode", "pk.campusName","pk.mainNumber", "pk.subNumber"));
        Page<ScoreRate> result = scoreRateDao.findAll(buildSpecification(query), query);
        fillResult(result, query);
        return query;
    }

    private Specification<ScoreRate> buildSpecification(final ScoreRateSearchQuery query) {
        return new Specification<ScoreRate>() {

            @Override
            public Predicate toPredicate(Root<ScoreRate> root, CriteriaQuery<?> cQuery, CriteriaBuilder cb) {

                List<Predicate> predicates = new LinkedList<Predicate>();
                if (query.getExamId() > 0) {
                    predicates.add(cb.equal(root.get("pk").get("examId"), query.getExamId()));
                }
                if (StringUtils.isNotEmpty(query.getCampusName())) {
                	predicates.add(cb.equal(root.get("pk").get("campusName"), query.getCampusName()));
                }
                if (StringUtils.isNotEmpty(query.getSubjectCode())) {
                    predicates.add(cb.equal(root.get("pk").get("subjectCode"), query.getSubjectCode()));
                }
                if (query.getObjective() != null) {
                    predicates.add(cb.equal(root.get("pk").get("objective"), query.getObjective()));
                }
                return predicates.isEmpty() ? cb.conjunction() : cb.and(predicates.toArray(new Predicate[predicates
                        .size()]));
            }
        };
    }
}
