Commit 738e0979 authored by renjie's avatar renjie

修改shop接口

parent 0423c2d0
......@@ -4,6 +4,7 @@ import com.edgec.browserbackend.account.dto.ResultDto;
import com.edgec.browserbackend.browser.domain.Shop;
import com.edgec.browserbackend.browser.dto.ShopDto;
import com.edgec.browserbackend.browser.dto.ShopRequestDto;
import com.edgec.browserbackend.browser.dto.ShopStringResultDto;
import com.edgec.browserbackend.browser.service.ShopService;
import com.edgec.browserbackend.common.commons.error.ClientRequestException;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -26,11 +27,12 @@ public class ShopController {
private ShopService shopService;
@RequestMapping(value = "/add", method = RequestMethod.POST)
public ResultDto addShop(Principal principal, @RequestBody Shop shop) {
public ResultDto addShop(Principal principal, @RequestBody ShopDto shopDto) {
ResultDto resultDto = new ResultDto();
try {
ShopDto shopDto = new ShopDto();
shopDto.setId(shopService.addShop(principal.getName(), shop));
ShopStringResultDto shopStringResultDto = new ShopStringResultDto();
shopStringResultDto.setId(shopService.addShop(principal.getName(), shopDto));
resultDto.setData(shopStringResultDto);
resultDto.setStatus(0);
}catch (ClientRequestException e) {
resultDto.setStatus(-1);
......@@ -48,12 +50,12 @@ public class ShopController {
}
@RequestMapping(value = "/update", method = RequestMethod.POST)
public ResultDto updateShop(Principal principal, @RequestBody Shop shop) {
public ResultDto updateShop(Principal principal, @RequestBody ShopDto shopDto) {
ResultDto resultDto = new ResultDto();
try {
ShopDto shopDto = new ShopDto();
shopDto.setId(shopService.updateShop(principal.getName(), shop));
resultDto.setData(shopDto);
ShopStringResultDto shopStringResultDto = new ShopStringResultDto();
shopStringResultDto.setId(shopService.updateShop(principal.getName(), shopDto));
resultDto.setData(shopStringResultDto);
resultDto.setStatus(0);
}catch (ClientRequestException e) {
resultDto.setStatus(-1);
......
package com.edgec.browserbackend.browser.domain;
import com.edgec.browserbackend.browser.dto.ShopDto;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
......@@ -17,9 +18,7 @@ public class Shop {
private TransferStatus transferStatus;
private String firstGroupId;
private String link;
private String shopUrl;
private String shopPlatform;
......@@ -33,21 +32,47 @@ public class Shop {
private String shopPassword;
private String shopUA;
private String shopCookie;
public Shop(){
}
//创建店铺使用
public static Shop of(ShopDto shopDto) {
Shop shop = new Shop();
if (shop.getShopUrl() != null)
shop.setShopUrl(shopDto.getShopUrl());
if (shop.getOwner() != null)
shop.setOwner(shopDto.getOwner());
if (shop.getShopPlatform() != null)
shop.setShopPlatform(shopDto.getShopPlatform());
if (shop.getShopName() != null)
shop.setShopName(shopDto.getShopName());
if (shop.getTransferStatus() != null)
shop.setTransferStatus(shopDto.getTransferStatus());
if (shop.getShopAccount() != null)
shop.setShopAccount(shopDto.getShopAccount());
if (shop.getShopPassword() != null)
shop.setShopPassword(shopDto.getShopPassword());
if (shop.getShopUA() != null)
shop.setShopUA(shopDto.getShopUA());
if (shop.getShopCookie() != null)
shop.setShopCookie(shopDto.getShopCookie());
return shop;
}
public Shop of(Shop shop) {
if (shop.getFirstGroupId() != null)
this.setFirstGroupId(shop.getFirstGroupId());
if (shop.getIp() != null)
this.setIp(shop.getIp());
if (shop.getIpId() != null)
this.setIpId(shop.getIpId());
if (shop.getIpRegion() != null)
this.setIpRegion(shop.getIpRegion());
if (shop.getLink() != null)
this.setLink(shop.getLink());
if (shop.getShopUrl() != null)
this.setShopUrl(shop.getShopUrl());
if (shop.getOwner() != null)
this.setOwner(shop.getOwner());
if (shop.getShopPlatform() != null)
......@@ -60,6 +85,10 @@ public class Shop {
this.setShopAccount(shop.getShopAccount());
if (shop.getShopPassword() != null)
this.setShopPassword(shop.getShopPassword());
if (shop.getShopUA() != null)
this.setShopUA(shop.getShopUA());
if (shop.getShopCookie() != null)
this.setShopCookie(shop.getShopCookie());
return this;
}
......@@ -71,10 +100,6 @@ public class Shop {
return id;
}
public String getFirstGroupId() {
return firstGroupId;
}
public String getIpId() {
return ipId;
}
......@@ -83,8 +108,8 @@ public class Shop {
return ipRegion;
}
public String getLink() {
return link;
public String getShopUrl() {
return shopUrl;
}
public String getOwner() {
......@@ -111,10 +136,6 @@ public class Shop {
this.id = id;
}
public void setFirstGroupId(String firstGroupId) {
this.firstGroupId = firstGroupId;
}
public void setIpId(String ipId) {
this.ipId = ipId;
}
......@@ -123,8 +144,8 @@ public class Shop {
this.ipRegion = ipRegion;
}
public void setLink(String link) {
this.link = link;
public void setShopUrl(String shopUrl) {
this.shopUrl = shopUrl;
}
public void setOwner(String owner) {
......@@ -158,4 +179,20 @@ public class Shop {
public void setShopPassword(String shopPassword) {
this.shopPassword = shopPassword;
}
public String getShopCookie() {
return shopCookie;
}
public void setShopCookie(String shopCookie) {
this.shopCookie = shopCookie;
}
public String getShopUA() {
return shopUA;
}
public void setShopUA(String shopUA) {
this.shopUA = shopUA;
}
}
......@@ -5,7 +5,7 @@ import org.springframework.data.mongodb.core.mapping.Document;
import java.util.List;
@Document(collection = "usershops")
@Document(collection = "usershop")
public class UserShop {
@Id
......@@ -18,7 +18,6 @@ public class UserShop {
private String groupId;
public UserShop(){
}
public UserShop(String username, String shopId) {
......
......@@ -18,9 +18,9 @@ public class ShopDto {
private TransferStatus transferStatus;
private String firstGroupId;
private String group;
private String link;
private String shopUrl;
private String shopPlatform;
......@@ -28,6 +28,10 @@ public class ShopDto {
private String shopPassword;
private String shopUA;
private String shopCookie;
private IpResource ipResources;
public static ShopDto of(Shop shop, IpResource ipResources) {
......@@ -39,14 +43,16 @@ public class ShopDto {
shopDto.setShopPlatform(shop.getShopPlatform());
if (shop.getShopPassword() != null)
shopDto.setShopPassword(shop.getShopPassword());
if (shop.getFirstGroupId() != null)
shopDto.setFirstGroupId(shop.getFirstGroupId());
if (shop.getTransferStatus() != null)
shopDto.setTransferStatus(shop.getTransferStatus());
if (shop.getOwner() != null)
shopDto.setOwner(shop.getOwner());
if (shop.getLink() != null)
shopDto.setLink(shop.getLink());
if (shop.getShopUrl() != null)
shopDto.setShopUrl(shop.getShopUrl());
if (shop.getShopUA() != null)
shopDto.setShopUA(shop.getShopUA());
if (shop.getShopCookie() != null)
shopDto.setShopCookie(shop.getShopCookie());
shopDto.setIpResources(ipResources);
return shopDto;
}
......@@ -59,12 +65,12 @@ public class ShopDto {
this.shopPlatform = shopPlatform;
}
public String getFirstGroupId() {
return firstGroupId;
public String getGroup() {
return group;
}
public void setFirstGroupId(String firstGroupId) {
this.firstGroupId = firstGroupId;
public void setGroup(String group) {
this.group = group;
}
public String getShopPassword() {
......@@ -115,12 +121,12 @@ public class ShopDto {
this.transferStatus = transferStatus;
}
public String getLink() {
return link;
public String getShopUrl() {
return shopUrl;
}
public void setLink(String link) {
this.link = link;
public void setShopUrl(String shopUrl) {
this.shopUrl = shopUrl;
}
public IpResource getIpResources() {
......@@ -130,4 +136,20 @@ public class ShopDto {
public void setIpResources(IpResource ipResources) {
this.ipResources = ipResources;
}
public String getShopUA() {
return shopUA;
}
public void setShopUA(String shopUA) {
this.shopUA = shopUA;
}
public String getShopCookie() {
return shopCookie;
}
public void setShopCookie(String shopCookie) {
this.shopCookie = shopCookie;
}
}
package com.edgec.browserbackend.browser.dto;
public class ShopStringResultDto {
String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
......@@ -50,7 +50,7 @@ public class ShopServiceImpl implements ShopService {
IpResourceRepository ipResourceRepository;
@Override
public String addShop(String username, Shop shop) {
public String addShop(String username, ShopDto shopDto) {
Account account = accountRepository.findByName(username);
String id = null;
if (account == null) {
......@@ -59,16 +59,22 @@ public class ShopServiceImpl implements ShopService {
if (account.getPermission() < 4) {
throw new ClientRequestException(AccountErrorCode.NOPERMISSION);
}
UserShop us = userShopRepository.findByUsernameAndShopId(username, shop.getId());
if (shop.getFirstGroupId() != null && us == null) {
UserShop userShop = new UserShop();
userShop.setShopId(shop.getId());
userShop.setGroupId(shop.getFirstGroupId());
userShopRepository.save(userShop);
UserShop us = userShopRepository.findByUsernameAndShopId(username, shopDto.getId());
Group group = groupRepository.findById(shopDto.getGroup()).orElse(null);
if (group == null) {
throw new ClientRequestException(BrowserErrorCode.GROUPNOTEXIST);
}
try {
shop.setOwner(username);
shopDto.setOwner(username);
Shop shop = Shop.of(shopDto);
id = shopRepository.save(shop).getId();
UserShop userShop = new UserShop();
userShop.setUsername(username);
userShop.setShopId(shopDto.getId());
if (shopDto.getGroup() != null && us == null) {
userShop.setGroupId(shopDto.getGroup());
}
userShopRepository.save(userShop);
}catch (Exception e) {
throw new ClientRequestException(BrowserErrorCode.INFORMATIONNOTCOMPELETE);
}
......@@ -81,24 +87,20 @@ public class ShopServiceImpl implements ShopService {
}
@Override
public String updateShop(String username, Shop shop) {
public String updateShop(String username, ShopDto shopDto) {
Account account = accountRepository.findByName(username);
if (account == null) {
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
}
UserShop userShop = userShopRepository.findByUsernameAndShopId(username, shop.getId());
UserShop userShop = userShopRepository.findByUsernameAndShopId(username, shopDto.getId());
if (account.getPermission() < 4 || userShop == null) {
throw new ClientRequestException(AccountErrorCode.NOPERMISSION);
}
if (shop.getFirstGroupId() != null) {
transferShop(username, shop.getId(), shop.getFirstGroupId());
}
Shop shop_old = shopRepository.findById(shop.getId()).orElseGet(null);
Shop shop_old = shopRepository.findById(shopDto.getId()).orElseGet(null);
if (shop_old == null)
throw new ClientRequestException(BrowserErrorCode.SHOPNOTEXIST);
try {
shop_old = shop_old.of(shop);
shop_old = shop_old.of(shopDto);
shopRepository.save(shop_old);
}catch (Exception e) {
logger.error("fail to update", e.getMessage());
......@@ -256,14 +258,21 @@ public class ShopServiceImpl implements ShopService {
if (account == null) {
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
}
Group group = groupRepository.findById(groupId).orElse(null);
Group group = null;
if (groupId != null && !groupId.equals("-1"))
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<String> shopIds = new ArrayList<>();
if (groupId.equals("-1")){
shopIds = userShopRepository.findByUsername(username).stream().map(x -> x.getShopId()).collect(Collectors.toList());
} else {
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());
......
......@@ -9,11 +9,11 @@ import java.util.List;
public interface ShopService {
String addShop(String useranme, Shop shop);
String addShop(String useranme, ShopDto shopDto);
List<String> addShops(String username, List<Shop> shops);
String updateShop(String username, Shop shop);
String updateShop(String username, ShopDto shopDto);
void deleteShop(String username, String shopId);
......
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