package org.jk.rag.service.impl;

import org.jk.common.core.utils.MapstructUtils;
import org.jk.common.core.utils.StringUtils;
import org.jk.common.mybatis.core.page.TableDataInfo;
import org.jk.common.mybatis.core.page.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 lombok.extern.slf4j.Slf4j;
import org.jk.rag.domain.ServerNetDownloadMonitor;
import org.jk.rag.domain.bo.ServerNetDownloadMonitorBo;
import org.jk.rag.domain.vo.ServerNetDownloadMonitorVo;
import org.jk.rag.mapper.ServerNetDownloadMonitorMapper;
import org.jk.rag.service.IServerNetDownloadMonitorService;
import org.springframework.stereotype.Service;

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

/**
 * 服务器网络下行状态监控-独立Service业务层处理
 *
 * @author Lion Li
 * @date 2026-01-19
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class ServerNetDownloadMonitorServiceImpl implements IServerNetDownloadMonitorService {

    private final ServerNetDownloadMonitorMapper baseMapper;

    /**
     * 查询服务器网络下行状态监控-独立
     *
     * @param id 主键
     * @return 服务器网络下行状态监控-独立
     */
    @Override
    public ServerNetDownloadMonitorVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询服务器网络下行状态监控-独立列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 服务器网络下行状态监控-独立分页列表
     */
    @Override
    public TableDataInfo<ServerNetDownloadMonitorVo> queryPageList(ServerNetDownloadMonitorBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ServerNetDownloadMonitor> lqw = buildQueryWrapper(bo);
        Page<ServerNetDownloadMonitorVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的服务器网络下行状态监控-独立列表
     *
     * @param bo 查询条件
     * @return 服务器网络下行状态监控-独立列表
     */
    @Override
    public List<ServerNetDownloadMonitorVo> queryList(ServerNetDownloadMonitorBo bo) {
        LambdaQueryWrapper<ServerNetDownloadMonitor> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ServerNetDownloadMonitor> buildQueryWrapper(ServerNetDownloadMonitorBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ServerNetDownloadMonitor> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(ServerNetDownloadMonitor::getId);
        lqw.eq(bo.getServerId() != null, ServerNetDownloadMonitor::getServerId, bo.getServerId());
        lqw.eq(StringUtils.isNotBlank(bo.getServerIp()), ServerNetDownloadMonitor::getServerIp, bo.getServerIp());
        lqw.like(StringUtils.isNotBlank(bo.getServerName()), ServerNetDownloadMonitor::getServerName, bo.getServerName());
        lqw.eq(StringUtils.isNotBlank(bo.getNetCard()), ServerNetDownloadMonitor::getNetCard, bo.getNetCard());
        lqw.eq(bo.getDownloadSpeed() != null, ServerNetDownloadMonitor::getDownloadSpeed, bo.getDownloadSpeed());
        lqw.eq(bo.getDownloadPackets() != null, ServerNetDownloadMonitor::getDownloadPackets, bo.getDownloadPackets());
        lqw.eq(bo.getDownloadErr() != null, ServerNetDownloadMonitor::getDownloadErr, bo.getDownloadErr());
        return lqw;
    }

    /**
     * 新增服务器网络下行状态监控-独立
     *
     * @param bo 服务器网络下行状态监控-独立
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(ServerNetDownloadMonitorBo bo) {
        ServerNetDownloadMonitor add = MapstructUtils.convert(bo, ServerNetDownloadMonitor.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改服务器网络下行状态监控-独立
     *
     * @param bo 服务器网络下行状态监控-独立
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(ServerNetDownloadMonitorBo bo) {
        ServerNetDownloadMonitor update = MapstructUtils.convert(bo, ServerNetDownloadMonitor.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 校验并批量删除服务器网络下行状态监控-独立信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
