package com.ruoyi.liandao.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.query.LambdaQueryWrapperX;
import com.ruoyi.liandao.domain.ResourcesConfig;
import com.ruoyi.liandao.dto.resourcesConfig.ResourcesConfigAddReqDto;
import com.ruoyi.liandao.dto.resourcesConfig.ResourcesConfigEditReqDto;
import com.ruoyi.liandao.dto.resourcesConfig.ResourcesConfigPageReqDto;
import com.ruoyi.liandao.mapper.ResourcesConfigMapper;
import com.ruoyi.liandao.service.ResourcesConfigService;
import org.springframework.stereotype.Service;

/**
* @author admin
* @description 针对表【ld_resources_config(资源配置)】的数据库操作Service实现
* @createDate 2024-03-14 10:51:09
*/
@Service
@DataSource(DataSourceType.MASTER)
public class ResourcesConfigServiceImpl extends ServiceImpl<ResourcesConfigMapper, ResourcesConfig>
    implements ResourcesConfigService{

    @Override
    public void add(ResourcesConfigAddReqDto dto) {
        ResourcesConfig one = getOne(new LambdaQueryWrapperX<ResourcesConfig>()
                .eq(ResourcesConfig::getName, dto.getName()));
        if(one!=null){
            throw new ServiceException("名称已存在！");
        }
        ResourcesConfig convert = Convert.convert(ResourcesConfig.class, dto);
        save(convert);
    }

    @Override
    public void edit(ResourcesConfigEditReqDto dto) {
        ResourcesConfig one = getOne(new LambdaQueryWrapperX<ResourcesConfig>()
                .eq(ResourcesConfig::getName, dto.getName())
                .ne(ResourcesConfig::getId, dto.getId()));
        if(one!=null){
            throw new ServiceException("名称已存在！");
        }
        ResourcesConfig convert = Convert.convert(ResourcesConfig.class, dto);
        updateById(convert);
    }

    @Override
    public IPage<ResourcesConfig> getPage(ResourcesConfigPageReqDto dto) {
        return page((IPage<ResourcesConfig>) dto.getPage(), new LambdaQueryWrapperX<ResourcesConfig>()
                .likeIfPresent(ResourcesConfig::getName, dto.getName())
                .likeIfPresent(ResourcesConfig::getCategory, dto.getCategory())
                .eqIfPresent(ResourcesConfig::getType, dto.getType())
                .orderByDesc(ResourcesConfig::getUpdateTime));
    }
}




