package com.njcn.gather.systemmonitor.disk.service.impl;

import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.gather.systemmonitor.disk.enums.DiskMonitorResponseEnum;
import com.njcn.gather.systemmonitor.disk.pojo.param.DiskMonitorParam;
import com.njcn.gather.systemmonitor.disk.pojo.po.DiskMonitorTarget;
import com.njcn.gather.systemmonitor.disk.service.IDiskMonitorJobService;
import com.njcn.gather.systemmonitor.disk.service.IDiskMonitorNotifyService;
import com.njcn.gather.systemmonitor.disk.service.IDiskMonitorPolicyService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 磁盘监控通知服务实现。
 */
@Service
@RequiredArgsConstructor
public class DiskMonitorNotifyServiceImpl implements IDiskMonitorNotifyService {

    private final IDiskMonitorPolicyService diskMonitorPolicyService;
    private final IDiskMonitorJobService diskMonitorJobService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean testNotify(DiskMonitorParam.NotifyTestParam param) {
        DiskMonitorTarget target = diskMonitorPolicyService.getTargetByDriveLetter(param.getDriveLetter());
        if (target == null) {
            throw new BusinessException(DiskMonitorResponseEnum.NOTIFY_TARGET_NOT_FOUND);
        }
        diskMonitorJobService.executeNotifyTest(target.getDriveLetter());
        return true;
    }
}
