TestExampleDbServiceImpl.java 3.26 KB
package com.tianting.infoloop.service.impl;


import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tianting.infoloop.grpc.exampleservice.TestExampleDbCreation;
import com.tianting.infoloop.grpc.exampleservice.TestExampleDbModification;
import com.tianting.infoloop.mapper.TestExampleDbMapper;
import com.tianting.infoloop.model.db.TestExampleDb;
import com.tianting.infoloop.service.TestExampleDbService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;

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

/**
* @author zhuyf
* @description 针对表【 test_example_db(例子DB)】的数据库操作Service实现
* @createDate 2024-10-12 13:53:16
*/
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class TestExampleDbServiceImpl extends ServiceImpl<TestExampleDbMapper, TestExampleDb> implements TestExampleDbService {

    private final TestExampleDbMapper testExampleDbMapper;

    @Override
    @Nullable
    public TestExampleDb createTestExampleDb(TestExampleDbCreation creation) {
        TestExampleDb testExampleDb = new TestExampleDb();
        testExampleDb.setName(creation.getName());
        testExampleDb.setStatus(creation.getStatusValue());
        final var insert = testExampleDbMapper.insert(testExampleDb);
        return insert > 0 ? testExampleDb : null;
    }

    @Override
    public List<TestExampleDb> batchCreateTestExampleDb(List<TestExampleDbCreation> creationsList) {
        var list = creationsList.stream().map(creation -> new TestExampleDb()
                .setName(creation.getName())
                .setStatus(creation.getStatusValue())).collect(Collectors.toList());
        return SpringUtil.getBean(TestExampleDbServiceImpl.class).saveBatch(list) ? list : Collections.emptyList();
    }

    @Override
    public boolean updateTestExampleDb(TestExampleDbModification modification) {
        var testExampleDb = new TestExampleDb();
        testExampleDb.setId(modification.getId());
        if (modification.hasName()) {
            testExampleDb.setName(modification.getName());
        }
        if (modification.hasStatus()) {
            testExampleDb.setStatus(modification.getStatusValue());
        }
        return testExampleDbMapper.updateById(testExampleDb) > 0;
    }

    @Override
    public boolean batchUpdateTestExampleDb(List<TestExampleDbModification> modificationsList) {
        final var list = modificationsList.stream().map(modification -> {
            var testExampleDb = new TestExampleDb();
            testExampleDb.setId(modification.getId());
            if (modification.hasName()) {
                testExampleDb.setName(modification.getName());
            }
            if (modification.hasStatus()) {
                testExampleDb.setStatus(modification.getStatusValue());
            }
            return testExampleDb;
        }).collect(Collectors.toList());
        return SpringUtil.getBean(TestExampleDbServiceImpl.class).updateBatchById(list);
    }
}