Commit 284e3a45 authored by xuxin's avatar xuxin

部分代码添加注释与调优

parent 4d35f9a8
...@@ -575,7 +575,7 @@ public class AccountServiceImpl implements AccountService { ...@@ -575,7 +575,7 @@ public class AccountServiceImpl implements AccountService {
if (level == -1) { if (level == -1) {
//find its parent. //find its parent.
String parent = accountRepository.findByName(name).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST)).getParent(); String parent = accountRepository.findByName(name).get().getParent();
if (StringUtils.isEmpty(parent)) if (StringUtils.isEmpty(parent))
return Arrays.asList(); return Arrays.asList();
...@@ -704,8 +704,6 @@ public class AccountServiceImpl implements AccountService { ...@@ -704,8 +704,6 @@ public class AccountServiceImpl implements AccountService {
@Override @Override
public void createSubUsers(String name, SubUsersRequestDto subUsersRequestDto) { public void createSubUsers(String name, SubUsersRequestDto subUsersRequestDto) {
Account existing = accountRepository.findByName(name).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST)); Account existing = accountRepository.findByName(name).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST));
if (existing == null)
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST, "account does not exist: " + name);
if (!existing.isAllowedToCreateSubUser() && !StringUtils.isEmpty(existing.getParent())) if (!existing.isAllowedToCreateSubUser() && !StringUtils.isEmpty(existing.getParent()))
throw new ClientRequestException(AccountErrorCode.NOTALLOWEDTOCREATESUBUSER, "Not allowed to create sub user"); throw new ClientRequestException(AccountErrorCode.NOTALLOWEDTOCREATESUBUSER, "Not allowed to create sub user");
...@@ -726,7 +724,7 @@ public class AccountServiceImpl implements AccountService { ...@@ -726,7 +724,7 @@ public class AccountServiceImpl implements AccountService {
else else
user.setName(existing.getName() + i); user.setName(existing.getName() + i);
Account child = accountRepository.findByName(user.getName()).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST)); Account child = accountRepository.findByName(user.getName()).orElse(null);
if (child != null) { if (child != null) {
time++; time++;
continue; continue;
......
...@@ -366,8 +366,6 @@ public class AdministratorServiceImpl implements AdministratorService { ...@@ -366,8 +366,6 @@ public class AdministratorServiceImpl implements AdministratorService {
@Override @Override
public void addPromotionCode(String username, String promotionCode) { public void addPromotionCode(String username, String promotionCode) {
Account account = accountRepository.findByName(username).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST)); Account account = accountRepository.findByName(username).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST));
if (account == null)
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
account.setPromotionCode(promotionCode); account.setPromotionCode(promotionCode);
accountRepository.save(account); accountRepository.save(account);
} }
...@@ -497,8 +495,6 @@ public class AdministratorServiceImpl implements AdministratorService { ...@@ -497,8 +495,6 @@ public class AdministratorServiceImpl implements AdministratorService {
@Override @Override
public void addUserWhiteList(String username, String website) { public void addUserWhiteList(String username, String website) {
Account account = accountRepository.findByName(username).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST)); Account account = accountRepository.findByName(username).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST));
if (account == null)
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
account.getWhiteList().add(website); account.getWhiteList().add(website);
accountRepository.save(account); accountRepository.save(account);
} }
......
...@@ -171,7 +171,7 @@ public class PaymentServiceImpl implements PaymentService { ...@@ -171,7 +171,7 @@ public class PaymentServiceImpl implements PaymentService {
if (chargeType == 0) { if (chargeType == 0) {
UserPrePaidBilling bill = new UserPrePaidBilling(); UserPrePaidBilling bill = new UserPrePaidBilling();
Account account = accountRepository.findByName(byTradeNo.getUsername()).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST)); Account account = accountRepository.findByName(byTradeNo.getUsername()).get();
if (account != null && account.getParent() != null) if (account != null && account.getParent() != null)
bill.setAdministrator(account.getParent()); bill.setAdministrator(account.getParent());
else else
...@@ -303,7 +303,7 @@ public class PaymentServiceImpl implements PaymentService { ...@@ -303,7 +303,7 @@ public class PaymentServiceImpl implements PaymentService {
if (chargeType == 0) { if (chargeType == 0) {
UserPrePaidBilling bill = new UserPrePaidBilling(); UserPrePaidBilling bill = new UserPrePaidBilling();
Account account = accountRepository.findByName(byTradeNo.getUsername()).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST)); Account account = accountRepository.findByName(byTradeNo.getUsername()).get();
if (account != null && account.getParent() != null) if (account != null && account.getParent() != null)
bill.setAdministrator(account.getParent()); bill.setAdministrator(account.getParent());
else else
...@@ -478,7 +478,7 @@ public class PaymentServiceImpl implements PaymentService { ...@@ -478,7 +478,7 @@ public class PaymentServiceImpl implements PaymentService {
UserBalance userBalance = userBalanceRepository.findById(username).orElse(null); UserBalance userBalance = userBalanceRepository.findById(username).orElse(null);
UserPrePaidBilling bill = new UserPrePaidBilling(); UserPrePaidBilling bill = new UserPrePaidBilling();
Account account = accountRepository.findByName(username).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST)); Account account = accountRepository.findByName(username).get();
if (account != null && account.getParent() != null) if (account != null && account.getParent() != null)
bill.setAdministrator(account.getParent()); bill.setAdministrator(account.getParent());
else else
......
...@@ -14,6 +14,7 @@ public enum BrowserErrorCode implements ErrorCode { ...@@ -14,6 +14,7 @@ public enum BrowserErrorCode implements ErrorCode {
IPNOTEXIST(BROWSER_BASE + 201, "The ip do not exist"), IPNOTEXIST(BROWSER_BASE + 201, "The ip do not exist"),
IPNOTBINDTOSHOP(BROWSER_BASE + 202, "The ip does not bind this shop."), IPNOTBINDTOSHOP(BROWSER_BASE + 202, "The ip does not bind this shop."),
USER_NOT_BIND_SHOP(BROWSER_BASE + 203, "The shop does not bind this user."),
GROUPNOTEXIST(BROWSER_BASE + 301, "The group does not exist"), GROUPNOTEXIST(BROWSER_BASE + 301, "The group does not exist"),
......
...@@ -22,6 +22,9 @@ public class GroupController { ...@@ -22,6 +22,9 @@ public class GroupController {
@Autowired @Autowired
private GroupService groupService; private GroupService groupService;
/**
* 添加分组
*/
@RequestMapping("/add") @RequestMapping("/add")
public ResultDto addGroup(Principal principal, @RequestBody GroupDto groupDto) { public ResultDto addGroup(Principal principal, @RequestBody GroupDto groupDto) {
ResultDto resultDto = new ResultDto(); ResultDto resultDto = new ResultDto();
...@@ -30,12 +33,8 @@ public class GroupController { ...@@ -30,12 +33,8 @@ public class GroupController {
groupDto1.setId(groupService.addGroup(principal.getName(), groupDto.getName())); groupDto1.setId(groupService.addGroup(principal.getName(), groupDto.getName()));
resultDto.setData(groupDto1); resultDto.setData(groupDto1);
resultDto.setStatus(0); resultDto.setStatus(0);
}catch (ClientRequestException e) { } catch (ClientRequestException e) {
resultDto.setStatus(-1); dealClientRequestException(resultDto, e);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
statusInfo.put("message", e.getMessage());
resultDto.setStatusInfo(statusInfo);
} }
return resultDto; return resultDto;
} }
...@@ -46,12 +45,8 @@ public class GroupController { ...@@ -46,12 +45,8 @@ public class GroupController {
try { try {
groupService.updateGroup(principal.getName(), group); groupService.updateGroup(principal.getName(), group);
resultDto.setStatus(0); resultDto.setStatus(0);
}catch (ClientRequestException e) { } catch (ClientRequestException e) {
resultDto.setStatus(-1); dealClientRequestException(resultDto, e);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
statusInfo.put("message", e.getMessage());
resultDto.setStatusInfo(statusInfo);
} }
return resultDto; return resultDto;
} }
...@@ -62,16 +57,15 @@ public class GroupController { ...@@ -62,16 +57,15 @@ public class GroupController {
try { try {
groupService.deleteGroup(principal.getName(), groupDto.getId()); groupService.deleteGroup(principal.getName(), groupDto.getId());
resultDto.setStatus(0); resultDto.setStatus(0);
}catch (ClientRequestException e) { } catch (ClientRequestException e) {
resultDto.setStatus(-1); dealClientRequestException(resultDto, e);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
statusInfo.put("message", e.getMessage());
resultDto.setStatusInfo(statusInfo);
} }
return resultDto; return resultDto;
} }
/**
* 分组列表
*/
@RequestMapping("/list") @RequestMapping("/list")
public ResultDto getGroupList(Principal principal) { public ResultDto getGroupList(Principal principal) {
ResultDto resultDto = new ResultDto(); ResultDto resultDto = new ResultDto();
...@@ -79,14 +73,19 @@ public class GroupController { ...@@ -79,14 +73,19 @@ public class GroupController {
List<GroupDto> groupDtos = groupService.getGroupList(principal.getName()); List<GroupDto> groupDtos = groupService.getGroupList(principal.getName());
resultDto.setData(groupDtos); resultDto.setData(groupDtos);
resultDto.setStatus(0); resultDto.setStatus(0);
}catch (ClientRequestException e) { } catch (ClientRequestException e) {
resultDto.setStatus(-1); dealClientRequestException(resultDto, e);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
statusInfo.put("message", e.getMessage());
resultDto.setStatusInfo(statusInfo);
} }
return resultDto; return resultDto;
} }
private void dealClientRequestException(ResultDto resultDto, ClientRequestException e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
statusInfo.put("message", e.getMessage());
resultDto.setStatusInfo(statusInfo);
}
} }
...@@ -138,6 +138,9 @@ public class ShopController { ...@@ -138,6 +138,9 @@ public class ShopController {
return resultDto; return resultDto;
} }
/**
* 店铺分配
*/
@RequestMapping(value = "/assign", method = RequestMethod.POST) @RequestMapping(value = "/assign", method = RequestMethod.POST)
public ResultDto assignShop(Principal principal, @RequestBody ShopRequestDto shopRequestDto) { public ResultDto assignShop(Principal principal, @RequestBody ShopRequestDto shopRequestDto) {
ResultDto resultDto = new ResultDto(); ResultDto resultDto = new ResultDto();
...@@ -150,6 +153,9 @@ public class ShopController { ...@@ -150,6 +153,9 @@ public class ShopController {
return resultDto; return resultDto;
} }
/**
* 获取带有 ip 资源信息的 shop
*/
@RequestMapping(value = "/list", method = RequestMethod.POST) @RequestMapping(value = "/list", method = RequestMethod.POST)
public ResultDto getShopList(Principal principal, @RequestBody ShopRequestDto shopRequestDto) { public ResultDto getShopList(Principal principal, @RequestBody ShopRequestDto shopRequestDto) {
logger.info("shop list params {}", JSONObject.toJSONString(shopRequestDto)); logger.info("shop list params {}", JSONObject.toJSONString(shopRequestDto));
...@@ -165,6 +171,9 @@ public class ShopController { ...@@ -165,6 +171,9 @@ public class ShopController {
return resultDto; return resultDto;
} }
/**
* 获取当前登录用户已经绑定了当前商铺的 所有子用户名
*/
@RequestMapping(value = "/subusers", method = RequestMethod.POST) @RequestMapping(value = "/subusers", method = RequestMethod.POST)
public ResultDto getShopSubUsers(Principal principal, @RequestBody ShopRequestDto shopRequestDto) { public ResultDto getShopSubUsers(Principal principal, @RequestBody ShopRequestDto shopRequestDto) {
ResultDto resultDto = new ResultDto(); ResultDto resultDto = new ResultDto();
...@@ -191,6 +200,9 @@ public class ShopController { ...@@ -191,6 +200,9 @@ public class ShopController {
return resultDto; return resultDto;
} }
/**
* 查询店铺信息
*/
@RequestMapping(value = "/query", method = RequestMethod.POST) @RequestMapping(value = "/query", method = RequestMethod.POST)
public ResultDto queryShop(Principal principal, @RequestBody ShopRequestDto shopRequestDto) { public ResultDto queryShop(Principal principal, @RequestBody ShopRequestDto shopRequestDto) {
ResultDto resultDto = new ResultDto(); ResultDto resultDto = new ResultDto();
......
...@@ -35,20 +35,26 @@ public class GroupServiceImpl implements GroupService { ...@@ -35,20 +35,26 @@ public class GroupServiceImpl implements GroupService {
@Override @Override
public String addGroup(String username, String groupName) { public String addGroup(String username, String groupName) {
if (StringUtils.isBlank(username) || StringUtils.isBlank(groupName)) // 1. 校验传参
if (StringUtils.isBlank(username) || StringUtils.isBlank(groupName)) {
throw new ClientRequestException(BrowserErrorCode.INFORMATIONNOTCOMPELETE); throw new ClientRequestException(BrowserErrorCode.INFORMATIONNOTCOMPELETE);
}
// 2. 获取account信息并校验account
Account account = accountRepository.findByName(username).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST)); Account account = accountRepository.findByName(username).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST));
if (account == null) if (account.getGroupCount() >= 100) {
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
if (account.getGroupCount() >= 100)
throw new ClientRequestException(AccountErrorCode.GROUPMAX); throw new ClientRequestException(AccountErrorCode.GROUPMAX);
}
Group group = new Group(); Group group = new Group();
group.setOwner(username); group.setOwner(username);
group.setName(groupName); group.setName(groupName);
String id; String id;
try { try {
// 3. 保存 group信息
Group group1 = groupRepository.save(group); Group group1 = groupRepository.save(group);
//可以优化
// 4. 更新 account 的 分组数
account.setGroupCount(account.getGroupCount() + 1); account.setGroupCount(account.getGroupCount() + 1);
accountRepository.save(account); accountRepository.save(account);
id = group1.getId(); id = group1.getId();
...@@ -80,8 +86,6 @@ public class GroupServiceImpl implements GroupService { ...@@ -80,8 +86,6 @@ public class GroupServiceImpl implements GroupService {
if (StringUtils.isBlank(username) || StringUtils.isBlank(groupId)) if (StringUtils.isBlank(username) || StringUtils.isBlank(groupId))
throw new ClientRequestException(BrowserErrorCode.INFORMATIONNOTCOMPELETE); throw new ClientRequestException(BrowserErrorCode.INFORMATIONNOTCOMPELETE);
Account account = accountRepository.findByName(username).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST)); Account account = accountRepository.findByName(username).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST));
if (account == null)
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
try { try {
userShopRepository.updateGroupId(groupId, null); userShopRepository.updateGroupId(groupId, null);
groupRepository.deleteById(groupId); groupRepository.deleteById(groupId);
...@@ -96,20 +100,17 @@ public class GroupServiceImpl implements GroupService { ...@@ -96,20 +100,17 @@ public class GroupServiceImpl implements GroupService {
@Override @Override
public List<GroupDto> getGroupList(String username) { public List<GroupDto> getGroupList(String username) {
if (StringUtils.isBlank(username)) if (StringUtils.isBlank(username)) {
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST); throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
List<Group> groups = groupRepository.findByOwner(username);
if (groups == null)
return new ArrayList<>();
List<GroupDto> groupDtos = new ArrayList<>();
try {
groups.stream().forEach(x -> {
groupDtos.add(new GroupDto(x.getId(), x.getName(), x.getDetails()));
});
} catch (Exception e) {
logger.error("fail to delete group", e.getMessage());
throw new ClientRequestException(BrowserErrorCode.UNKNOWN);
} }
List<GroupDto> groupDtos = new ArrayList<>();
List<Group> groups = groupRepository.findByOwner(username);
groups.forEach(
x -> groupDtos.add(new GroupDto(x.getId(), x.getName(), x.getDetails()))
);
return groupDtos; return groupDtos;
} }
} }
...@@ -157,24 +157,27 @@ public class IpResourceServiceImpl implements IpResourceService { ...@@ -157,24 +157,27 @@ public class IpResourceServiceImpl implements IpResourceService {
@Override @Override
public List<String> buyIp(String username, IpResourceRequestDto ipResourceRequestDto) throws Exception { public List<String> buyIp(String username, IpResourceRequestDto ipResourceRequestDto) throws Exception {
if (ipResourceRequestDto.getRegion() == null) {
throw new ClientRequestException(BrowserErrorCode.INFORMATIONNOTCOMPELETE);
}
if (ipResourceRequestDto.getRegionCn() == null) {
throw new ClientRequestException(BrowserErrorCode.INFORMATIONNOTCOMPELETE);
}
Account account = accountRepository.findByName(username).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST)); Account account = accountRepository.findByName(username).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST));
if (account == null) if (account.getPermission() < 4) {
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
if (account.getPermission() < 4)
throw new ClientRequestException(AccountErrorCode.NOPERMISSION); throw new ClientRequestException(AccountErrorCode.NOPERMISSION);
}
Map<String, List<String>> priceList = ipOptionsRepository.findAll().get(0).getIpPlatForm();
if (ipResourceRequestDto.getRegion() == null)
throw new ClientRequestException(BrowserErrorCode.INFORMATIONNOTCOMPELETE);
if (ipResourceRequestDto.getRegionCn() == null)
throw new ClientRequestException(BrowserErrorCode.INFORMATIONNOTCOMPELETE);
double newprice = 0; double newprice = 0;
if (!ipResourceRequestDto.getVendor().equals("own")) { if (!"own".equals(ipResourceRequestDto.getVendor())) {
Map<String, List<String>> priceList = ipOptionsRepository.findAll().get(0).getIpPlatForm();
List<String> vendorPrices = priceList.get(ipResourceRequestDto.getRegionCn()); List<String> vendorPrices = priceList.get(ipResourceRequestDto.getRegionCn());
String price = vendorPrices.stream() String price = vendorPrices.stream()
.filter(x -> Vendor.valueOf(ipResourceRequestDto.getVendor()).getValue().equals(x.substring(0, x.indexOf("-")))) .filter(x -> Vendor.valueOf(ipResourceRequestDto.getVendor()).getValue().equals(x.substring(0, x.indexOf("-"))))
.map(x -> x.substring(x.lastIndexOf("-") + 1)).collect(Collectors.joining()); .map(x -> x.substring(x.lastIndexOf("-") + 1))
newprice = ipResourceRequestDto.getUnit().equals("week") ? (Integer.valueOf(price) / 3) : Integer.valueOf(price); .collect(Collectors.joining());
newprice = "week".equals(ipResourceRequestDto.getUnit()) ? (Integer.valueOf(price) / 3) : Integer.valueOf(price);
} }
IpChargeResultDto ipChargeResultDto = accountService.preChargeByMoney(username, newprice * ipResourceRequestDto.getAmount() * ipResourceRequestDto.getPeriod()); IpChargeResultDto ipChargeResultDto = accountService.preChargeByMoney(username, newprice * ipResourceRequestDto.getAmount() * ipResourceRequestDto.getPeriod());
...@@ -182,12 +185,7 @@ public class IpResourceServiceImpl implements IpResourceService { ...@@ -182,12 +185,7 @@ public class IpResourceServiceImpl implements IpResourceService {
throw new ClientRequestException(AccountErrorCode.NOTENOUGHBALANCE); throw new ClientRequestException(AccountErrorCode.NOTENOUGHBALANCE);
} }
String password; String password = StringUtils.isNotBlank(ipResourceRequestDto.getPassword()) ? ipResourceRequestDto.getPassword() : genRandom(3, 12);
if (StringUtils.isNotBlank(ipResourceRequestDto.getPassword()))
password = ipResourceRequestDto.getPassword();
else
password = genRandom(3, 12);
List<IpResourceDto> ipResourceDtos = new ArrayList<>(); List<IpResourceDto> ipResourceDtos = new ArrayList<>();
List<String> ipIds = new ArrayList<>(); List<String> ipIds = new ArrayList<>();
...@@ -195,10 +193,11 @@ public class IpResourceServiceImpl implements IpResourceService { ...@@ -195,10 +193,11 @@ public class IpResourceServiceImpl implements IpResourceService {
IpResource ipResource = new IpResource(); IpResource ipResource = new IpResource();
ipResource.setPeriod(ipResourceRequestDto.getPeriod()); ipResource.setPeriod(ipResourceRequestDto.getPeriod());
//充6送1 //充6送1
if (ipResourceRequestDto.getUnit().equals("month") && ipResourceRequestDto.getPeriod() == 6) if (ipResourceRequestDto.getUnit().equals("month") && ipResourceRequestDto.getPeriod() == 6) {
ipResource.setPeriod(7); ipResource.setPeriod(7);
else if (ipResourceRequestDto.getUnit().equals("month") && ipResourceRequestDto.getPeriod() == 12) } else if (ipResourceRequestDto.getUnit().equals("month") && ipResourceRequestDto.getPeriod() == 12) {
ipResource.setPeriod(14); ipResource.setPeriod(14);
}
if (ipResourceRequestDto.getVendor().equals("local")) { if (ipResourceRequestDto.getVendor().equals("local")) {
ipResource.setAddr("本地Ip未使用"); ipResource.setAddr("本地Ip未使用");
...@@ -207,16 +206,23 @@ public class IpResourceServiceImpl implements IpResourceService { ...@@ -207,16 +206,23 @@ public class IpResourceServiceImpl implements IpResourceService {
ipResource.setVendorCn("本地"); ipResource.setVendorCn("本地");
ipResource.setStatus(4); ipResource.setStatus(4);
ipResource.setUsername(USERNAME); ipResource.setUsername(USERNAME);
if (ipResourceRequestDto.getUnit().equals("week"))
ipResource.setValidTime(Instant.now().atZone(ZoneOffset.UTC).plusWeeks(ipResource.getPeriod()).toInstant().toEpochMilli()); long validTime = 0;
else if (ipResourceRequestDto.getUnit().equals("week")) {
ipResource.setValidTime(Instant.now().atZone(ZoneOffset.UTC).plusMonths(ipResource.getPeriod()).toInstant().toEpochMilli()); validTime = Instant.now().atZone(ZoneOffset.UTC).plusWeeks(ipResource.getPeriod()).toInstant().toEpochMilli();
} else {
validTime = Instant.now().atZone(ZoneOffset.UTC).plusMonths(ipResource.getPeriod()).toInstant().toEpochMilli();
}
ipResource.setValidTime(validTime);
ipResource.setPort(port); ipResource.setPort(port);
} else if (ipResourceRequestDto.getVendor().equals("own")) { } else if (ipResourceRequestDto.getVendor().equals("own")) {
if (ipResourceRequestDto.getAddr() == null || ipResourceRequestDto.getAddr().size() == 0) if (ipResourceRequestDto.getAddr() == null || ipResourceRequestDto.getAddr().isEmpty()) {
throw new ClientRequestException(BrowserErrorCode.INFORMATIONNOTCOMPELETE); throw new ClientRequestException(BrowserErrorCode.INFORMATIONNOTCOMPELETE);
if (StringUtils.isBlank(ipResourceRequestDto.getAddr().get(i))) }
if (StringUtils.isBlank(ipResourceRequestDto.getAddr().get(i))) {
throw new ClientRequestException(BrowserErrorCode.INFORMATIONNOTCOMPELETE); throw new ClientRequestException(BrowserErrorCode.INFORMATIONNOTCOMPELETE);
}
ipResource.setSpecialLine(ipResourceRequestDto.isSpecialLine()); ipResource.setSpecialLine(ipResourceRequestDto.isSpecialLine());
ipResource.setAddr(ipResourceRequestDto.getAddr().get(i)); ipResource.setAddr(ipResourceRequestDto.getAddr().get(i));
ipResource.setIpType(IpType.OWN); ipResource.setIpType(IpType.OWN);
...@@ -240,26 +246,37 @@ public class IpResourceServiceImpl implements IpResourceService { ...@@ -240,26 +246,37 @@ public class IpResourceServiceImpl implements IpResourceService {
case "aws": case "aws":
ipResource.setVendorCn("亚马逊云"); ipResource.setVendorCn("亚马逊云");
break; break;
default:
break;
} }
ipResource.setStatus(6); ipResource.setStatus(6);
if (ipResourceRequestDto.getUnit().equals("week")) if (ipResourceRequestDto.getUnit().equals("week")) {
ipResource.setValidTime(Instant.now().atZone(ZoneOffset.UTC).plusWeeks(ipResource.getPeriod()).toInstant().toEpochMilli()); ipResource.setValidTime(Instant.now().atZone(ZoneOffset.UTC).plusWeeks(ipResource.getPeriod()).toInstant().toEpochMilli());
else } else {
ipResource.setValidTime(Instant.now().atZone(ZoneOffset.UTC).plusMonths(ipResource.getPeriod()).toInstant().toEpochMilli()); ipResource.setValidTime(Instant.now().atZone(ZoneOffset.UTC).plusMonths(ipResource.getPeriod()).toInstant().toEpochMilli());
}
ipResource.setUsername(USERNAME); ipResource.setUsername(USERNAME);
ipResource.setPort(port); ipResource.setPort(port);
} }
ipResource.setPurchasedTime(Instant.now().toEpochMilli()); ipResource.setPurchasedTime(Instant.now().toEpochMilli());
if (account.getParent() != null) if (account.getParent() != null) {
ipResource.setUserParent(account.getParent()); ipResource.setUserParent(account.getParent());
}
ipResource.setRegion(ipResourceRequestDto.getRegion()); ipResource.setRegion(ipResourceRequestDto.getRegion());
if ((StringUtils.isNotBlank(ipResource.getRegion()) && region.contains(ipResource.getRegion())) || (ipResourceRequestDto.getVendor().equals("own") && ipResource.isSpecialLine())) {
boolean condition1 = StringUtils.isNotBlank(ipResource.getRegion()) && region.contains(ipResource.getRegion());
boolean condition2 = ipResourceRequestDto.getVendor().equals("own") && ipResource.isSpecialLine();
if (condition1 || condition2) {
ipResource.setProxyUsername(ipResource.getAddr()); ipResource.setProxyUsername(ipResource.getAddr());
ipResource.setProxyPassword(genRandom(3, 12)); ipResource.setProxyPassword(genRandom(3, 12));
ipResource.setSpecialLine(true); ipResource.setSpecialLine(true);
ipResource.setHealthLockTimestamp(Instant.now().minusSeconds(60 * 20).toEpochMilli()); ipResource.setHealthLockTimestamp(Instant.now().minusSeconds(60 * 20).toEpochMilli());
} }
ipResource.setRegionCn(ipResourceRequestDto.getRegionCn()); ipResource.setRegionCn(ipResourceRequestDto.getRegionCn());
ipResource.setProtocol(protocol); ipResource.setProtocol(protocol);
ipResource.setPassword(password); ipResource.setPassword(password);
...@@ -268,8 +285,11 @@ public class IpResourceServiceImpl implements IpResourceService { ...@@ -268,8 +285,11 @@ public class IpResourceServiceImpl implements IpResourceService {
ipResource.setLockTimestamp(Instant.now().toEpochMilli()); ipResource.setLockTimestamp(Instant.now().toEpochMilli());
ipResource.setUnit(ipResourceRequestDto.getUnit()); ipResource.setUnit(ipResourceRequestDto.getUnit());
ipResource.setPrice(newprice); ipResource.setPrice(newprice);
IpResource ipResource1 = ipResourceRepository.save(ipResource); IpResource ipResource1 = ipResourceRepository.save(ipResource);
ipResourceDtos.add(new IpResourceDto(ipResource1, null, false)); ipResourceDtos.add(new IpResourceDto(ipResource1, null, false));
ipIds.add(ipResource1.getId()); ipIds.add(ipResource1.getId());
} }
...@@ -292,8 +312,6 @@ public class IpResourceServiceImpl implements IpResourceService { ...@@ -292,8 +312,6 @@ public class IpResourceServiceImpl implements IpResourceService {
public IpOperationResultDto renewIp(String username, IpResourceRequestDto ipResourceRequestDto) { public IpOperationResultDto renewIp(String username, IpResourceRequestDto ipResourceRequestDto) {
String URL = (profiles.equals("dev") || profiles.equals("staging")) ? TESTURL : CLOUDAMURL; String URL = (profiles.equals("dev") || profiles.equals("staging")) ? TESTURL : CLOUDAMURL;
Account account = accountRepository.findByName(username).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST)); Account account = accountRepository.findByName(username).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST));
if (account == null)
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
List<String> failedList = ipResourceRequestDto.getAddr(); List<String> failedList = ipResourceRequestDto.getAddr();
...@@ -685,8 +703,6 @@ public class IpResourceServiceImpl implements IpResourceService { ...@@ -685,8 +703,6 @@ public class IpResourceServiceImpl implements IpResourceService {
@Override @Override
public boolean queryIpExist(String username, IpResourceUpdateDto ipResourceUpdateDto) { public boolean queryIpExist(String username, IpResourceUpdateDto ipResourceUpdateDto) {
Account account = accountRepository.findByName(username).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST)); Account account = accountRepository.findByName(username).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST));
if (account == null)
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
IpResource ipResource = ipResourceRepository.findByAddrAndIsDeleted(ipResourceUpdateDto.getAddr(), false); IpResource ipResource = ipResourceRepository.findByAddrAndIsDeleted(ipResourceUpdateDto.getAddr(), false);
if (ipResource != null) { if (ipResource != null) {
return true; return true;
...@@ -697,8 +713,6 @@ public class IpResourceServiceImpl implements IpResourceService { ...@@ -697,8 +713,6 @@ public class IpResourceServiceImpl implements IpResourceService {
@Override @Override
public IpResourceDto queryIp(String username, IpResourceRequestDto ipResourceRequestDto) { public IpResourceDto queryIp(String username, IpResourceRequestDto ipResourceRequestDto) {
Account account = accountRepository.findByName(username).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST)); Account account = accountRepository.findByName(username).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST));
if (account == null)
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
IpResource ipResource = null; IpResource ipResource = null;
if (ipResourceRequestDto.getAddr() != null && ipResourceRequestDto.getAddr().size() > 0) if (ipResourceRequestDto.getAddr() != null && ipResourceRequestDto.getAddr().size() > 0)
ipResource = ipResourceRepository.findByAddrAndIsDeleted(ipResourceRequestDto.getAddr().get(0), false); ipResource = ipResourceRepository.findByAddrAndIsDeleted(ipResourceRequestDto.getAddr().get(0), false);
......
...@@ -247,7 +247,6 @@ public class ShopServiceImpl implements ShopService { ...@@ -247,7 +247,6 @@ public class ShopServiceImpl implements ShopService {
a.setShopCount(account.getShopCount() - 1); a.setShopCount(account.getShopCount() - 1);
accountRepository.save(a); accountRepository.save(a);
} }
} else { } else {
throw new ClientRequestException(AccountErrorCode.NOPERMISSION); throw new ClientRequestException(AccountErrorCode.NOPERMISSION);
} }
...@@ -255,15 +254,25 @@ public class ShopServiceImpl implements ShopService { ...@@ -255,15 +254,25 @@ public class ShopServiceImpl implements ShopService {
@Override @Override
public void transferShop(String username, String shopId, String groupId) { public void transferShop(String username, String shopId, String groupId) {
// 1. 校验当前用户是否存在账户信息
Account account = accountRepository.findByName(username).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST)); Account account = accountRepository.findByName(username).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST));
// 2. 校验 shop 是否存在
UserShop userShop = userShopRepository.findByUsernameAndShopId(username, shopId); Shop shop = shopRepository.findById(shopId).orElseThrow(() -> new ClientRequestException(BrowserErrorCode.SHOPNOTEXIST));
// 3. 校验 分组信息是否存在 以及 group 是否是当前 用户创建
Group group = groupRepository.findById(groupId).orElseThrow(() -> new ClientRequestException(BrowserErrorCode.GROUPNOTEXIST)); Group group = groupRepository.findById(groupId).orElseThrow(() -> new ClientRequestException(BrowserErrorCode.GROUPNOTEXIST));
if (userShop == null || (!group.getId().equals("-1") && group.getOwner() != null && !group.getOwner().equals(username))) { if (!group.getId().equals("-1") && group.getOwner() != null && !group.getOwner().equals(username)) {
throw new ClientRequestException(AccountErrorCode.NOPERMISSION); throw new ClientRequestException(AccountErrorCode.NOPERMISSION);
} }
Shop shop = shopRepository.findById(shopId).orElseThrow(() -> new ClientRequestException(BrowserErrorCode.SHOPNOTEXIST));
// 4. 获取 usershop 信息 并校验
UserShop userShop = userShopRepository.findByUsernameAndShopId(username, shopId);
if (userShop == null) {
throw new ClientRequestException(BrowserErrorCode.USER_NOT_BIND_SHOP);
}
try { try {
// 5.更该 店铺 分组 并保存
userShop.setGroupId(groupId); userShop.setGroupId(groupId);
userShopRepository.save(userShop); userShopRepository.save(userShop);
} catch (Exception e) { } catch (Exception e) {
...@@ -275,98 +284,115 @@ public class ShopServiceImpl implements ShopService { ...@@ -275,98 +284,115 @@ public class ShopServiceImpl implements ShopService {
@Override @Override
public void assignShops(String username, List<String> shopIds, List<String> users) { public void assignShops(String username, List<String> shopIds, List<String> users) {
// 1. 查询当前登录用户信息,并校验是否具备分配权限
Account account = accountRepository.findByName(username).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST)); Account account = accountRepository.findByName(username).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST));
if (account.getPermission() < 8) {
throw new ClientRequestException(AccountErrorCode.NOPERMISSION);
}
// 2. 校验当前用户是否绑定了对应的 shops,并获取 usershops信息
List<UserShop> userShops = userShopRepository.findByUsernameAndShopIdIn(username, shopIds); List<UserShop> userShops = userShopRepository.findByUsernameAndShopIdIn(username, shopIds);
if (account.getPermission() < 8 || userShops == null || userShops.size() < 1) { if (userShops == null || userShops.size() < 1) {
throw new ClientRequestException(AccountErrorCode.NOPERMISSION); throw new ClientRequestException(BrowserErrorCode.USER_NOT_BIND_SHOP);
} }
Pageable pageable = PageRequest.of(0, 100);
List<Shop> shops = shopRepository.findByShopIdInOrderByCreateTimeDesc(shopIds, pageable).getContent(); // 3. 查询 目标 账户是否存在,并获取账户信息。注意:目标账户只能是当前用户的子账户
if (shops == null || shops.size() < 1)
throw new ClientRequestException(BrowserErrorCode.SHOPNOTEXIST);
List<Account> accounts = accountRepository.findByNameIn(users); List<Account> accounts = accountRepository.findByNameIn(users);
if (accounts == null || accounts.size() != users.size()) if (accounts == null || accounts.size() != users.size()) {
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST); throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
}
for (Account ac : accounts) { for (Account ac : accounts) {
if (ac.getParent() == null || !ac.getParent().equals(username)) { if (ac.getParent() == null || !ac.getParent().equals(username)) {
throw new ClientRequestException(AccountErrorCode.NOPERMISSION); throw new ClientRequestException(AccountErrorCode.NOPERMISSION);
} }
} }
if (shops.size() == 1) {
try { // 4. 获取 shop 信息 并校验
Pageable pageable = PageRequest.of(0, 100);
List<Shop> shops = shopRepository.findByShopIdInOrderByCreateTimeDesc(shopIds, pageable).getContent();
if (shops == null || shops.size() < 1) {
throw new ClientRequestException(BrowserErrorCode.SHOPNOTEXIST);
}
try {
if (shops.size() == 1) {
// 5. 删除除当前用户外的所有 usershop 信息
userShopRepository.deleteByShopIdExceptOwner(shops.get(0).getShopId(), shops.get(0).getOwner()); userShopRepository.deleteByShopIdExceptOwner(shops.get(0).getShopId(), shops.get(0).getOwner());
for (Account account1 : accounts) { for (Account account1 : accounts) {
// todo 这个地方有点多余
UserShop userShop1 = userShopRepository.findByUsernameAndShopId(account1.getName(), shops.get(0).getShopId()); UserShop userShop1 = userShopRepository.findByUsernameAndShopId(account1.getName(), shops.get(0).getShopId());
if (userShop1 != null) if (userShop1 != null) {
continue; continue;
}
// 6. 封装 目标账户的 usershop信息并保存
userShop1 = new UserShop(account1.getName(), shops.get(0).getShopId(), "-1"); userShop1 = new UserShop(account1.getName(), shops.get(0).getShopId(), "-1");
userShopRepository.save(userShop1); userShopRepository.save(userShop1);
} }
} catch (Exception e) { } else {
logger.error("fail to assign", e.getMessage()); shops.forEach(
throw new ClientRequestException(BrowserErrorCode.UNKNOWN); shop -> {
for (Account account1 : accounts) {
UserShop userShop1 = userShopRepository.findByUsernameAndShopId(account1.getName(), shop.getShopId());
if (userShop1 != null) {
return;
}
userShop1 = new UserShop(account1.getName(), shop.getShopId(), "-1");
userShopRepository.save(userShop1);
}
}
);
} }
} else { } catch (Exception e) {
shops.forEach(shop -> { logger.error("fail to assign", e.getMessage());
try { throw new ClientRequestException(BrowserErrorCode.UNKNOWN);
for (Account account1 : accounts) {
UserShop userShop1 = userShopRepository.findByUsernameAndShopId(account1.getName(), shop.getShopId());
if (userShop1 != null)
continue;
userShop1 = new UserShop(account1.getName(), shop.getShopId(), "-1");
userShopRepository.save(userShop1);
}
} catch (Exception e) {
logger.error("fail to assign", e.getMessage());
throw new ClientRequestException(BrowserErrorCode.UNKNOWN);
}
});
} }
} }
@Override @Override
public ShopResultDto queryShop(String username, String shopId) { public ShopResultDto queryShop(String username, String shopId) {
// 1. 校验 账户、商铺、IP资源、usershop 信息
Account account = accountRepository.findByName(username).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST)); Account account = accountRepository.findByName(username).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST));
Shop shop = shopRepository.findById(shopId).orElseThrow(() -> new ClientRequestException(BrowserErrorCode.SHOPNOTEXIST));
IpResource ipResource = ipResourceRepository.findFirstByShopIdsIsAndIsDeleted(shopId, false);
if (ipResource == null) {
throw new ClientRequestException(BrowserErrorCode.IPNOTEXIST);
}
UserShop userShop = userShopRepository.findByUsernameAndShopId(username, shopId); UserShop userShop = userShopRepository.findByUsernameAndShopId(username, shopId);
if (userShop == null) { if (userShop == null) {
throw new ClientRequestException(AccountErrorCode.NOPERMISSION); throw new ClientRequestException(AccountErrorCode.NOPERMISSION);
} }
Shop shop = shopRepository.findById(shopId).orElse(null);
if (shop == null) { // 2. 封装 ip 资源信息
throw new ClientRequestException(BrowserErrorCode.SHOPNOTEXIST); IpResourceDto ipResourceDto = null;
}
IpResource ipResource = ipResourceRepository.findFirstByShopIdsIsAndIsDeleted(shopId, false);
if (ipResource == null)
throw new ClientRequestException(BrowserErrorCode.IPNOTEXIST);
String group = userShopRepository.findByUsernameAndShopId(username, shop.getShopId()).getGroupId();
ShopResultDto shopResultDto = null;
if (ipResource.isSpecialLine()) { if (ipResource.isSpecialLine()) {
SpecialLine specialLine = specialLineRepository.findAll().get(0); SpecialLine specialLine = specialLineRepository.findAll().get(0);
shopResultDto = ShopResultDto.of(shop, group, new IpResourceDto(ipResource, null, false, specialLine)); ipResourceDto = new IpResourceDto(ipResource, null, false, specialLine);
} else
shopResultDto = ShopResultDto.of(shop, group, new IpResourceDto(ipResource, null, false)); } else {
return shopResultDto; ipResourceDto = new IpResourceDto(ipResource, null, false);
}
// 3. 封装 ShopResultDto 信息并返回
return ShopResultDto.of(shop, userShop.getGroupId(), ipResourceDto);
} }
@Override @Override
public ShopPageResultDto getShopList(String username, String groupId, int pageNum, int amount, ShopFilterDto shopFilterDto) { public ShopPageResultDto getShopList(String username, String groupId, int pageNum, int amount, ShopFilterDto shopFilterDto) {
// 当前登录用户的账户是否存在 // 1. 校验当前登录用户的账户是否存在
Account account = accountRepository.findByName(username).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST)); Account account = accountRepository.findByName(username).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST));
// 当前查询的分组信息是否正确 // 2. 如有有分组校验当前查询的分组信息是否正确
Group group = null; Group group = null;
if (groupId != null) { if (groupId != null) {
group = groupRepository.findById(groupId).orElseThrow(() -> new ClientRequestException(BrowserErrorCode.GROUPNOTEXIST)); group = groupRepository.findById(groupId).orElseThrow(() -> new ClientRequestException(BrowserErrorCode.GROUPNOTEXIST));
if (group.getOwner() != null && !group.getOwner().equals(username)) { if (group.getOwner() != null && !group.getOwner().equals(username)) {
throw new ClientRequestException(AccountErrorCode.NOPERMISSION); throw new ClientRequestException(AccountErrorCode.NOPERMISSION);
} }
} }
// 根据 groupId 与 username 来查询 shopIds (如果当前用户是父账户,则查询结果包含子账户的shopId) // 3. 根据 groupId 与 username 来查询 shopIds (如果当前用户是父账户,则查询结果包含子账户的shopId)
List<String> allIds = null; List<String> allIds = null;
if ("-1".equals(groupId)) { if ("-1".equals(groupId)) {
allIds = userShopRepository.findByUsername(username).stream() allIds = userShopRepository.findByUsername(username).stream()
...@@ -377,6 +403,7 @@ public class ShopServiceImpl implements ShopService { ...@@ -377,6 +403,7 @@ public class ShopServiceImpl implements ShopService {
.map(UserShop::getShopId).collect(Collectors.toList()); .map(UserShop::getShopId).collect(Collectors.toList());
} }
// 4. 根据传入的过滤条件得到 shopIds
List<String> shopIds = null; List<String> shopIds = null;
if (shopFilterDto.getBindIp() == 0) { if (shopFilterDto.getBindIp() == 0) {
shopIds = allIds; shopIds = allIds;
...@@ -391,8 +418,8 @@ public class ShopServiceImpl implements ShopService { ...@@ -391,8 +418,8 @@ public class ShopServiceImpl implements ShopService {
} else { } else {
// 店铺绑定的 ip 资源被删除的 店铺 // 店铺绑定的 ip 资源被删除的 店铺
for (String id : allIds) { for (String id : allIds) {
IpResource ipResource = ipResourceRepository.findFirstByShopIdsIsAndIsDeleted(id, false); IpResource ipResource = ipResourceRepository.findFirstByShopIdsIsAndIsDeleted(id, true);
if (ipResource == null) { if (ipResource != null) {
shopIds.add(id); shopIds.add(id);
} }
} }
...@@ -400,13 +427,15 @@ public class ShopServiceImpl implements ShopService { ...@@ -400,13 +427,15 @@ public class ShopServiceImpl implements ShopService {
amount = amount > 100 ? 100 : amount; amount = amount > 100 ? 100 : amount;
Pageable pageable = PageRequest.of(pageNum, amount); Pageable pageable = PageRequest.of(pageNum, amount);
// 根据商铺ids 与 过滤条件 得到商铺 // 5. 根据过滤后的商铺ids 与 其他过滤条件 得到商铺信息并分页
Page<Shop> shops = getShopsByFilter(shopFilterDto, shopIds, pageable); Page<Shop> shops = getShopsByFilter(shopFilterDto, shopIds, pageable);
// 6. 封装 shopResultDtos 信息并返回
ShopPageResultDto<ShopResultDto> shopPageResultDto = new ShopPageResultDto<>(); ShopPageResultDto<ShopResultDto> shopPageResultDto = new ShopPageResultDto<>();
if (shops != null && shops.getNumberOfElements() >= 1) { if (shops != null && shops.getNumberOfElements() >= 1) {
List<ShopResultDto> shopResultDtos = new ArrayList<>(); List<ShopResultDto> shopResultDtos = new ArrayList<>();
shops.getContent().stream().forEach( shops.getContent().stream().forEach(
// 设置ip资源状态 并 封装 shopResultDto信息
x -> { x -> {
IpResource ipResource = ipResourceRepository.findFirstByShopIdsIsAndIsDeleted(x.getShopId(), false); IpResource ipResource = ipResourceRepository.findFirstByShopIdsIsAndIsDeleted(x.getShopId(), false);
// 如果 ip资源非空 且 addr 也非空 // 如果 ip资源非空 且 addr 也非空
...@@ -444,7 +473,7 @@ public class ShopServiceImpl implements ShopService { ...@@ -444,7 +473,7 @@ public class ShopServiceImpl implements ShopService {
ipResource.setStatus(1); ipResource.setStatus(1);
ipResourceRepository.save(ipResource); ipResourceRepository.save(ipResource);
// 4. 其他 // 4. 其他,将ip资源状态设置为 未使用
} else { } else {
if ((ipResource.getStatus() == 0 || ipResource.getStatus() == 1 || ipResource.getStatus() == 2) && ipResource.getVendor() != Vendor.local) { if ((ipResource.getStatus() == 0 || ipResource.getStatus() == 1 || ipResource.getStatus() == 2) && ipResource.getVendor() != Vendor.local) {
ipResource.setStatus(0); ipResource.setStatus(0);
...@@ -504,21 +533,25 @@ public class ShopServiceImpl implements ShopService { ...@@ -504,21 +533,25 @@ public class ShopServiceImpl implements ShopService {
@Override @Override
public List<String> getShopUsers(String username, String shopId) { public List<String> getShopUsers(String username, String shopId) {
Account account = accountRepository.findByName(username).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST)); Account account = accountRepository.findByName(username).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST));
if (account == null) if (account.getParent() != null) {
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
if (account.getParent() != null)
throw new ClientRequestException(AccountErrorCode.NOPERMISSION); throw new ClientRequestException(AccountErrorCode.NOPERMISSION);
List<String> shopUsers = userShopRepository.findByShopId(shopId).stream().map(x -> x.getUsername()).filter(x -> !x.equals(username)).collect(Collectors.toList()); }
// 获取当前登录用户绑定了当前商铺的 所有子用户名
List<String> shopUsers = userShopRepository.findByShopId(shopId).stream()
.map(x -> x.getUsername())
.filter(x -> !x.equals(username))
.collect(Collectors.toList());
return shopUsers; return shopUsers;
} }
@Override @Override
public List<String> getBatchShopUsers(String username, List<String> shopIds) { public List<String> getBatchShopUsers(String username, List<String> shopIds) {
Account account = accountRepository.findByName(username).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST)); Account account = accountRepository.findByName(username).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST));
if (account == null) if (account.getParent() != null) {
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
if (account.getParent() != null)
throw new ClientRequestException(AccountErrorCode.NOPERMISSION); throw new ClientRequestException(AccountErrorCode.NOPERMISSION);
}
List<String> shopUsers = new ArrayList<>(); List<String> shopUsers = new ArrayList<>();
if (shopIds != null && shopIds.size() > 0) { if (shopIds != null && shopIds.size() > 0) {
String maxShopId = null; String maxShopId = null;
...@@ -530,15 +563,19 @@ public class ShopServiceImpl implements ShopService { ...@@ -530,15 +563,19 @@ public class ShopServiceImpl implements ShopService {
maxShopId = shopId; maxShopId = shopId;
} }
} }
if (maxShopId == null) if (maxShopId == null) {
return new ArrayList<>(); return shopUsers;
}
List<String> users = userShopRepository.findByShopId(maxShopId).stream().map(x -> x.getUsername()).filter(x -> !x.equals(username)).collect(Collectors.toList()); List<String> users = userShopRepository.findByShopId(maxShopId).stream().map(x -> x.getUsername()).filter(x -> !x.equals(username)).collect(Collectors.toList());
for (String user : users) { for (String user : users) {
int shopCount = userShopRepository.countByUsernameAndShopIdIn(user, shopIds); int shopCount = userShopRepository.countByUsernameAndShopIdIn(user, shopIds);
if (shopCount < shopIds.size()) if (shopCount < shopIds.size()) {
continue; continue;
else } else {
shopUsers.add(user); shopUsers.add(user);
}
} }
} }
return shopUsers; return shopUsers;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment