Commit 284e3a45 authored by xuxin's avatar xuxin

部分代码添加注释与调优

parent 4d35f9a8
......@@ -575,7 +575,7 @@ public class AccountServiceImpl implements AccountService {
if (level == -1) {
//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))
return Arrays.asList();
......@@ -704,8 +704,6 @@ public class AccountServiceImpl implements AccountService {
@Override
public void createSubUsers(String name, SubUsersRequestDto subUsersRequestDto) {
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()))
throw new ClientRequestException(AccountErrorCode.NOTALLOWEDTOCREATESUBUSER, "Not allowed to create sub user");
......@@ -726,7 +724,7 @@ public class AccountServiceImpl implements AccountService {
else
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) {
time++;
continue;
......
......@@ -366,8 +366,6 @@ public class AdministratorServiceImpl implements AdministratorService {
@Override
public void addPromotionCode(String username, String promotionCode) {
Account account = accountRepository.findByName(username).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST));
if (account == null)
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
account.setPromotionCode(promotionCode);
accountRepository.save(account);
}
......@@ -497,8 +495,6 @@ public class AdministratorServiceImpl implements AdministratorService {
@Override
public void addUserWhiteList(String username, String website) {
Account account = accountRepository.findByName(username).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST));
if (account == null)
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
account.getWhiteList().add(website);
accountRepository.save(account);
}
......
......@@ -171,7 +171,7 @@ public class PaymentServiceImpl implements PaymentService {
if (chargeType == 0) {
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)
bill.setAdministrator(account.getParent());
else
......@@ -303,7 +303,7 @@ public class PaymentServiceImpl implements PaymentService {
if (chargeType == 0) {
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)
bill.setAdministrator(account.getParent());
else
......@@ -478,7 +478,7 @@ public class PaymentServiceImpl implements PaymentService {
UserBalance userBalance = userBalanceRepository.findById(username).orElse(null);
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)
bill.setAdministrator(account.getParent());
else
......
......@@ -14,6 +14,7 @@ public enum BrowserErrorCode implements ErrorCode {
IPNOTEXIST(BROWSER_BASE + 201, "The ip do not exist"),
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"),
......
......@@ -22,6 +22,9 @@ public class GroupController {
@Autowired
private GroupService groupService;
/**
* 添加分组
*/
@RequestMapping("/add")
public ResultDto addGroup(Principal principal, @RequestBody GroupDto groupDto) {
ResultDto resultDto = new ResultDto();
......@@ -30,12 +33,8 @@ public class GroupController {
groupDto1.setId(groupService.addGroup(principal.getName(), groupDto.getName()));
resultDto.setData(groupDto1);
resultDto.setStatus(0);
}catch (ClientRequestException e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
statusInfo.put("message", e.getMessage());
resultDto.setStatusInfo(statusInfo);
} catch (ClientRequestException e) {
dealClientRequestException(resultDto, e);
}
return resultDto;
}
......@@ -46,12 +45,8 @@ public class GroupController {
try {
groupService.updateGroup(principal.getName(), group);
resultDto.setStatus(0);
}catch (ClientRequestException e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
statusInfo.put("message", e.getMessage());
resultDto.setStatusInfo(statusInfo);
} catch (ClientRequestException e) {
dealClientRequestException(resultDto, e);
}
return resultDto;
}
......@@ -62,16 +57,15 @@ public class GroupController {
try {
groupService.deleteGroup(principal.getName(), groupDto.getId());
resultDto.setStatus(0);
}catch (ClientRequestException e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
statusInfo.put("message", e.getMessage());
resultDto.setStatusInfo(statusInfo);
} catch (ClientRequestException e) {
dealClientRequestException(resultDto, e);
}
return resultDto;
}
/**
* 分组列表
*/
@RequestMapping("/list")
public ResultDto getGroupList(Principal principal) {
ResultDto resultDto = new ResultDto();
......@@ -79,14 +73,19 @@ public class GroupController {
List<GroupDto> groupDtos = groupService.getGroupList(principal.getName());
resultDto.setData(groupDtos);
resultDto.setStatus(0);
}catch (ClientRequestException e) {
} catch (ClientRequestException e) {
dealClientRequestException(resultDto, e);
}
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);
}
return resultDto;
}
}
......@@ -138,6 +138,9 @@ public class ShopController {
return resultDto;
}
/**
* 店铺分配
*/
@RequestMapping(value = "/assign", method = RequestMethod.POST)
public ResultDto assignShop(Principal principal, @RequestBody ShopRequestDto shopRequestDto) {
ResultDto resultDto = new ResultDto();
......@@ -150,6 +153,9 @@ public class ShopController {
return resultDto;
}
/**
* 获取带有 ip 资源信息的 shop
*/
@RequestMapping(value = "/list", method = RequestMethod.POST)
public ResultDto getShopList(Principal principal, @RequestBody ShopRequestDto shopRequestDto) {
logger.info("shop list params {}", JSONObject.toJSONString(shopRequestDto));
......@@ -165,6 +171,9 @@ public class ShopController {
return resultDto;
}
/**
* 获取当前登录用户已经绑定了当前商铺的 所有子用户名
*/
@RequestMapping(value = "/subusers", method = RequestMethod.POST)
public ResultDto getShopSubUsers(Principal principal, @RequestBody ShopRequestDto shopRequestDto) {
ResultDto resultDto = new ResultDto();
......@@ -191,6 +200,9 @@ public class ShopController {
return resultDto;
}
/**
* 查询店铺信息
*/
@RequestMapping(value = "/query", method = RequestMethod.POST)
public ResultDto queryShop(Principal principal, @RequestBody ShopRequestDto shopRequestDto) {
ResultDto resultDto = new ResultDto();
......
......@@ -35,20 +35,26 @@ public class GroupServiceImpl implements GroupService {
@Override
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);
}
// 2. 获取account信息并校验account
Account account = accountRepository.findByName(username).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST));
if (account == null)
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
if (account.getGroupCount() >= 100)
if (account.getGroupCount() >= 100) {
throw new ClientRequestException(AccountErrorCode.GROUPMAX);
}
Group group = new Group();
group.setOwner(username);
group.setName(groupName);
String id;
try {
// 3. 保存 group信息
Group group1 = groupRepository.save(group);
//可以优化
// 4. 更新 account 的 分组数
account.setGroupCount(account.getGroupCount() + 1);
accountRepository.save(account);
id = group1.getId();
......@@ -80,8 +86,6 @@ public class GroupServiceImpl implements GroupService {
if (StringUtils.isBlank(username) || StringUtils.isBlank(groupId))
throw new ClientRequestException(BrowserErrorCode.INFORMATIONNOTCOMPELETE);
Account account = accountRepository.findByName(username).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST));
if (account == null)
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
try {
userShopRepository.updateGroupId(groupId, null);
groupRepository.deleteById(groupId);
......@@ -96,20 +100,17 @@ public class GroupServiceImpl implements GroupService {
@Override
public List<GroupDto> getGroupList(String username) {
if (StringUtils.isBlank(username))
if (StringUtils.isBlank(username)) {
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;
}
}
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