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

import java.util.Date;
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.FormallyLibraryDao;
import cn.com.qmth.stmms.biz.distributpaper.dao.TryLibraryDao;
import cn.com.qmth.stmms.biz.distributpaper.model.FormallyLibrary;
import cn.com.qmth.stmms.biz.distributpaper.model.TryLibrary;
import cn.com.qmth.stmms.biz.subjectheader.dao.ArbitrationHistoryDao;
import cn.com.qmth.stmms.biz.subjectheader.dao.ArbitrationLibraryDao;
import cn.com.qmth.stmms.biz.subjectheader.model.ArbitrationHistory;
import cn.com.qmth.stmms.biz.subjectheader.model.ArbitrationLibrary;
import cn.com.qmth.stmms.biz.subjectheader.service.ArbitrationHistoryService;
import cn.com.qmth.stmms.common.enums.LibraryStatus;

@Service
public class ArbitrationHistoryServiceImpl implements ArbitrationHistoryService {

    @Autowired
    private ArbitrationHistoryDao arbitrationHistoryDao;

    @Autowired
    private ArbitrationLibraryDao arbitrationLibraryDao;

    @Autowired
    private TryLibraryDao tryLibraryDao;

    @Autowired
    private FormallyLibraryDao formallyLibraryDao;

    @Transactional
    public ArbitrationHistory save(ArbitrationHistory arbitrationHistory) {
        return arbitrationHistoryDao.save(arbitrationHistory);
    }

    public ArbitrationHistory findById(Integer id) {
        return arbitrationHistoryDao.findOne(id);
    }

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

    @Transactional
    public void delete(ArbitrationHistory arbitrationHistory) {
        arbitrationHistoryDao.delete(arbitrationHistory);
    }

    @Override
    public void saveMark(ArbitrationHistory arbitrationHistory) {
        ArbitrationLibrary arbitrationLibrary = arbitrationLibraryDao.findOne(arbitrationHistory.getLibraryId());
        if (arbitrationLibrary.getType().getValue() == 1) {
            List<TryLibrary> libList = tryLibraryDao
                    .findByExamIdAndSubjectCodeAndBlockIdAndSecretNoAndStatus(arbitrationLibrary.getExamId(),
                            arbitrationLibrary.getSubjectCode(),
                            arbitrationLibrary.getBlockId(), arbitrationLibrary.getSecretNo(), LibraryStatus.MARKED);
            for (TryLibrary library : libList) {
                library.setStatus(LibraryStatus.MARKED);
                tryLibraryDao.save(library);
            }
        }

        if (arbitrationLibrary.getType().getValue() == 2) {
            List<FormallyLibrary> libList = formallyLibraryDao
                    .findByExamIdAndSubjectCodeAndBlockIdAndSecretNoAndStatus(arbitrationLibrary.getExamId(),
                            arbitrationLibrary.getSubjectCode(),
                            arbitrationLibrary.getBlockId(), arbitrationLibrary.getSecretNo(), LibraryStatus.MARKED);
            for (FormallyLibrary library : libList) {
                library.setStatus(LibraryStatus.MARKED);
                formallyLibraryDao.save(library);
            }
        }

        arbitrationLibrary.setStatus(LibraryStatus.MARKED);
        arbitrationLibraryDao.save(arbitrationLibrary);

        long spent = new Date().getTime() - arbitrationHistory.getSpent();
        arbitrationHistory.setSpent(spent);
        arbitrationHistory.setStatus(LibraryStatus.MARKED);
        arbitrationHistory.setSubmitTime(new Date());
        arbitrationHistoryDao.save(arbitrationHistory);
    }
}
