CZT
2023-09-06 71c4fa1e27f75ae4b765c95c67a3069c84dc72ba
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
package com.ld.igds.sys;
 
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
 
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.hibernate.Session;
import org.springframework.security.authentication.encoding.PasswordEncoder;
import org.springframework.stereotype.Component;
 
import com.bstek.bdf2.core.CoreHibernateDao;
import com.bstek.bdf2.core.business.IUser;
import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.bdf2.core.exception.NoneLoginException;
import com.bstek.bdf2.core.model.DefaultUser;
import com.bstek.bdf2.core.model.UserDept;
import com.bstek.bdf2.core.model.UserPosition;
import com.bstek.bdf2.core.service.IRoleService;
import com.bstek.bdf2.core.service.IUserService;
import com.bstek.bdf2.core.service.MemberType;
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Page;
 
 
 
/**
 * 用户管理
 * @author Andy
 *
 */
@Component("core.userMaintain")
@SuppressWarnings("deprecation")
public class UserMaintain extends CoreHibernateDao {
    private PasswordEncoder passwordEncoder;
 
    private IUserService userService;
    private IRoleService roleService;
 
    /**
     * 分页获取用户信息
     * 
     * @param page
     * @param criteria
     * @throws Exception
     */
    @DataProvider
    public void loadUsers(Page<DefaultUser> page) throws Exception {
        DefaultUser user = (DefaultUser) ContextHolder.getLoginUser();
        if (user == null) {
            throw new NoneLoginException("Please login first");
        }
 
        String hql = " from " + DefaultUser.class.getName()
                + " where companyId=:companyId";
        Map<String, Object> args = new HashMap<>();
        args.put("companyId", user.getCompanyId());
 
        String countHql = "select count(*) " + hql;
 
        this.pagingQuery(page, hql, countHql, args);
    }
 
    @DataResolver
    public void saveUsers(Collection<DefaultUser> users) throws Exception {
        IUser loginuser = ContextHolder.getLoginUser();
        if (loginuser == null) {
            throw new NoneLoginException("Please login first!");
        }
        String companyId = loginuser.getCompanyId();
        if (StringUtils.isNotEmpty(getFixedCompanyId())) {
            companyId = getFixedCompanyId();
        }
        Session session = this.getSessionFactory().openSession();
        try {
            for (DefaultUser user : users) {
                EntityState state = EntityUtils.getState(user);
                if (state.equals(EntityState.NEW)) {
                    String salt = String.valueOf(RandomUtils.nextInt(100));
                    String password = passwordEncoder.encodePassword(
                            user.getPassword(), salt);
                    user.setPassword(password);
                    user.setSalt(salt);
                    user.setCompanyId(companyId);
                    session.save(user);
                } else if (state.equals(EntityState.MODIFIED)) {
                    session.update(user);
                } else if (state.equals(EntityState.DELETED)) {
                    roleService.deleteRoleMemeber(user.getUsername(),
                            MemberType.User);
                    session.delete(user);
                }
            }
        } finally {
            session.flush();
            session.close();
        }
    }
 
    /**
     * 这个方法用来判断在添加新用户时用户名是否已经存在
     * 
     * @param username
     *            用户输入的用户名
     */
    @Expose
    public String userIsExists(String username) {
        String hql = "select count(*) from " + DefaultUser.class.getName()
                + " u where u.username = :username";
        Map<String, Object> parameterMap = new HashMap<String, Object>();
        parameterMap.put("username", username);
        int count = this.queryForInt(hql, parameterMap);
 
        String returnStr = null;
        if (count > 0) {
            returnStr = "此用户已存在!";
        }
        return returnStr;
    }
 
    /**
     * 重置指定用户密码
     * 
     * @param username
     * @return
     */
    @Expose
    public String resetPassword(String username) {
        Integer newPassword = (int) (Math.random() * 900000 + 100000);
        userService.changePassword(username, newPassword.toString());
        return newPassword.toString();
    }
 
    /**
     * 设置指定用户为业务管理员
     * 
     * @param username
     * @return
     */
    @Expose
    public String saveUserAdmin(String username) {
        DefaultUser user = (DefaultUser) userService
                .loadUserByUsername(username);
        if (user == null)
            return "为查询到用户,请联系管理员!!";
        Session session = this.getSessionFactory().openSession();
        session.update(user);
        session.flush();
        session.close();
        return null;
    }
 
    @Expose
    public void insertUserPosition(String username, String ids) {
        Session session = this.getSessionFactory().openSession();
        try {
            session.createQuery(
                    "delete " + UserPosition.class.getName()
                            + " u where u.username = :username")
                    .setString("username", username).executeUpdate();
 
            if (StringUtils.isNotEmpty(ids)) {
                UserPosition userPosition;
                for (String id : ids.split(",")) {
                    userPosition = new UserPosition();
                    userPosition.setId(UUID.randomUUID().toString());
                    userPosition.setPositionId(id);
                    userPosition.setUsername(username);
                    session.save(userPosition);
                }
            }
        } finally {
            session.flush();
            session.close();
        }
    }
 
    @Expose
    public void insertUserDept(String username, String ids) {
        Session session = this.getSessionFactory().openSession();
        try {
            session.createQuery(
                    "delete " + UserDept.class.getName()
                            + " u where u.username = :username")
                    .setString("username", username).executeUpdate();
 
            if (StringUtils.isNotEmpty(ids)) {
                UserDept userDept;
                for (String id : ids.split(",")) {
                    userDept = new UserDept();
                    userDept.setId(UUID.randomUUID().toString());
                    userDept.setDeptId(id);
                    userDept.setUsername(username);
                    session.save(userDept);
                }
            }
        } finally {
            session.flush();
            session.close();
        }
    }
 
}