package com.auth.service.base.service.impl;

import com.auth.common.model.common.result.PageResult;
import com.auth.model.base.dto.EmailConfigDto;
import com.auth.model.base.vo.EmailConfigVo;
import com.auth.dao.base.entity.EmailConfigEntity;
import com.auth.dao.base.mapper.v1.EmailConfigMapper;
import com.auth.service.base.service.EmailConfigService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 系统邮件服务器配置 服务实现类
 * </p>
 *
 * @author Bunny
 * @since 2025-07-17 23:06:38
 */
@Service
@Transactional
public class EmailConfigServiceImpl extends ServiceImpl<EmailConfigMapper, EmailConfigEntity> implements EmailConfigService {

    /**
     * 系统邮件服务器配置 服务实现类
     *
     * @param pageParams 系统邮件服务器配置分页查询page对象
     * @param dto        系统邮件服务器配置分页查询对象
     * @return 查询分页系统邮件服务器配置返回对象
     */
    @Override
    public PageResult<EmailConfigVo> getEmailConfigPage(Page<EmailConfigEntity> pageParams, EmailConfigDto dto) {
        IPage<EmailConfigVo> page = baseMapper.selectListByPage(pageParams, dto);

        return PageResult.<EmailConfigVo>builder()
                .list(page.getRecords())
                .pageNo(page.getCurrent())
                .pageSize(page.getSize())
                .total(page.getTotal())
                .build();
    }

    /**
     * 根据id查询系统邮件服务器配置详情
     *
     * @param id 主键
     * @return 系统邮件服务器配置详情 EmailConfigVo}
     */
    public EmailConfigVo getEmailConfigById(Long id) {
        EmailConfigEntity emailConfigEntity = getById(id);

        EmailConfigVo emailConfigVo = new EmailConfigVo();
        BeanUtils.copyProperties(emailConfigEntity, emailConfigVo);

        return emailConfigVo;
    }

    /**
     * 添加系统邮件服务器配置
     *
     * @param dto 系统邮件服务器配置添加
     */
    @Override
    public void addEmailConfig(EmailConfigDto dto) {
        EmailConfigEntity emailConfig = new EmailConfigEntity();
        BeanUtils.copyProperties(dto, emailConfig);

        save(emailConfig);
    }

    /**
     * 更新系统邮件服务器配置
     *
     * @param dto 系统邮件服务器配置更新
     */
    @Override
    public void updateEmailConfig(EmailConfigDto dto) {
        EmailConfigEntity emailConfig = new EmailConfigEntity();
        BeanUtils.copyProperties(dto, emailConfig);

        updateById(emailConfig);
    }

    /**
     * 删除|批量删除系统邮件服务器配置
     *
     * @param ids 删除id列表
     */
    @Override
    public void deleteEmailConfig(List<Long> ids) {
        removeByIds(ids);
    }
}