elkers
2025-04-07 2dba8dbb14af4616eec876fd9af744651e8beeda
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
/**
#                                                    __----~~~~~~~~~~~------___
#                                   .  .   ~~//====......          __--~ ~~
#                   -.            \_|//     |||\\  ~~~~~~::::... /~
#                ___-==_       _-~o~  \/    |||  \\            _/~~-
#        __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
#    _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
#  .~       .~       |   \\ -_    /  /-   /   ||      \   /
# /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
# |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
#          '         ~-|      /|    |-~\~~       __--~~
#                      |-~~-_/ |    |   ~\_   _-~            /\
#                           /  \     \__   \/~                \__
#                       _--~ _/ | .-~~____--~-/                  ~~==.
#                      ((->/~   '.|||' -_|    ~~-/ ,              . _||
#                                 -_     ~\      ~~---l__i__i__i--~~_/
#                                 _-~-__   ~)  \--______________--~~
#                               //.-~~~-~_--~- |-------~~~~~~~~
#                                      //.-~~~--\
#                  神兽保佑
#                  永无BUG!
*/
package com.nanjing.water.service;
 
import com.nanjing.water.common.*;
import com.nanjing.water.common.enums.*;
import com.nanjing.water.common.model.Tuple;
import com.nanjing.water.common.util.*;
import com.nanjing.water.entity.request.adminuser.ReqCreateAdminUser;
import com.nanjing.water.entity.request.adminuser.ReqModifyAdminUser;
import com.nanjing.water.entity.search.SearchAdminUser;
import com.nanjing.water.repository.impl.AdminUserMapperImpl;
import com.nanjing.water.repository.po.AdminUserPO;
import com.nanjing.water.repository.vo.AdminUserVO;
import com.nanjing.water.service.convert.AdminUserConvert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * 管理员帐号
 * @author lin.liu
 */
@Service
public class AdminUserService extends BaseService {
    @Autowired
    private AdminUserMapperImpl mapper;
 
    public ExecutedResult<Long> create(ReqCreateAdminUser request) {
        // 转换po
        AdminUserPO item = AdminUserConvert.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(ReqModifyAdminUser request) {
        // 验证记录是否存在
        ExecutedResult<AdminUserPO> checkExists = this.check4Id(request.getId());
        if (checkExists.isFailed()) {
            return ExecutedResult.failed(checkExists.getMsg());
        }
        // 转换po
        AdminUserPO item = AdminUserConvert.INSTANCE.toModify(request);
 
        int rowCount = mapper.updateById(item);
        if (rowCount != 1) {
            return ExecutedResult.failed("编辑[管理员帐号]失败。");
        }
        return ExecutedResult.success();
    }
 
    public ExecutedResult<AdminUserVO> get(Long id) {
        AdminUserVO result = new AdminUserVO();
 
        AdminUserPO find = mapper.get(id);
        if (null != find) {
            // 转换vo
            result = AdminUserConvert.INSTANCE.toVo(find);
        }
        return ExecutedResult.success(result);
    }
 
//    public ExecutedResult<String> stop(Long id) {
//        // 验证记录是否存在
//        ExecutedResult<AdminUserPO> checkExists = this.check4Id(id);
//        if (checkExists.isFailed()) {
//            return ExecutedResult.failed(checkExists.getMsg());
//        }
//        AdminUserPO item = new AdminUserPO();
//        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<AdminUserPO> checkExists = this.check4Id(id);
//        if (checkExists.isFailed()) {
//            return ExecutedResult.failed(checkExists.getMsg());
//        }
//        AdminUserPO item = new AdminUserPO();
//        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<AdminUserPO> checkExists = this.check4Id(request.getId());
//        if (checkExists.isFailed()) {
//            return ExecutedResult.failed(checkExists.getMsg());
//        }
//        AdminUserPO item = new AdminUserPO();
//        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<AdminUserPO>> checkExists = this.check4Id(listId);
//        if (checkExists.isFailed()) {
//            return ExecutedResult.failed(checkExists.getMsg());
//        }
//
//        List<AdminUserPO> listUpdate = request.getList().stream()
//                .map(c -> {
//                    AdminUserPO item = new AdminUserPO();
//                    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) {
//        Boolean result = mapper.deleteLogic(id);
//        if (BooleanUtils.isFalse(result)) {
//            return ExecutedResult.failed("删除[管理员帐号]失败。");
//        }
//        return ExecutedResult.success();
//    }
//
//    public ExecutedResult<String> removeList(List<Long> ids) {
//        Boolean result = mapper.deleteLogic(ids);
//        if (BooleanUtils.isFalse(result)) {
//            return ExecutedResult.failed("删除[管理员帐号]失败。");
//        }
//        return ExecutedResult.success();
//    }
 
    public ExecutedResult<List<AdminUserVO>> getList(List<Long> listId) {
        List<AdminUserVO> result = new ArrayList<>();
 
        List<AdminUserPO> list = mapper.getList(listId);
        if (ListUtil.isNotNullOrEmpty(list)) {
            // 转换vo
            result = AdminUserConvert.INSTANCE.toVo(list);
        }
        return ExecutedResult.success(result);
    }
 
    public ExecutedResult<PagerResult<AdminUserVO>> search(SearchAdminUser 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<AdminUserPO> pageList = mapper.search(search);
        List<AdminUserVO> listVo = new ArrayList<>();
        List<AdminUserPO> list = pageList.getList();
        if (ListUtil.isNotNullOrEmpty(list)) {
            pageList.setLastId(list.get(list.size() - 1).getId());
            // 转换vo
            listVo = AdminUserConvert.INSTANCE.toVo(list);
        }
        PagerResult<AdminUserVO> result = new PagerResult<>(pageList.getLimit(), pageList.getPage(), pageList.getTotal(), listVo);
        result.setLastId(pageList.getLastId());
        return ExecutedResult.success(result);
    }
 
    protected ExecutedResult<AdminUserPO> check4Id(Long id) {
        AdminUserPO exists = mapper.get(id);
        if (Objects.isNull(exists)) {
            return ExecutedResult.failed("[管理员帐号]不存在:" + id);
        }
        return ExecutedResult.success(exists);
    }
    protected ExecutedResult<List<AdminUserPO>> check4Id(List<Long> listId) {
        // 从数据库查找管理员帐号
        List<AdminUserPO> list = mapper.getList(listId);
        if (ListUtil.isNullOrEmpty(list)) {
            return ExecutedResult.failed("[管理员帐号]不存在." + listId);
        }
        // 数据库找到的id列表
        List<Long> listIdFind = list.stream().map(AdminUserPO::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);
    }}