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

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.qmth.stmms.biz.distributpaper.dao.CaseHistoryDao;
import cn.com.qmth.stmms.biz.distributpaper.dao.CaseLibraryDao;
import cn.com.qmth.stmms.biz.distributpaper.model.CaseHistory;
import cn.com.qmth.stmms.biz.distributpaper.model.CaseLibrary;
import cn.com.qmth.stmms.biz.distributpaper.service.CaseHistoryService;

@Service
public class CaseHistoryServiceImpl implements CaseHistoryService {

    @Autowired
    private CaseHistoryDao caseHistoryDao;

    @Autowired
    private CaseLibraryDao libraryDao;

    @Transactional
    public CaseHistory save(CaseHistory caseHistory) {
        return caseHistoryDao.save(caseHistory);
    }

    public CaseHistory findById(Integer id) {
        return caseHistoryDao.findOne(id);
    }

    @Transactional
    public void deleteById(Integer id) {
        caseHistoryDao.delete(id);
    }

    @Transactional
    public void delete(CaseHistory caseHistory) {
        caseHistoryDao.delete(caseHistory);
    }

    public List<CaseHistory> getHistoryByScorer(Integer examId, String subjectCode, Integer blockId, Integer scorer) {
        List<CaseLibrary> list = libraryDao.findByExamIdAndBlockIdAndSubjectCode(examId, blockId, subjectCode);
        if (list != null && list.size() > 0) {
            return caseHistoryDao.findByLibraryIdAndScorer(list.get(0).getId(), scorer);
        } else {
            return null;
        }
    }

    public List<CaseHistory> findByExamIdAndSubjectIdAndBlockIdAndSchoolId(Integer examId, String subjectCode,
            Integer blockId) {
        List<CaseLibrary> list = libraryDao.findByExamIdAndBlockIdAndSubjectCode(examId, blockId, subjectCode);
        if (list != null && list.size() > 0) {
            return caseHistoryDao.findByLibraryId(list.get(0).getId());
        } else {
            return null;
        }
    }

    public List<CaseHistory> findByLibraryId(int libraryId) {
        return caseHistoryDao.findByLibraryId(libraryId);
    }

}
