package com.zcloud.fire.check.command;

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


/**
 * web-app
 *
 * @Author zhaokai
 * @Date 2025-11-26 10:13:09
 */
@Component
@AllArgsConstructor
public class FireRegionRemoveExe {
    private final FireRegionGateway fireRegionGateway;
    private final FireRegionRepository fireRegionRepository;
    private final FirePointRepository firePointRepository;

    @Transactional(rollbackFor = Exception.class)
    public boolean execute(Long id) {
        FireRegionDO fireRegionDO = fireRegionRepository.getById(id);
        if (fireRegionDO == null) {
            throw new BizException("区域不存在");
        }
        //判断是否有点位数
        long count = firePointRepository.countByFireRegionId(fireRegionDO.getFireRegionId());
        FireRegionE.checkFireRegion(fireRegionDO.getFireRegionName(),count);

        boolean res = fireRegionGateway.deletedFireRegionById(id);
        if (!res) {
            throw new BizException("删除失败");
        }
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean execute(Long[] ids) {
        for (Long id : ids) {
            FireRegionDO fireRegionDO = fireRegionRepository.getById(id);
            if (fireRegionDO == null) {
                throw new BizException("区域不存在");
            }
            //判断是否有点位数
            long count = firePointRepository.countByFireRegionId(fireRegionDO.getFireRegionId());
            FireRegionE.checkFireRegion(fireRegionDO.getFireRegionName(),count);
        }
        boolean res = fireRegionGateway.deletedFireRegionByIds(ids);
        if (!res) {
            throw new BizException("删除失败");
        }
        return true;
    }
}

