package com.ruoyi.system.service.impl;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.web.page.PageDomain;
import com.ruoyi.system.domain.RmResourceGroup;
import com.ruoyi.system.domain.RmResourceRegistration;
import com.ruoyi.system.mapper.RmResourceGroupMapper;
import com.ruoyi.system.mapper.RmResourceRegistrationMapper;
import com.ruoyi.system.service.IRmResourceGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.ruoyi.common.core.utils.PageUtils.startPage;

/**
 * 资源分组Service业务层处理
 * 
 * @author gyt
 * @date 2025-08-12
 */
@Service
public class RmResourceGroupServiceImpl implements IRmResourceGroupService 
{
    @Autowired
    private RmResourceGroupMapper rmResourceGroupMapper;
    @Autowired
    private RmResourceRegistrationMapper rmResourceRegistrationMapper;

    /**
     * 查询资源分组
     * 
     * @param id 资源分组主键
     * @return 资源分组
     */
    @Override
    public RmResourceGroup selectRmResourceGroupById(Long id)
    {
        return rmResourceGroupMapper.selectRmResourceGroupById(id);
    }

    /**
     * 查询资源分组列表
     * 
     * @param rmResourceGroup 资源分组
     * @return 资源分组
     */
    @Override
    public List<RmResourceGroup> selectRmResourceGroupList(RmResourceGroup rmResourceGroup)
    {
        return rmResourceGroupMapper.selectRmResourceGroupList(rmResourceGroup);
    }

    /**
     * 新增资源分组
     * 
     * @param rmResourceGroup 资源分组
     * @return 结果
     */
    @Override
    public int insertRmResourceGroup(RmResourceGroup rmResourceGroup)
    {
        rmResourceGroup.setCreateTime(DateUtils.getNowDate());
        return rmResourceGroupMapper.insertRmResourceGroup(rmResourceGroup);
    }

    /**
     * 修改资源分组
     * 
     * @param rmResourceGroup 资源分组
     * @return 结果
     */
    @Override
    public int updateRmResourceGroup(RmResourceGroup rmResourceGroup)
    {
        rmResourceGroup.setUpdateTime(DateUtils.getNowDate());
        rmResourceGroupMapper.updateRmResourceGroup(rmResourceGroup);
        // 资源组修改，相关策略状态改为待下发
        updatePolicyStatus(rmResourceGroup.getId());
        return 1;
    }

    /**
     * 相关策略状态改为待下发
     * @param resourceGroupId
     */
    public void updatePolicyStatus(Long resourceGroupId){
        // 资源监控策略状态修改
        rmResourceGroupMapper.updateRmPolicyStatus(resourceGroupId);
        // 脚本策略状态修改
        rmResourceGroupMapper.updateScriptPolicyStatus(resourceGroupId);
    }
    /**
     * 批量删除资源分组
     * 
     * @param ids 需要删除的资源分组主键
     * @return 结果
     */
    @Override
    public int deleteRmResourceGroupByIds(Long[] ids)
    {
        return rmResourceGroupMapper.deleteRmResourceGroupByIds(ids);
    }

    /**
     * 删除资源分组信息
     * 
     * @param id 资源分组主键
     * @return 结果
     */
    @Override
    public int deleteRmResourceGroupById(Long id)
    {
        return rmResourceGroupMapper.deleteRmResourceGroupById(id);
    }

    /**
     * 根据资源分组获取资源列表
     * @param rmResourceGroup
     * @return
     */
    @Override
    public PageInfo<RmResourceRegistration> getRegisterList(RmResourceGroup rmResourceGroup) {
        // 配置分页参数
        PageDomain pageDomain = new PageDomain();
        pageDomain.setPageNum(rmResourceGroup.getPageNum());
        pageDomain.setPageSize(rmResourceGroup.getPageSize());
        if(rmResourceGroup.getId() == null){
            RmResourceRegistration queryParam = new RmResourceRegistration();
            queryParam.setResourceType(rmResourceGroup.getResourceType());
            queryParam.setQueryName(rmResourceGroup.getQueryName());
            // 开启分页
            startPage(pageDomain);
            List<RmResourceRegistration> rmResourceRegistrationList = rmResourceRegistrationMapper.selectRmResourceRegistrationList(queryParam);
            // 监控项赋值-暂时不需要
            for (RmResourceRegistration rmResourceRegistration : rmResourceRegistrationList) {
                RmResourceGroup item = rmResourceGroupMapper.selectMonitorMsgAndGroupMsg(rmResourceRegistration.getId());
                if(item != null){
                    rmResourceRegistration.setMonitorItems(item.getMonitorItems());
                    rmResourceRegistration.setDiscoveryRules(item.getDiscoveryRules());
                }else{
                    rmResourceRegistration.setMonitorItems("0");
                    rmResourceRegistration.setDiscoveryRules("0");
                }
            }
            return new PageInfo<>(rmResourceRegistrationList);
        }else{
            RmResourceGroup group = rmResourceGroupMapper.selectRmResourceGroupById(rmResourceGroup.getId());
            String ids = group.getIncludedDevicesId();
            String[] resourceIds = ids.split(",");
            // 开启分页
            startPage(pageDomain);
            // 拿到资源信息
            List<RmResourceRegistration> rmResourceRegistrationList = rmResourceRegistrationMapper.getRegistrationByIds(resourceIds);
            // 监控项赋值-暂时不需要
            for (RmResourceRegistration rmResourceRegistration : rmResourceRegistrationList) {
                RmResourceGroup item = rmResourceGroupMapper.selectMonitorMsgAndGroupMsg(rmResourceRegistration.getId());
                if(item != null){
                    rmResourceRegistration.setMonitorItems(item.getMonitorItems());
                    rmResourceRegistration.setDiscoveryRules(item.getDiscoveryRules());
                }else{
                    rmResourceRegistration.setMonitorItems("0");
                    rmResourceRegistration.setDiscoveryRules("0");
                }
            }
            return new PageInfo<>(rmResourceRegistrationList);
        }
    }
    /**
     * 根据资源分组获取所欲资源列表
     * @param rmResourceGroup
     * @return
     */
    @Override
    public List<RmResourceRegistration> getAllRegisterList(RmResourceGroup rmResourceGroup) {
        if(rmResourceGroup.getId() == null){
            return new ArrayList<>();
        }
        RmResourceGroup group = rmResourceGroupMapper.selectRmResourceGroupById(rmResourceGroup.getId());
        String ids = group.getIncludedDevicesId();
        String[] resourceIds = ids.split(",");
        // 拿到资源信息
        List<RmResourceRegistration> rmResourceRegistrationList = rmResourceRegistrationMapper.getRegistrationByIds(resourceIds);
        // 监控项赋值-暂时不需要
        for (RmResourceRegistration rmResourceRegistration : rmResourceRegistrationList) {
            RmResourceGroup item = rmResourceGroupMapper.selectMonitorMsgAndGroupMsg(rmResourceRegistration.getId());
            if(item != null){
                rmResourceRegistration.setMonitorItems(item.getMonitorItems());
                rmResourceRegistration.setDiscoveryRules(item.getDiscoveryRules());
            }else{
                rmResourceRegistration.setMonitorItems("0");
                rmResourceRegistration.setDiscoveryRules("0");
            }
        }
        return rmResourceRegistrationList;
    }

    @Override
    public String exitsResourceById(RmResourceGroup rmResourceGroup) {
        String ids = rmResourceGroup.getResourceIds();
        StringBuilder resultStr = new StringBuilder();
        String[] idArray = ids.split(",");
        for (String s : idArray) {
            rmResourceGroup.setResourceIds(s);
            int rows = rmResourceGroupMapper.exitsResourceById(rmResourceGroup);
            if(rows > 0){
                resultStr.append(s);
            }
        }
        return resultStr.toString();
    }
}
