package com.zcloud.personnel.positioning.command;

import com.zcloud.personnel.positioning.domain.gateway.AlarmRuleItemGateway;
import com.zcloud.personnel.positioning.domain.model.AlarmRuleItemE;
import com.zcloud.personnel.positioning.dto.AlarmRuleItemAddCmd;
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 fangjiakai
 * @Date 2026-07-07
 */
@Component
@AllArgsConstructor
public class AlarmRuleItemAddExe {
    private final AlarmRuleItemGateway alarmRuleItemGateway;

    @Transactional(rollbackFor = Exception.class)
    public boolean execute(AlarmRuleItemAddCmd cmd) {
        AlarmRuleItemE alarmRuleItemE = new AlarmRuleItemE();
        BeanUtils.copyProperties(cmd, alarmRuleItemE);
        boolean res;
        try {
            res = alarmRuleItemGateway.add(alarmRuleItemE);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (!res) {
            throw new BizException("保存失败");
        }
        return true;
    }
}
