|
@@ -3,22 +3,34 @@ package cn.rankin.userservice.controller;
|
|
|
import cn.rankin.common.utils.api.model.APIResult;
|
|
|
import cn.rankin.common.utils.api.page.Page;
|
|
|
import cn.rankin.common.utils.enums.BaseOrderEnum;
|
|
|
+import cn.rankin.common.utils.util.ListUtil;
|
|
|
import cn.rankin.data.api.user.dto.WhiteUserDTO;
|
|
|
import cn.rankin.data.api.user.dto.WhiteUserSearchDTO;
|
|
|
+import cn.rankin.data.api.user.entity.Merchant;
|
|
|
+import cn.rankin.data.api.user.entity.TerminalUser;
|
|
|
import cn.rankin.data.api.user.entity.WhiteUser;
|
|
|
import cn.rankin.userservice.code.UserServiceAPICode;
|
|
|
+import cn.rankin.userservice.service.MerchantService;
|
|
|
+import cn.rankin.userservice.service.TerminalUserService;
|
|
|
import cn.rankin.userservice.service.WhiteUserService;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
-import java.util.LinkedHashMap;
|
|
|
+import java.util.*;
|
|
|
|
|
|
@RestController
|
|
|
@RequestMapping(value = "/white/user")
|
|
|
public class WhiteUserController {
|
|
|
|
|
|
@Autowired
|
|
|
+ private MerchantService merchantService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private TerminalUserService terminalUserService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
private WhiteUserService whiteUserService;
|
|
|
|
|
|
@RequestMapping(value = "/{userId}", method = RequestMethod.GET)
|
|
@@ -27,15 +39,24 @@ public class WhiteUserController {
|
|
|
if (whiteUser == null) {
|
|
|
return APIResult.error(UserServiceAPICode.NOT_EXISTS);
|
|
|
}
|
|
|
+ setUserInfo(whiteUser);
|
|
|
return APIResult.ok(whiteUser);
|
|
|
}
|
|
|
|
|
|
@RequestMapping(method = RequestMethod.POST)
|
|
|
public APIResult<WhiteUser> create(@RequestBody WhiteUserDTO whiteUserDTO) {
|
|
|
+ String userId = whiteUserDTO.getUserId();
|
|
|
+ TerminalUser terminalUser = terminalUserService.find(userId);
|
|
|
+ if (terminalUser == null) {
|
|
|
+ return APIResult.error(UserServiceAPICode.NOT_EXISTS);
|
|
|
+ }
|
|
|
+ whiteUserDTO.setMerchantId(terminalUser.getMerchantId());
|
|
|
+
|
|
|
WhiteUser whiteUser = whiteUserService.addUser(whiteUserDTO);
|
|
|
if (whiteUser != null) {
|
|
|
return APIResult.ok(whiteUser);
|
|
|
}
|
|
|
+ setUserInfo(whiteUser);
|
|
|
return APIResult.error(UserServiceAPICode.error("创建失败"));
|
|
|
}
|
|
|
|
|
@@ -45,6 +66,7 @@ public class WhiteUserController {
|
|
|
if (whiteUser != null) {
|
|
|
return APIResult.ok(whiteUser);
|
|
|
}
|
|
|
+ setUserInfo(whiteUser);
|
|
|
return APIResult.error(UserServiceAPICode.error("更新失败"));
|
|
|
}
|
|
|
|
|
@@ -58,18 +80,72 @@ public class WhiteUserController {
|
|
|
return APIResult.error(UserServiceAPICode.NOT_EXISTS);
|
|
|
}
|
|
|
|
|
|
- @RequestMapping(value = "/list", method = RequestMethod.GET)
|
|
|
+ @RequestMapping(value = "/list", method = RequestMethod.POST)
|
|
|
public APIResult<Page<WhiteUser>> search(WhiteUserSearchDTO searchDTO) {
|
|
|
WhiteUser sample = new WhiteUser();
|
|
|
String code = searchDTO.getCode();
|
|
|
if (!StringUtils.isEmpty(code)) {
|
|
|
sample.setCode("%" + code + "%");
|
|
|
}
|
|
|
+ String merchantId = searchDTO.getMerchantId();
|
|
|
+ if (!StringUtils.isEmpty(merchantId)) {
|
|
|
+ sample.setMerchantId(merchantId);
|
|
|
+ }
|
|
|
Integer pageNo = searchDTO.getPageNo();
|
|
|
Integer pageSize = searchDTO.getPageSize();
|
|
|
LinkedHashMap<String, BaseOrderEnum> sort = new LinkedHashMap<>();
|
|
|
sort.put("gmtModified", BaseOrderEnum.DESC);
|
|
|
Page<WhiteUser> page = whiteUserService.search(sample, pageNo, pageSize, sort);
|
|
|
+ List<WhiteUser> whiteUserList = page.getList();
|
|
|
+ setUserInfo(whiteUserList);
|
|
|
return APIResult.ok(page);
|
|
|
}
|
|
|
+
|
|
|
+ public void setUserInfo(WhiteUser whiteUser) {
|
|
|
+ if (whiteUser == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String userId = whiteUser.getUserId();
|
|
|
+ TerminalUser terminalUser = terminalUserService.find(userId);
|
|
|
+ Merchant merchant = merchantService.findOne(whiteUser.getMerchantId());
|
|
|
+ if (terminalUser != null) {
|
|
|
+ whiteUser.setCode(terminalUser.getCode());
|
|
|
+ }
|
|
|
+ if (merchant != null) {
|
|
|
+ whiteUser.setMerchantName(merchant.getName());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setUserInfo(List<WhiteUser> whiteUserList) {
|
|
|
+ if (CollectionUtils.isEmpty(whiteUserList)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ List<String> userIdList = new ArrayList<>();
|
|
|
+ List<String> merchantIdList = new ArrayList<>();
|
|
|
+ for (WhiteUser whiteUser : whiteUserList) {
|
|
|
+ userIdList.add(whiteUser.getUserId());
|
|
|
+ String merchantId = whiteUser.getMerchantId();
|
|
|
+ if (!merchantIdList.contains(merchantId)) {
|
|
|
+ merchantIdList.add(merchantId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ List<TerminalUser> terminalUserList = terminalUserService.findByIds(userIdList);
|
|
|
+ Map<String, TerminalUser> terminalUserMap = ListUtil.convert(terminalUserList, "id", TerminalUser.class);
|
|
|
+ Map<String, Merchant> merchantMap = merchantService.getMerchantMap(merchantIdList);
|
|
|
+
|
|
|
+ for (WhiteUser whiteUser : whiteUserList) {
|
|
|
+ String userId = whiteUser.getUserId();
|
|
|
+ TerminalUser terminalUser = terminalUserMap.get(userId);
|
|
|
+ if (terminalUser != null) {
|
|
|
+ whiteUser.setCode(terminalUser.getCode());
|
|
|
+ }
|
|
|
+ String merchantId = whiteUser.getMerchantId();
|
|
|
+ Merchant merchant = merchantMap.get(merchantId);
|
|
|
+ if (merchant != null) {
|
|
|
+ whiteUser.setMerchantName(merchant.getName());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|