jinpengyong
2023-09-22 48bd10b213992dd416620e338cf732ee972110de
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package com.moral.api.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moral.api.config.mybatis.wrapper.NullFilterWrapper;
import com.moral.api.entity.Group;
import com.moral.api.entity.Organization;
import com.moral.api.entity.User;
import com.moral.api.entity.UserGroup;
import com.moral.api.mapper.*;
import com.moral.api.pojo.dto.user.UserDTO;
import com.moral.api.pojo.dto.user.UserQueryDTO;
import com.moral.api.pojo.form.user.UserDeleteForm;
import com.moral.api.pojo.form.user.UserInsertForm;
import com.moral.api.pojo.form.user.UserQueryForm;
import com.moral.api.pojo.form.user.UserUpdateForm;
import com.moral.api.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.api.util.CompareFieldUtils;
import com.moral.api.util.LogUtils;
import com.moral.constant.Constants;
import com.moral.constant.ResponseCodeEnum;
import com.moral.pojo.CompareFieldResult;
import com.moral.util.ConvertUtils;
import com.moral.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.SimpleFormatter;
 
/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-03-09
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
 
    @Autowired
    UserMapper userMapper;
    @Autowired
    OrganizationMapper organizationMapper;
    @Autowired
    GroupMapper groupMapper;
    @Autowired
    UserGroupMapper userGroupMapper;
    @Autowired
    GroupMenuMapper groupMenuMapper;
 
 
    @Override
    public UserQueryDTO queryUsers(UserQueryForm form) {
        //创建返回对象
        UserQueryDTO dto = new UserQueryDTO();
        //取参
        Integer page = form.getPage();
        Integer size = form.getSize();
        String email = form.getEmail();
        String mobile = form.getMobile();
        String wechat = form.getWechat();
        String order = form.getOrder();
        String orderType = form.getOrderType();
        Integer organizationId = form.getOrganizationId();
        Date exipreStartTime = form.getExpireStartTime();
        Date exipreEndTime = form.getExpireEndTime();
        Date createStartTime = form.getCreateStartTime();
        Date createEndTime = form.getCreateEndTime();
        Integer isDelete = form.getIsDelete();
 
        //组装查询条件
        Page<User> queryPage = new Page<>(page, size);
        NullFilterWrapper<User> wrapper = new NullFilterWrapper<>();
        wrapper.like("email", email);
        wrapper.like("wechat", wechat);
        wrapper.like("mobile", mobile);
        wrapper.eq("is_admin", 1);
        wrapper.eq("organization_id", organizationId);
        wrapper.between("create_time", createStartTime, createEndTime);
        wrapper.between("expire_time", exipreStartTime, exipreEndTime);
        //排序顺序条件构造
        if (!ObjectUtils.isEmpty(order) && !ObjectUtils.isEmpty(orderType)) {
            if (orderType.equals(Constants.ORDER_ASC))
                wrapper.orderByAsc(ConvertUtils.toLine(order));
            else
                wrapper.orderByDesc(ConvertUtils.toLine(order));
        }
        //逻辑删除条件构造
        if (!ObjectUtils.isEmpty(isDelete))
            wrapper.eq("is_delete", isDelete);
        else
            wrapper.eq("is_delete", Constants.NOT_DELETE);
 
        //查询结果
        Page<User> resultPage = userMapper.selectPage(queryPage, wrapper);
        List<User> users = resultPage.getRecords();
 
        //查询用户对应组织
        List<UserDTO> userDTOS = new ArrayList<>();
        for (User user : users) {
            UserDTO userDTO = new UserDTO();
            Organization organization = new Organization();
            organization.setId(user.getOrganizationId());
            organization.setIsDelete(Constants.NOT_DELETE);
            QueryWrapper<Organization> organizationWrapper = new QueryWrapper<>();
            organizationWrapper.setEntity(organization);
            organization = organizationMapper.selectOne(organizationWrapper);
            userDTO.setUser(user);
            userDTO.setOrganization(organization);
            userDTOS.add(userDTO);
        }
 
        //封装返回结果
        dto.setUserDTOS(userDTOS);
        dto.setCurrent(resultPage.getCurrent());
        dto.setPages(resultPage.getPages());
        dto.setSize(resultPage.getSize());
        dto.setTotal(resultPage.getTotal());
        dto.setCode(ResponseCodeEnum.SUCCESS.getCode());
        dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
 
        return dto;
    }
 
    @Override
    @Transactional
    public UserDTO updateUser(UserUpdateForm form) {
        //创建返回对象
        UserDTO dto = new UserDTO();
 
        //取参
        User user = form.formConvertEntity();
 
        //查找要更新的用户 用于插入日志
        QueryWrapper<User> oldUserWrapper = new QueryWrapper<>();
        User oldUser = new User();
        oldUser.setId(user.getId());
        oldUser.setIsDelete(Constants.NOT_DELETE);
        oldUserWrapper.setEntity(oldUser);
        oldUser = userMapper.selectOne(oldUserWrapper);
        if (ObjectUtils.isEmpty(oldUser)) {
            dto.setCode(ResponseCodeEnum.USER_NOT_EXIST.getCode());
            dto.setMsg(ResponseCodeEnum.USER_NOT_EXIST.getMsg());
            return dto;
        }
 
        //更新
        userMapper.updateById(user);
 
        //获取更新后的用户
        user = userMapper.selectById(user.getId());
 
        //插入日志
        updatelog(oldUser,user);
 
        //封装返回结果
        dto.setCode(ResponseCodeEnum.SUCCESS.getCode());
        dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
 
        return dto;
    }
 
    @Override
    @Transactional
    public UserDTO insertUser(UserInsertForm form) {
        //创建返回对象
        UserDTO dto = new UserDTO();
        //取参
        User user = form.formConvertEntity();
        //判断账号是否存在
        User existUser = new User();
        existUser.setAccount(user.getAccount());
        existUser.setIsDelete(Constants.NOT_DELETE);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.setEntity(existUser);
        User existUserResult = userMapper.selectOne(wrapper);
        if (!ObjectUtils.isEmpty(existUserResult)) {
            dto.setCode(ResponseCodeEnum.USER_EXIST.getCode());
            dto.setMsg(ResponseCodeEnum.USER_EXIST.getMsg());
            return dto;
        }
        //判断组织是否已经存在管理员账号
        Integer organizationId = user.getOrganizationId();
        User organizationUser = new User();
        organizationUser.setIsAdmin(true);
        organizationUser.setIsDelete(Constants.NOT_DELETE);
        organizationUser.setOrganizationId(organizationId);
        QueryWrapper<User> organizationUserWrapper = new QueryWrapper<>();
        organizationUserWrapper.setEntity(organizationUser);
        organizationUser = userMapper.selectOne(organizationUserWrapper);
        if (!ObjectUtils.isEmpty(organizationUser)) {
            dto.setCode(ResponseCodeEnum.ORGANIZATION_USER_EXIST.getCode());
            dto.setMsg(ResponseCodeEnum.ORGANIZATION_USER_EXIST.getMsg());
            return dto;
        }
        //插入用户表
        userMapper.insert(user);
        //创建组织admin角色
        Group group = new Group();
        group.setOrganizationId(organizationId);
        group.setGroupName("admin");
        groupMapper.insert(group);
        //插入账号角色关联表
        UserGroup userGroup = new UserGroup();
        userGroup.setUserId(user.getId());
        userGroup.setGroupId(group.getId());
        userGroup.setOrganizationId(organizationId);
        userGroupMapper.insert(userGroup);
        //更新组织
        Organization organization = new Organization();
        organization.setAdminUserId(user.getId());
        organization.setId(organizationId);
        organizationMapper.updateById(organization);
        //操作插入日志
        insertLog(user,organizationId);
        //封装返回结果
        dto.setCode(ResponseCodeEnum.SUCCESS.getCode());
        dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
        return dto;
    }
 
 
    @Override
    @Transactional
    public UserDTO deleteUsersByOrganizationId(Integer organizationId) {
        //创建返回对象
        UserDTO dto = new UserDTO();
        //逻辑删除,删除账号以及该组织下的所有账号
        UpdateWrapper<User> deleteUserChildrenWrapper = new UpdateWrapper<>();
        deleteUserChildrenWrapper.eq("organization_id", organizationId);
        deleteUserChildrenWrapper.set("is_delete", Constants.DELETE);
        userMapper.update(null, deleteUserChildrenWrapper);
        //逻辑删除,groupMenu
        UpdateWrapper deleteGroupMenuWrapper = new UpdateWrapper();
        deleteGroupMenuWrapper.eq("organization_id", organizationId);
        deleteGroupMenuWrapper.set("is_delete", Constants.DELETE);
        groupMenuMapper.update(null, deleteGroupMenuWrapper);
        //逻辑删除,userGroup
        UpdateWrapper deleteUserGroupWrapper = new UpdateWrapper();
        deleteUserGroupWrapper.eq("organization_id", organizationId);
        deleteUserGroupWrapper.set("is_delete", Constants.DELETE);
        userGroupMapper.update(null, deleteUserGroupWrapper);
        //逻辑删除,group
        UpdateWrapper deleteGroupWrapper = new UpdateWrapper();
        deleteGroupWrapper.eq("organization_id", organizationId);
        deleteGroupWrapper.set("is_delete", Constants.DELETE);
        groupMapper.update(null, deleteGroupWrapper);
        //返回结果
        dto.setCode(ResponseCodeEnum.SUCCESS.getCode());
        dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
        return dto;
    }
 
    /**
    * @Description: 插入操作插入日志
            * @Param: [user, organizationId]
            * @return: void
            * @Author: 陈凯裕
            * @Date: 2021/8/24
            */
    private void insertLog(User user,Integer organizationId) {
        StringBuilder content = new StringBuilder("添加了前台用户;");
        content.append("角色:admin;");
        //查询添加admin用户的组织
        Organization organization = organizationMapper.selectById(organizationId);
        content.append("组织:"+organization.getName()+";");
        content.append("账号:"+user.getAccount()+";");
        content.append("用户名称:"+user.getUserName()+";");
        content.append("过期时间:"+ DateUtils.dateToDateString(user.getExpireTime(),"yyyy-MM-dd")+";");
        if(user.getEmail()!=null)
            content.append("邮箱:"+user.getEmail()+";");
        if(user.getMobile()!=null)
            content.append("手机:"+user.getMobile()+";");
        if(user.getWechat()!=null)
            content.append("微信:"+user.getWechat()+";");
        LogUtils.saveOperationForManage(content.toString(), Constants.INSERT_OPERATE_TYPE);
    }
 
 
    /**
    * @Description: 更新操作插入日志
            * @Param: [oldUser, newUser]
            * @return: void
            * @Author: 陈凯裕
            * @Date: 2021/8/24
            */
    private void updatelog(User oldUser,User newUser){
        List<CompareFieldResult> results = CompareFieldUtils.compare(User.class, oldUser, newUser);
        StringBuilder content = new StringBuilder("修改了前台用户;");
        content.append("所属组织:"+organizationMapper.selectById(oldUser.getOrganizationId()).getName()+";");
        //密码特殊处理
        if(newUser.getPassword()!=null)
            content.append("修改了密码;");
        String resultContent = CompareFieldUtils.resultsConvertContent(results,content.toString());
        LogUtils.saveOperationForManage(resultContent, Constants.UPDATE_OPERATE_TYPE);
    }
 
 
}