package com.ssj.service.sys.opentest;

import com.alibaba.dubbo.config.annotation.Service;
import com.ssj.bean.sys.opentest.OpenTestQuestion;
import com.ssj.dao.sys.opentest.OpenTestQuestionDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.framework.core.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Mount Liu
 * @date 2020/2/11
 */
@Service(version = "1.0")
@Transactional
public class OpenTestQuestionServiceImpl extends BaseServiceImpl<OpenTestQuestion, String> implements OpenTestQuestionService {

    @Autowired
    private OpenTestQuestionDao dao;

    @Override
    public PagingAndSortingRepository<OpenTestQuestion, String> getDao() {
        return dao;
    }

    @Override
    public List<OpenTestQuestion> findQuestionList(String subject, String grade, String version, Integer updateVersion) {
        if (updateVersion == null) {
            updateVersion = dao.findLatestUpdateVersion();
        }
        if (StringUtil.isBlank(version)) {
            return dao.findBySubjectAndGradeAndUpdateVersionOrderBySortNo(subject, grade, updateVersion);
        } else {
            return dao.findBySubjectAndGradeAndVersionAndUpdateVersionOrderBySortNo(subject, grade, version, updateVersion);
        }
    }

    @Override
    public List<OpenTestQuestion> findQuestionList(String subject, String grade, String version, String unit, Integer updateVersion, int type, String semester) {
        if (updateVersion == null) {
            updateVersion = dao.findLatestUpdateVersion(type);
        }
        if (StringUtil.isBlank(unit)) {
            return dao.findBySubjectAndGradeAndVersionAndUpdateVersionAndTypeAndSemesterOrderBySortNo(subject, grade, version, updateVersion, type, semester);
        } else {
            return dao.findBySubjectAndGradeAndVersionAndUnitAndUpdateVersionAndTypeAndSemesterOrderBySortNo(subject, grade, version, unit, updateVersion, type, semester);
        }
    }

}
