Subversion Repositories SmartDukaan

Rev

Rev 22980 | Rev 23031 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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;
23025 ashik.ali 14
import com.spice.profitmandi.common.model.CustomAddress;
15
import com.spice.profitmandi.common.model.CustomShop;
16
import com.spice.profitmandi.common.model.UpdateRetailerRequest;
22980 ashik.ali 17
import com.spice.profitmandi.dao.entity.dtr.Retailer;
18
import com.spice.profitmandi.dao.entity.dtr.Shop;
19
import com.spice.profitmandi.dao.entity.dtr.ShopAddress;
20
import com.spice.profitmandi.dao.entity.dtr.User;
21
import com.spice.profitmandi.dao.entity.dtr.UserRole;
22
import com.spice.profitmandi.dao.entity.user.Address;
23
import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;
24
import com.spice.profitmandi.dao.repository.dtr.RetailerRegisteredAddressRepository;
25
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
26
import com.spice.profitmandi.dao.repository.dtr.ShopAddressRepository;
27
import com.spice.profitmandi.dao.repository.dtr.ShopRepository;
28
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
29
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
30
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
31
import com.spice.profitmandi.dao.repository.user.AddressRepository;
32
 
33
@Component
34
public class RetailerServiceImpl implements RetailerService {
35
 
36
	@Autowired
37
	private RetailerRepository retailerRepository;
38
 
39
	@Autowired
40
	private UserAccountRepository userAccountRepository;
41
 
42
	@Autowired
43
	private UserRepository userRepository;
44
 
45
	@Autowired
46
	private RetailerRegisteredAddressRepository retailerRegisteredAddressRepository;
47
 
48
	@Autowired
49
	private AddressRepository addressRepository;
50
 
51
	@Autowired
52
	private ShopRepository shopRepository;
53
 
54
	@Autowired
55
	private ShopAddressRepository shopAddressRepository;
56
 
57
	@Autowired
58
	private UserRoleRepository userRoleRepository;
59
 
60
	@Autowired
61
	private DocumentRepository documentRepository;
62
 
63
	@Override
64
	public Map<String, Object> getByEmailIdOrMobileNumber(String emailIdOrMobileNumber)
65
			throws ProfitMandiBusinessException {
66
		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());
75
		Map<String, Object> map = new HashMap<>();
76
		map.put("user", user);
77
		map.put("retailer", retailer);
78
		map.put("retailerAddress", retailerAddress);
79
		map.put("shops", shops);
80
		map.put("userRoles", this.toString(userRoles));
81
		return map;
82
	}
83
 
84
	@Override
85
	public Map<String, Object> updateRetailerDocument(String emailIdOrMobileNumber, int documentId)
86
			throws ProfitMandiBusinessException {
87
		Map<String, Object> map = this.getByEmailIdOrMobileNumber(emailIdOrMobileNumber);
88
		Retailer retailer = (Retailer)map.get("retailer");
89
		retailer.setDocumentId(documentId);
90
		retailerRepository.persist(retailer);
91
		documentRepository.markDocumentAsPersisted(documentId);
92
		return map;
93
	}
94
 
95
	@SuppressWarnings("unchecked")
96
	@Override
97
	public Map<String, Object> updateRetailerShopDocument(String emailIdOrMobileNumber, int shopId, int documentId)
98
			throws ProfitMandiBusinessException {
99
		Map<String, Object> map = this.getByEmailIdOrMobileNumber(emailIdOrMobileNumber);
100
		List<Shop> shops = (List<Shop>)map.get("shops");
101
		Shop foundShop = null;
102
		for(Shop shop : shops){
103
			if(shop.getId() == shopId){
104
				foundShop = shop;
105
				break;
106
			}
107
		}
108
		if(foundShop != null){
109
			foundShop.setDocumentId(documentId);
110
			shopRepository.persist(foundShop);
111
			documentRepository.markDocumentAsPersisted(documentId);
112
		}
113
		return map;
114
 
115
	}
116
 
23025 ashik.ali 117
	@SuppressWarnings("unchecked")
118
	@Override
119
	public Map<String, Object> updateRetailerDetails(UpdateRetailerRequest updateRetailerRequest)
120
			throws ProfitMandiBusinessException {
121
		Map<String, Object> map = this.getByEmailIdOrMobileNumber(updateRetailerRequest.getEmailIdOrMobileNumber());
122
		Retailer retailer = (Retailer)map.get("retailer");
123
		this.updateRetailer(retailer, updateRetailerRequest);
124
		Address retailerAddress = (Address)map.get("retailerAddress");
125
		this.updateAddress(retailerAddress, updateRetailerRequest.getAddress());
126
		List<Shop> shops = (List<Shop>)map.get("shops");
127
		this.updateRetailerShops(shops, updateRetailerRequest.getShops());
128
		return map;
129
	}
130
 
131
	private void updateRetailer(Retailer retailer, UpdateRetailerRequest updateRetailerRequest) throws ProfitMandiBusinessException{
132
		retailer.setName(updateRetailerRequest.getName());
133
		retailer.setNumber(updateRetailerRequest.getNumber());
134
		if(updateRetailerRequest.getDocumentId() > 0){
135
			retailer.setDocumentId(updateRetailerRequest.getDocumentId());
136
		}
137
		retailerRepository.persist(retailer);
138
	}
139
 
140
	private void updateAddress(Address address, CustomAddress customAddress){
141
		address.setName(customAddress.getName());
142
		address.setLine1(customAddress.getLine1());
143
		address.setLine2(customAddress.getLine2());
144
		address.setCity(customAddress.getCity());
145
		address.setPinCode(customAddress.getPinCode());
146
		address.setState(customAddress.getState());
147
		addressRepository.persist(address);
148
	}
149
 
150
	private void updateRetailerShops(List<Shop> shops, Set<CustomShop> customShops) throws ProfitMandiBusinessException{
151
 
152
		//Shop foundShop = null;
153
		for(Shop shop : shops){
154
			for(CustomShop customShop : customShops){
155
				if(shop.getId() == customShop.getShopId()){
156
					if(customShop.getDocumentId() > 0){
157
						shop.setDocumentId(customShop.getDocumentId());
158
						shopRepository.persist(shop);
159
						documentRepository.markDocumentAsPersisted(customShop.getDocumentId());
160
					}
161
					this.updateAddress(shop.getAddress(), customShop.getAddress());
162
				}
163
			}
164
		}
165
	}
166
 
167
 
22980 ashik.ali 168
	private void addAddress(List<Shop> shops){
169
		Set<Integer> shopIds = this.toShopIds(shops);
170
		if(shopIds.isEmpty()){
171
			return;
172
		}
173
		List<ShopAddress> shopAddresses = shopAddressRepository.selectByShopIds(shopIds);
174
		Map<Integer, Address> addressIdAddressMap = this.toAddressIdAddressMap(shopAddresses);
175
 
176
		for(Shop shop : shops){
177
			shop.setAddress(addressIdAddressMap.get(shop.getAddressId()));
178
		}
179
	}
180
 
181
	private Map<Integer, Address> toAddressIdAddressMap(List<ShopAddress> shopAddresses){
182
		Map<Integer, Address> addressIdAddressMap = new HashMap<>();
183
		List<Integer> addressIds = this.toAddressIds(shopAddresses);
184
		List<Address> addresses = addressRepository.selectByIds(addressIds);
185
		for(Address address : addresses){
186
			addressIdAddressMap.put(address.getId(), address);
187
		}
188
		return addressIdAddressMap;
189
	}
190
 
191
	private List<Integer> toAddressIds(List<ShopAddress> shopAddresses){
192
		Function<ShopAddress, Integer> shopAddressToAddressIdFunction = new Function<ShopAddress, Integer>(){
193
			@Override
194
			public Integer apply(ShopAddress shopAddress) {
195
				return shopAddress.getAddressId();
196
			}
197
		};
198
		return shopAddresses.stream().map(shopAddressToAddressIdFunction).collect(Collectors.toList());
199
	}
200
 
201
	private Set<Integer> toShopIds(List<Shop> shops){
202
		Function<Shop, Integer> shopToAddressIdFunction = new Function<Shop, Integer>(){
203
			@Override
204
			public Integer apply(Shop shop) {
205
				return shop.getId();
206
			}
207
		};
208
		return shops.stream().map(shopToAddressIdFunction).collect(Collectors.toSet());
209
	}
210
 
211
	private String toString(List<UserRole> userRoles){
212
		Function<UserRole, String> userRoleToRoleNameFunction = new Function<UserRole, String>(){
213
			public String apply(UserRole userRole) {
214
				return userRole.getRoleType().toString();
215
			};
216
		};
217
		Set<String> userRoleStrings = userRoles.stream().map(userRoleToRoleNameFunction).collect(Collectors.toSet());
218
		return String.join(", ", userRoleStrings);
219
	}
220
 
221
}