package com.test.bijihoudaun.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.test.bijihoudaun.entity.SystemSetting;
import com.test.bijihoudaun.mapper.SystemSettingMapper;
import com.test.bijihoudaun.service.SystemSettingService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class SystemSettingServiceImpl extends ServiceImpl<SystemSettingMapper, SystemSetting> implements SystemSettingService {

    private static final String REGISTRATION_ENABLED_KEY = "registration.enabled";

    @Override
    public boolean isRegistrationEnabled() {
        SystemSetting setting = getById(REGISTRATION_ENABLED_KEY);
        // 默认开启注册
        return ObjectUtil.isNull(setting) || Boolean.parseBoolean(setting.getSettingValue());
    }

    @Override
    public void setRegistrationEnabled(boolean enabled) {
        SystemSetting setting = new SystemSetting();
        setting.setSettingKey(REGISTRATION_ENABLED_KEY);
        setting.setSettingValue(String.valueOf(enabled));
        saveOrUpdate(setting);
    }
}