package com.zcloud.fire.check.command;

import com.alibaba.cola.exception.BizException;
import com.zcloud.fire.check.domain.gateway.FireRegionGateway;
import com.zcloud.fire.check.domain.model.FireRegionE;
import com.zcloud.fire.check.dto.FireRegionAddCmd;
import com.zcloud.fire.check.persistence.repository.FireRegionRepository;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;


/**
 * web-app
 *
 * @Author zhaokai
 * @Date 2025-11-26 10:13:09
 */
@Component
@AllArgsConstructor
public class FireRegionAddExe {
    private final FireRegionGateway fireRegionGateway;
    private final FireRegionRepository fireRegionRepository;

    @Transactional(rollbackFor = Exception.class)
    public boolean execute(FireRegionAddCmd cmd) {
        //区域编码唯一
        if (fireRegionRepository.existsByFireRegionCode(cmd.getFireRegionCode())) {
            throw new BizException("区域编码已存在");
        }
        FireRegionE fireRegionE = new FireRegionE();
        BeanUtils.copyProperties(cmd, fireRegionE);
        boolean res = false;
        try {
            res = fireRegionGateway.add(fireRegionE);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (!res) {
            throw new BizException("保存失败");
        }
        return true;
    }
}

