Subversion Repositories SmartDukaan

Rev

Rev 23031 | Rev 23046 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 23031 Rev 23043
Line 5... Line 5...
5
import java.util.Map;
5
import java.util.Map;
6
import java.util.Set;
6
import java.util.Set;
7
import java.util.function.Function;
7
import java.util.function.Function;
8
import java.util.stream.Collectors;
8
import java.util.stream.Collectors;
9
 
9
 
-
 
10
import org.slf4j.Logger;
-
 
11
import org.slf4j.LoggerFactory;
10
import org.springframework.beans.factory.annotation.Autowired;
12
import org.springframework.beans.factory.annotation.Autowired;
-
 
13
import org.springframework.beans.factory.annotation.Qualifier;
11
import org.springframework.stereotype.Component;
14
import org.springframework.stereotype.Component;
12
 
15
 
13
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
16
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
14
import com.spice.profitmandi.common.model.CustomAddress;
17
import com.spice.profitmandi.common.model.CustomAddress;
15
import com.spice.profitmandi.common.model.CustomShop;
18
import com.spice.profitmandi.common.model.CustomShop;
16
import com.spice.profitmandi.common.model.UpdateRetailerRequest;
19
import com.spice.profitmandi.common.model.UpdateRetailerRequest;
17
import com.spice.profitmandi.dao.entity.dtr.Retailer;
20
import com.spice.profitmandi.dao.entity.dtr.Retailer;
-
 
21
import com.spice.profitmandi.dao.entity.dtr.RetailerRegisteredAddress;
18
import com.spice.profitmandi.dao.entity.dtr.Shop;
22
import com.spice.profitmandi.dao.entity.dtr.Shop;
19
import com.spice.profitmandi.dao.entity.dtr.ShopAddress;
23
import com.spice.profitmandi.dao.entity.dtr.ShopAddress;
20
import com.spice.profitmandi.dao.entity.dtr.User;
24
import com.spice.profitmandi.dao.entity.dtr.User;
21
import com.spice.profitmandi.dao.entity.dtr.UserRole;
25
import com.spice.profitmandi.dao.entity.dtr.UserRole;
22
import com.spice.profitmandi.dao.entity.user.Address;
26
import com.spice.profitmandi.dao.entity.user.Address;
-
 
27
import com.spice.profitmandi.dao.entity.user.Cart;
23
import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;
28
import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;
24
import com.spice.profitmandi.dao.repository.dtr.RetailerRegisteredAddressRepository;
29
import com.spice.profitmandi.dao.repository.dtr.RetailerRegisteredAddressRepository;
25
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
30
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
26
import com.spice.profitmandi.dao.repository.dtr.ShopAddressRepository;
31
import com.spice.profitmandi.dao.repository.dtr.ShopAddressRepository;
27
import com.spice.profitmandi.dao.repository.dtr.ShopRepository;
32
import com.spice.profitmandi.dao.repository.dtr.ShopRepository;
28
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
33
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
29
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
34
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
30
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
35
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
31
import com.spice.profitmandi.dao.repository.user.AddressRepository;
36
import com.spice.profitmandi.dao.repository.user.AddressRepository;
-
 
37
import com.spice.profitmandi.dao.repository.user.CartRepository;
-
 
38
 
-
 
39
import in.shop2020.model.v1.user.CartStatus;
32
 
40
 
33
@Component
41
@Component
34
public class RetailerServiceImpl implements RetailerService {
42
public class RetailerServiceImpl implements RetailerService {
-
 
43
	
-
 
44
	private static final Logger LOGGER = LoggerFactory.getLogger(RetailerServiceImpl.class);
35
 
45
	
36
	@Autowired
46
	@Autowired
37
	private RetailerRepository retailerRepository;
47
	private RetailerRepository retailerRepository;
38
	
48
	
39
	@Autowired
49
	@Autowired
40
	private UserAccountRepository userAccountRepository;
50
	private UserAccountRepository userAccountRepository;
41
	
51
	
42
	@Autowired
52
	@Autowired
43
	private UserRepository userRepository;
53
	private UserRepository userRepository;
44
	
54
	
45
	@Autowired
55
	@Autowired
-
 
56
	private CartRepository cartRepository;
-
 
57
	
-
 
58
	@Autowired
46
	private RetailerRegisteredAddressRepository retailerRegisteredAddressRepository;
59
	private RetailerRegisteredAddressRepository retailerRegisteredAddressRepository;
47
	
60
	
48
	@Autowired
61
	@Autowired
49
	private AddressRepository addressRepository;
62
	private AddressRepository addressRepository;
50
	
63
	
Line 58... Line 71...
58
	private UserRoleRepository userRoleRepository;
71
	private UserRoleRepository userRoleRepository;
59
	
72
	
60
	@Autowired
73
	@Autowired
61
	private DocumentRepository documentRepository;
74
	private DocumentRepository documentRepository;
62
	
75
	
-
 
76
	@Autowired
-
 
77
	@Qualifier("userUserRepository")
-
 
78
	private com.spice.profitmandi.dao.repository.user.UserRepository userUserRepository;
-
 
79
	
63
	@Override
80
	@Override
64
	public Map<String, Object> getByEmailIdOrMobileNumber(String emailIdOrMobileNumber)
81
	public Map<String, Object> getByEmailIdOrMobileNumber(String emailIdOrMobileNumber)
65
			throws ProfitMandiBusinessException {
82
			throws ProfitMandiBusinessException {
66
		User user = userRepository.selectByEmailIdOrMobileNumber(emailIdOrMobileNumber);
83
		User user = userRepository.selectByEmailIdOrMobileNumber(emailIdOrMobileNumber);
67
		int retailerId = userAccountRepository.selectRetailerIdByUserId(user.getId());
-
 
68
		Retailer retailer = retailerRepository.selectById(retailerId);
-
 
69
		int retailerAddressId = retailerRegisteredAddressRepository.selectAddressIdByRetailerId(retailer.getId());
-
 
70
		Address retailerAddress = addressRepository.selectById(retailerAddressId);
-
 
71
		List<Shop> shops = shopRepository.selectByRetailerId(retailer.getId());
-
 
72
		this.addAddress(shops);
-
 
73
		//Map<Integer, Address> shopIdAddressMap = this.getShopAddressesMap(shops);
-
 
74
		List<UserRole> userRoles = userRoleRepository.selectByUserId(user.getId());
84
		List<UserRole> userRoles = userRoleRepository.selectByUserId(user.getId());
75
		Map<String, Object> map = new HashMap<>();
85
		Map<String, Object> map = new HashMap<>();
76
		map.put("user", user);
86
		map.put("user", user);
77
		map.put("retailer", retailer);
87
		//map.put("retailer", retailer);
78
		map.put("retailerAddress", retailerAddress);
-
 
-
 
88
		
79
		map.put("shops", shops);
89
		
80
		map.put("userRoles", this.toString(userRoles));
90
		map.put("userRoles", this.toString(userRoles));
-
 
91
		try{
-
 
92
			int retailerId = userAccountRepository.selectRetailerIdByUserId(user.getId());
-
 
93
			Retailer retailer = retailerRepository.selectById(retailerId);
-
 
94
			
-
 
95
			map.put("retailer", retailer);
-
 
96
			try{
-
 
97
				int retailerAddressId = retailerRegisteredAddressRepository.selectAddressIdByRetailerId(retailer.getId());
-
 
98
				Address retailerAddress = addressRepository.selectById(retailerAddressId);
-
 
99
				map.put("retailerAddress", retailerAddress);
-
 
100
				List<Shop> shops = shopRepository.selectByRetailerId(retailer.getId());
-
 
101
				map.put("shops", shops);
-
 
102
				this.addAddress(shops);
-
 
103
			}catch(ProfitMandiBusinessException profitMandiBusinessException){
-
 
104
				LOGGER.error("Retailer Registered Address not found");
-
 
105
			}
-
 
106
		}catch(ProfitMandiBusinessException profitMandiBusinessException){
-
 
107
			LOGGER.error("Retailer not found");
-
 
108
		}
-
 
109
		
81
		return map;
110
		return map;
82
	}
111
	}
83
	
112
	
84
	@Override
113
	@Override
85
	public Map<String, Object> updateRetailerDocument(String emailIdOrMobileNumber, int documentId)
114
	public Map<String, Object> updateRetailerDocument(String emailIdOrMobileNumber, int documentId)
Line 117... Line 146...
117
	@SuppressWarnings("unchecked")
146
	@SuppressWarnings("unchecked")
118
	@Override
147
	@Override
119
	public Map<String, Object> updateRetailerDetails(UpdateRetailerRequest updateRetailerRequest)
148
	public Map<String, Object> updateRetailerDetails(UpdateRetailerRequest updateRetailerRequest)
120
			throws ProfitMandiBusinessException {
149
			throws ProfitMandiBusinessException {
121
		Map<String, Object> map = this.getByEmailIdOrMobileNumber(updateRetailerRequest.getEmailIdOrMobileNumber());
150
		Map<String, Object> map = this.getByEmailIdOrMobileNumber(updateRetailerRequest.getEmailIdOrMobileNumber());
-
 
151
		User user = (User)map.get("user");
122
		Retailer retailer = (Retailer)map.get("retailer");
152
		Retailer retailer = (Retailer)map.get("retailer");
123
		this.updateRetailer(retailer, updateRetailerRequest);
153
		this.updateRetailer(user.getEmailId(), retailer, updateRetailerRequest);
124
		Address retailerAddress = (Address)map.get("retailerAddress");
154
		Address retailerAddress = (Address)map.get("retailerAddress");
125
		this.updateAddress(retailerAddress, updateRetailerRequest.getAddress());
155
		this.updateRetailerAddress(retailerAddress, updateRetailerRequest.getAddress(), retailer.getId());
126
		List<Shop> shops = (List<Shop>)map.get("shops");
156
		List<Shop> shops = (List<Shop>)map.get("shops");
127
		this.updateRetailerShops(shops, updateRetailerRequest.getShops());
157
		this.updateRetailerShops(shops, updateRetailerRequest.getShops(), retailer.getId());
128
		return map;
158
		return map;
129
	}
159
	}
130
	
160
	
-
 
161
	private int createSaholicUser(String emailId, String retailerName){
-
 
162
		com.spice.profitmandi.dao.entity.user.User saholicUser = null;
-
 
163
		try {
-
 
164
			saholicUser = userUserRepository.selectByEmailId(emailId);
-
 
165
		}catch (ProfitMandiBusinessException e) {
-
 
166
			LOGGER.info("User doesnt exist in old system");
-
 
167
		}
-
 
168
		if(saholicUser == null){
-
 
169
			Cart cart = new Cart();
-
 
170
			cart.setCartStatus(CartStatus.ACTIVE);
-
 
171
			cartRepository.persist(cart);
-
 
172
			saholicUser = new com.spice.profitmandi.dao.entity.user.User();
-
 
173
			saholicUser.setEmailId(emailId);
-
 
174
			saholicUser.setName(retailerName);
-
 
175
			saholicUser.setActiveCartId(cart.getId());
-
 
176
			userUserRepository.persist(saholicUser);
-
 
177
		}
-
 
178
		return saholicUser.getId();
-
 
179
	}
-
 
180
	
131
	private void updateRetailer(Retailer retailer, UpdateRetailerRequest updateRetailerRequest) throws ProfitMandiBusinessException{
181
	private void updateRetailer(String emailId, Retailer retailer, UpdateRetailerRequest updateRetailerRequest) throws ProfitMandiBusinessException{
-
 
182
		if(retailer == null){
-
 
183
			int saholicUserId = this.createSaholicUser(emailId, updateRetailerRequest.getName());
-
 
184
			retailer = new Retailer();
-
 
185
			retailer.setId(saholicUserId);
-
 
186
		}
132
		retailer.setName(updateRetailerRequest.getName());
187
		retailer.setName(updateRetailerRequest.getName());
133
		retailer.setNumber(updateRetailerRequest.getNumber());
188
		retailer.setNumber(updateRetailerRequest.getNumber());
134
		if(updateRetailerRequest.getDocumentId() > 0){
189
		if(updateRetailerRequest.getDocumentId() > 0){
135
			retailer.setDocumentId(updateRetailerRequest.getDocumentId());
190
			retailer.setDocumentId(updateRetailerRequest.getDocumentId());
136
		}
191
		}
137
		retailerRepository.persist(retailer);
192
		retailerRepository.persist(retailer);
138
	}
193
	}
139
	
194
	
-
 
195
	private void updateRetailerAddress(Address address, CustomAddress customAddress, int retailerId) throws ProfitMandiBusinessException{
-
 
196
		if(address == null){
-
 
197
			address = new Address();
-
 
198
			address.setRetaierId(retailerId);
-
 
199
			this.updateAddress(address, customAddress);
-
 
200
			//addressRepository.persist(addressRetailer);
-
 
201
			
-
 
202
			final RetailerRegisteredAddress retailerRegisteredAddress = new RetailerRegisteredAddress();
-
 
203
			retailerRegisteredAddress.setRetailerId(retailerId);
-
 
204
			retailerRegisteredAddress.setAddressId(address.getId());
-
 
205
			retailerRegisteredAddressRepository.persist(retailerRegisteredAddress);
-
 
206
		}
-
 
207
	}
-
 
208
	
140
	private void updateAddress(Address address, CustomAddress customAddress){
209
	private void updateAddress(Address address, CustomAddress customAddress){
141
		address.setName(customAddress.getName());
210
		address.setName(customAddress.getName());
142
		address.setLine1(customAddress.getLine1());
211
		address.setLine1(customAddress.getLine1());
143
		address.setLine2(customAddress.getLine2());
212
		address.setLine2(customAddress.getLine2());
144
		address.setCity(customAddress.getCity());
213
		address.setCity(customAddress.getCity());
145
		address.setPinCode(customAddress.getPinCode());
214
		address.setPinCode(customAddress.getPinCode());
146
		address.setState(customAddress.getState());
215
		address.setState(customAddress.getState());
147
		addressRepository.persist(address);
216
		addressRepository.persist(address);
148
	}
217
	}
149
	
218
	
150
	private void updateRetailerShops(List<Shop> shops, Set<CustomShop> customShops) throws ProfitMandiBusinessException{
219
	private void updateRetailerShops(List<Shop> shops, Set<CustomShop> customShops, int retailerId) throws ProfitMandiBusinessException{
151
		
-
 
152
		//Shop foundShop = null;
-
 
153
		for(Shop shop : shops){
220
		if(shops.isEmpty()){
154
			for(CustomShop customShop : customShops){
221
			for(CustomShop customShop : customShops){
-
 
222
				Shop shop = new Shop();
155
				if(shop.getId() == customShop.getShopId()){
223
				this.createOrUpdateShop(shop, customShop, retailerId);
-
 
224
				shops.add(shop);
-
 
225
			}
-
 
226
		}else{
-
 
227
			for(Shop shop : shops){
156
					if(customShop.getDocumentId() > 0){
228
				for(CustomShop customShop : customShops){
157
						shop.setDocumentId(customShop.getDocumentId());
229
					if(shop.getId() == customShop.getShopId()){
158
						documentRepository.markDocumentAsPersisted(customShop.getDocumentId());
230
						this.createOrUpdateShop(shop, customShop, retailerId);
159
					}
231
					}
160
					shop.setName(customShop.getName());
-
 
161
					shopRepository.persist(shop);
-
 
162
					this.updateAddress(shop.getAddress(), customShop.getAddress());
-
 
163
				}
232
				}
164
			}
233
			}
-
 
234
			for(CustomShop customShop : customShops){
-
 
235
				if(customShop.getShopId() == 0){
-
 
236
					Shop shop = new Shop();
-
 
237
					this.createOrUpdateShop(shop, customShop, retailerId);
-
 
238
					shops.add(shop);
-
 
239
				}
-
 
240
			}
-
 
241
		}
-
 
242
	}
-
 
243
	
-
 
244
	private void createOrUpdateShop(Shop shop, CustomShop customShop, int retailerId) throws ProfitMandiBusinessException{
-
 
245
		shop.setRetailerId(retailerId);
-
 
246
		shop.setName(customShop.getName());
-
 
247
		if(customShop.getDocumentId() > 0){
-
 
248
			shop.setDocumentId(customShop.getDocumentId());
-
 
249
			documentRepository.markDocumentAsPersisted(customShop.getDocumentId());
165
		}
250
		}
-
 
251
		if(shop.getAddressId() == null){
-
 
252
			//shop.setDocumentId(customShop.getDocumentId());
-
 
253
			Address address = new Address();
-
 
254
			this.updateAddress(address, customShop.getAddress());
-
 
255
			shop.setAddressId(address.getId());
-
 
256
			shop.setAddress(address);
-
 
257
			shopRepository.persist(shop);
-
 
258
			ShopAddress shopAddress = new ShopAddress();
-
 
259
			shopAddress.setAddressId(address.getId());
-
 
260
			shopAddress.setShopId(shop.getId());
-
 
261
			shopAddressRepository.persist(shopAddress);
-
 
262
		}else{
-
 
263
			Address address = addressRepository.selectById(shop.getAddressId());
-
 
264
			this.updateAddress(address, customShop.getAddress());
-
 
265
			shop.setAddress(address);
-
 
266
			shopRepository.persist(shop);
-
 
267
		}
-
 
268
		
166
	}
269
	}
167
	
270
	
168
	
271
	
169
	private void addAddress(List<Shop> shops){
272
	private void addAddress(List<Shop> shops){
170
		Set<Integer> shopIds = this.toShopIds(shops);
273
		Set<Integer> shopIds = this.toShopIds(shops);