package com.zcloud.basic.info.gatewayimpl;

import com.jjb.saas.framework.auth.utils.AuthContext;
import com.zcloud.basic.info.domain.gateway.UserCorpRecordGateway;
import com.zcloud.basic.info.domain.model.UserCorpRecordE;
import com.zcloud.basic.info.persistence.dataobject.UserCorpRecordDO;
import com.zcloud.basic.info.persistence.repository.UserCorpRecordRepository;
import com.zcloud.gbscommon.utils.Tools;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Collections;

/**
 * web-infrastructure
 *
 * @Author zhaokai
 * @Date 2025-12-03 10:21:59
 */
@Service
@AllArgsConstructor
public class UserCorpRecordGatewayImpl implements UserCorpRecordGateway {
    private final UserCorpRecordRepository userCorpRecordRepository;

    @Override
    public Boolean add(UserCorpRecordE userCorpRecordE) {
        if(StringUtils.isEmpty(userCorpRecordE.getUserCorpRecordId())){
            userCorpRecordE.setUserCorpRecordId(Tools.get32UUID());
        }
        if(userCorpRecordE.getCorpinfoId() == null){
            userCorpRecordE.setCorpinfoId(AuthContext.getTenantId());
        }
        UserCorpRecordDO d = new UserCorpRecordDO();
        BeanUtils.copyProperties(userCorpRecordE, d);
        userCorpRecordRepository.save(d);
        userCorpRecordE.setId(d.getId());
        return true;
    }

    @Override
    public Boolean update(UserCorpRecordE userCorpRecordE) {
        UserCorpRecordDO d = new UserCorpRecordDO();
        BeanUtils.copyProperties(userCorpRecordE, d);
        userCorpRecordRepository.updateById(d);
        return true;
    }

    @Override
    public Boolean deletedUserCorpRecordById(Long id) {
        return userCorpRecordRepository.removeById(id);
    }

    @Override
    public Boolean deletedUserCorpRecordByIds(Long[] ids) {
        return userCorpRecordRepository.removeByIds(Collections.singletonList(ids));
    }
}

