Commit 5fc74d35 authored by renjie's avatar renjie

修改shop接口

parent 738e0979
...@@ -119,7 +119,7 @@ public class ShopController { ...@@ -119,7 +119,7 @@ public class ShopController {
public ResultDto transferShop(Principal principal, @RequestBody ShopRequestDto shopRequestDto) { public ResultDto transferShop(Principal principal, @RequestBody ShopRequestDto shopRequestDto) {
ResultDto resultDto = new ResultDto(); ResultDto resultDto = new ResultDto();
try { try {
shopService.transferShop(principal.getName(), shopRequestDto.getId(), shopRequestDto.getGroupId()); shopService.transferShop(principal.getName(), shopRequestDto.getShopId(), shopRequestDto.getToGroup());
resultDto.setStatus(0); resultDto.setStatus(0);
}catch (ClientRequestException e) { }catch (ClientRequestException e) {
resultDto.setStatus(-1); resultDto.setStatus(-1);
...@@ -135,7 +135,7 @@ public class ShopController { ...@@ -135,7 +135,7 @@ public class ShopController {
public ResultDto assignShop(Principal principal, @RequestBody ShopRequestDto shopRequestDto) { public ResultDto assignShop(Principal principal, @RequestBody ShopRequestDto shopRequestDto) {
ResultDto resultDto = new ResultDto(); ResultDto resultDto = new ResultDto();
try { try {
shopService.assignShops(principal.getName(), shopRequestDto.getShopIds(), shopRequestDto.getUsers()); shopService.assignShops(principal.getName(), shopRequestDto.getShopIds(), shopRequestDto.getToUsers());
resultDto.setStatus(0); resultDto.setStatus(0);
}catch (ClientRequestException e) { }catch (ClientRequestException e) {
resultDto.setStatus(-1); resultDto.setStatus(-1);
...@@ -151,8 +151,8 @@ public class ShopController { ...@@ -151,8 +151,8 @@ public class ShopController {
public ResultDto getShopList(Principal principal, @RequestBody ShopRequestDto shopRequestDto) { public ResultDto getShopList(Principal principal, @RequestBody ShopRequestDto shopRequestDto) {
ResultDto resultDto = new ResultDto(); ResultDto resultDto = new ResultDto();
try { try {
List<ShopDto> shopDtos = shopService.getShopList(principal.getName(), shopRequestDto.getGroupId(), shopRequestDto.getPage(), List<ShopDto> shopDtos = shopService.getShopList(principal.getName(), shopRequestDto.getGroup(), shopRequestDto.getPage(),
shopRequestDto.getAmount(), shopRequestDto.getFilterDto()); shopRequestDto.getAmount(), shopRequestDto.getFilter());
resultDto.setData(shopDtos); resultDto.setData(shopDtos);
resultDto.setStatus(0); resultDto.setStatus(0);
}catch (ClientRequestException e) { }catch (ClientRequestException e) {
......
...@@ -41,27 +41,26 @@ public class Shop { ...@@ -41,27 +41,26 @@ public class Shop {
} }
//创建店铺使用 //创建店铺使用
public static Shop of(ShopDto shopDto) { public Shop of(ShopDto shop) {
Shop shop = new Shop();
if (shop.getShopUrl() != null) if (shop.getShopUrl() != null)
shop.setShopUrl(shopDto.getShopUrl()); this.setShopUrl(shop.getShopUrl());
if (shop.getOwner() != null) if (shop.getOwner() != null)
shop.setOwner(shopDto.getOwner()); this.setOwner(shop.getOwner());
if (shop.getShopPlatform() != null) if (shop.getShopPlatform() != null)
shop.setShopPlatform(shopDto.getShopPlatform()); this.setShopPlatform(shop.getShopPlatform());
if (shop.getShopName() != null) if (shop.getShopName() != null)
shop.setShopName(shopDto.getShopName()); this.setShopName(shop.getShopName());
if (shop.getTransferStatus() != null) if (shop.getTransferStatus() != null)
shop.setTransferStatus(shopDto.getTransferStatus()); this.setTransferStatus(shop.getTransferStatus());
if (shop.getShopAccount() != null) if (shop.getShopAccount() != null)
shop.setShopAccount(shopDto.getShopAccount()); this.setShopAccount(shop.getShopAccount());
if (shop.getShopPassword() != null) if (shop.getShopPassword() != null)
shop.setShopPassword(shopDto.getShopPassword()); this.setShopPassword(shop.getShopPassword());
if (shop.getShopUA() != null) if (shop.getShopUA() != null)
shop.setShopUA(shopDto.getShopUA()); this.setShopUA(shop.getShopUA());
if (shop.getShopCookie() != null) if (shop.getShopCookie() != null)
shop.setShopCookie(shopDto.getShopCookie()); this.setShopCookie(shop.getShopCookie());
return shop; return this;
} }
public Shop of(Shop shop) { public Shop of(Shop shop) {
......
...@@ -9,25 +9,29 @@ public class ShopRequestDto { ...@@ -9,25 +9,29 @@ public class ShopRequestDto {
private String id; private String id;
private String shopId;
private String shopName; private String shopName;
private String owner; private String owner;
private String ipAddr; private String ipAddr;
private String groupId; private String group;
private String toGroup;
private List<String> shopIds; private List<String> shopIds;
private List<String> users; private List<String> toUsers;
private int type; private int type;
private int page; private int page = 0;
private int amount; private int amount = 50;
private FilterDto filterDto; private FilterDto filter;
public String getId() { public String getId() {
return id; return id;
...@@ -61,16 +65,20 @@ public class ShopRequestDto { ...@@ -61,16 +65,20 @@ public class ShopRequestDto {
this.owner = owner; this.owner = owner;
} }
public String getGroupId() { public String getGroup() {
return groupId; return group;
} }
public void setGroupId(String groupId) { public void setGroup(String group) {
this.groupId = groupId; this.group = group;
} }
public FilterDto getFilterDto() { public FilterDto getFilter() {
return filterDto; return filter;
}
public void setFilter(FilterDto filter) {
this.filter = filter;
} }
public void setAmount(int amount) { public void setAmount(int amount) {
...@@ -81,10 +89,6 @@ public class ShopRequestDto { ...@@ -81,10 +89,6 @@ public class ShopRequestDto {
return amount; return amount;
} }
public void setFilterDto(FilterDto filterDto) {
this.filterDto = filterDto;
}
public int getPage() { public int getPage() {
return page; return page;
} }
...@@ -101,19 +105,35 @@ public class ShopRequestDto { ...@@ -101,19 +105,35 @@ public class ShopRequestDto {
this.type = type; this.type = type;
} }
public List<String> getUsers() { public List<String> getShopIds() {
return users; return shopIds;
} }
public void setUsers(List<String> users) { public void setShopIds(List<String> shopId) {
this.users = users; this.shopIds = shopId;
} }
public List<String> getShopIds() { public List<String> getToUsers() {
return shopIds; return toUsers;
}
public void setToUsers(List<String> toUsers) {
this.toUsers = toUsers;
}
public String getToGroup() {
return toGroup;
}
public void setToGroup(String toGroup) {
this.toGroup = toGroup;
}
public String getShopId() {
return shopId;
} }
public void setShopIds(List<String> shopIds) { public void setShopId(String shopId) {
this.shopIds = shopIds; this.shopId = shopId;
} }
} }
...@@ -4,5 +4,5 @@ import com.edgec.browserbackend.browser.domain.IpResource; ...@@ -4,5 +4,5 @@ import com.edgec.browserbackend.browser.domain.IpResource;
import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.data.mongodb.repository.MongoRepository;
public interface IpResourceRepository extends MongoRepository<IpResource, String> { public interface IpResourceRepository extends MongoRepository<IpResource, String> {
IpResource findByIpAddrAndDeleted(String ipAddr, boolean isDeleted); IpResource findByIpAddrAndIsDeleted(String ipAddr, boolean isDeleted);
} }
package com.edgec.browserbackend.browser.repository; package com.edgec.browserbackend.browser.repository;
import com.edgec.browserbackend.browser.domain.Shop; import com.edgec.browserbackend.browser.domain.Shop;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.data.mongodb.repository.MongoRepository;
import java.util.List; import java.util.List;
public interface ShopRepository extends MongoRepository<Shop, String> { public interface ShopRepository extends MongoRepository<Shop, String> {
List<Shop> findByIdInAndIpRegionLike(List<String> shopIds, String ipRegion); Page<Shop> findByIdInAndIpRegionLike(List<String> shopIds, String ipRegion, Pageable pageable);
List<Shop> findByIdInAndShopAccountLike(List<String> shopIds, String shopAccount); Page<Shop> findByIdInAndShopAccountLike(List<String> shopIds, String shopAccount, Pageable pageable);
List<Shop> findByIdInAndShopNameLike(List<String> shopIds, String shopName); Page<Shop> findByIdInAndShopNameLike(List<String> shopIds, String shopName, Pageable pageable);
List<Shop> findByIdIn(List<String> shopIds); Page<Shop> findByIdIn(List<String> shopIds, Pageable pageable);
} }
...@@ -34,7 +34,7 @@ public class IpResourceServiceImpl implements IpResourceService { ...@@ -34,7 +34,7 @@ public class IpResourceServiceImpl implements IpResourceService {
@Override @Override
public IpResource getIpResourceByIpAddr(String ipAddr) { public IpResource getIpResourceByIpAddr(String ipAddr) {
IpResource ipResource = ipResourceRepository.findByIpAddrAndDeleted(ipAddr, false); IpResource ipResource = ipResourceRepository.findByIpAddrAndIsDeleted(ipAddr, false);
if (ipResource == null) if (ipResource == null)
throw new ClientRequestException(BrowserErrorCode.IPNOTEXIST); throw new ClientRequestException(BrowserErrorCode.IPNOTEXIST);
return ipResource; return ipResource;
......
...@@ -17,9 +17,14 @@ import com.edgec.browserbackend.browser.repository.ShopRepository; ...@@ -17,9 +17,14 @@ import com.edgec.browserbackend.browser.repository.ShopRepository;
import com.edgec.browserbackend.browser.repository.UserShopRepository; import com.edgec.browserbackend.browser.repository.UserShopRepository;
import com.edgec.browserbackend.browser.service.ShopService; import com.edgec.browserbackend.browser.service.ShopService;
import com.edgec.browserbackend.common.commons.error.ClientRequestException; import com.edgec.browserbackend.common.commons.error.ClientRequestException;
import org.apache.commons.lang3.StringUtils;
import org.omg.CORBA.UNKNOWN;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
...@@ -66,11 +71,14 @@ public class ShopServiceImpl implements ShopService { ...@@ -66,11 +71,14 @@ public class ShopServiceImpl implements ShopService {
} }
try { try {
shopDto.setOwner(username); shopDto.setOwner(username);
Shop shop = Shop.of(shopDto); Shop shop = new Shop();
shop.of(shopDto);
logger.error(shop.getShopName());
logger.error(shopDto.getShopName());
id = shopRepository.save(shop).getId(); id = shopRepository.save(shop).getId();
UserShop userShop = new UserShop(); UserShop userShop = new UserShop();
userShop.setUsername(username); userShop.setUsername(username);
userShop.setShopId(shopDto.getId()); userShop.setShopId(id);
if (shopDto.getGroup() != null && us == null) { if (shopDto.getGroup() != null && us == null) {
userShop.setGroupId(shopDto.getGroup()); userShop.setGroupId(shopDto.getGroup());
} }
...@@ -147,7 +155,7 @@ public class ShopServiceImpl implements ShopService { ...@@ -147,7 +155,7 @@ public class ShopServiceImpl implements ShopService {
if (shop.getIpId() != null && shop.getIp() != null) { if (shop.getIpId() != null && shop.getIp() != null) {
unBindShop(username, shopId, shop.getIp()); unBindShop(username, shopId, shop.getIp());
} }
IpResource ipResource = ipResourceRepository.findByIpAddrAndDeleted(ipAddr, false); IpResource ipResource = ipResourceRepository.findByIpAddrAndIsDeleted(ipAddr, false);
if (ipResource == null) if (ipResource == null)
throw new ClientRequestException(BrowserErrorCode.IPNOTEXIST); throw new ClientRequestException(BrowserErrorCode.IPNOTEXIST);
try { try {
...@@ -180,7 +188,7 @@ public class ShopServiceImpl implements ShopService { ...@@ -180,7 +188,7 @@ public class ShopServiceImpl implements ShopService {
Shop shop = shopRepository.findById(shopId).orElse(null); Shop shop = shopRepository.findById(shopId).orElse(null);
if (shop == null) if (shop == null)
throw new ClientRequestException(BrowserErrorCode.SHOPNOTEXIST); throw new ClientRequestException(BrowserErrorCode.SHOPNOTEXIST);
IpResource ipResource = ipResourceRepository.findByIpAddrAndDeleted(ipAddr, false); IpResource ipResource = ipResourceRepository.findByIpAddrAndIsDeleted(ipAddr, false);
if (ipResource == null) if (ipResource == null)
throw new ClientRequestException(BrowserErrorCode.IPNOTEXIST); throw new ClientRequestException(BrowserErrorCode.IPNOTEXIST);
if (!ipAddr.equals(shop.getIp())) if (!ipAddr.equals(shop.getIp()))
...@@ -206,7 +214,7 @@ public class ShopServiceImpl implements ShopService { ...@@ -206,7 +214,7 @@ public class ShopServiceImpl implements ShopService {
} }
UserShop userShop = userShopRepository.findByUsernameAndShopId(username, shopId); UserShop userShop = userShopRepository.findByUsernameAndShopId(username, shopId);
Group group = groupRepository.findById(groupId).orElse(null); Group group = groupRepository.findById(groupId).orElse(null);
if (account.getPermission() < 4 || (userShop == null) || !group.getOwner().equals(username)) { if (account.getPermission() < 4 || userShop == null || !group.getOwner().equals(username)) {
throw new ClientRequestException(AccountErrorCode.NOPERMISSION); throw new ClientRequestException(AccountErrorCode.NOPERMISSION);
} }
Shop shop = shopRepository.findById(shopId).orElse(null); Shop shop = shopRepository.findById(shopId).orElse(null);
...@@ -232,7 +240,8 @@ public class ShopServiceImpl implements ShopService { ...@@ -232,7 +240,8 @@ public class ShopServiceImpl implements ShopService {
if (account.getPermission() < 8 || userShops == null || userShops.size() < 1) { if (account.getPermission() < 8 || userShops == null || userShops.size() < 1) {
throw new ClientRequestException(AccountErrorCode.NOPERMISSION); throw new ClientRequestException(AccountErrorCode.NOPERMISSION);
} }
List<Shop> shops = shopRepository.findByIdIn(shopIds); Pageable pageable = PageRequest.of(0, 100);
List<Shop> shops = shopRepository.findByIdIn(shopIds, pageable).getContent();
if (shops == null || shops.size() < 1) if (shops == null || shops.size() < 1)
throw new ClientRequestException(BrowserErrorCode.SHOPNOTEXIST); throw new ClientRequestException(BrowserErrorCode.SHOPNOTEXIST);
List<Account> accounts = accountRepository.findByNameIn(users); List<Account> accounts = accountRepository.findByNameIn(users);
...@@ -248,12 +257,16 @@ public class ShopServiceImpl implements ShopService { ...@@ -248,12 +257,16 @@ public class ShopServiceImpl implements ShopService {
} }
} catch (Exception e) { } catch (Exception e) {
logger.error("fail to assign", e.getMessage()); logger.error("fail to assign", e.getMessage());
throw new ClientRequestException(BrowserErrorCode.UNKNOWN);
} }
}); });
} }
@Override @Override
public List<ShopDto> getShopList(String username, String groupId, int page, int amout, FilterDto filterDto) { public List<ShopDto> getShopList(String username, String groupId, int page, int amout, FilterDto filterDto) {
if (amout > 100)
amout = 100;
Pageable pageable = PageRequest.of(page, amout);
Account account = accountRepository.findByName(username); Account account = accountRepository.findByName(username);
if (account == null) { if (account == null) {
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST); throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
...@@ -261,9 +274,9 @@ public class ShopServiceImpl implements ShopService { ...@@ -261,9 +274,9 @@ public class ShopServiceImpl implements ShopService {
Group group = null; Group group = null;
if (groupId != null && !groupId.equals("-1")) if (groupId != null && !groupId.equals("-1"))
group = groupRepository.findById(groupId).orElse(null); group = groupRepository.findById(groupId).orElse(null);
if (group == null) if (group == null && !groupId.equals("-1"))
throw new ClientRequestException(BrowserErrorCode.GROUPNOTEXIST); throw new ClientRequestException(BrowserErrorCode.GROUPNOTEXIST);
if (account.getPermission() < 8 || !group.getOwner().equals(username)) { if (account.getPermission() < 8 || (group != null && !group.getOwner().equals(username))) {
throw new ClientRequestException(AccountErrorCode.NOPERMISSION); throw new ClientRequestException(AccountErrorCode.NOPERMISSION);
} }
List<String> shopIds = new ArrayList<>(); List<String> shopIds = new ArrayList<>();
...@@ -273,20 +286,20 @@ public class ShopServiceImpl implements ShopService { ...@@ -273,20 +286,20 @@ public class ShopServiceImpl implements ShopService {
shopIds = userShopRepository.findByUsernameAndGroupId(username, groupId).stream(). shopIds = userShopRepository.findByUsernameAndGroupId(username, groupId).stream().
map(x -> x.getShopId()).collect(Collectors.toList()); map(x -> x.getShopId()).collect(Collectors.toList());
} }
List<Shop> shops; Page<Shop> shops;
if (filterDto.getIpRegion() != null) if (filterDto != null && StringUtils.isNotBlank(filterDto.getIpRegion()))
shops = shopRepository.findByIdInAndIpRegionLike(shopIds, filterDto.getIpRegion()); shops = shopRepository.findByIdInAndIpRegionLike(shopIds, filterDto.getIpRegion(), pageable);
else if (filterDto.getShopAccount() != null) else if (filterDto != null && StringUtils.isNotBlank(filterDto.getShopAccount()))
shops = shopRepository.findByIdInAndShopAccountLike(shopIds, filterDto.getShopAccount()); shops = shopRepository.findByIdInAndShopAccountLike(shopIds, filterDto.getShopAccount(), pageable);
else if (filterDto.getShopAccount() != null) else if (filterDto != null && StringUtils.isNotBlank(filterDto.getShopName()))
shops = shopRepository.findByIdInAndShopNameLike(shopIds, filterDto.getShopName()); shops = shopRepository.findByIdInAndShopNameLike(shopIds, filterDto.getShopName(), pageable);
else else
shops = shopRepository.findByIdIn(shopIds); shops = shopRepository.findByIdIn(shopIds, pageable);
if (shops == null || shops.size() < 1) if (shops == null || shops.getNumberOfElements() < 1)
return new ArrayList<>(); return new ArrayList<>();
List<ShopDto> shopDtos = new ArrayList<>(); List<ShopDto> shopDtos = new ArrayList<>();
shops.stream().forEach(x -> { shops.stream().forEach(x -> {
IpResource ipResource = ipResourceRepository.findByIpAddrAndDeleted(x.getIp(), false); IpResource ipResource = ipResourceRepository.findByIpAddrAndIsDeleted(x.getIp(), false);
shopDtos.add(ShopDto.of(x, ipResource)); shopDtos.add(ShopDto.of(x, ipResource));
}); });
return shopDtos; return shopDtos;
......
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