package com.zcloud.basic.info.command;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.cola.exception.BizException;
import com.zcloud.basic.info.domain.gateway.UserQualificationInfoGateway;
import com.zcloud.basic.info.domain.model.UserQualificationInfoE;
import com.zcloud.basic.info.dto.UserQualificationInfoUpdateCmd;
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:46
*/
@Component
@AllArgsConstructor
public class UserQualificationInfoUpdateExe {
    private final UserQualificationInfoGateway userQualificationInfoGateway;
    private final UserQualificationInfoRepository userQualificationInfoRepository;

    @Transactional(rollbackFor = Exception.class)
    public void execute(UserQualificationInfoUpdateCmd cmd) {
        UserQualificationInfoE userQualificationInfoE = new UserQualificationInfoE();

        // 证书编号验重
        List<UserQualificationInfoDO> list = userQualificationInfoRepository.listByIdAndCertificateNo(cmd.getId(), cmd.getCertificateNo());
        userQualificationInfoE.checkNull(list == null ? 0 : list.size());

        BeanUtils.copyProperties(cmd, userQualificationInfoE);

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

        boolean res = userQualificationInfoGateway.update(userQualificationInfoE);
        if (!res) {
            throw new BizException("修改失败");
        }
    }
}

