package com.zcloud.primeport.command;

import com.alibaba.cola.exception.BizException;
import com.zcloud.primeport.domain.gateway.MkmjPassageGateway;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
@AllArgsConstructor
public class MkmjPassageRemoveExe {
    private final MkmjPassageGateway mkmjPassageGateway;

    @Transactional(rollbackFor = Exception.class)
    public boolean execute(Long id) {
        Integer gateCount = mkmjPassageGateway.countByPassageId(String.valueOf(id));
        if (gateCount > 0) {
            throw new BizException("该通道下有关联闸机，无法删除");
        }
        boolean res = mkmjPassageGateway.deletedMkmjPassageById(id);
        if (!res) {
            throw new BizException("删除失败");
        }
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean execute(Long[] ids) {
        for (Long id : ids) {
            Integer gateCount = mkmjPassageGateway.countByPassageId(String.valueOf(id));
            if (gateCount > 0) {
                throw new BizException("该通道下有关联闸机，无法删除");
            }
        }
        boolean res = mkmjPassageGateway.deletedMkmjPassageByIds(ids);
        if (!res) {
            throw new BizException("删除失败");
        }
        return true;
    }
}

