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.ServerDiskMonitor;
import org.jk.rag.domain.bo.ServerDiskMonitorBo;
import org.jk.rag.domain.vo.ServerDiskMonitorVo;
import org.jk.rag.mapper.ServerDiskMonitorMapper;
import org.jk.rag.service.IServerDiskMonitorService;
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 ServerDiskMonitorServiceImpl implements IServerDiskMonitorService {

    private final ServerDiskMonitorMapper baseMapper;

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

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

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

    private LambdaQueryWrapper<ServerDiskMonitor> buildQueryWrapper(ServerDiskMonitorBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ServerDiskMonitor> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(ServerDiskMonitor::getId);
        lqw.eq(bo.getServerId() != null, ServerDiskMonitor::getServerId, bo.getServerId());
        lqw.eq(StringUtils.isNotBlank(bo.getServerIp()), ServerDiskMonitor::getServerIp, bo.getServerIp());
        lqw.like(StringUtils.isNotBlank(bo.getServerName()), ServerDiskMonitor::getServerName, bo.getServerName());
        lqw.eq(StringUtils.isNotBlank(bo.getDiskPath()), ServerDiskMonitor::getDiskPath, bo.getDiskPath());
        lqw.eq(bo.getDiskTotal() != null, ServerDiskMonitor::getDiskTotal, bo.getDiskTotal());
        lqw.eq(bo.getDiskUsed() != null, ServerDiskMonitor::getDiskUsed, bo.getDiskUsed());
        lqw.eq(bo.getDiskFree() != null, ServerDiskMonitor::getDiskFree, bo.getDiskFree());
        lqw.eq(bo.getDiskUsage() != null, ServerDiskMonitor::getDiskUsage, bo.getDiskUsage());
        return lqw;
    }

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

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

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

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