package com.zcloud.basic.info.command;

import com.zcloud.basic.info.domain.gateway.PostGateway;
import com.zcloud.basic.info.domain.model.PostE;
import com.zcloud.basic.info.dto.PostAddCmd;
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;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;


/**
 * web-app
 *
 * @Author SondonYong
 * @Date 2025-10-31 17:24:04
 */
@Component
@AllArgsConstructor
public class PostAddExe {
    private final PostGateway postGateway;

    @Transactional(rollbackFor = Exception.class)
    public boolean execute(PostAddCmd cmd) {

        PostE postE = new PostE();
        BeanUtils.copyProperties(cmd, postE);
        boolean res = false;
        try {
            // 新增岗位
            Long postId = postGateway.add(postE);
            if(Objects.nonNull(postId) && postId > 0) {
                res = true;
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (!res) {
            throw new BizException("保存失败");
        }
        return true;
    }
}

