package com.zcloud.zcGbsServicer.command;

import ch.qos.logback.core.pattern.color.RedCompositeConverter;
import com.zcloud.zcGbsServicer.command.convertor.ResourceDeviceCoConvertor;
import com.zcloud.zcGbsServicer.domain.gateway.PumpRoomGateway;
import com.zcloud.zcGbsServicer.domain.gateway.ResourceDeviceGateway;
import com.zcloud.zcGbsServicer.domain.model.PumpRoomE;
import com.zcloud.zcGbsServicer.domain.model.ResourceDeviceE;
import com.zcloud.zcGbsServicer.dto.PumpRoomAddCmd;
import com.alibaba.cola.exception.BizException;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
* web-app
* @Author makejava
* @Date 2026-01-21 17:22:16
*/
@Component
@AllArgsConstructor
public class PumpRoomAddExe {
    private final PumpRoomGateway pumpRoomGateway;
    private final ResourceDeviceGateway resourceDeviceGateway;
    private final ResourceDeviceCoConvertor resourceDeviceCoConvertor;

    @Transactional(rollbackFor = Exception.class)
    public boolean execute(PumpRoomAddCmd cmd) {
        PumpRoomE pumpRoomE = new PumpRoomE();
        BeanUtils.copyProperties(cmd, pumpRoomE);
        List<ResourceDeviceE> devices = resourceDeviceCoConvertor.converAddToE(cmd.getDevices());
        pumpRoomE.addInit(devices);
        boolean res = false;
        boolean resDevice = false;
        try {
            res = pumpRoomGateway.add(pumpRoomE);
            resDevice = resourceDeviceGateway.addBatch(pumpRoomE.getDevices());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (!res || !resDevice) {
            throw new BizException("保存失败");
        }
        return true;
    }
}

