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

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

import cn.com.qmth.stmms.biz.exam.dao.SubjectiveBlockDao;
import cn.com.qmth.stmms.biz.exam.model.MarkStep;
import cn.com.qmth.stmms.biz.exam.model.SubjectiveBlock;
import cn.com.qmth.stmms.biz.exam.service.MarkStepService;
import cn.com.qmth.stmms.biz.exam.service.SubjectiveBlockCoreService;

@Service("subjectiveBlockCoreService")
public class SubjectiveBlockCoreServiceImpl implements SubjectiveBlockCoreService {

    @Autowired
    private MarkStepService markStepService;

    @Autowired
    private SubjectiveBlockDao subjectiveBlockDao;

    @Transactional
    // @CachePut(value = "subjective_block_cache", key = "#block.id", condition
    // = "#block!=null && #block.id!=null")
    public SubjectiveBlock saveBlock(SubjectiveBlock block) {
        SubjectiveBlock item = subjectiveBlockDao.save(block);
        if (block.getMarkStepList() != null) {
            markStepService.deleteByBlockId(item.getId());
            // int number = 0;
            for (MarkStep step : block.getMarkStepList()) {
                step.setBlockId(item.getId());
                // step.setNumber(++number);
                markStepService.save(step);
            }
        }
        return item;
    }

    @Transactional
    // @CacheEvict(value = "subjective_block_cache", key = "#blockId",
    // beforeInvocation = true)
    public void deleteBlockById(int blockId) {
        markStepService.deleteByBlockId(blockId);
        subjectiveBlockDao.delete(blockId);
    }

    // @Cacheable(value = "subjective_block_cache", key = "#blockId")
    public SubjectiveBlock findBlockById(int blockId) {
        return subjectiveBlockDao.findOne(blockId);
    }

}
