CommonUtil.java
11.5 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
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
package cn.com.glsx.dj.pad.util;
import cn.com.glsx.dj.carboutique.response.LoginUserInfoResponse;
import cn.com.glsx.dj.pad.constant.RopConstants;
import cn.com.glsx.dj.pad.enums.LoginRoleEnum;
import cn.com.glsx.dj.smartcarlife.dto.ShopMerchantDirectorConfigDTO;
import cn.com.glsx.dj.smartcarlife.enums.DirectorTypeEnum;
import cn.com.glsx.dj.smartcarlife.enums.SceneTypeEnum;
import cn.com.glsx.dj.smartcarlife.remote.ShopMerchantDirectorConfigRemote;
import cn.com.glsx.framework.core.beans.rpc.RpcPagination;
import cn.com.glsx.framework.core.beans.rpc.RpcResponse;
import cn.com.glsx.framework.rop.response.RopResponse;
import com.rop.RopRequestContext;
import com.rop.session.SimpleSession;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.oreframework.redis.service.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.stream.Collectors;
@Component
@Getter
@Setter
public class CommonUtil {
private static final Logger logger = LoggerFactory.getLogger(CommonUtil.class);
private static RedisService redisService;
private static Environment env;
//无权限时商户号ID
public static final int NO_PERMISSION_MERCHANT_ID = -1;
public static final List<Integer> NO_PERMISSION_LIST = new ArrayList<>(Arrays.asList(NO_PERMISSION_MERCHANT_ID));//没有权限时的列表数据
private static ShopMerchantDirectorConfigRemote shopMerchantDirectorConfigRemote;
public static <T> T convertRequest(Class<T> clazz) {
try {
RopRequestContext content = ThreadLocalCache.getRopRequestContext();
T bean = clazz.newInstance();
BeanUtils.copyProperties(content.getRopRequest(), bean);
return bean;
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
public static <T> RopResponse<T> convertRopResponse(RpcResponse<T> rpcRes) {
return RopResponse.result(rpcRes);
}
public static <T, V> RopResponse<V> convertRopResponse(RpcResponse<T> rpcRes, ConvertHandler<T, V> handler) {
if (rpcRes.isSuccess()) {
return RopResponse.success(handler.convert(rpcRes.getResult()));
}
return RopResponse.error(rpcRes);
}
public static <T> RpcPagination<T> getRpcPagination(Class<T> conditionClass) {
RpcPagination<T> rpcPagination = new RpcPagination<>();
RopRequestContext content = ThreadLocalCache.getRopRequestContext();
rpcPagination.setPageNum(1);
rpcPagination.setPageSize(10);
if (StringUtils.isNotBlank(content.getParamValue("currentPage"))) {
rpcPagination.setPageNum(Integer.valueOf(content.getParamValue("currentPage")));
} else if (StringUtils.isNotBlank(content.getParamValue("pageNum"))) {
rpcPagination.setPageNum(Integer.valueOf(content.getParamValue("pageNum")));
}
if (StringUtils.isNotBlank(content.getParamValue("pageSize"))) {
rpcPagination.setPageSize(Integer.valueOf(content.getParamValue("pageSize")));
}
rpcPagination.setCondition(convertRequest(conditionClass));
return rpcPagination;
}
public static <T, K> RpcPagination<T> getPageResponse(RpcPagination<K> pagination) {
RpcPagination<T> resp = new RpcPagination<>();
resp.setPageSize(pagination.getPageSize());
resp.setPageNum(pagination.getPageNum());
resp.setTotal(pagination.getTotal());
resp.setPages(pagination.getPages());
resp.setStartRow(pagination.getStartRow());
resp.setEndRow(pagination.getEndRow());
resp.setList(new ArrayList<>());
return resp;
}
public static LoginUserInfoResponse getLoginUserInfo() {
RopRequestContext context = ThreadLocalCache.getRopRequestContext();
SimpleSession session = (SimpleSession) context.getSession();
if (session == null) {
return null;
}
return (LoginUserInfoResponse) session.getAttribute(RopConstants.LOGIN_SESSION);
}
public static LoginUserInfoResponse getLoginUserInfo(boolean isSubLogin) {
RopRequestContext context = ThreadLocalCache.getRopRequestContext();
SimpleSession session = (SimpleSession) context.getSession();
if (session == null) {
return null;
}
LoginUserInfoResponse loginUserInfo = (LoginUserInfoResponse) session.getAttribute(RopConstants.LOGIN_SESSION);
return isSubLogin ? loginUserInfo.getSubLoginUser() : loginUserInfo;
}
public static Integer getMerchantId() {
LoginUserInfoResponse loginUserInfo = getLoginUserInfo();
if (loginUserInfo != null) {
return loginUserInfo.getMerchantId();
}
return null;
}
public static Integer getMerchantId(boolean isSubLogin) {
LoginUserInfoResponse loginUserInfo = getLoginUserInfo(isSubLogin);
if (loginUserInfo != null) {
return loginUserInfo.getMerchantId();
}
return null;
}
public static String getAccountName() {
LoginUserInfoResponse loginUserInfo = getLoginUserInfo();
if (loginUserInfo != null) {
return loginUserInfo.getAccountName();
}
return null;
}
public static boolean isSuperOfLoginUser(boolean isSubLogin) {
LoginUserInfoResponse loginUser = getLoginUserInfo(isSubLogin);
return loginUser != null && loginUser.getIsSuper() != null && loginUser.getIsSuper();
}
public static LoginRoleEnum getLoginRole() {
return getLoginRole(false);
}
public static LoginRoleEnum getLoginRole(boolean isSubLogin) {
LoginUserInfoResponse loginUserInfo = getLoginUserInfo(isSubLogin);
if(loginUserInfo == null || loginUserInfo.getLoginRole() == null) {
return null;
}
return LoginRoleEnum.getByValue(loginUserInfo.getLoginRole());
}
public static boolean isSpecficBusinessRole(){
return isAreaSupervisionOfLoginUser() || isSaleOfLoginUser() || isFlightSupervisionOfLoginUser();
}
//当前登录用户是否是区域督导
public static boolean isAreaSupervisionOfLoginUser(){
return Objects.equals(CommonUtil.getLoginRole(),LoginRoleEnum.AREA_SUPERVISION);
}
//当前登录用户是否是广联销售
public static boolean isSaleOfLoginUser(){
return Objects.equals(CommonUtil.getLoginRole(),LoginRoleEnum.SALES);
}
//当前登录用户是否是飞行督导
public static boolean isFlightSupervisionOfLoginUser(){
return Objects.equals(CommonUtil.getLoginRole(), LoginRoleEnum.FLIGHT_SUPERVISION);
}
public static List<Integer> getSpecificBusinessRoleShopMerchantIds(){
if (isAreaSupervisionOfLoginUser()) {
return getAreaSupervisionShopMerchantIds();
}else if (isSaleOfLoginUser()) {
return getSalesShopMerchantIds();
}else if(isFlightSupervisionOfLoginUser()) {
return getFlightSupervisionShopMerchantIds();
}
return Collections.singletonList(NO_PERMISSION_MERCHANT_ID);
}
//获取区域督导角色可见的4S店商户列表
public static List<Integer> getAreaSupervisionShopMerchantIds() {
return getAreaSupervisionShopMerchantIds(CommonUtil.getLoginUserInfo());
}
//获取广联销售角色可见的4S店商户列表
public static List<Integer> getSalesShopMerchantIds() {
return getSalesShopMerchantIds(CommonUtil.getLoginUserInfo());
}
public static List<Integer> getFlightSupervisionShopMerchantIds() {
return getFlightSupervisionShopMerchantIds(CommonUtil.getLoginUserInfo());
}
public static List<Integer> getFlightSupervisionShopMerchantIds(LoginUserInfoResponse loginUserInfo) {
if (loginUserInfo == null || !Objects.equals(loginUserInfo.getLoginRole(), LoginRoleEnum.FLIGHT_SUPERVISION.getValue())) {
return NO_PERMISSION_LIST;
}
ShopMerchantDirectorConfigDTO dto = ShopMerchantDirectorConfigDTO.builder().directorUserId(loginUserInfo.getUuid()).directorTypes(Collections.singletonList(DirectorTypeEnum.FLIGHT_SUPERVISION.getValue())).build();
List<ShopMerchantDirectorConfigDTO> shopMerchantDirectorConfigs = shopMerchantDirectorConfigRemote.listWithYq(dto).getResult();
if(CollectionUtils.isNotEmpty(shopMerchantDirectorConfigs)){
return shopMerchantDirectorConfigs.stream().map(ShopMerchantDirectorConfigDTO::getMerchantId).distinct().collect(Collectors.toList());
}else{
return NO_PERMISSION_LIST;
}
}
public static List<Integer> getAreaSupervisionShopMerchantIds(LoginUserInfoResponse loginUserInfo) {
if (loginUserInfo == null) {
logger.error("getAreaSupervisionShopMerchantIds:{}", "登录用户信息为空");
return NO_PERMISSION_LIST;
}
if (!Objects.equals(loginUserInfo.getLoginRole(), LoginRoleEnum.AREA_SUPERVISION.getValue())) {
logger.error("getAreaSupervisionShopMerchantIds:{}", "当前登录用户不是区域运营");
return NO_PERMISSION_LIST;
}
ShopMerchantDirectorConfigDTO condition = ShopMerchantDirectorConfigDTO.builder()
.directorUserId(loginUserInfo.getUuid()).build();
if(Objects.equals(loginUserInfo.getSceneType(), SceneTypeEnum.SCENE1_1.getValue())){
condition.setDirectorType(DirectorTypeEnum.AREA_SUPERVISION_1_1.getValue());
}else if(Objects.equals(loginUserInfo.getSceneType(),SceneTypeEnum.SCENE1_0.getValue())){
condition.setDirectorType(DirectorTypeEnum.AREA_SUPERVISION_2_1.getValue());
}else {
return NO_PERMISSION_LIST;
}
List<ShopMerchantDirectorConfigDTO> list = shopMerchantDirectorConfigRemote.list(condition).getResult();
if(CollectionUtils.isNotEmpty(list)){
return list.stream().map(ShopMerchantDirectorConfigDTO::getMerchantId).distinct().collect(Collectors.toList());
}else{
logger.error("获取督导4S店商户权限失败,无关联门店");
return NO_PERMISSION_LIST;
}
}
public static List<Integer> getSalesShopMerchantIds(LoginUserInfoResponse loginUserInfo) {
if (loginUserInfo == null) {
logger.error("getSalesShopMerchantIds:{}", "登录用户信息为空");
return NO_PERMISSION_LIST;
}
if (!Objects.equals(loginUserInfo.getLoginRole(), LoginRoleEnum.SALES.getValue())) {
logger.error("getSalesShopMerchantIds:{}", "当前登录用户不是广联销售");
return NO_PERMISSION_LIST;
}
ShopMerchantDirectorConfigDTO dto = ShopMerchantDirectorConfigDTO.builder().directorUserId(loginUserInfo.getUuid())
.directorTypes(Arrays.asList(DirectorTypeEnum.BOUTIQUE_DIRECTOR.getValue(), DirectorTypeEnum.QUOTATION_DIRECTOR.getValue())).build();
RpcResponse<List<ShopMerchantDirectorConfigDTO>> res = shopMerchantDirectorConfigRemote.listWithYq(dto);
if(res.hasResult()){
return res.getResult().stream().map(ShopMerchantDirectorConfigDTO::getMerchantId).distinct().collect(Collectors.toList());
}else{
logger.error("获取广联销售4s店商户权限失败,msg:{}", res.getMessage());
return NO_PERMISSION_LIST;
}
}
}