ExampleServiceRpcClient.java 6.51 KB
package com.infoloop.tianting.service.client;

import com.infoloop.tianting.model.common.PageInfo;
import com.infoloop.tianting.model.create.TestExampleDbCreate;
import com.infoloop.tianting.model.query.TestExampleDbQuery;
import com.infoloop.tianting.model.update.TestExampleDbUpdate;
import com.tianting.infoloop.grpc.exampleservice.BatchCreateTestExampleDbRpcRequest;
import com.tianting.infoloop.grpc.exampleservice.BatchCreateTestExampleDbRpcResponse;
import com.tianting.infoloop.grpc.exampleservice.BatchUpdateTestExampleDbRpcRequest;
import com.tianting.infoloop.grpc.exampleservice.CreateTestExampleDbRpcRequest;
import com.tianting.infoloop.grpc.exampleservice.CreateTestExampleDbRpcResponse;
import com.tianting.infoloop.grpc.exampleservice.DeleteTestExampleDbByIdsRpcRequest;
import com.tianting.infoloop.grpc.exampleservice.ExampleServiceRpcGrpc;
import com.tianting.infoloop.grpc.exampleservice.GetAllTestExampleDbRpcRequest;
import com.tianting.infoloop.grpc.exampleservice.GetTestExampleDbByIdRpcRequest;
import com.tianting.infoloop.grpc.exampleservice.GetTestExampleDbByIdsRpcRequest;
import com.tianting.infoloop.grpc.exampleservice.QueryTestExampleDbByConditionRpcRequest;
import com.tianting.infoloop.grpc.exampleservice.QueryTestExampleDbByPaginationRpcRequest;
import com.tianting.infoloop.grpc.exampleservice.QueryTestExampleDbByPaginationRpcResponse;
import com.tianting.infoloop.grpc.exampleservice.SingleTestExampleDbRpcResponse;
import com.tianting.infoloop.grpc.exampleservice.TestExampleDbCreation;
import com.tianting.infoloop.grpc.exampleservice.TestExampleDbModification;
import com.tianting.infoloop.grpc.exampleservice.UpdateTestExampleDbRpcRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;

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

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ExampleServiceRpcClient {

    private final ExampleServiceRpcGrpc.ExampleServiceRpcBlockingStub exampleServiceRpcBlockingStub;

    /**
     * RPC Client示例
     */

    @Nullable
    public SingleTestExampleDbRpcResponse getTestExampleDbById(int id) {
        final var response = exampleServiceRpcBlockingStub.getTestExampleDbById(GetTestExampleDbByIdRpcRequest.newBuilder()
                .setId(id)
                .build());
        if (!response.hasResponse()) {
            return null;
        }
        return response.getResponse();
    }

    public List<SingleTestExampleDbRpcResponse> getTestExampleDbByIds(List<Integer> ids) {
        return exampleServiceRpcBlockingStub.getTestExampleDbByIds(GetTestExampleDbByIdsRpcRequest.newBuilder()
                .addAllIds(ids)
                .build()).getResponsesList();
    }

    public List<SingleTestExampleDbRpcResponse> getAllTestExampleDb() {
        return exampleServiceRpcBlockingStub.getAllTestExampleDb(GetAllTestExampleDbRpcRequest.newBuilder().build()).getResponsesList();
    }

    public List<SingleTestExampleDbRpcResponse> queryTestExampleDbByCondition(TestExampleDbQuery query) {
        final var builder = QueryTestExampleDbByConditionRpcRequest.newBuilder();
        if (query.getName() != null) {
            builder.setName(query.getName());
        }
        if (query.getStatus() != null) {
            builder.setStatus(query.getStatus());
        }
        return exampleServiceRpcBlockingStub.queryTestExampleDbByCondition(builder.build()).getResponsesList();
    }

    public QueryTestExampleDbByPaginationRpcResponse queryTestExampleDbByPagination(PageInfo pageInfo, TestExampleDbQuery query) {
        final var builder = QueryTestExampleDbByPaginationRpcRequest.newBuilder();
        builder.setPageNo(pageInfo.getPageNo());
        builder.setPageSize(pageInfo.getPageSize());
        if (query.getName() != null) {
            builder.setName(query.getName());
        }
        if (query.getStatus() != null) {
            builder.setStatus(query.getStatus());
        }
        return exampleServiceRpcBlockingStub.queryTestExampleDbByPagination(builder.build());
    }

    public CreateTestExampleDbRpcResponse createTestExampleDb(TestExampleDbCreate create) {
        final var creation = TestExampleDbCreation.newBuilder()
                .setName(create.getName())
                .setStatus(create.getStatus())
                .build();
        return exampleServiceRpcBlockingStub.createTestExampleDb(CreateTestExampleDbRpcRequest.newBuilder().setCreation(creation).build());
    }

    public BatchCreateTestExampleDbRpcResponse batchCreateTestExampleDb(List<TestExampleDbCreate> createList) {
        final var creationList = createList.stream().map(e -> TestExampleDbCreation.newBuilder()
                .setName(e.getName())
                .setStatus(e.getStatus())
                .build()).collect(Collectors.toList());
        return exampleServiceRpcBlockingStub.batchCreateTestExampleDb(BatchCreateTestExampleDbRpcRequest.newBuilder().addAllCreations(creationList).build());
    }

    public boolean updateTestExampleDb(TestExampleDbUpdate update) {
        final var builder = TestExampleDbModification.newBuilder()
                .setId(update.getId());
        if (update.getName() != null) {
            builder.setName(update.getName());
        }
        if (update.getStatus() != null) {
            builder.setStatus(update.getStatus());
        }
        return exampleServiceRpcBlockingStub.updateTestExampleDb(UpdateTestExampleDbRpcRequest.newBuilder().setModification(builder.build()).build()).getIsUpdated();
    }

    public boolean batchUpdateTestExampleDb(List<TestExampleDbUpdate> updateList) {
        final var modificationList = updateList.stream().map(update -> {
            final var builder = TestExampleDbModification.newBuilder()
                    .setId(update.getId());
            if (update.getName() != null) {
                builder.setName(update.getName());
            }
            if (update.getStatus() != null) {
                builder.setStatus(update.getStatus());
            }
            return builder.build();
        }).collect(Collectors.toList());
        return exampleServiceRpcBlockingStub.batchUpdateTestExampleDb(BatchUpdateTestExampleDbRpcRequest.newBuilder().addAllModifications(modificationList).build()).getIsUpdated();
    }

    public boolean deleteTestExampleDbByIds(List<Integer> ids) {
        return exampleServiceRpcBlockingStub.deleteTestExampleDbByIds(DeleteTestExampleDbByIdsRpcRequest.newBuilder().addAllIds(ids).build()).getIsDeleted();
    }
}