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.StandardHistoryDao;
import cn.com.qmth.stmms.biz.distributpaper.dao.StandardLibraryDao;
import cn.com.qmth.stmms.biz.distributpaper.model.StandardHistory;
import cn.com.qmth.stmms.biz.distributpaper.model.StandardLibrary;
import cn.com.qmth.stmms.biz.distributpaper.service.StandardHistoryService;

@Service
public class StandardHistoryServiceImpl implements StandardHistoryService {

    @Autowired
    private StandardHistoryDao standardHistoryDao;

    @Autowired
    private StandardLibraryDao libraryDao;

    @Transactional
    public StandardHistory save(StandardHistory standardHistory) {
        return standardHistoryDao.save(standardHistory);
    }

    public StandardHistory findById(Integer id) {
        return standardHistoryDao.findOne(id);
    }

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

    @Transactional
    public void delete(StandardHistory standardHistory) {
        standardHistoryDao.delete(standardHistory);
    }

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

    public List<StandardHistory> findByExamIdAndSubjectCodeAndBlockId(Integer examId, String subjectCode, Integer blockId) {
        List<StandardLibrary> list = libraryDao.findByExamIdAndBlockIdAndSubjectCode(examId, blockId, subjectCode);
        if (list != null && list.size() > 0) {
            return standardHistoryDao.findByLibraryId(list.get(0).getId());
        } else {
            return null;
        }
    }

    public List<StandardHistory> findByLibraryId(int libraryId) {
        return standardHistoryDao.findByLibraryId(libraryId);
    }

    @Override
    public List<StandardHistory> findByScorer(Integer scorerId) {
        return standardHistoryDao.findByScorer(scorerId);
    }

}
