1 package gboat2.web.service.impl;
2
3 import gboat2.web.Constants;
4 import gboat2.web.business.IGroupBusiness;
5 import gboat2.web.business.IRoleBusiness;
6 import gboat2.web.business.IUserBusiness;
7 import gboat2.web.model.Group;
8 import gboat2.web.model.Profile;
9 import gboat2.web.model.Role;
10 import gboat2.web.model.User;
11 import gboat2.web.model.UserGroupOrganMapper;
12 import gboat2.web.model.UserRoleGroupOrganVO;
13 import gboat2.web.service.IUserService;
14 import gboat2.web.service.UserException;
15
16 import java.util.List;
17
18 import org.apache.commons.lang3.StringUtils;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import org.springframework.beans.factory.annotation.Autowired;
22
23
24
25
26
27
28 public class UserServiceImpl implements IUserService {
29 private static Logger logger = LoggerFactory.getLogger(IUserService.class);
30
31 @Autowired
32 private IUserBusiness userService;
33 @Autowired
34 private IRoleBusiness roleService;
35 @Autowired
36 private IGroupBusiness groupService;
37
38 @Override
39 public void addUser(User user, String roleCode, String groupCode, String organId, String organName)
40 throws UserException{
41 if(StringUtils.isEmpty(user.getLoginId())){
42 throw new UserException("loginId cannot be null");
43 }
44 User currentUser = userService.getUserByLoginId(user.getLoginId());
45 if(currentUser==null){
46
47 userService.save(user);
48
49 Profile profile = new Profile();
50 profile.setUserId(user.getUserId());
51 profile.setTheme(Constants.DEFAULT_THEME);
52 profile.setWallPaper(Constants.DEFAULT_THEME);
53 profile.setStretch("0");
54 userService.save(profile);
55 }else{
56 userService.update(user);
57 logger.info("user["+user.getLoginId()+"] is added before,update......");
58 }
59
60
61 Role role = roleService.findRoleByRoleCode(roleCode);
62 if(role==null){
63 throw new UserException("role["+roleCode+"] is invalid.");
64 }
65 Group group = groupService.findGroupByGroupCode(groupCode);
66 if(group==null){
67 throw new UserException("group["+groupCode+"] is invalid.");
68 }
69 UserGroupOrganMapper urgo = userService.getURGOMapper(user.getUserId(), role.getRoleId(), group.getGroupId(), organId);
70 if(urgo==null){
71 UserGroupOrganMapper mapper = new UserGroupOrganMapper();
72 mapper.setUserId(user.getUserId());
73 mapper.setGroupId(group.getGroupId());
74 mapper.setRoleId(role.getRoleId());
75 mapper.setOrganId(organId);
76 mapper.setOrganName(organName);
77 userService.save(mapper);
78 logger.info("add user["+user.getLoginId()+","+roleCode+","+groupCode+"]");
79 }else{
80 if(!urgo.getOrganName().equals(organName)){
81 urgo.setOrganName(organName);
82 userService.update(urgo);
83 }
84 }
85 }
86
87 @Override
88 public String addUserAndReturnUserId(User user, String roleCode, String groupCode, String organId, String organName)
89 throws UserException{
90 this.addUser(user, roleCode, groupCode, organId, organName);
91 return user.getUserId();
92 }
93
94
95 @Override
96 public void updateUser(String loginId, User user) throws UserException {
97 User currentUser = userService.getUserByLoginId(loginId);
98 if(currentUser==null){
99 throw new UserException("user "+loginId+" isn't exist.");
100 }else{
101 currentUser.setLoginId(user.getLoginId());
102 currentUser.setUserNameEn(user.getUserNameEn());
103 currentUser.setUserNameZh(user.getUserNameZh());
104 userService.update(currentUser);
105 }
106 }
107
108 @Override
109 public void updatePassword(String loginId, String newPwd) throws UserException {
110 User currentUser = userService.getUserByLoginId(loginId);
111 if(currentUser==null){
112 throw new UserException("user "+loginId+" isn't exist.");
113 }else{
114 currentUser.setPassword(newPwd);
115 userService.update(currentUser);
116 }
117 }
118
119
120
121
122
123 @Override
124 public void updateUser(User user){
125 if(user==null || StringUtils.isEmpty(user.getUserId())){
126 logger.info("user isn't exist.");
127 return;
128 }
129 userService.update(user);
130 }
131
132 @Override
133 public void updateLoginIdAndUserNameZH(String loginId,String enterpriseId) throws UserException {
134 User currentUser = userService.getUserByEnterpriseId(enterpriseId);
135 if(currentUser==null){
136 throw new UserException("user "+enterpriseId+" isn't exist.");
137 }else{
138 currentUser.setLoginId(loginId);
139 currentUser.setUserNameZh(loginId);
140 userService.update(currentUser);
141 }
142 this.updateOrganNameOfUserGroupOrganMapper(loginId, enterpriseId);
143 }
144
145 @Override
146 public User getUserByLoginId(String loginId) {
147 return userService.getUserByLoginId(loginId);
148 }
149
150 @Override
151 public User getUserByUserId(String userId) {
152 return userService.getUserByUserId(userId);
153 }
154
155 @Override
156 public List<UserGroupOrganMapper> getURGOMapperByUserId(String userId) {
157 return userService.getURGOMapperByUserId(userId);
158 }
159
160 @Override
161 public List<UserGroupOrganMapper> getURGOMapperByOrganId(String organId) {
162 return userService.getURGOMapperByOrganId(organId);
163 }
164
165 @Override
166 public List<UserRoleGroupOrganVO> getMapperVOByUserId(String userId) {
167 return userService.getMapperVOByUserId(userId);
168 }
169
170 @Override
171 public UserGroupOrganMapper getUserGroupOrganMapperById(String mapperId) {
172 return userService.getUserGroupOrganMapperById(mapperId);
173 }
174
175
176
177
178
179
180
181 private void updateOrganNameOfUserGroupOrganMapper(String loginId,String enterpriseId) throws UserException {
182 List<UserGroupOrganMapper> userGroupOrganList = ( List<UserGroupOrganMapper>) userService.getURGOMapperByOrganId(enterpriseId);
183 UserGroupOrganMapper userGroupOrgan;
184 if(userGroupOrganList == null || userGroupOrganList.size()!=1){
185 throw new UserException("userGroupOrgan isn't exist.");
186 }else{
187 userGroupOrgan = userGroupOrganList.get(0);
188 userGroupOrgan.setOrganName(loginId);
189 userService.update(userGroupOrgan);
190 }
191 }
192
193 @Override
194 public void deleteURGOMapperBy(String userId, String roleId, String groupId, String organId) {
195 userService.deleteURGOMapperBy(userId, roleId, groupId, organId);
196 }
197
198 @Override
199 public void deleteURGOMapperByUserId(String userId) {
200 userService.deleteURGOMapperByUserId(userId);
201 }
202
203 @Override
204 public void deleteUserRoleGroupOrganByUserId(String userId) {
205 User user = userService.getUserByUserId(userId);
206 userService.delete(user);
207 userService.deleteURGOMapperByUserId(userId);
208 }
209
210 @Override
211 public void deleteUserByOrganId(String organId){
212 userService.deleteUserByOrganId(organId);
213 }
214
215 }