package com.zcloud.primeport.command;

import com.alibaba.cola.exception.BizException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zcloud.primeport.domain.gateway.MkmjApprovalUserGateway;
import com.zcloud.primeport.domain.model.MkmjApprovalUserE;
import com.zcloud.primeport.dto.MkmjApprovalUserAddCmd;
import com.zcloud.primeport.persistence.dataobject.MkmjApprovalUserDO;
import com.zcloud.primeport.persistence.repository.MkmjApprovalUserRepository;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;


/**
 * web-app
 *
 * @Author makejava
 * @Date 2026-03-05 11:33:16
 */
@Component
@AllArgsConstructor
public class MkmjApprovalUserAddExe {
    private final MkmjApprovalUserGateway mkmjApprovalUserGateway;
    private final MkmjApprovalUserRepository mkmjApprovalUserRepository;

    @Transactional(rollbackFor = Exception.class)
    public boolean execute(MkmjApprovalUserAddCmd cmd) {
        QueryWrapper<MkmjApprovalUserDO> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", cmd.getUserId());
        wrapper.eq("delete_enum", "FALSE");
        MkmjApprovalUserDO exist = mkmjApprovalUserRepository.getOne(wrapper);
        if (exist != null) {
            throw new BizException("该审批人已录入，不可重复添加");
        }
        MkmjApprovalUserE examTypeE = new MkmjApprovalUserE();
        BeanUtils.copyProperties(cmd, examTypeE);
        boolean res = false;
        try {
            res = mkmjApprovalUserGateway.add(examTypeE);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (!res) {
            throw new BizException("保存失败");
        }
        return true;
    }
}
