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.ServerIoMonitor;
import org.jk.rag.domain.bo.ServerIoMonitorBo;
import org.jk.rag.domain.vo.ServerIoMonitorVo;
import org.jk.rag.mapper.ServerIoMonitorMapper;
import org.jk.rag.service.IServerIoMonitorService;
import org.springframework.stereotype.Service;

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

/**
 * 服务器IO吞吐状态监控-独立Service业务层处理
 *
 * @author Lion Li
 * @date 2026-01-19
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class ServerIoMonitorServiceImpl implements IServerIoMonitorService {

    private final ServerIoMonitorMapper baseMapper;

    /**
     * 查询服务器IO吞吐状态监控-独立
     *
     * @param id 主键
     * @return 服务器IO吞吐状态监控-独立
     */
    @Override
    public ServerIoMonitorVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询服务器IO吞吐状态监控-独立列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 服务器IO吞吐状态监控-独立分页列表
     */
    @Override
    public TableDataInfo<ServerIoMonitorVo> queryPageList(ServerIoMonitorBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ServerIoMonitor> lqw = buildQueryWrapper(bo);
        Page<ServerIoMonitorVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的服务器IO吞吐状态监控-独立列表
     *
     * @param bo 查询条件
     * @return 服务器IO吞吐状态监控-独立列表
     */
    @Override
    public List<ServerIoMonitorVo> queryList(ServerIoMonitorBo bo) {
        LambdaQueryWrapper<ServerIoMonitor> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ServerIoMonitor> buildQueryWrapper(ServerIoMonitorBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ServerIoMonitor> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(ServerIoMonitor::getId);
        lqw.eq(bo.getServerId() != null, ServerIoMonitor::getServerId, bo.getServerId());
        lqw.eq(StringUtils.isNotBlank(bo.getServerIp()), ServerIoMonitor::getServerIp, bo.getServerIp());
        lqw.like(StringUtils.isNotBlank(bo.getServerName()), ServerIoMonitor::getServerName, bo.getServerName());
        lqw.eq(StringUtils.isNotBlank(bo.getDiskPath()), ServerIoMonitor::getDiskPath, bo.getDiskPath());
        lqw.eq(bo.getIoReadMb() != null, ServerIoMonitor::getIoReadMb, bo.getIoReadMb());
        lqw.eq(bo.getIoWriteMb() != null, ServerIoMonitor::getIoWriteMb, bo.getIoWriteMb());
        lqw.eq(bo.getIoReadIops() != null, ServerIoMonitor::getIoReadIops, bo.getIoReadIops());
        lqw.eq(bo.getIoWriteIops() != null, ServerIoMonitor::getIoWriteIops, bo.getIoWriteIops());
        lqw.eq(bo.getIoWait() != null, ServerIoMonitor::getIoWait, bo.getIoWait());
        return lqw;
    }

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

    /**
     * 修改服务器IO吞吐状态监控-独立
     *
     * @param bo 服务器IO吞吐状态监控-独立
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(ServerIoMonitorBo bo) {
        ServerIoMonitor update = MapstructUtils.convert(bo, ServerIoMonitor.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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