package com.competition.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.competition.common.enums.ErrorCode;
import com.competition.common.exception.BusinessException;
import com.competition.common.result.PageResult;
import com.competition.modules.sys.dto.CreateConfigDto;
import com.competition.modules.sys.entity.SysConfig;
import com.competition.modules.sys.mapper.SysConfigMapper;
import com.competition.modules.sys.service.ISysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements ISysConfigService {

    @Override
    public SysConfig createConfig(CreateConfigDto dto, Long tenantId) {
        log.info("开始创建配置，键：{}，租户：{}", dto.getKey(), tenantId);
        SysConfig config = new SysConfig();
        config.setTenantId(tenantId);
        config.setKey(dto.getKey());
        config.setValue(dto.getValue());
        config.setDescription(dto.getDescription());
        save(config);
        return config;
    }

    @Override
    public PageResult<SysConfig> findAll(Long page, Long pageSize, Long tenantId) {
        IPage<SysConfig> result = baseMapper.selectPage(
                new Page<>(page, pageSize),
                new LambdaQueryWrapper<SysConfig>()
                        .eq(tenantId != null, SysConfig::getTenantId, tenantId)
                        .orderByDesc(SysConfig::getCreateTime));
        return PageResult.from(result);
    }

    @Override
    public SysConfig findByKey(String key, Long tenantId) {
        if (tenantId == null) {
            throw BusinessException.of(ErrorCode.BAD_REQUEST, "缺少租户信息");
        }
        SysConfig config = getOne(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getKey, key)
                .eq(SysConfig::getTenantId, tenantId), false);
        if (config == null) {
            throw BusinessException.of(ErrorCode.NOT_FOUND, "配置不存在");
        }
        return config;
    }

    @Override
    public SysConfig findDetail(Long id, Long tenantId) {
        SysConfig config = getById(id);
        if (config == null) {
            throw BusinessException.of(ErrorCode.NOT_FOUND, "配置不存在");
        }
        return config;
    }

    @Override
    public SysConfig updateConfig(Long id, CreateConfigDto dto, Long tenantId) {
        log.info("开始更新配置，ID：{}", id);
        findDetail(id, tenantId);
        SysConfig config = new SysConfig();
        config.setId(id);
        if (dto.getKey() != null) config.setKey(dto.getKey());
        if (dto.getValue() != null) config.setValue(dto.getValue());
        if (dto.getDescription() != null) config.setDescription(dto.getDescription());
        updateById(config);
        return getById(id);
    }

    @Override
    public void removeConfig(Long id, Long tenantId) {
        log.info("删除配置，ID：{}", id);
        findDetail(id, tenantId);
        removeById(id);
    }
}
