package com.zcloud.primeport.command;

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

import java.util.Map;

@Component
@AllArgsConstructor
public class MkmjRemoveExe {
    private final MkmjGateway mkmjGateway;

    @Transactional(rollbackFor = Exception.class)
    public boolean execute(Long id) {
        Map<String, Integer> relatedData = mkmjGateway.countRelatedData(id);
        int totalCount = relatedData.values().stream().mapToInt(Integer::intValue).sum();
        if (totalCount > 0) {
            throw new BizException("该口门下有关联数据，无法删除");
        }
        boolean res = mkmjGateway.deletedMkmjById(id);
        if (!res) {
            throw new BizException("删除失败");
        }
        return true;
    }
}

