package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.ZsOperationSmtpBo;
import com.ruoyi.system.domain.vo.ZsOperationSmtpVo;
import com.ruoyi.system.domain.ZsOperationSmtp;
import com.ruoyi.system.mapper.ZsOperationSmtpMapper;
import com.ruoyi.system.service.IZsOperationSmtpService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 邮件配置Service业务层处理
 *
 * @author 王强
 * @date 2025-05-09
 */
@RequiredArgsConstructor
@Service
public class ZsOperationSmtpServiceImpl implements IZsOperationSmtpService {

    private final ZsOperationSmtpMapper baseMapper;

    /**
     * 查询邮件配置
     */
    @Override
    public ZsOperationSmtpVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询邮件配置列表
     */
    @Override
    public TableDataInfo<ZsOperationSmtpVo> queryPageList(ZsOperationSmtpBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZsOperationSmtp> lqw = buildQueryWrapper(bo);
        Page<ZsOperationSmtpVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询邮件配置列表
     */
    @Override
    public List<ZsOperationSmtpVo> queryList(ZsOperationSmtpBo bo) {
        LambdaQueryWrapper<ZsOperationSmtp> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZsOperationSmtp> buildQueryWrapper(ZsOperationSmtpBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZsOperationSmtp> lqw = Wrappers.lambdaQuery();
        return lqw;
    }

    /**
     * 新增邮件配置
     */
    @Override
    public Boolean insertByBo(ZsOperationSmtpBo bo) {
        ZsOperationSmtp add = BeanUtil.toBean(bo, ZsOperationSmtp.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改邮件配置
     */
    @Override
    public Boolean updateByBo(ZsOperationSmtpBo bo) {
        ZsOperationSmtp update = BeanUtil.toBean(bo, ZsOperationSmtp.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZsOperationSmtp entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除邮件配置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
