Commit b79142ac authored by renjie's avatar renjie

shop

parent 569a504f
...@@ -25,4 +25,6 @@ public interface AccountRepository extends MongoRepository<Account, String>, Acc ...@@ -25,4 +25,6 @@ public interface AccountRepository extends MongoRepository<Account, String>, Acc
Page<Account> findAllBySignupDateBetween(Pageable pageable, Date startDate, Date endDate); Page<Account> findAllBySignupDateBetween(Pageable pageable, Date startDate, Date endDate);
Page<Account> findAll(Pageable pageable); Page<Account> findAll(Pageable pageable);
List<Account> findByNameIn(List<String> names);
} }
...@@ -18,7 +18,7 @@ public class IpResource { ...@@ -18,7 +18,7 @@ public class IpResource {
private long validTime; private long validTime;
private long leftTime; private long leftTime;
private String details; private String details;
List<Long> bindHistory; List<String> bindHistory;
private boolean isDeleted; private boolean isDeleted;
public String getDetails() { public String getDetails() {
...@@ -61,11 +61,11 @@ public class IpResource { ...@@ -61,11 +61,11 @@ public class IpResource {
this.ipType = ipType; this.ipType = ipType;
} }
public List<Long> getBindHistory() { public List<String> getBindHistory() {
return bindHistory; return bindHistory;
} }
public void setBindHistory(List<Long> bindHistory) { public void setBindHistory(List<String> bindHistory) {
this.bindHistory = bindHistory; this.bindHistory = bindHistory;
} }
......
...@@ -17,7 +17,7 @@ public class Shop { ...@@ -17,7 +17,7 @@ public class Shop {
private TransferStatus transferStatus; private TransferStatus transferStatus;
private String groupId; private String firstGroupId;
private String groupName; private String groupName;
...@@ -31,13 +31,17 @@ public class Shop { ...@@ -31,13 +31,17 @@ public class Shop {
private String ipId; private String ipId;
private String shopAccount;
private String shopPassword;
public Shop(){ public Shop(){
} }
public Shop of(Shop shop) { public Shop of(Shop shop) {
if (shop.getGroupId() != null) if (shop.getFirstGroupId() != null)
this.setGroupId(shop.getGroupId()); this.setFirstGroupId(shop.getFirstGroupId());
if (shop.getGroupName() != null) if (shop.getGroupName() != null)
this.setGroupName(shop.getGroupName()); this.setGroupName(shop.getGroupName());
if (shop.getIp() != null) if (shop.getIp() != null)
...@@ -56,6 +60,10 @@ public class Shop { ...@@ -56,6 +60,10 @@ public class Shop {
this.setShopName(shop.getShopName()); this.setShopName(shop.getShopName());
if (shop.getTransferStatus() != null) if (shop.getTransferStatus() != null)
this.setTransferStatus(shop.getTransferStatus()); this.setTransferStatus(shop.getTransferStatus());
if (shop.getShopAccount() != null)
this.setShopAccount(shop.getShopAccount());
if (shop.getShopPassword() != null)
this.setShopPassword(shop.getShopPassword());
return this; return this;
} }
...@@ -67,8 +75,8 @@ public class Shop { ...@@ -67,8 +75,8 @@ public class Shop {
return id; return id;
} }
public String getGroupId() { public String getFirstGroupId() {
return groupId; return firstGroupId;
} }
public String getGroupName() { public String getGroupName() {
...@@ -111,8 +119,8 @@ public class Shop { ...@@ -111,8 +119,8 @@ public class Shop {
this.id = id; this.id = id;
} }
public void setGroupId(String groupId) { public void setFirstGroupId(String firstGroupId) {
this.groupId = groupId; this.firstGroupId = firstGroupId;
} }
public void setGroupName(String groupName) { public void setGroupName(String groupName) {
...@@ -146,4 +154,20 @@ public class Shop { ...@@ -146,4 +154,20 @@ public class Shop {
public void setTransferStatus(TransferStatus transferStatus) { public void setTransferStatus(TransferStatus transferStatus) {
this.transferStatus = transferStatus; this.transferStatus = transferStatus;
} }
public String getShopAccount() {
return shopAccount;
}
public void setShopAccount(String shopAccount) {
this.shopAccount = shopAccount;
}
public String getShopPassword() {
return shopPassword;
}
public void setShopPassword(String shopPassword) {
this.shopPassword = shopPassword;
}
} }
...@@ -26,6 +26,12 @@ public class UserShop { ...@@ -26,6 +26,12 @@ public class UserShop {
this.shopId = shopId; this.shopId = shopId;
} }
public UserShop(String username, String shopId, String groupId) {
this.username = username;
this.shopId = shopId;
this.groupId = groupId;
}
public String getId() { public String getId() {
return id; return id;
} }
......
package com.edgec.browserbackend.browser.dto;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class FilterDto {
private String shopName;
private String IpRegion;
private String shopAccount;
public String getShopAccount() {
return shopAccount;
}
public void setShopAccount(String shopAccount) {
this.shopAccount = shopAccount;
}
public String getIpRegion() {
return IpRegion;
}
public void setIpRegion(String ipRegion) {
IpRegion = ipRegion;
}
public String getShopName() {
return shopName;
}
public void setShopName(String shopName) {
this.shopName = shopName;
}
}
...@@ -3,6 +3,11 @@ package com.edgec.browserbackend.browser.repository; ...@@ -3,6 +3,11 @@ package com.edgec.browserbackend.browser.repository;
import com.edgec.browserbackend.browser.domain.Shop; import com.edgec.browserbackend.browser.domain.Shop;
import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.data.mongodb.repository.MongoRepository;
public interface ShopRepository extends MongoRepository<Shop, String> { import java.util.List;
public interface ShopRepository extends MongoRepository<Shop, String> {
List<Shop> findByIdInAndIpRegionLike(List<String> shopIds, String ipRegion);
List<Shop> findByIdInAndShopAccountLike(List<String> shopIds, String shopAccount);
List<Shop> findByIdInAndShopNameLike(List<String> shopIds, String shopName);
List<Shop> findByIdIn(List<String> shopIds);
} }
...@@ -9,4 +9,5 @@ public interface UserShopRepository extends MongoRepository<UserShop, String>, U ...@@ -9,4 +9,5 @@ public interface UserShopRepository extends MongoRepository<UserShop, String>, U
List<UserShop> findByUsername(String username); List<UserShop> findByUsername(String username);
UserShop findByUsernameAndShopId(String username, String shopId); UserShop findByUsernameAndShopId(String username, String shopId);
UserShop findByShopId(String shopId); UserShop findByShopId(String shopId);
List<UserShop> findByUsernameAndGroupId(String username, String groupId);
} }
...@@ -8,22 +8,30 @@ import com.edgec.browserbackend.browser.domain.Group; ...@@ -8,22 +8,30 @@ import com.edgec.browserbackend.browser.domain.Group;
import com.edgec.browserbackend.browser.domain.IpResource; import com.edgec.browserbackend.browser.domain.IpResource;
import com.edgec.browserbackend.browser.domain.Shop; import com.edgec.browserbackend.browser.domain.Shop;
import com.edgec.browserbackend.browser.domain.UserShop; import com.edgec.browserbackend.browser.domain.UserShop;
import com.edgec.browserbackend.browser.dto.FilterDto;
import com.edgec.browserbackend.browser.repository.GroupRepository; import com.edgec.browserbackend.browser.repository.GroupRepository;
import com.edgec.browserbackend.browser.repository.IpResourceRepository; import com.edgec.browserbackend.browser.repository.IpResourceRepository;
import com.edgec.browserbackend.browser.repository.ShopRepository; 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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import javax.jws.soap.SOAPBinding;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
@Service @Service
@Transactional @Transactional
public class ShopServiceImpl implements ShopService { public class ShopServiceImpl implements ShopService {
private final Logger logger = LoggerFactory.getLogger(ShopServiceImpl.class);
@Autowired @Autowired
ShopRepository shopRepository; ShopRepository shopRepository;
...@@ -49,13 +57,13 @@ public class ShopServiceImpl implements ShopService { ...@@ -49,13 +57,13 @@ public class ShopServiceImpl implements ShopService {
if (account.getPermission() < 4) { if (account.getPermission() < 4) {
throw new ClientRequestException(AccountErrorCode.NOPERMISSION); throw new ClientRequestException(AccountErrorCode.NOPERMISSION);
} }
if (shop.getGroupId() != null) { userShopRepository.findByUsernameAndShopId(username, shop.getId());
transferShop(username, shop.getId(), shop.getGroupId()); if (shop.getFirstGroupId() != null) {
transferShop(username, shop.getId(), shop.getFirstGroupId());
} }
try { try {
shop.setOwner(username); shop.setOwner(username);
id = shopRepository.save(shop).getId(); id = shopRepository.save(shop).getId();
userShopRepository.save(new UserShop(username, id));
}catch (Exception e) { }catch (Exception e) {
throw new ClientRequestException(BrowserErrorCode.INFORMATIONNOTCOMPELETE); throw new ClientRequestException(BrowserErrorCode.INFORMATIONNOTCOMPELETE);
} }
...@@ -73,12 +81,15 @@ public class ShopServiceImpl implements ShopService { ...@@ -73,12 +81,15 @@ public class ShopServiceImpl implements ShopService {
if (account == null) { if (account == null) {
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST); throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
} }
if (account.getPermission() < 4) { UserShop userShop = userShopRepository.findByUsernameAndShopId(username, shop.getId());
if (account.getPermission() < 4 || userShop == null) {
throw new ClientRequestException(AccountErrorCode.NOPERMISSION); throw new ClientRequestException(AccountErrorCode.NOPERMISSION);
} }
if (shop.getGroupId() != null) { if (shop.getFirstGroupId() != null) {
transferShop(username, shop.getId(), shop.getGroupId()); transferShop(username, shop.getId(), shop.getFirstGroupId());
} }
Shop shop_old = shopRepository.findById(shop.getId()).orElseGet(null); Shop shop_old = shopRepository.findById(shop.getId()).orElseGet(null);
if (shop_old == null) if (shop_old == null)
throw new ClientRequestException(BrowserErrorCode.SHOPNOTEXIST); throw new ClientRequestException(BrowserErrorCode.SHOPNOTEXIST);
...@@ -86,9 +97,10 @@ public class ShopServiceImpl implements ShopService { ...@@ -86,9 +97,10 @@ public class ShopServiceImpl implements ShopService {
shop_old = shop_old.of(shop); shop_old = shop_old.of(shop);
shopRepository.save(shop_old); shopRepository.save(shop_old);
}catch (Exception e) { }catch (Exception e) {
logger.error("fail to update", e.getMessage());
throw new ClientRequestException(BrowserErrorCode.INFORMATIONNOTCOMPELETE); throw new ClientRequestException(BrowserErrorCode.INFORMATIONNOTCOMPELETE);
} }
return null; return shop_old.getId();
} }
@Override @Override
...@@ -97,7 +109,8 @@ public class ShopServiceImpl implements ShopService { ...@@ -97,7 +109,8 @@ public class ShopServiceImpl implements ShopService {
if (account == null) { if (account == null) {
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST); throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
} }
if (account.getPermission() < 4) { UserShop userShop = userShopRepository.findByUsernameAndShopId(username, shopId);
if (account.getPermission() < 4 || userShop == null) {
throw new ClientRequestException(AccountErrorCode.NOPERMISSION); throw new ClientRequestException(AccountErrorCode.NOPERMISSION);
} }
Shop shop = shopRepository.findById(shopId).orElse(null); Shop shop = shopRepository.findById(shopId).orElse(null);
...@@ -118,7 +131,8 @@ public class ShopServiceImpl implements ShopService { ...@@ -118,7 +131,8 @@ public class ShopServiceImpl implements ShopService {
if (account == null) { if (account == null) {
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST); throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
} }
if (account.getPermission() < 4) { UserShop userShop = userShopRepository.findByUsernameAndShopId(username, shopId);
if (account.getPermission() < 4 || userShop == null) {
throw new ClientRequestException(AccountErrorCode.NOPERMISSION); throw new ClientRequestException(AccountErrorCode.NOPERMISSION);
} }
Shop shop = shopRepository.findById(shopId).orElse(null); Shop shop = shopRepository.findById(shopId).orElse(null);
...@@ -130,36 +144,124 @@ public class ShopServiceImpl implements ShopService { ...@@ -130,36 +144,124 @@ public class ShopServiceImpl implements ShopService {
IpResource ipResource = ipResourceRepository.findByIpAddr(ipAddr); IpResource ipResource = ipResourceRepository.findByIpAddr(ipAddr);
if (ipResource == null) if (ipResource == null)
throw new ClientRequestException(BrowserErrorCode.IPNOTEXIST); throw new ClientRequestException(BrowserErrorCode.IPNOTEXIST);
UserShop userShop = userShopRepository.findByShopId(shopId);
if (userShop == null)
throw new ClientRequestException(AccountErrorCode.NOPERMISSION);
try { try {
shop.setIp(ipAddr); shop.setIp(ipAddr);
shop.setIpId(ipResource.getId()); shop.setIpId(ipResource.getId());
shop.setIpRegion(ipResource.getIpRegion()); shop.setIpRegion(ipResource.getIpRegion());
shopRepository.save(shop); shopRepository.save(shop);
List<String> history = ipResource.getBindHistory();
history.add(shop.getId());
ipResourceRepository.save(ipResource);
} catch (Exception e) { } catch (Exception e) {
logger.error("fail to bind shop and ip", e.getMessage());
throw new ClientRequestException(BrowserErrorCode.UNKNOWN);
} }
} }
@Override @Override
public void unBindShop(String username, String shopId) { public void unBindShop(String username, String shopId) {
Account account = accountRepository.findByName(username);
if (account == null) {
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
}
UserShop userShop = userShopRepository.findByUsernameAndShopId(username, shopId);
if (account.getPermission() < 4 || userShop == null) {
throw new ClientRequestException(AccountErrorCode.NOPERMISSION);
}
Shop shop = shopRepository.findById(shopId).orElse(null);
if (shop == null)
throw new ClientRequestException(BrowserErrorCode.SHOPNOTEXIST);
IpResource ipResource = ipResourceRepository.findByIpAddr(shop.getIp());
if (ipResource == null)
throw new ClientRequestException(BrowserErrorCode.IPNOTEXIST);
try {
shop.setIpRegion(null);
shop.setIp(null);
shop.setIpId(null);
shopRepository.save(shop);
} catch (Exception e) {
logger.error("fail to unbind", e.getMessage());
throw new ClientRequestException(BrowserErrorCode.UNKNOWN);
}
} }
@Override @Override
public void transferShop(String useranme, String shopId, String groupId) { public void transferShop(String username, String shopId, String groupId) {
Account account = accountRepository.findByName(username);
if (account == null) {
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
}
UserShop userShop = userShopRepository.findByUsernameAndShopId(username, shopId);
Group group = groupRepository.findById(groupId).orElse(null);
if (account.getPermission() < 4 || userShop == null || !group.getOwner().equals(username)) {
throw new ClientRequestException(AccountErrorCode.NOPERMISSION);
}
Shop shop = shopRepository.findById(shopId).orElse(null);
if (shop == null)
throw new ClientRequestException(BrowserErrorCode.SHOPNOTEXIST);
try {
userShop.setGroupId(groupId);
userShopRepository.save(userShop);
} catch (Exception e) {
logger.error("fail to unbind", e.getMessage());
throw new ClientRequestException(BrowserErrorCode.UNKNOWN);
}
} }
@Override @Override
public void assignShop(String username, String shopId, List<String> users) { public void assignShop(String username, String shopId, List<String> users) {
Account account = accountRepository.findByName(username);
if (account == null) {
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
}
UserShop userShop = userShopRepository.findByUsernameAndShopId(username, shopId);
if (account.getPermission() < 8 || userShop == null) {
throw new ClientRequestException(AccountErrorCode.NOPERMISSION);
}
Shop shop = shopRepository.findById(shopId).orElse(null);
if (shop == null)
throw new ClientRequestException(BrowserErrorCode.SHOPNOTEXIST);
List<Account> accounts = accountRepository.findByNameIn(users);
if (accounts != null || accounts.size() != users.size())
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
accounts.stream().forEach(x -> {
try {
UserShop userShop1 = userShopRepository.findByUsernameAndShopId(x.getName(), shopId);
if (userShop1 != null)
return;
userShopRepository.save(new UserShop(x.getName(), shopId));
} catch (Exception e) {
logger.error("fail to assign", e.getMessage());
}
});
} }
@Override @Override
public List<Shop> getShopList(String username, String groupId) { public List<Shop> getShopList(String username, String groupId, int page, int amout, FilterDto filterDto) {
return null; Account account = accountRepository.findByName(username);
if (account == null) {
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
}
Group group = groupRepository.findById(groupId).orElse(null);
if (group == null)
throw new ClientRequestException(BrowserErrorCode.GROUPNOTEXIST);
if (account.getPermission() < 8 || !group.getOwner().equals(username)) {
throw new ClientRequestException(AccountErrorCode.NOPERMISSION);
}
List<String> shopIds = userShopRepository.findByUsernameAndGroupId(username, groupId).stream().
map(x -> x.getShopId()).collect(Collectors.toList());
List<Shop> shops;
if (filterDto.getIpRegion() != null)
shops = shopRepository.findByIdInAndIpRegionLike(shopIds, filterDto.getIpRegion());
else if (filterDto.getShopAccount() != null)
shops = shopRepository.findByIdInAndShopAccountLike(shopIds, filterDto.getShopAccount());
else if (filterDto.getShopAccount() != null)
shops = shopRepository.findByIdInAndShopNameLike(shopIds, filterDto.getShopName());
else
shops = shopRepository.findByIdIn(shopIds);
if (shops == null || shops.size() < 1)
shops = new ArrayList<>();
return shops;
} }
} }
package com.edgec.browserbackend.browser.service; package com.edgec.browserbackend.browser.service;
import com.edgec.browserbackend.browser.domain.Shop; import com.edgec.browserbackend.browser.domain.Shop;
import com.edgec.browserbackend.browser.dto.FilterDto;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List; import java.util.List;
...@@ -19,9 +20,9 @@ public interface ShopService { ...@@ -19,9 +20,9 @@ public interface ShopService {
void unBindShop(String username, String shopId); void unBindShop(String username, String shopId);
void transferShop(String useranme, String shopId, String groupId); void transferShop(String username, String shopId, String groupId);
void assignShop(String username, String shopId, List<String> users); void assignShop(String username, String shopId, List<String> users);
List<Shop> getShopList(String username, String groupId); List<Shop> getShopList(String username, String groupId, int page, int amount, FilterDto filterDto);
} }
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