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;
} }
} }
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