package com.ssj.service.sys.opentest;

import com.ssj.bean.sys.opentest.OpenTestResult;
import com.ssj.dao.sys.opentest.OpenTestResultDao;
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.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Mount Liu
 * @date 2020/2/11
 */
@Service
@Transactional
public class OpenTestResultServiceImpl extends BaseServiceImpl<OpenTestResult, String> implements OpenTestResultService {

    @Autowired
    private OpenTestResultDao dao;

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

    @Override
    public List<OpenTestResult> findByUserId(String userId) {
        return dao.findByUserIdAndIsDeleteOrderByCreateTimeDesc(userId, 0);
    }

    @Override
    public List<OpenTestResult> findByUserIdAndType(String userId, int type) {
        return dao.findByUserIdAndTypeAndIsDeleteOrderByCreateTimeDesc(userId, type, 0);
    }

    @Override
    public List<OpenTestResult> findAllByUserIdAndType(String userId, int type) {
        return dao.findByUserIdAndTypeOrderByCreateTime(userId, type);
    }

    @Override
    public OpenTestResult findResult(String userId, String grade, String subject, String version) {
        if (StringUtil.isNotBlank(version)) {
            return dao.findTopByUserIdAndGradeAndSubjectAndVersionAndIsDelete(userId, grade, subject, version, 0);
        }
        return dao.findTopByUserIdAndGradeAndSubjectAndIsDelete(userId, grade, subject, 0);
    }

    @Override
    public OpenTestResult findResult(String userId, String grade, String subject, String version, String unit, String semester, int type) {
        if (StringUtil.isNotBlank(unit)) {
            return dao.findTopByUserIdAndGradeAndSubjectAndVersionAndUnitAndSemesterAndTypeAndIsDelete(userId, grade, subject, version, unit, semester, type, 0);
        }
        return dao.findTopByUserIdAndGradeAndSubjectAndVersionAndSemesterAndTypeAndIsDelete(userId, grade, subject, version, semester, type, 0);
    }

    @Override
    public List<OpenTestResult> findAllResult(String userId, String grade, String subject, String version, String unit, int type) {
        if (StringUtil.isNotBlank(unit)) {
            return dao.findByUserIdAndGradeAndSubjectAndVersionAndUnitAndTypeOrderByCreateTime(userId, grade, subject, version, unit, type);
        }
        return dao.findByUserIdAndGradeAndSubjectAndVersionAndTypeOrderByCreateTime(userId, grade, subject, version, type);
    }

    @Override
    public int getMyRankingNo(int score, String createTime, String grade, String subject, String version) {
        if (StringUtil.isNotBlank(version)) {
            return dao.getExceedNum(grade, subject, score, createTime, version);
        }
        return dao.getExceedNum(grade, subject, score, createTime);
    }

    @Override
    public int getMyRankingNo(int score, String createTime, String grade, String subject, String version, String unit, int type) {
        if (StringUtil.isNotBlank(unit)) {
            return dao.getExceedNum(grade, subject, score, createTime, version, unit, type);
        }
        return dao.getExceedNum(grade, subject, score, createTime, version, type);
    }

}
