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.ZsOperationSendconfigBo;
import com.ruoyi.system.domain.vo.ZsOperationSendconfigVo;
import com.ruoyi.system.domain.ZsOperationSendconfig;
import com.ruoyi.system.mapper.ZsOperationSendconfigMapper;
import com.ruoyi.system.service.IZsOperationSendconfigService;

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

/**
 * 发送信息配置Service业务层处理
 *
 * @author 王强
 * @date 2025-05-09
 */
@RequiredArgsConstructor
@Service
public class ZsOperationSendconfigServiceImpl implements IZsOperationSendconfigService {

    private final ZsOperationSendconfigMapper baseMapper;

    /**
     * 查询发送信息配置
     */
    @Override
    public ZsOperationSendconfigVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询发送信息配置列表
     */
    @Override
    public TableDataInfo<ZsOperationSendconfigVo> queryPageList(ZsOperationSendconfigBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZsOperationSendconfig> lqw = buildQueryWrapper(bo);
        Page<ZsOperationSendconfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询发送信息配置列表
     */
    @Override
    public List<ZsOperationSendconfigVo> queryList(ZsOperationSendconfigBo bo) {
        LambdaQueryWrapper<ZsOperationSendconfig> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZsOperationSendconfig> buildQueryWrapper(ZsOperationSendconfigBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZsOperationSendconfig> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getModule()), ZsOperationSendconfig::getModule, bo.getModule());
        return lqw;
    }

    /**
     * 新增发送信息配置
     */
    @Override
    public Boolean insertByBo(ZsOperationSendconfigBo bo) {
        ZsOperationSendconfig add = BeanUtil.toBean(bo, ZsOperationSendconfig.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改发送信息配置
     */
    @Override
    public Boolean updateByBo(ZsOperationSendconfigBo bo) {
        ZsOperationSendconfig update = BeanUtil.toBean(bo, ZsOperationSendconfig.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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