package com.zcloud.personnel.positioning.command;

import com.zcloud.personnel.positioning.domain.gateway.OpenapiConfigGateway;
import com.zcloud.personnel.positioning.domain.model.OpenapiConfigE;
import com.zcloud.personnel.positioning.dto.OpenapiConfigAddCmd;
import com.zcloud.personnel.positioning.integration.finds.OpenapiConfigCache;
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 OpenapiConfigAddExe {
    private final OpenapiConfigGateway openapiConfigGateway;
    private final OpenapiConfigCache openapiConfigCache;

    @Transactional(rollbackFor = Exception.class)
    public boolean execute(OpenapiConfigAddCmd cmd) {
        OpenapiConfigE openapiConfigE = new OpenapiConfigE();
        BeanUtils.copyProperties(cmd, openapiConfigE);
        boolean res;
        try {
            res = openapiConfigGateway.add(openapiConfigE);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (res) {
            openapiConfigCache.evictAll();
        }
        if (!res) {
            throw new BizException("保存失败");
        }
        return true;
    }
}
