package com.zcloud.zcGbsServicer.gatewayimpl;

import cn.hutool.core.collection.CollUtil;
import com.zcloud.zcGbsServicer.domain.gateway.ResourceDeviceGateway;
import com.zcloud.zcGbsServicer.domain.model.ResourceDeviceE;
import com.zcloud.zcGbsServicer.persistence.dataobject.ResourceDeviceDO;
import com.zcloud.zcGbsServicer.persistence.repository.ResourceDeviceRepository;
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:16
*/
@Service
@AllArgsConstructor
public class ResourceDeviceGatewayImpl implements ResourceDeviceGateway {
    private final ResourceDeviceRepository resourceDeviceRepository;

    @Override
    public Boolean add(ResourceDeviceE resourceDeviceE) {
        ResourceDeviceDO d = new ResourceDeviceDO(resourceDeviceE.getDeviceId());
        BeanUtils.copyProperties(resourceDeviceE, d,"deviceId");
        resourceDeviceRepository.save(d);
        return true;
    }

    @Override
    public Boolean addBatch(List<ResourceDeviceE> resourceDeviceEList) {
        if (CollUtil.isEmpty(resourceDeviceEList)) return true;
        List<ResourceDeviceDO> resourceDeviceDOList = resourceDeviceEList.stream().map(resourceDeviceE -> {
            ResourceDeviceDO d = new ResourceDeviceDO(resourceDeviceE.getDeviceId());
            BeanUtils.copyProperties(resourceDeviceE, d,"deviceId");
            return d;
        }).collect(Collectors.toList());
        return resourceDeviceRepository.saveBatch(resourceDeviceDOList);
    }

    @Override
    public Boolean update(ResourceDeviceE resourceDeviceE) {
        ResourceDeviceDO d = new ResourceDeviceDO();
        BeanUtils.copyProperties(resourceDeviceE, d);
        resourceDeviceRepository.updateById(d);
        return true;
    }

    @Override
    public Boolean deletedResourceDeviceById(Long id) {
        return resourceDeviceRepository.removeById(id);
    }

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

    @Override
    public Boolean deletedResourceDeviceByBizId(String roomId, Integer deviceType) {
        List<ResourceDeviceDO> deviceDOList = resourceDeviceRepository.listByBizIdAndBizType(CollUtil.newArrayList(roomId), deviceType);
        if (CollUtil.isEmpty(deviceDOList)) {
            return true;
        }
        return resourceDeviceRepository.removeBatchByIds(deviceDOList.stream().map(ResourceDeviceDO::getId).collect(Collectors.toList()));
    }
}

