懒羊羊
2023-08-30 71e81ed1d12e4d69f53c8ad9e066650ad4186293
提交 | 用户 | 时间
71e81e 1 /**
2  * Copyright 2018-2020 stylefeng & fengshuonan (https://gitee.com/stylefeng)
3  * <p>
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * <p>
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * <p>
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package cn.stylefeng.guns.sys.modular.rest.factory;
17
18 import cn.hutool.core.bean.BeanUtil;
19 import cn.hutool.core.date.DateUtil;
20 import cn.stylefeng.guns.sys.core.constant.state.ManagerStatus;
21 import cn.stylefeng.guns.sys.modular.rest.entity.RestUser;
22 import cn.stylefeng.guns.sys.modular.system.model.UserDto;
23 import cn.stylefeng.roses.core.util.ToolUtil;
24 import org.springframework.beans.BeanUtils;
25
26 import java.util.Date;
27 import java.util.HashMap;
28 import java.util.Map;
29
30 /**
31  * 用户创建工厂
32  *
33  * @author fengshuonan
34  * @date 2017-05-05 22:43
35  */
36 public class RestUserFactory {
37
38     /**
39      * 根据请求创建实体
40      */
41     public static RestUser createRestUser(UserDto userDto, String md5Password, String salt) {
42         if (userDto == null) {
43             return null;
44         } else {
45             RestUser user = new RestUser();
46             BeanUtils.copyProperties(userDto, user);
47             user.setCreateTime(new Date());
48             user.setStatus(ManagerStatus.OK.getCode());
49             user.setPassword(md5Password);
50             user.setSalt(salt);
51             return user;
52         }
53     }
54
55     /**
56      * 更新user
57      */
58     public static RestUser editRestUser(UserDto newUser, RestUser oldUser) {
59         if (newUser == null || oldUser == null) {
60             return oldUser;
61         } else {
62             if (ToolUtil.isNotEmpty(newUser.getAvatar())) {
63                 oldUser.setAvatar(newUser.getAvatar());
64             }
65             if (ToolUtil.isNotEmpty(newUser.getName())) {
66                 oldUser.setName(newUser.getName());
67             }
68             if (ToolUtil.isNotEmpty(newUser.getBirthday())) {
69                 oldUser.setBirthday(newUser.getBirthday());
70             }
71             if (ToolUtil.isNotEmpty(newUser.getDeptId())) {
72                 oldUser.setDeptId(newUser.getDeptId());
73             }
74             if (ToolUtil.isNotEmpty(newUser.getSex())) {
75                 oldUser.setSex(newUser.getSex());
76             }
77             if (ToolUtil.isNotEmpty(newUser.getEmail())) {
78                 oldUser.setEmail(newUser.getEmail());
79             }
80             if (ToolUtil.isNotEmpty(newUser.getPhone())) {
81                 oldUser.setPhone(newUser.getPhone());
82             }
83             return oldUser;
84         }
85     }
86
87     /**
88      * 过滤不安全字段并转化为map
89      */
90     public static Map<String, Object> removeUnSafeFieldsRest(RestUser user) {
91         if (user == null) {
92             return new HashMap<>();
93         } else {
94             Map<String, Object> map = BeanUtil.beanToMap(user);
95             map.remove("password");
96             map.remove("salt");
97             map.put("birthday", DateUtil.formatDate(user.getBirthday()));
98             return map;
99         }
100     }
101
102 }