package com.enterprise.manage.modules.system.service.impl;

import com.enterprise.manage.common.base.BaseServiceImpl;
import com.enterprise.manage.modules.system.mapper.DictItemMapper;
import com.enterprise.manage.modules.system.service.IDictItemService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.enterprise.manage.modules.system.domain.po.DictItem;

import java.util.List;

/**
 * system dictionary item service implementation class
 *
 * @author PeiZhi Guo
 * 2021-05-13 14:50
 */
@Service
public class DictItemServiceImpl extends BaseServiceImpl<DictItemMapper, DictItem> implements IDictItemService {

    @Cacheable(value = "dictCache",key = "#type",unless = "#result == null or #result.size() == 0")
    @Override
    public List<DictItem> queryDictByType(String type) {
        DictItem sysDictItem = new DictItem();
        sysDictItem.setEnable(1);
        sysDictItem.setType(type);
        return baseMapper.selectByParam(sysDictItem);
    }

    @CacheEvict(value = "dictCache",key = "#sysDictItem.type",condition = "#sysDictItem.type ne null ")
    @Override
    public boolean save(DictItem sysDictItem) {
        return baseMapper.insert(sysDictItem) > 0;
    }

    @CacheEvict(value = "dictCache",key = "#sysDictItem.type",condition = "#sysDictItem.type ne null ")
    @Override
    public boolean update(DictItem sysDictItem) {
        return baseMapper.update(sysDictItem) > 0;
    }

    @CacheEvict(value = "dictCache",key = "#result",beforeInvocation = false,condition = "#result ne  null ")
    @Override
    public String deleteById(Long id) {
        DictItem dict = baseMapper.selectById(id);
        baseMapper.delete(id);
        return dict.getType();
    }
}
