package com.zcloud.zcGbsServicer.command;

import com.zcloud.zcGbsServicer.domain.gateway.RescueTeamGateway;
import com.zcloud.zcGbsServicer.domain.gateway.ResourcePersonGateway;
import com.zcloud.zcGbsServicer.domain.model.RescueTeamE;
import com.zcloud.zcGbsServicer.dto.RescueTeamAddCmd;
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;




/**
* web-app
* @Author makejava
* @Date 2026-01-21 17:23:00
*/
@Component
@AllArgsConstructor
public class RescueTeamAddExe {
    private final RescueTeamGateway rescueTeamGateway;
    private final ResourcePersonGateway resourcePersonGateway;

    @Transactional(rollbackFor = Exception.class)
    public boolean execute(RescueTeamAddCmd cmd) {
        RescueTeamE rescueTeamE = new RescueTeamE();
        BeanUtils.copyProperties(cmd, rescueTeamE);
        rescueTeamE.addInit();
        boolean res = false;
        boolean addPerRes = false;
        try {
            res = rescueTeamGateway.add(rescueTeamE);
            addPerRes = resourcePersonGateway.addBatch(rescueTeamE.getRescueMembers());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (!res || !addPerRes) {
            throw new BizException("保存失败");
        }
        return true;
    }
}

