package com.qxgmat.service.inline;

import com.github.pagehelper.Page;
import com.nuliji.tools.AbstractService;
import com.nuliji.tools.exception.ParameterException;
import com.nuliji.tools.exception.SystemException;
import com.qxgmat.data.dao.ExaminationStructMapper;
import com.qxgmat.data.dao.ExerciseStructMapper;
import com.qxgmat.data.dao.entity.ExaminationStruct;
import com.qxgmat.data.dao.entity.ExerciseStruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

@Service
public class ExaminationStructService extends AbstractService {
    private static final Logger logger = LoggerFactory.getLogger(ExaminationStructService.class);

    @Resource
    private ExaminationStructMapper examinationStructMapper;

    @Cacheable(value = "examination_struct")
    public List<ExaminationStruct> all(){
        return select(examinationStructMapper);
    }

    @CacheEvict(value="examination_struct",allEntries=true)
    public ExaminationStruct add(ExaminationStruct struct){
        int result = insert(examinationStructMapper, struct);
        struct = one(examinationStructMapper, struct.getId());
        if(struct == null){
            throw new SystemException("添加失败");
        }
        return struct;
    }

    @CacheEvict(value="examination_struct",allEntries=true)
    public ExaminationStruct edit(ExaminationStruct struct){
        ExaminationStruct in = one(examinationStructMapper, struct.getId());
        if(in == null){
            throw new ParameterException("层级不存在");
        }
        int result = update(examinationStructMapper, struct);
        return struct;
    }

    @CacheEvict("examination_struct")
    public boolean delete(Number id){
        ExaminationStruct in = one(examinationStructMapper, id);
        if(in == null){
            throw new ParameterException("层级不存在");
        }
        int result = delete(examinationStructMapper, id);
        return result > 0;
    }

    public ExaminationStruct get(Number id){
        ExaminationStruct in = one(examinationStructMapper, id);

        if(in == null){
            throw new ParameterException("层级不存在");
        }
        return in;
    }

    public Page<ExaminationStruct> select(int page, int pageSize){
        return select(examinationStructMapper, page, pageSize);
    }

    public List<ExaminationStruct> select(Collection ids){
        return select(examinationStructMapper, ids);
    }

}
