Subversion Repositories SmartDukaan

Rev

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

Rev 23642 Rev 23781
Line 1... Line 1...
1
package com.spice.profitmandi.service.user;
1
package com.spice.profitmandi.service.user;
2
 
2
 
3
import java.time.LocalDateTime;
3
import java.time.LocalDateTime;
4
import java.util.ArrayList;
4
import java.util.ArrayList;
5
import java.util.HashMap;
5
import java.util.HashMap;
-
 
6
import java.util.HashSet;
6
import java.util.List;
7
import java.util.List;
7
import java.util.Map;
8
import java.util.Map;
8
import java.util.Set;
9
import java.util.Set;
9
import java.util.function.Function;
10
import java.util.function.Function;
10
import java.util.stream.Collectors;
11
import java.util.stream.Collectors;
11
 
12
 
12
import org.apache.logging.log4j.Logger;
-
 
13
import org.apache.logging.log4j.LogManager;
13
import org.apache.logging.log4j.LogManager;
-
 
14
import org.apache.logging.log4j.Logger;
14
import org.springframework.beans.factory.annotation.Autowired;
15
import org.springframework.beans.factory.annotation.Autowired;
15
import org.springframework.beans.factory.annotation.Qualifier;
16
import org.springframework.beans.factory.annotation.Qualifier;
16
import org.springframework.stereotype.Component;
17
import org.springframework.stereotype.Component;
17
 
18
 
18
import com.spice.profitmandi.common.ResponseCodeHolder;
19
import com.spice.profitmandi.common.ResponseCodeHolder;
19
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
20
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
20
import com.spice.profitmandi.common.model.CustomAddress;
21
import com.spice.profitmandi.common.model.CustomAddress;
21
import com.spice.profitmandi.common.model.CustomRetailer;
22
import com.spice.profitmandi.common.model.CustomRetailer;
22
import com.spice.profitmandi.common.model.CustomShop;
23
import com.spice.profitmandi.common.model.CustomShop;
-
 
24
import com.spice.profitmandi.common.model.MapWrapper;
23
import com.spice.profitmandi.common.model.ProfitMandiConstants;
25
import com.spice.profitmandi.common.model.ProfitMandiConstants;
24
import com.spice.profitmandi.common.model.UpdateRetailerRequest;
26
import com.spice.profitmandi.common.model.UpdateRetailerRequest;
25
import com.spice.profitmandi.common.util.StringUtils;
27
import com.spice.profitmandi.common.util.StringUtils;
26
import com.spice.profitmandi.common.util.Utils;
28
import com.spice.profitmandi.common.util.Utils;
27
import com.spice.profitmandi.dao.entity.dtr.DistrictMaster;
29
import com.spice.profitmandi.dao.entity.dtr.DistrictMaster;
28
import com.spice.profitmandi.dao.entity.dtr.Retailer;
30
import com.spice.profitmandi.dao.entity.dtr.Retailer;
29
import com.spice.profitmandi.dao.entity.dtr.RetailerRegisteredAddress;
31
import com.spice.profitmandi.dao.entity.dtr.RetailerRegisteredAddress;
-
 
32
import com.spice.profitmandi.dao.entity.dtr.Role;
30
import com.spice.profitmandi.dao.entity.dtr.Shop;
33
import com.spice.profitmandi.dao.entity.dtr.Shop;
31
import com.spice.profitmandi.dao.entity.dtr.ShopAddress;
34
import com.spice.profitmandi.dao.entity.dtr.ShopAddress;
32
import com.spice.profitmandi.dao.entity.dtr.User;
35
import com.spice.profitmandi.dao.entity.dtr.User;
33
import com.spice.profitmandi.dao.entity.dtr.UserAccount;
36
import com.spice.profitmandi.dao.entity.dtr.UserAccount;
34
import com.spice.profitmandi.dao.entity.dtr.UserRole;
37
import com.spice.profitmandi.dao.entity.dtr.UserRole;
Line 49... Line 52...
49
import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;
52
import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;
50
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
53
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
51
import com.spice.profitmandi.dao.repository.dtr.FofoStoreSequenceGenerationRepository;
54
import com.spice.profitmandi.dao.repository.dtr.FofoStoreSequenceGenerationRepository;
52
import com.spice.profitmandi.dao.repository.dtr.RetailerRegisteredAddressRepository;
55
import com.spice.profitmandi.dao.repository.dtr.RetailerRegisteredAddressRepository;
53
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
56
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
-
 
57
import com.spice.profitmandi.dao.repository.dtr.RoleRepository;
54
import com.spice.profitmandi.dao.repository.dtr.ShopAddressRepository;
58
import com.spice.profitmandi.dao.repository.dtr.ShopAddressRepository;
55
import com.spice.profitmandi.dao.repository.dtr.ShopRepository;
59
import com.spice.profitmandi.dao.repository.dtr.ShopRepository;
56
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
60
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
57
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
61
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
58
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
62
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
Line 128... Line 132...
128
	private PaymentOptionRepository paymentOptionRepository;
132
	private PaymentOptionRepository paymentOptionRepository;
129
	
133
	
130
	@Autowired
134
	@Autowired
131
	private FofoPartnerPaymentOptionRepository fofoPartnerPaymentOptionRepository;
135
	private FofoPartnerPaymentOptionRepository fofoPartnerPaymentOptionRepository;
132
	
136
	
-
 
137
	@Autowired
-
 
138
	private RoleRepository roleRepository;
-
 
139
	
133
	
140
	
134
	@Override
141
	@Override
135
	public Map<String, Object> getByEmailIdOrMobileNumber(String emailIdOrMobileNumber)
142
	public Map<String, Object> getByEmailIdOrMobileNumber(String emailIdOrMobileNumber)
136
			throws ProfitMandiBusinessException {
143
			throws ProfitMandiBusinessException {
137
		User user = null;
144
		User user = null;
Line 152... Line 159...
152
		
159
		
153
		if(user != null){
160
		if(user != null){
154
		
161
		
155
			List<UserRole> userRoles = userRoleRepository.selectByUserId(user.getId());
162
			List<UserRole> userRoles = userRoleRepository.selectByUserId(user.getId());
156
			
163
			
-
 
164
			Role role = roleRepository.selectByName(RoleType.FOFO.toString());
-
 
165
			
157
			map.put("userRoles", userRoles);
166
			map.put("userRoles", userRoles);
158
			map.put("user", user);
167
			map.put("user", user);
159
			//map.put("retailer", retailer);
168
			//map.put("retailer", retailer);
160
			map.put("fofoRole", this.containsRoleType(userRoles, RoleType.FOFO));
169
			map.put("fofoRole", this.containsRoleType(userRoles, role.getId()));
161
		
170
		
162
			map.put("userRoleNames", this.toString(userRoles));
171
			map.put("userRoleNames", this.toString(userRoles));
163
			try{
172
			try{
164
				int retailerId = userAccountRepository.selectRetailerIdByUserId(user.getId());
173
				int retailerId = userAccountRepository.selectRetailerIdByUserId(user.getId());
165
				Retailer retailer = retailerRepository.selectById(retailerId);
174
				Retailer retailer = retailerRepository.selectById(retailerId);
166
			
175
			
167
				map.put("retailer", retailer);
176
				map.put("retailer", retailer);
168
				
177
				
169
				if(this.containsRoleType(userRoles, RoleType.FOFO)){
178
				if(this.containsRoleType(userRoles, role.getId())){
170
					try{
179
					try{
171
						FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(retailerId);
180
						FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(retailerId);
172
						map.put("fofoStore", fofoStore);
181
						map.put("fofoStore", fofoStore);
173
					}catch(ProfitMandiBusinessException profitMandiBusinessException){
182
					}catch(ProfitMandiBusinessException profitMandiBusinessException){
174
						LOGGER.error("FofoStore code not found");
183
						LOGGER.error("FofoStore code not found");
Line 211... Line 220...
211
		}
220
		}
212
		
221
		
213
		return map;
222
		return map;
214
	}
223
	}
215
	
224
	
216
	private boolean containsRoleType(List<UserRole> userRoles, RoleType roleType){
225
	private boolean containsRoleType(List<UserRole> userRoles, int roleId){
217
		for(UserRole userRole : userRoles){
226
		for(UserRole userRole : userRoles){
218
			if(userRole.getRoleType() == roleType){
227
			if(userRole.getRoleId() == roleId){
219
				return true;
228
				return true;
220
			}
229
			}
221
		}
230
		}
222
		return false;
231
		return false;
223
	}
232
	}
224
	
233
	
225
	private List<UserRole> addRole(List<UserRole> userRoles, int userId, RoleType roleType){
234
	private List<UserRole> addRole(List<UserRole> userRoles, int userId, int roleId){
226
		if(userRoles == null){
235
		if(userRoles == null){
227
			userRoles = new ArrayList<>();
236
			userRoles = new ArrayList<>();
228
		}
237
		}
229
		if(!this.containsRoleType(userRoles, roleType)){
238
		if(!this.containsRoleType(userRoles, roleId)){
230
			UserRole userRole = new UserRole();
239
			UserRole userRole = new UserRole();
231
			userRole.setUserId(userId);
240
			userRole.setUserId(userId);
232
			userRole.setRoleType(roleType);
241
			userRole.setRoleId(roleId);
233
			try {
242
			try {
234
				userRoleRepository.persist(userRole);
243
				userRoleRepository.persist(userRole);
235
			} catch (ProfitMandiBusinessException e) {
244
			} catch (ProfitMandiBusinessException e) {
236
				LOGGER.error("UserRole is already exist");
245
				LOGGER.error("UserRole is already exist");
237
			}
246
			}
Line 251... Line 260...
251
		user = this.createUser(user, updateRetailerRequest);
260
		user = this.createUser(user, updateRetailerRequest);
252
		map.put("user", user);
261
		map.put("user", user);
253
		
262
		
254
		List<UserRole> userRoles = (List<UserRole>)map.get("userRoles");
263
		List<UserRole> userRoles = (List<UserRole>)map.get("userRoles");
255
		
264
		
-
 
265
		Role roleUser = roleRepository.selectByName(RoleType.USER.toString());
-
 
266
		
256
		userRoles = this.addRole(userRoles, user.getId(), RoleType.USER);
267
		userRoles = this.addRole(userRoles, user.getId(), roleUser.getId());
257
		
268
		
258
		Retailer retailer = (Retailer)map.get("retailer");
269
		Retailer retailer = (Retailer)map.get("retailer");
259
		retailer = this.updateRetailer(user, retailer, updateRetailerRequest);
270
		retailer = this.updateRetailer(user, retailer, updateRetailerRequest);
260
		map.put("retailer", retailer);
271
		map.put("retailer", retailer);
261
		
272
		
-
 
273
		Role roleRetailer = roleRepository.selectByName(RoleType.RETAILER.toString());
-
 
274
		
262
		userRoles = this.addRole(userRoles, user.getId(), RoleType.RETAILER);
275
		userRoles = this.addRole(userRoles, user.getId(), roleRetailer.getId());
-
 
276
		
-
 
277
		Role roleFofo = roleRepository.selectByName(RoleType.FOFO.toString());
263
		
278
		
264
		if(updateRetailerRequest.isFofo()){
279
		if(updateRetailerRequest.isFofo()){
-
 
280
			
-
 
281
			
265
			userRoles = this.addRole(userRoles, user.getId(), RoleType.FOFO);
282
			userRoles = this.addRole(userRoles, user.getId(), roleFofo.getId());
266
			map.put("fofoRole", this.containsRoleType(userRoles, RoleType.FOFO));
283
			map.put("fofoRole", this.containsRoleType(userRoles, roleFofo.getId()));
267
		}
284
		}
268
		
285
		
269
		if(this.containsRoleType(userRoles, RoleType.FOFO)){
286
		if(this.containsRoleType(userRoles, roleFofo.getId())){
270
			this.createDefaultPaymentOption(retailer.getId());
287
			this.createDefaultPaymentOption(retailer.getId());
271
		}
288
		}
272
		
289
		
273
		map.put("userRoles", userRoles);
290
		map.put("userRoles", userRoles);
274
		map.put("userRoleNames", this.toString(userRoles));
291
		map.put("userRoleNames", this.toString(userRoles));
Line 627... Line 644...
627
		};
644
		};
628
		return shops.stream().map(shopToAddressIdFunction).collect(Collectors.toSet());
645
		return shops.stream().map(shopToAddressIdFunction).collect(Collectors.toSet());
629
	}
646
	}
630
	
647
	
631
	private String toString(List<UserRole> userRoles){
648
	private String toString(List<UserRole> userRoles){
-
 
649
		Set<Integer> roleIds = new HashSet<>();
-
 
650
		for(UserRole userRole : userRoles) {
-
 
651
			roleIds.add(userRole.getRoleId());
-
 
652
		}
-
 
653
		List<Role> roles = roleRepository.selectByIds(roleIds);
632
		Function<UserRole, String> userRoleToRoleNameFunction = new Function<UserRole, String>(){
654
		Function<Role, String> roleToNameFunction = new Function<Role, String>(){
633
			public String apply(UserRole userRole) {
655
			public String apply(Role role) {
634
				return userRole.getRoleType().toString();
656
				return String.valueOf(role.getName());
635
			};
657
			};
636
		};
658
		};
637
		Set<String> userRoleStrings = userRoles.stream().map(userRoleToRoleNameFunction).collect(Collectors.toSet());
659
		Set<String> userRoleStrings = roles.stream().map(roleToNameFunction).collect(Collectors.toSet());
638
		return String.join(", ", userRoleStrings);
660
		return String.join(", ", userRoleStrings);
639
	}
661
	}
640
	
662
	
641
	private FofoStore createFofoStoreCodeByRetailerId(int retailerId, String districtName, String stateName) throws ProfitMandiBusinessException{
663
	private FofoStore createFofoStoreCodeByRetailerId(int retailerId, String districtName, String stateName) throws ProfitMandiBusinessException{
642
		FofoStore fofoStore = null;
664
		FofoStore fofoStore = null;
Line 693... Line 715...
693
	public FofoStore createFofoStoreCodeByUserId(int userId, String districtName, String stateName) throws ProfitMandiBusinessException {
715
	public FofoStore createFofoStoreCodeByUserId(int userId, String districtName, String stateName) throws ProfitMandiBusinessException {
694
		User user = userRepository.selectById(userId);
716
		User user = userRepository.selectById(userId);
695
		// = userAccountRepository.selectRetailerIdByUserId(user.getId());
717
		// = userAccountRepository.selectRetailerIdByUserId(user.getId());
696
		UserAccount userAccounts = userAccountRepository.selectSaholicByUserId(user.getId());
718
		UserAccount userAccounts = userAccountRepository.selectSaholicByUserId(user.getId());
697
		Retailer retailer = retailerRepository.selectById(userAccounts.getAccountKey());
719
		Retailer retailer = retailerRepository.selectById(userAccounts.getAccountKey());
-
 
720
		Role roleFofo = roleRepository.selectByName(RoleType.FOFO.toString());
698
		try{
721
		try{
699
			userRoleRepository.selectByUserIdAndRoleType(user.getId(), RoleType.FOFO);
722
			userRoleRepository.selectByUserIdAndRoleId(user.getId(), roleFofo.getId());
700
		}catch(ProfitMandiBusinessException profitMandiBusinessException){
723
		}catch(ProfitMandiBusinessException profitMandiBusinessException){
701
			throw new ProfitMandiBusinessException(ProfitMandiConstants.USER_ID, user.getId(), "USR_1013");
724
			throw new ProfitMandiBusinessException(ProfitMandiConstants.USER_ID, user.getId(), "USR_1013");
702
		}
725
		}
703
		return this.createFofoStoreCodeByRetailerId(retailer.getId(), districtName, stateName);
726
		return this.createFofoStoreCodeByRetailerId(retailer.getId(), districtName, stateName);
704
		
727
		
Line 745... Line 768...
745
			customRetailersMap.put(address.getRetaierId(), customRetailer);
768
			customRetailersMap.put(address.getRetaierId(), customRetailer);
746
		}
769
		}
747
		return customRetailersMap;
770
		return customRetailersMap;
748
	}
771
	}
749
	
772
	
-
 
773
	@Override
-
 
774
	public Map<Integer, String> getAllFofoRetailerIdEmailIdMap() {
-
 
775
		Role roleFofo = null;
-
 
776
		try {
-
 
777
			roleFofo = roleRepository.selectByName(RoleType.FOFO.toString());
-
 
778
		} catch (ProfitMandiBusinessException e) {
-
 
779
			// TODO Auto-generated catch block
-
 
780
			e.printStackTrace();
-
 
781
		}
-
 
782
		Role roleRetailer = null;
-
 
783
		try {
-
 
784
			roleRetailer = roleRepository.selectByName(RoleType.RETAILER.toString());
-
 
785
		} catch (ProfitMandiBusinessException e) {
-
 
786
			// TODO Auto-generated catch block
-
 
787
			e.printStackTrace();
-
 
788
		}
-
 
789
		Set<Integer> roleIds = new HashSet<>();
-
 
790
		roleIds.add(roleFofo.getId());
-
 
791
		roleIds.add(roleRetailer.getId());
-
 
792
		List<Integer> userIds = userRoleRepository.selectUserIdsByRoleIds(roleIds);
-
 
793
		List<User> users = userRepository.selectAllByIds(new HashSet<>(userIds));
-
 
794
		Map<Integer, Integer> userIdRetailerIdMap = this.getUserIdRetailerIdMap(userIds);
-
 
795
		Map<Integer, String> retailerIdEmailIdMap = new HashMap<>();
-
 
796
		for(User user : users) {
-
 
797
			retailerIdEmailIdMap.put(userIdRetailerIdMap.get(user.getId()), user.getEmailId());
-
 
798
		}
-
 
799
		return retailerIdEmailIdMap;
-
 
800
	}
-
 
801
	
-
 
802
	@Override
-
 
803
	public List<MapWrapper<Integer, String>> getFofoRetailerIdEmailIdMap() {
-
 
804
		List<MapWrapper<Integer, String>> mapWrappers = new ArrayList<>();
-
 
805
		for(Map.Entry<Integer, String> retailerIdEmailIdEntry : this.getAllFofoRetailerIdEmailIdMap().entrySet()) {
-
 
806
			MapWrapper<Integer, String> mapWrapper = new MapWrapper<>();
-
 
807
			mapWrapper.setKey(retailerIdEmailIdEntry.getKey());
-
 
808
			mapWrapper.setValue(retailerIdEmailIdEntry.getValue());
-
 
809
			mapWrappers.add(mapWrapper);
-
 
810
		}
-
 
811
		return mapWrappers;
-
 
812
	}
-
 
813
	
-
 
814
	Map<Integer, Integer> getUserIdRetailerIdMap(List<Integer> userIds){
-
 
815
		List<UserAccount> userAccounts = userAccountRepository.selectAllSaholicByUserIds(new HashSet<>(userIds));
-
 
816
		Map<Integer, Integer> userIdRetailerIdMap = new HashMap<>();
-
 
817
		for(UserAccount userAccount : userAccounts) {
-
 
818
			userIdRetailerIdMap.put(userAccount.getUserId(), userAccount.getAccountKey());
-
 
819
		}
-
 
820
		return userIdRetailerIdMap;
-
 
821
	}
-
 
822
	
750
	
823
	
751
 
824
 
752
}
825
}