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.nuliji.tools.mybatis.Example;
import com.qxgmat.data.constants.enums.SettingKey;
import com.qxgmat.data.dao.SettingMapper;
import com.qxgmat.data.dao.entity.Setting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
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 SettingService extends AbstractService {
    private static final Logger logger = LoggerFactory.getLogger(SettingService.class);
    @Resource
    private SettingMapper settingMapper;

    @Cacheable(value="setting", key="#settingKey.key", unless="#result==null")
    public Setting getByKey(SettingKey settingKey) {
        Example example = new Example(Setting.class);
        example.and(
                example.createCriteria()
                        .andEqualTo("key", settingKey.key)
        );
        return one(settingMapper, example);
    }

    @CacheEvict(value = "setting", key="#settingKey.key")
    public boolean editByKey(SettingKey settingKey, Setting setting){
        Example example = new Example(Setting.class);
        example.and(
                example.createCriteria()
                        .andEqualTo("key", settingKey.key)
        );
        int result = update(settingMapper, example, setting);
        return result > 0;
    }

    public Setting add(Setting setting){
        int result = insert(settingMapper, setting);
        setting = one(settingMapper, setting.getId());
        if(setting == null){
            throw new SystemException("配置添加失败");
        }
        return setting;
    }
    @CachePut(value = "setting", key="#setting.key", unless="#result==null")
    public Setting edit(Setting setting){
        Setting in = one(settingMapper, setting.getId());
        if(in == null){
            throw new ParameterException("配置不存在");
        }
        int result = update(settingMapper, setting);
        return setting;
    }

    public boolean delete(Number id){
        Setting in = one(settingMapper, id);
        if(in == null){
            throw new ParameterException("配置不存在");
        }
        int result = delete(settingMapper, id);
        return result > 0;
    }

    public Setting get(Number id){
        Setting in = one(settingMapper, id);

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

    public Page<Setting> select(int page, int pageSize){
        return select(settingMapper, page, pageSize);
    }

    public List<Setting> select(Collection ids){
        return select(settingMapper, ids);
    }
}
