TestServiceImpl.java
4 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.canrd.webmagic.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.canrd.webmagic.common.constant.Constant;
import com.canrd.webmagic.common.constant.ServerResult;
import com.canrd.webmagic.common.utils.PageUtils;
import com.canrd.webmagic.domain.dto.TestDO;
import com.canrd.webmagic.domain.vo.TestQueryVO;
import com.canrd.webmagic.domain.vo.TestVO;
import com.canrd.webmagic.mapper.TestMapper;
import com.canrd.webmagic.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;
/**
* (Test)表服务实现类
*
* @author makejava
* @since 2024-01-12 14:36:57
*/
@Slf4j
@Service
public class TestServiceImpl extends ServiceImpl<TestMapper, TestDO> implements TestService {
/**
* 通过ID查询单条数据
* <p>
* testQueryVO 主键
*
* @return 实例对象
*/
@Override
public ServerResult queryById(TestQueryVO testQueryVO) {
if (Objects.isNull(testQueryVO.getId())) {
return ServerResult.fail("id 不能为空");
}
TestDO TestDo = getById(testQueryVO.getId());
if (Objects.isNull(TestDo)) {
return ServerResult.success(null);
}
return ServerResult.success(BeanUtil.copyProperties(TestDo, TestVO.class));
}
/**
* 分页查询
*
* @param testQueryVO 筛选条件
* @return 查询结果
*/
@Override
public ServerResult list(TestQueryVO testQueryVO) {
LambdaQueryWrapper<TestDO> queryWapper = new LambdaQueryWrapper<TestDO>()
.eq(TestDO::getEnableFlag, Constant.ENABLE_TEN)
.orderByDesc(TestDO::getId);
Page page = new Page<>(testQueryVO.getCurrent(), testQueryVO.getSize());
IPage<TestDO> iPage = page(page, queryWapper);
testQueryVO.setTotal(Long.valueOf(iPage.getTotal()).intValue());
return ServerResult.success(PageUtils.getPageReturn(iPage.getRecords(), testQueryVO));
}
/**
* 新增数据
*
* @param testVO 实例对象
* @return 实例对象
*/
@Override
public ServerResult add(TestVO testVO) {
//todo 校验
if (Objects.nonNull(testVO.getId())) {
testVO.setId(null);
}
TestDO testDo = BeanUtil.copyProperties(testVO, TestDO.class);
save(testDo);
return ServerResult.success();
}
/**
* 修改数据
*
* @param testVO 实例对象
* @return 实例对象
*/
@Override
public ServerResult edit(TestVO testVO) {
//todo 校验
if (Objects.isNull(testVO.getId())) {
return ServerResult.fail("id 不能为空");
}
TestDO testDo = BeanUtil.copyProperties(testVO, TestDO.class);
updateById(testDo);
return ServerResult.success();
}
/**
* 通过主键删除数据
*
* @param testQueryVO 筛选条件
* @return 是否成功
*/
@Override
public ServerResult deleteById(TestQueryVO testQueryVO) {
List<Long> ids = testQueryVO.getIds();
if (CollUtil.isEmpty(ids)) {
return ServerResult.fail("ids 参数不能为空");
}
List<TestDO> testList = listByIds(ids);
if (CollUtil.isEmpty(testList)) {
return ServerResult.success();
}
//todo 校验是否可以逻辑删除
LambdaUpdateWrapper<TestDO> updateWrapper = new LambdaUpdateWrapper<TestDO>()
.in(TestDO::getId, ids)
.set(TestDO::getEnableFlag, Constant.UNABLE_TWENTY);
update(updateWrapper);
return ServerResult.success();
}
}