package com.zcloud.fire.check.command;

import com.alibaba.cola.exception.BizException;
import com.zcloud.fire.check.domain.gateway.FirePointGateway;
import com.zcloud.fire.check.domain.model.FirePointE;
import com.zcloud.fire.check.domain.model.FireRegionE;
import com.zcloud.fire.check.persistence.dataobject.FirePointDO;
import com.zcloud.fire.check.persistence.dataobject.FireRegionDO;
import com.zcloud.fire.check.persistence.repository.FireDeviceRepository;
import com.zcloud.fire.check.persistence.repository.FirePointRepository;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;


/**
 * web-app
 *
 * @Author zhaokai
 * @Date 2025-11-26 10:12:55
 */
@Component
@AllArgsConstructor
public class FirePointRemoveExe {
    private final FirePointGateway firePointGateway;
    private final FirePointRepository firePointRepository;
    private final FireDeviceRepository fireDeviceRepository;

    @Transactional(rollbackFor = Exception.class)
    public boolean execute(Long id) {
        if (id == null) {
            return true;
        }
        FirePointDO firePointDO = firePointRepository.getById(id);
        if (firePointDO == null) {
            return true;
        }
        //判断是否有点位数
        long count = fireDeviceRepository.countByFirePointId(firePointDO.getFirePointId());
        FirePointE.checkFirePoint(firePointDO.getFirePointName(),count);
        boolean res = firePointGateway.deletedFirePointById(id);
        if (!res) {
            throw new BizException("删除失败");
        }
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean execute(Long[] ids) {
        for (Long id : ids) {
            FirePointDO firePointDO = firePointRepository.getById(id);
            if (firePointDO == null) {
                continue;
            }
            //判断是否有点位数
            long count = fireDeviceRepository.countByFirePointId(firePointDO.getFirePointId());
            FirePointE.checkFirePoint(firePointDO.getFirePointName(),count);
        }
        boolean res = firePointGateway.deletedFirePointByIds(ids);
        if (!res) {
            throw new BizException("删除失败");
        }
        return true;
    }
}

