package com.zcloud.zcGbsServicer.gatewayimpl;

import cn.hutool.core.collection.CollUtil;
import com.zcloud.zcGbsServicer.domain.gateway.ResourcePersonGateway;
import com.zcloud.zcGbsServicer.domain.model.ResourcePersonE;
import com.zcloud.zcGbsServicer.persistence.dataobject.ResourcePersonDO;
import com.zcloud.zcGbsServicer.persistence.repository.ResourcePersonRepository;
import lombok.AllArgsConstructor;
import com.zcloud.gbscommon.utils.Tools;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* web-infrastructure
* @Author makejava
* @Date 2026-01-21 17:23:34
*/
@Service
@AllArgsConstructor
public class ResourcePersonGatewayImpl implements ResourcePersonGateway {
    private final ResourcePersonRepository resourcePersonRepository;

    @Override
    public Boolean add(ResourcePersonE resourcePersonE) {
        ResourcePersonDO d = new ResourcePersonDO(resourcePersonE.getPersonId());
        BeanUtils.copyProperties(resourcePersonE, d,"personId");
        resourcePersonRepository.save(d);
        return true;
    }

    @Override
    public Boolean addBatch(List<ResourcePersonE> resourcePersonEList) {
        if (CollUtil.isEmpty(resourcePersonEList)) return true;
        // 使用 Stream 流进行转换
        List<ResourcePersonDO> doList = resourcePersonEList.stream()
                .map(e -> {
                    ResourcePersonDO d = new ResourcePersonDO(Tools.get32UUID());
                    BeanUtils.copyProperties(e, d, "personId");
                    d.setId(null);
                    return d;
                })
                .collect(Collectors.toList());
        return resourcePersonRepository.saveBatch(doList);
    }

    @Override
    public Boolean update(ResourcePersonE resourcePersonE) {
        ResourcePersonDO d = new ResourcePersonDO();
        BeanUtils.copyProperties(resourcePersonE, d);
        resourcePersonRepository.updateById(d);
        return true;
    }

    @Override
    public Boolean deletedResourcePersonById(Long id) {
        return resourcePersonRepository.removeById(id);
    }

    @Override
    public Boolean deletedResourcePersonByIds(Long[] ids) {
        return resourcePersonRepository.removeByIds(Arrays.asList(ids));
    }

    @Override
    public Boolean deletedResourcePersonByBizId(String bizId, Integer bizType) {
        List<ResourcePersonDO> resourcePersonDOList = resourcePersonRepository.listByBizIdAndBizType(CollUtil.newArrayList(bizId), bizType);
        if (CollUtil.isEmpty(resourcePersonDOList)) {
            return true;
        }
        return resourcePersonRepository.removeBatchByIds(resourcePersonDOList.stream().map(ResourcePersonDO::getId).collect(Collectors.toList()));
    }
}

