TestExampleDbServiceImpl.java
3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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);
}
}