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.ServerMemoryMonitor;
import org.jk.rag.domain.bo.ServerMemoryMonitorBo;
import org.jk.rag.domain.vo.ServerMemoryMonitorVo;
import org.jk.rag.mapper.ServerMemoryMonitorMapper;
import org.jk.rag.service.IServerMemoryMonitorService;
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 ServerMemoryMonitorServiceImpl implements IServerMemoryMonitorService {

    private final ServerMemoryMonitorMapper baseMapper;

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

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

    /**
     * 查询符合条件的服务器内存状态监控-独立列表
     *
     * @param bo 查询条件
     * @return 服务器内存状态监控-独立列表
     */
    @Override
    public List<ServerMemoryMonitorVo> queryList(ServerMemoryMonitorBo bo) {
        LambdaQueryWrapper<ServerMemoryMonitor> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ServerMemoryMonitor> buildQueryWrapper(ServerMemoryMonitorBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ServerMemoryMonitor> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(ServerMemoryMonitor::getId);
        lqw.eq(bo.getServerId() != null, ServerMemoryMonitor::getServerId, bo.getServerId());
        lqw.eq(StringUtils.isNotBlank(bo.getServerIp()), ServerMemoryMonitor::getServerIp, bo.getServerIp());
        lqw.like(StringUtils.isNotBlank(bo.getServerName()), ServerMemoryMonitor::getServerName, bo.getServerName());
        lqw.eq(bo.getMemTotal() != null, ServerMemoryMonitor::getMemTotal, bo.getMemTotal());
        lqw.eq(bo.getMemUsed() != null, ServerMemoryMonitor::getMemUsed, bo.getMemUsed());
        lqw.eq(bo.getMemFree() != null, ServerMemoryMonitor::getMemFree, bo.getMemFree());
        lqw.eq(bo.getMemUsage() != null, ServerMemoryMonitor::getMemUsage, bo.getMemUsage());
        lqw.eq(bo.getSwapTotal() != null, ServerMemoryMonitor::getSwapTotal, bo.getSwapTotal());
        lqw.eq(bo.getSwapUsed() != null, ServerMemoryMonitor::getSwapUsed, bo.getSwapUsed());
        lqw.eq(bo.getSwapUsage() != null, ServerMemoryMonitor::getSwapUsage, bo.getSwapUsage());
        return lqw;
    }

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

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

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

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