UserGroupServiceImpl.java
5.45 KB
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
package com.objecteye.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.objecteye.entity.PageResult;
import com.objecteye.pojo.UserDetailsMsg;
import com.objecteye.pojo.UserGroup;
import com.objecteye.service.IUserGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
@Component
public class UserGroupServiceImpl implements IUserGroupService {
@Autowired
private MongoTemplate mongoTemplate;
/**
* 分页查询
*
* @param requestMap 请求参数
* @return 结果集
*/
@Override
public PageResult findByPage(Map<String, Object> requestMap) {
Integer pagevolume = (Integer) requestMap.get("pagevolume");
Integer currentpage = (Integer) requestMap.get("currentpage");
List<UserGroup> userGroups = mongoTemplate.find(Query.query(new Criteria())
.limit(pagevolume).skip((currentpage - 1) * pagevolume), UserGroup.class);
long total = mongoTemplate.count(new Query(), UserGroup.class);
total = (long) Math.ceil((double) total / pagevolume);
return new PageResult<>(total, userGroups);
}
/**
* 检查用户组是否存在
*
* @param requestMap 请求参数
* @return 结果集
*/
@Override
public JSONObject checkGroup(Map<String, Object> requestMap) {
String groupName = (String) requestMap.get("groupName");
long count = mongoTemplate.count(Query.query(Criteria.where("groupName").is(groupName)), UserGroup.class);
JSONObject resultObj = new JSONObject();
if (count > 0) {
resultObj.put("count", count);
}
return resultObj;
}
/**
* 添加用户组
*
* @param requestMap 请求参数
* @return 结果集
*/
@Override
public JSONObject addGroup(Map<String, Object> requestMap) {
UserGroup userGroup = JSON.parseObject(JSON.toJSONString(requestMap), UserGroup.class);
// 默认是根组织
String parentCode = "-1";
if (userGroup.getParentCode() != null && !"".equals(userGroup.getParentCode())) {
parentCode = userGroup.getParentCode();
}
UserGroup lastMaxCodeGroup = mongoTemplate
.findOne(new Query(Criteria.where("parentCode").is(parentCode))
.with(Sort.by(Sort.Order.desc("groupCode"))).limit(1), UserGroup.class);
if (lastMaxCodeGroup == null) {
userGroup.setGroupCode("001");
userGroup.setParentCode("-1");
} else {
String lastMaxCode = lastMaxCodeGroup.getGroupCode();
userGroup.setGroupCode(newMaxCode(lastMaxCode, 3));
}
userGroup = mongoTemplate.insert(userGroup);
JSONObject resultObj = new JSONObject();
if (userGroup.getId() != null) {
resultObj.put("id", userGroup.getId());
} else {
resultObj.put("error", "创建失败");
}
return resultObj;
}
/**
* 获取下一个同层级最大code
*
* @param lastMaxCode 历史最大code
* @param length 单个层级位数(e.g. 001是3位, 0001是4位)
* @return 下一个同层级最大code
*/
private String newMaxCode(String lastMaxCode, int length) {
String prefix = "";
String mainCode;
if (lastMaxCode.length() > length) {
prefix = lastMaxCode.substring(0, lastMaxCode.length() - length);
mainCode = lastMaxCode.substring(lastMaxCode.length() - length);
} else {
mainCode = lastMaxCode;
}
mainCode = String.format("%0" + length + "d", Integer.parseInt(mainCode) + 1);
return prefix + mainCode;
}
/**
* 更新用户组
*
* @param requestMap 请求参数
* @return 结果集
*/
@Override
public JSONObject updateGroup(Map<String, Object> requestMap) {
UserGroup userGroup = JSON.parseObject(JSON.toJSONString(requestMap), UserGroup.class);
UserGroup oldUserGroup = mongoTemplate.findOne(Query.query(Criteria.where("id").is(userGroup.getId())), UserGroup.class);
JSONObject resultObj = new JSONObject();
if (oldUserGroup == null) {
resultObj.put("error", "数据不存在");
return resultObj;
}
if (userGroup.getParentCode().equals(oldUserGroup.getParentCode())) {
oldUserGroup.setGroupLevel(userGroup.getGroupLevel());
oldUserGroup.setGroupName(userGroup.getGroupName());
mongoTemplate.save(oldUserGroup, "userGroup");
}
return resultObj;
}
/**
* 删除用户组
*
* @param requestMap 请求参数
* @return 结果集
*/
@Override
public JSONObject deleteGroup(Map<String, Object> requestMap) {
String groupId = (String) requestMap.get("groupId");
mongoTemplate.updateMulti(Query.query(Criteria.where("group").is(groupId)), Update.update("group", null), UserDetailsMsg.class);
mongoTemplate.remove(Query.query(Criteria.where("id").is(groupId)), UserGroup.class);
return new JSONObject();
}
}