package com.zcloud.personnel.positioning.command;

import com.alibaba.cola.exception.BizException;
import com.zcloud.personnel.positioning.persistence.dataobject.AlarmRuleDO;
import com.zcloud.personnel.positioning.persistence.repository.AlarmRuleRepository;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * web-app
 *
 * @Author fangjiakai
 * @Date 2026-07-07
 */
@Component
@AllArgsConstructor
public class AlarmRuleRemoveExe {
    private final AlarmRuleRepository alarmRuleRepository;
    private final FindsPendingSyncService findsPendingSyncService;

    @Transactional(rollbackFor = Exception.class)
    public boolean execute(Long id) {
        AlarmRuleDO alarmRuleDO = alarmRuleRepository.getById(id);
        if (alarmRuleDO == null) {
            return true;
        }
        alarmRuleDO.setSyncStatus(FindsPendingSyncService.STATUS_DELETE_WAIT);
        alarmRuleDO.setSyncToken(FindsSyncToken.next());
        if (!alarmRuleRepository.updateById(alarmRuleDO)) {
            throw new BizException("删除失败");
        }
        findsPendingSyncService.scheduleRule(id);
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean execute(Long[] ids) {
        if (ids == null || ids.length == 0) {
            return true;
        }
        for (Long id : ids) {
            execute(id);
        }
        return true;
    }
}
