package com.zcloud.zcGbsServicer.command;

import cn.hutool.core.util.StrUtil;
import com.alibaba.cola.exception.BizException;
import com.zcloud.zcGbsServicer.domain.enums.DeviceTypeEnum;
import com.zcloud.zcGbsServicer.domain.enums.PersonTypeEnum;
import com.zcloud.zcGbsServicer.domain.gateway.ControlRoomGateway;
import com.zcloud.zcGbsServicer.domain.gateway.ResourceDeviceGateway;
import com.zcloud.zcGbsServicer.domain.gateway.ResourcePersonGateway;
import com.zcloud.zcGbsServicer.persistence.dataobject.ControlRoomDO;
import com.zcloud.zcGbsServicer.persistence.repository.ControlRoomRepository;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* web-app
* @Author makejava
* @Date 2026-01-21 17:21:50
*/
@Component
@AllArgsConstructor
public class ControlRoomRemoveExe {
    private final ControlRoomGateway controlRoomGateway;
    private final ControlRoomRepository controlRoomRepository;
    private final ResourcePersonGateway resourcePersonGateway;
    private final ResourceDeviceGateway resourceDeviceGateway;

    @Transactional(rollbackFor = Exception.class)
    public boolean execute(Long id) {
        ControlRoomDO controlRoomDO = controlRoomRepository.getById(id);
        if (controlRoomDO == null) {
            throw new BizException("数据不存在");
        }
        boolean res = controlRoomGateway.deletedControlRoomById(id);
        boolean resDelPer = resourcePersonGateway.deletedResourcePersonByBizId(
                controlRoomDO.getRoomId(), PersonTypeEnum.CONTROL_ROOM.getCode());
        boolean resDelDev = resourceDeviceGateway.deletedResourceDeviceByBizId(
                controlRoomDO.getRoomId(), DeviceTypeEnum.CONTROL_ROOM.getCode());
        if (!res || !resDelPer || !resDelDev) {
            throw new BizException("删除失败");
        }
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean execute(Long[] ids) {
        if (ids == null || ids.length == 0) {
            return true;
        }
        List<String> roomIds = Arrays.stream(ids)
                .map(controlRoomRepository::getById)
                .filter(Objects::nonNull)
                .map(ControlRoomDO::getRoomId)
                .filter(StrUtil::isNotBlank)
                .collect(Collectors.toList());

        boolean res = controlRoomGateway.deletedControlRoomByIds(ids);
        boolean resDelPer = true;
        boolean resDelDev = true;
        for (String roomId : roomIds) {
            resDelPer = resDelPer && resourcePersonGateway.deletedResourcePersonByBizId(
                    roomId, PersonTypeEnum.CONTROL_ROOM.getCode());
            resDelDev = resDelDev && resourceDeviceGateway.deletedResourceDeviceByBizId(
                    roomId, DeviceTypeEnum.CONTROL_ROOM.getCode());
        }
        if (!res || !resDelPer || !resDelDev) {
            throw new BizException("Delete failed");
        }
        return true;
    }
}
