package com.zcloud.fire.check.command;

import com.alibaba.cola.exception.BizException;
import com.zcloud.fire.check.domain.gateway.FireDeviceGateway;
import com.zcloud.fire.check.domain.model.FireDeviceE;
import com.zcloud.fire.check.dto.FireDeviceUpdateCmd;
import com.zcloud.fire.check.persistence.dataobject.FireDeviceDO;
import com.zcloud.fire.check.persistence.repository.FireDeviceRepository;
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:12:35
 */
@Component
@AllArgsConstructor
public class FireDeviceUpdateExe {
    private final FireDeviceGateway fireDeviceGateway;
    private final FireDeviceRepository fireDeviceRepository;

    @Transactional(rollbackFor = Exception.class)
    public void execute(FireDeviceUpdateCmd fireDeviceUpdateCmd) {

        FireDeviceDO fireDeviceDO = fireDeviceRepository.getInfoByCode(fireDeviceUpdateCmd.getFireDeviceCode());
        if(fireDeviceDO != null && !fireDeviceDO.getId().equals(fireDeviceUpdateCmd.getId())){
            throw new BizException("器材编号已存在");
        }

        FireDeviceE fireDeviceE = new FireDeviceE();
        BeanUtils.copyProperties(fireDeviceUpdateCmd, fireDeviceE);
        boolean res = fireDeviceGateway.update(fireDeviceE);
        if (!res) {
            throw new BizException("修改失败");
        }
    }
}

