package com.zcloud.basic.info.command;

import cn.hutool.core.bean.BeanUtil;
import com.zcloud.basic.info.domain.gateway.UserQualificationInfoGateway;
import com.zcloud.basic.info.domain.model.UserQualificationInfoE;
import com.zcloud.basic.info.dto.UserQualificationInfoAddCmd;
import com.alibaba.cola.exception.BizException;
import com.zcloud.basic.info.persistence.dataobject.UserQualificationInfoDO;
import com.zcloud.basic.info.persistence.repository.UserQualificationInfoRepository;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
* web-app
* @Author SondonYong
* @Date 2025-10-31 09:50:21
*/
@Component
@AllArgsConstructor
public class UserQualificationInfoAddExe {
    private final UserQualificationInfoGateway userQualificationInfoGateway;
    private final UserQualificationInfoRepository userQualificationInfoRepository;

    @Transactional(rollbackFor = Exception.class)
    public boolean execute(UserQualificationInfoAddCmd cmd) {
        UserQualificationInfoE userQualificationInfoE = new UserQualificationInfoE();
        // 证书编号验重
        List<UserQualificationInfoDO> list = userQualificationInfoRepository.listByCertificateNo(cmd.getCertificateNo());
        userQualificationInfoE.checkNull(list == null ? 0 : list.size());

        BeanUtils.copyProperties(cmd, userQualificationInfoE);

        // 校验特种作业证书是否有操作项目
        userQualificationInfoE.checkOperatingProject(userQualificationInfoE);

        boolean res = false;
        try {
            res = userQualificationInfoGateway.add(userQualificationInfoE);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (!res) {
            throw new BizException("保存失败");
        }
        return true;
    }
}

