| 22980 |
ashik.ali |
1 |
package com.spice.profitmandi.service.user;
|
|
|
2 |
|
|
|
3 |
import java.util.HashMap;
|
|
|
4 |
import java.util.List;
|
|
|
5 |
import java.util.Map;
|
|
|
6 |
import java.util.Set;
|
|
|
7 |
import java.util.function.Function;
|
|
|
8 |
import java.util.stream.Collectors;
|
|
|
9 |
|
|
|
10 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
11 |
import org.springframework.stereotype.Component;
|
|
|
12 |
|
|
|
13 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
14 |
import com.spice.profitmandi.dao.entity.dtr.Retailer;
|
|
|
15 |
import com.spice.profitmandi.dao.entity.dtr.Shop;
|
|
|
16 |
import com.spice.profitmandi.dao.entity.dtr.ShopAddress;
|
|
|
17 |
import com.spice.profitmandi.dao.entity.dtr.User;
|
|
|
18 |
import com.spice.profitmandi.dao.entity.dtr.UserRole;
|
|
|
19 |
import com.spice.profitmandi.dao.entity.user.Address;
|
|
|
20 |
import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;
|
|
|
21 |
import com.spice.profitmandi.dao.repository.dtr.RetailerRegisteredAddressRepository;
|
|
|
22 |
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
|
|
|
23 |
import com.spice.profitmandi.dao.repository.dtr.ShopAddressRepository;
|
|
|
24 |
import com.spice.profitmandi.dao.repository.dtr.ShopRepository;
|
|
|
25 |
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
|
|
|
26 |
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
|
|
|
27 |
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
|
|
|
28 |
import com.spice.profitmandi.dao.repository.user.AddressRepository;
|
|
|
29 |
|
|
|
30 |
@Component
|
|
|
31 |
public class RetailerServiceImpl implements RetailerService {
|
|
|
32 |
|
|
|
33 |
@Autowired
|
|
|
34 |
private RetailerRepository retailerRepository;
|
|
|
35 |
|
|
|
36 |
@Autowired
|
|
|
37 |
private UserAccountRepository userAccountRepository;
|
|
|
38 |
|
|
|
39 |
@Autowired
|
|
|
40 |
private UserRepository userRepository;
|
|
|
41 |
|
|
|
42 |
@Autowired
|
|
|
43 |
private RetailerRegisteredAddressRepository retailerRegisteredAddressRepository;
|
|
|
44 |
|
|
|
45 |
@Autowired
|
|
|
46 |
private AddressRepository addressRepository;
|
|
|
47 |
|
|
|
48 |
@Autowired
|
|
|
49 |
private ShopRepository shopRepository;
|
|
|
50 |
|
|
|
51 |
@Autowired
|
|
|
52 |
private ShopAddressRepository shopAddressRepository;
|
|
|
53 |
|
|
|
54 |
@Autowired
|
|
|
55 |
private UserRoleRepository userRoleRepository;
|
|
|
56 |
|
|
|
57 |
@Autowired
|
|
|
58 |
private DocumentRepository documentRepository;
|
|
|
59 |
|
|
|
60 |
@Override
|
|
|
61 |
public Map<String, Object> getByEmailIdOrMobileNumber(String emailIdOrMobileNumber)
|
|
|
62 |
throws ProfitMandiBusinessException {
|
|
|
63 |
User user = userRepository.selectByEmailIdOrMobileNumber(emailIdOrMobileNumber);
|
|
|
64 |
int retailerId = userAccountRepository.selectRetailerIdByUserId(user.getId());
|
|
|
65 |
Retailer retailer = retailerRepository.selectById(retailerId);
|
|
|
66 |
int retailerAddressId = retailerRegisteredAddressRepository.selectAddressIdByRetailerId(retailer.getId());
|
|
|
67 |
Address retailerAddress = addressRepository.selectById(retailerAddressId);
|
|
|
68 |
List<Shop> shops = shopRepository.selectByRetailerId(retailer.getId());
|
|
|
69 |
this.addAddress(shops);
|
|
|
70 |
//Map<Integer, Address> shopIdAddressMap = this.getShopAddressesMap(shops);
|
|
|
71 |
List<UserRole> userRoles = userRoleRepository.selectByUserId(user.getId());
|
|
|
72 |
Map<String, Object> map = new HashMap<>();
|
|
|
73 |
map.put("user", user);
|
|
|
74 |
map.put("retailer", retailer);
|
|
|
75 |
map.put("retailerAddress", retailerAddress);
|
|
|
76 |
map.put("shops", shops);
|
|
|
77 |
map.put("userRoles", this.toString(userRoles));
|
|
|
78 |
return map;
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
@Override
|
|
|
82 |
public Map<String, Object> updateRetailerDocument(String emailIdOrMobileNumber, int documentId)
|
|
|
83 |
throws ProfitMandiBusinessException {
|
|
|
84 |
Map<String, Object> map = this.getByEmailIdOrMobileNumber(emailIdOrMobileNumber);
|
|
|
85 |
Retailer retailer = (Retailer)map.get("retailer");
|
|
|
86 |
retailer.setDocumentId(documentId);
|
|
|
87 |
retailerRepository.persist(retailer);
|
|
|
88 |
documentRepository.markDocumentAsPersisted(documentId);
|
|
|
89 |
return map;
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
@SuppressWarnings("unchecked")
|
|
|
93 |
@Override
|
|
|
94 |
public Map<String, Object> updateRetailerShopDocument(String emailIdOrMobileNumber, int shopId, int documentId)
|
|
|
95 |
throws ProfitMandiBusinessException {
|
|
|
96 |
Map<String, Object> map = this.getByEmailIdOrMobileNumber(emailIdOrMobileNumber);
|
|
|
97 |
List<Shop> shops = (List<Shop>)map.get("shops");
|
|
|
98 |
Shop foundShop = null;
|
|
|
99 |
for(Shop shop : shops){
|
|
|
100 |
if(shop.getId() == shopId){
|
|
|
101 |
foundShop = shop;
|
|
|
102 |
break;
|
|
|
103 |
}
|
|
|
104 |
}
|
|
|
105 |
if(foundShop != null){
|
|
|
106 |
foundShop.setDocumentId(documentId);
|
|
|
107 |
shopRepository.persist(foundShop);
|
|
|
108 |
documentRepository.markDocumentAsPersisted(documentId);
|
|
|
109 |
}
|
|
|
110 |
return map;
|
|
|
111 |
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
private void addAddress(List<Shop> shops){
|
|
|
115 |
Set<Integer> shopIds = this.toShopIds(shops);
|
|
|
116 |
if(shopIds.isEmpty()){
|
|
|
117 |
return;
|
|
|
118 |
}
|
|
|
119 |
List<ShopAddress> shopAddresses = shopAddressRepository.selectByShopIds(shopIds);
|
|
|
120 |
Map<Integer, Address> addressIdAddressMap = this.toAddressIdAddressMap(shopAddresses);
|
|
|
121 |
|
|
|
122 |
for(Shop shop : shops){
|
|
|
123 |
shop.setAddress(addressIdAddressMap.get(shop.getAddressId()));
|
|
|
124 |
}
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
private Map<Integer, Address> toAddressIdAddressMap(List<ShopAddress> shopAddresses){
|
|
|
128 |
Map<Integer, Address> addressIdAddressMap = new HashMap<>();
|
|
|
129 |
List<Integer> addressIds = this.toAddressIds(shopAddresses);
|
|
|
130 |
List<Address> addresses = addressRepository.selectByIds(addressIds);
|
|
|
131 |
for(Address address : addresses){
|
|
|
132 |
addressIdAddressMap.put(address.getId(), address);
|
|
|
133 |
}
|
|
|
134 |
return addressIdAddressMap;
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
private List<Integer> toAddressIds(List<ShopAddress> shopAddresses){
|
|
|
138 |
Function<ShopAddress, Integer> shopAddressToAddressIdFunction = new Function<ShopAddress, Integer>(){
|
|
|
139 |
@Override
|
|
|
140 |
public Integer apply(ShopAddress shopAddress) {
|
|
|
141 |
return shopAddress.getAddressId();
|
|
|
142 |
}
|
|
|
143 |
};
|
|
|
144 |
return shopAddresses.stream().map(shopAddressToAddressIdFunction).collect(Collectors.toList());
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
private Set<Integer> toShopIds(List<Shop> shops){
|
|
|
148 |
Function<Shop, Integer> shopToAddressIdFunction = new Function<Shop, Integer>(){
|
|
|
149 |
@Override
|
|
|
150 |
public Integer apply(Shop shop) {
|
|
|
151 |
return shop.getId();
|
|
|
152 |
}
|
|
|
153 |
};
|
|
|
154 |
return shops.stream().map(shopToAddressIdFunction).collect(Collectors.toSet());
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
private String toString(List<UserRole> userRoles){
|
|
|
158 |
Function<UserRole, String> userRoleToRoleNameFunction = new Function<UserRole, String>(){
|
|
|
159 |
public String apply(UserRole userRole) {
|
|
|
160 |
return userRole.getRoleType().toString();
|
|
|
161 |
};
|
|
|
162 |
};
|
|
|
163 |
Set<String> userRoleStrings = userRoles.stream().map(userRoleToRoleNameFunction).collect(Collectors.toSet());
|
|
|
164 |
return String.join(", ", userRoleStrings);
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
}
|