liulin
2024-08-02 12c82c37acb998a3c269871e22e339d66b9f1c1d
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package com.lunhan.xxx.service;
 
import com.lunhan.xxx.common.ExecutedResult;
import com.lunhan.xxx.common.PagerResult;
import com.lunhan.xxx.common.enums.EYesOrNo;
import com.lunhan.xxx.common.model.Tuple;
import com.lunhan.xxx.common.util.*;
import com.lunhan.xxx.entity.dto.search.SearchTestInfo;
import com.lunhan.xxx.entity.enums.EState;
import com.lunhan.xxx.entity.request.ReqListSetSort;
import com.lunhan.xxx.entity.request.ReqSetSort;
import com.lunhan.xxx.entity.request.test.ReqCreateTestInfo;
import com.lunhan.xxx.entity.request.test.ReqModifyTestInfo;
import com.lunhan.xxx.repository.impl.TestInfoMapperImpl;
import com.lunhan.xxx.repository.po.TestInfoPO;
import com.lunhan.xxx.repository.vo.TestInfoVO;
import com.lunhan.xxx.service.convert.TestInfoConvert;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 测试信息 相关服务
 */
@Service
public class TestInfoService extends BaseService {
    @Autowired
    private TestInfoMapperImpl mapper;
 
    public ExecutedResult<Long> create(ReqCreateTestInfo request) {
        // 转换po
        TestInfoPO item = TestInfoConvert.INSTANCE.toCreate(request);
        // 设置状态
        item.setStatus(EState.NORMAL.getValue());
        // 设置记录创建时间
        item.setCreateTime(LocalDateTimeUtil.nowTimeStamp());
        // 非逻辑删除
        item.setIsDelete(EYesOrNo.NO.getValue());
 
        int rowCount = mapper.insert(item);
        if (rowCount != 1) {
            return ExecutedResult.failed("创建[测试信息]失败。");
        }
        return ExecutedResult.success(item.getId());
    }
 
    public ExecutedResult<String> modify(ReqModifyTestInfo request) {
        // 验证记录是否存在
        ExecutedResult<TestInfoPO> checkExists = this.check4Id(request.getId());
        if (checkExists.isFailed()) {
            return ExecutedResult.failed(checkExists.getMsg());
        }
 
        // 转换po
        TestInfoPO item = TestInfoConvert.INSTANCE.toModify(request);
 
        int rowCount = mapper.updateById(item);
        if (rowCount != 1) {
            return ExecutedResult.failed("编辑[测试信息]失败。");
        }
        return ExecutedResult.success();
    }
 
    public ExecutedResult<TestInfoVO> get(Long id) {
        TestInfoVO result = new TestInfoVO();
 
        TestInfoPO find = mapper.selectById(id);
        if (null != find) {
            result = TestInfoConvert.INSTANCE.toVo(find);
        }
        return ExecutedResult.success(result);
    }
 
    public ExecutedResult<String> stop(Long id) {
        // 验证记录是否存在
        ExecutedResult<TestInfoPO> checkExists = this.check4Id(id);
        if (checkExists.isFailed()) {
            return ExecutedResult.failed(checkExists.getMsg());
        }
 
        TestInfoPO item = new TestInfoPO();
        item.setId(id);
        item.setStatus(EState.DISABLED.getValue());
 
        int rowCount = mapper.updateById(item);
        if (rowCount != 1) {
            return ExecutedResult.failed("停用[测试信息]失败。");
        }
        return ExecutedResult.success();
    }
 
    public ExecutedResult<String> enable(Long id) {
        // 验证记录是否存在
        ExecutedResult<TestInfoPO> checkExists = this.check4Id(id);
        if (checkExists.isFailed()) {
            return ExecutedResult.failed(checkExists.getMsg());
        }
 
        TestInfoPO item = new TestInfoPO();
        item.setId(id);
        item.setStatus(EState.NORMAL.getValue());
 
        int rowCount = mapper.updateById(item);
        if (rowCount != 1) {
            return ExecutedResult.failed("启用[测试信息]失败。");
        }
        return ExecutedResult.success();
    }
 
    public ExecutedResult<String> setSort(ReqSetSort request) {
        // 验证记录是否存在
        ExecutedResult<TestInfoPO> checkExists = this.check4Id(request.getId());
        if (checkExists.isFailed()) {
            return ExecutedResult.failed(checkExists.getMsg());
        }
 
        TestInfoPO item = new TestInfoPO();
        item.setId(request.getId());
        item.setSort(request.getSort());
 
        int rowCount = mapper.updateById(item);
        if (rowCount != 1) {
            return ExecutedResult.failed("[测试信息]设置排序值失败。");
        }
        return ExecutedResult.success();
    }
 
    public ExecutedResult<String> listSetSort(ReqListSetSort request) {
        // id列表
        List<Long> listId = request.getList().stream().map(ReqSetSort::getId).collect(Collectors.toList());
        // 验证记录是否存在
        ExecutedResult<List<TestInfoPO>> checkExists = this.check4Id(listId);
        if (checkExists.isFailed()) {
            return ExecutedResult.failed(checkExists.getMsg());
        }
 
        List<TestInfoPO> listUpdate = request.getList().stream()
                .map(c -> {
                    TestInfoPO item = new TestInfoPO();
                    item.setId(c.getId());
                    item.setSort(c.getSort());
                    return item;
                })
                .collect(Collectors.toList());
        Boolean result = mapper.modifyList(listUpdate);
        if (result) {
            return ExecutedResult.success();
        }
        return ExecutedResult.failed("[测试信息]设置排序值失败");
    }
 
    public ExecutedResult<String> remove(Long id) {
        // 验证记录是否存在
        ExecutedResult<TestInfoPO> checkExists = this.check4Id(id);
        if (checkExists.isFailed()) {
            return ExecutedResult.failed(checkExists.getMsg());
        }
 
        Boolean result = mapper.deleteLogic(id);
        if (BooleanUtils.isFalse(result)) {
            return ExecutedResult.failed("删除[测试信息]失败。");
        }
        return ExecutedResult.success();
    }
 
    public ExecutedResult<String> removeList(List<Long> listId) {
        // 验证记录是否存在
        ExecutedResult<List<TestInfoPO>> checkExists = this.check4Id(listId);
        if (checkExists.isFailed()) {
            return ExecutedResult.failed(checkExists.getMsg());
        }
 
        Boolean result = mapper.deleteLogic(listId);
        if (BooleanUtils.isFalse(result)) {
            return ExecutedResult.failed("删除[测试信息]失败。");
        }
        return ExecutedResult.success();
    }
 
    public ExecutedResult<List<TestInfoVO>> getList(List<Long> listId) {
        List<TestInfoVO> result = new ArrayList<>();
 
        List<TestInfoPO> list = mapper.getList(listId);
        if (ListUtil.isNotNullOrEmpty(list)) {
            result = CopierUtil.mapTo(list, TestInfoVO.class);
        }
        return ExecutedResult.success(result);
    }
 
    public ExecutedResult<PagerResult<TestInfoVO>> search(SearchTestInfo search) {
        // 处理创建时间范围-查询参数
        Tuple<String, String> createTimeRange = ParameterUtil.getTimeRange(search.getCreateTimeRange());
        if (StringUtil.isNotNullOrEmpty(createTimeRange.getItem1())) {
            search.setCreateTimeStart(LocalDateTimeUtil.getTimeStamp(createTimeRange.getItem1()).getTime());
        }
        if (StringUtil.isNotNullOrEmpty(createTimeRange.getItem2())) {
            search.setCreateTimeEnd(LocalDateTimeUtil.getTimeStamp(createTimeRange.getItem2()).getTime());
        }
 
        PagerResult<TestInfoPO> pageList = mapper.search(search);
        List<TestInfoVO> listVo = new ArrayList<>();
        List<TestInfoPO> list = pageList.getList();
        if (ListUtil.isNotNullOrEmpty(list)) {
            pageList.setLastId(list.get(list.size() - 1).getId());
            // 转换vo
            listVo = TestInfoConvert.INSTANCE.toVo(list);
        }
        PagerResult<TestInfoVO> result = new PagerResult<>(pageList.getLimit(), pageList.getPage(), pageList.getTotal(), listVo);
        result.setLastId(pageList.getLastId());
        return ExecutedResult.success(result);
    }
 
    protected ExecutedResult<TestInfoPO> check4Id(Long id) {
        TestInfoPO exists = mapper.get(id);
        if (Objects.isNull(exists)) {
            return ExecutedResult.failed("[测试信息]不存在:" + id);
        }
        return ExecutedResult.success(exists);
    }
    protected ExecutedResult<List<TestInfoPO>> check4Id(List<Long> listId) {
        // 从数据库查找测试信息
        List<TestInfoPO> list = mapper.getList(listId);
        if (ListUtil.isNullOrEmpty(list)) {
            return ExecutedResult.failed("[测试信息]不存在." + listId);
        }
        // 数据库找到的id列表
        List<Long> listIdFind = list.stream().map(TestInfoPO::getId).collect(Collectors.toList());
        // 数量不一致
        if (listId.size() != listIdFind.size()) {
            // 筛选数据库不存在的测试信息
            List<Long> listIdNotFound = listId.stream().filter(c -> !listIdFind.contains(c)).collect(Collectors.toList());
            if (ListUtil.isNullOrEmpty(list)) {
                return ExecutedResult.failed("[测试信息]不存在." + listIdNotFound);
            }
        }
        return ExecutedResult.success(list);
    }
 
    protected Map<Long, TestInfoPO> getMap4Id(List<Long> listId) {
        List<TestInfoPO> list = mapper.getList(listId);
        if (ListUtil.isNullOrEmpty(list)) {
            return new HashMap<>(0);
        }
        return list.stream().collect(Collectors.toMap(TestInfoPO::getId, c -> c));
    }
 
}