Subversion Repositories SmartDukaan

Rev

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

Rev 35372 Rev 35394
Line 28... Line 28...
28
import com.spice.profitmandi.dao.repository.inventory.StateRepository;
28
import com.spice.profitmandi.dao.repository.inventory.StateRepository;
29
import com.spice.profitmandi.dao.repository.trialOnboarding.TrialFormRepository;
29
import com.spice.profitmandi.dao.repository.trialOnboarding.TrialFormRepository;
30
import com.spice.profitmandi.dao.repository.user.*;
30
import com.spice.profitmandi.dao.repository.user.*;
31
import com.spice.profitmandi.service.PostOfficeService;
31
import com.spice.profitmandi.service.PostOfficeService;
32
import com.spice.profitmandi.service.catalog.BrandsService;
32
import com.spice.profitmandi.service.catalog.BrandsService;
-
 
33
import com.spice.profitmandi.dao.entity.catalog.BrandCatalog;
-
 
34
import com.spice.profitmandi.dao.entity.dtr.Brand;
33
import in.shop2020.model.v1.user.CartStatus;
35
import in.shop2020.model.v1.user.CartStatus;
34
import org.apache.logging.log4j.LogManager;
36
import org.apache.logging.log4j.LogManager;
35
import org.apache.logging.log4j.Logger;
37
import org.apache.logging.log4j.Logger;
36
import org.springframework.beans.factory.annotation.Autowired;
38
import org.springframework.beans.factory.annotation.Autowired;
37
import org.springframework.beans.factory.annotation.Qualifier;
39
import org.springframework.beans.factory.annotation.Qualifier;
Line 183... Line 185...
183
            } catch (ProfitMandiBusinessException profitMandiBusinessException) {
185
            } catch (ProfitMandiBusinessException profitMandiBusinessException) {
184
 
186
 
185
            }
187
            }
186
        }
188
        }
187
        Map<String, Object> map = new HashMap<>();
189
        Map<String, Object> map = new HashMap<>();
188
        map.put("stateNames", stateRepository.selectAll().stream().map(x -> x.getName()).collect(Collectors.toList()));
190
        map.put("stateNames", stateRepository.selectAll().stream().map(State::getName).collect(Collectors.toList()));
189
        LOGGER.info("user - {}", user);
191
        LOGGER.info("user - {}", user);
190
        if (user != null) {
192
        if (user != null) {
191
 
193
 
192
            List<UserRole> userRoles = userRoleRepository.selectByUserId(user.getId());
194
            List<UserRole> userRoles = userRoleRepository.selectByUserId(user.getId());
193
 
195
 
Line 206... Line 208...
206
                map.put("retailer", retailer);
208
                map.put("retailer", retailer);
207
 
209
 
208
                List<String> retailerBlockBrands = null;
210
                List<String> retailerBlockBrands = null;
209
                Set<String> brands = null;
211
                Set<String> brands = null;
210
                if (retailer.getId() != fofoId) {
212
                if (retailer.getId() != fofoId) {
211
                    brands = brandsService.getBrands(retailer.getId(), null, 3).stream().map(x -> x.getName()).collect(Collectors.toSet());
213
                    brands = brandsService.getBrands(retailer.getId(), null, 3).stream().map(BrandCatalog::getName).collect(Collectors.toSet());
212
                    retailerBlockBrands = brandsService.partnerIneligibleBrands(fofoId);
214
                    retailerBlockBrands = brandsService.partnerIneligibleBrands(fofoId);
213
 
215
 
214
                } else {
216
                } else {
215
                    LOGGER.info("fofoId" + fofoId);
217
                    LOGGER.info("fofoId" + fofoId);
216
                    brands = brandsService.getBrands(fofoId, null, 3).stream().map(x -> x.getName()).collect(Collectors.toSet());
218
                    brands = brandsService.getBrands(fofoId, null, 3).stream().map(BrandCatalog::getName).collect(Collectors.toSet());
217
                }
219
                }
218
                map.put("brands", brands);
220
                map.put("brands", brands);
219
                map.put("retailerBlockBrands", retailerBlockBrands);
221
                map.put("retailerBlockBrands", retailerBlockBrands);
220
 
222
 
221
                try {
223
                try {
Line 275... Line 277...
275
 
277
 
276
        return map;
278
        return map;
277
    }
279
    }
278
 
280
 
279
    private boolean containsRoleType(List<UserRole> userRoles, int roleId) {
281
    private boolean containsRoleType(List<UserRole> userRoles, int roleId) {
280
        for (UserRole userRole : userRoles) {
-
 
281
            if (userRole.getRoleId() == roleId) {
282
        return userRoles != null && userRoles.stream().anyMatch(ur -> ur.getRoleId() == roleId);
282
                return true;
-
 
283
            }
-
 
284
        }
-
 
285
        return false;
-
 
286
    }
283
    }
287
 
284
 
288
    private List<UserRole> addRole(List<UserRole> userRoles, int userId, int roleId) {
285
    private List<UserRole> addRole(List<UserRole> userRoles, int userId, int roleId) {
289
        if (userRoles == null) {
286
        if (userRoles == null) {
290
            userRoles = new ArrayList<>();
287
            userRoles = new ArrayList<>();
Line 806... Line 803...
806
        }
803
        }
807
        return addressIdAddressMap;
804
        return addressIdAddressMap;
808
    }
805
    }
809
 
806
 
810
    private List<Integer> toAddressIds(List<ShopAddress> shopAddresses) {
807
    private List<Integer> toAddressIds(List<ShopAddress> shopAddresses) {
811
        Function<ShopAddress, Integer> shopAddressToAddressIdFunction = new Function<ShopAddress, Integer>() {
-
 
812
            @Override
-
 
813
            public Integer apply(ShopAddress shopAddress) {
808
        return shopAddresses.stream()
814
                return shopAddress.getAddressId();
809
                .map(ShopAddress::getAddressId)
815
            }
-
 
816
        };
-
 
817
        return shopAddresses.stream().map(shopAddressToAddressIdFunction).collect(Collectors.toList());
810
                .collect(Collectors.toList());
818
    }
811
    }
819
 
812
 
820
    private Set<Integer> toShopIds(List<Shop> shops) {
813
    private Set<Integer> toShopIds(List<Shop> shops) {
821
        Function<Shop, Integer> shopToAddressIdFunction = new Function<Shop, Integer>() {
-
 
822
            @Override
814
        return shops.stream()
823
            public Integer apply(Shop shop) {
-
 
824
                return shop.getId();
815
                .map(Shop::getId)
825
            }
-
 
826
        };
-
 
827
        return shops.stream().map(shopToAddressIdFunction).collect(Collectors.toSet());
816
                .collect(Collectors.toSet());
828
    }
817
    }
829
 
818
 
830
    private String toString(List<UserRole> userRoles) throws ProfitMandiBusinessException {
819
    private String toString(List<UserRole> userRoles) throws ProfitMandiBusinessException {
831
        Set<Integer> roleIds = new HashSet<>();
820
        Set<Integer> roleIds = new HashSet<>();
832
        for (UserRole userRole : userRoles) {
821
        for (UserRole userRole : userRoles) {
Line 978... Line 967...
978
    }
967
    }
979
 
968
 
980
    @Override
969
    @Override
981
    @Cacheable(value = "getFofoRetailer", cacheManager = "thirtyMinsTimeOutCacheManager")
970
    @Cacheable(value = "getFofoRetailer", cacheManager = "thirtyMinsTimeOutCacheManager")
982
    public Map<Integer, CustomRetailer> getFofoRetailers(List<Integer> fofoIds) throws ProfitMandiBusinessException {
971
    public Map<Integer, CustomRetailer> getFofoRetailers(List<Integer> fofoIds) throws ProfitMandiBusinessException {
-
 
972
        if (fofoIds == null || fofoIds.isEmpty()) {
-
 
973
            return new HashMap<>();
-
 
974
        }
-
 
975
 
983
        List<com.spice.profitmandi.dao.entity.user.User> saholicUsers = userUserRepository.selectByIds(fofoIds);
976
        List<com.spice.profitmandi.dao.entity.user.User> saholicUsers = userUserRepository.selectByIds(fofoIds);
984
        Map<Integer, com.spice.profitmandi.dao.entity.user.User> userAddressMap = saholicUsers.stream().filter(x -> x.getAddressId() != null).collect(Collectors.toMap(x -> x.getAddressId(), x -> x));
977
        Map<Integer, com.spice.profitmandi.dao.entity.user.User> userAddressMap = saholicUsers.stream()
-
 
978
                .filter(x -> x.getAddressId() != null)
-
 
979
                .collect(Collectors.toMap(com.spice.profitmandi.dao.entity.user.User::getAddressId, x -> x));
985
        List<Address> addresses = addressRepository.selectByIds(new ArrayList<>(userAddressMap.keySet()));
980
        List<Address> addresses = addressRepository.selectByIds(new ArrayList<>(userAddressMap.keySet()));
-
 
981
 
-
 
982
        // Batch fetch all FofoStores to avoid N+1 queries
-
 
983
        Set<Integer> retailerIds = addresses.stream().map(Address::getRetaierId).collect(Collectors.toSet());
-
 
984
        Map<Integer, FofoStore> fofoStoreMap = fofoStoreRepository.selectByRetailerIds(new ArrayList<>(retailerIds))
-
 
985
                .stream().collect(Collectors.toMap(FofoStore::getId, fs -> fs));
-
 
986
 
-
 
987
        // Batch fetch all PrivateDealUsers to avoid N+1 queries
-
 
988
        Map<Integer, PrivateDealUser> privateDealUserMap = privateDealUserRepository.selectByIds(new ArrayList<>(retailerIds))
-
 
989
                .stream().collect(Collectors.toMap(PrivateDealUser::getId, pdu -> pdu));
-
 
990
 
-
 
991
        // Batch fetch all Counters to avoid N+1 queries
-
 
992
        Set<Integer> counterIds = privateDealUserMap.values().stream()
-
 
993
                .filter(pdu -> pdu.getCounterId() != null)
-
 
994
                .map(PrivateDealUser::getCounterId)
-
 
995
                .collect(Collectors.toSet());
-
 
996
        Map<Integer, Counter> counterMap = counterIds.isEmpty() ? Collections.emptyMap()
-
 
997
                : counterRepository.selectByIds(new ArrayList<>(counterIds))
-
 
998
                        .stream().collect(Collectors.toMap(Counter::getId, c -> c));
-
 
999
 
986
        Map<Integer, CustomRetailer> customRetailersMap = new HashMap<>();
1000
        Map<Integer, CustomRetailer> customRetailersMap = new HashMap<>();
987
        for (Address address : addresses) {
1001
        for (Address address : addresses) {
988
            com.spice.profitmandi.dao.entity.user.User user = userAddressMap.get(address.getId());
1002
            com.spice.profitmandi.dao.entity.user.User user = userAddressMap.get(address.getId());
989
            CustomRetailer customRetailer = new CustomRetailer();
-
 
990
            customRetailer.setEmail(user.getEmailId());
-
 
991
            customRetailer.setBusinessName(address.getName());
-
 
992
            customRetailer.setMobileNumber(address.getPhoneNumber());
-
 
993
            try {
-
 
994
                FofoStore fs = fofoStoreRepository.selectByRetailerId(address.getRetaierId());
1003
            FofoStore fs = fofoStoreMap.get(address.getRetaierId());
995
                customRetailer.setCode(fs.getCode());
-
 
996
                customRetailer.setAstId(fs.getAstId());
-
 
997
                customRetailer.setActivationType(fs.getActivationType());
-
 
998
                customRetailer.setCounterSize(fs.getCounterSize());
-
 
999
 
-
 
1000
 
-
 
1001
                customRetailer.setCounterPotential(fs.getCounterPotential());
-
 
1002
 
-
 
1003
                customRetailer.setWarehouseId(fs.getWarehouseId());
-
 
1004
                customRetailer.setPartnerId(fs.getId());
-
 
1005
                customRetailer.setFofoType(fs.getFofoType());
-
 
1006
            } catch (Exception e) {
1004
            if (fs == null) {
1007
                continue;
1005
                continue;
1008
            }
1006
            }
1009
            try {
-
 
1010
                customRetailer.setCartId(user.getActiveCartId());
-
 
1011
                PrivateDealUser pdu = privateDealUserRepository.selectById(address.getRetaierId());
-
 
1012
                Counter counter = counterRepository.selectById(pdu.getCounterId());
-
 
1013
                customRetailer.setGstNumber(counter.getGstin());
-
 
1014
            } catch (Exception e) {
-
 
1015
                customRetailer.setGstNumber(null);
-
 
1016
            }
1007
 
1017
            CustomAddress address1 = new CustomAddress();
-
 
1018
            address1.setCity(address.getCity());
-
 
1019
            address1.setState(address.getState());
-
 
1020
            address1.setLine1(address.getLine1());
-
 
1021
            address1.setLine2(address.getLine2());
-
 
1022
            address1.setPinCode(address.getPinCode());
-
 
1023
            address1.setName(address.getName());
-
 
1024
            customRetailer.setAddress(address1);
-
 
1025
            customRetailer.setDisplayName(
-
 
1026
                    customRetailer.getBusinessName() + "-" + customRetailer.getCode() + "- " + address1.getCity());
1008
            CustomRetailer customRetailer = buildCustomRetailer(user, address, fs, privateDealUserMap, counterMap);
1027
            customRetailersMap.put(customRetailer.getPartnerId(), customRetailer);
1009
            customRetailersMap.put(customRetailer.getPartnerId(), customRetailer);
1028
        }
1010
        }
1029
        return customRetailersMap;
1011
        return customRetailersMap;
1030
    }
1012
    }
1031
 
1013
 
-
 
1014
    /**
-
 
1015
     * Helper method to build CustomRetailer from fetched entities
-
 
1016
     */
-
 
1017
    private CustomRetailer buildCustomRetailer(com.spice.profitmandi.dao.entity.user.User user, Address address,
-
 
1018
                                                FofoStore fs, Map<Integer, PrivateDealUser> privateDealUserMap,
-
 
1019
                                                Map<Integer, Counter> counterMap) {
-
 
1020
        CustomRetailer customRetailer = new CustomRetailer();
-
 
1021
        customRetailer.setEmail(user.getEmailId());
-
 
1022
        customRetailer.setBusinessName(address.getName());
-
 
1023
        customRetailer.setMobileNumber(address.getPhoneNumber());
-
 
1024
        customRetailer.setCode(fs.getCode());
-
 
1025
        customRetailer.setAstId(fs.getAstId());
-
 
1026
        customRetailer.setActivationType(fs.getActivationType());
-
 
1027
        customRetailer.setCounterSize(fs.getCounterSize());
-
 
1028
        customRetailer.setCounterPotential(fs.getCounterPotential());
-
 
1029
        customRetailer.setWarehouseId(fs.getWarehouseId());
-
 
1030
        customRetailer.setPartnerId(fs.getId());
-
 
1031
        customRetailer.setFofoType(fs.getFofoType());
-
 
1032
        customRetailer.setCartId(user.getActiveCartId());
-
 
1033
 
-
 
1034
        // Get GST number from pre-fetched maps
-
 
1035
        PrivateDealUser pdu = privateDealUserMap.get(address.getRetaierId());
-
 
1036
        if (pdu != null && pdu.getCounterId() != null) {
-
 
1037
            Counter counter = counterMap.get(pdu.getCounterId());
-
 
1038
            customRetailer.setGstNumber(counter != null ? counter.getGstin() : null);
-
 
1039
        }
-
 
1040
 
-
 
1041
        CustomAddress customAddress = buildCustomAddress(address);
-
 
1042
        customRetailer.setAddress(customAddress);
-
 
1043
        customRetailer.setDisplayName(customRetailer.getBusinessName() + "-" + customRetailer.getCode() + "- " + customAddress.getCity());
-
 
1044
        return customRetailer;
-
 
1045
    }
-
 
1046
 
-
 
1047
    /**
-
 
1048
     * Helper method to build CustomAddress from Address entity
-
 
1049
     */
-
 
1050
    private CustomAddress buildCustomAddress(Address address) {
-
 
1051
        CustomAddress customAddress = new CustomAddress();
-
 
1052
        customAddress.setCity(address.getCity());
-
 
1053
        customAddress.setState(address.getState());
-
 
1054
        customAddress.setLine1(address.getLine1());
-
 
1055
        customAddress.setLine2(address.getLine2());
-
 
1056
        customAddress.setPinCode(address.getPinCode());
-
 
1057
        customAddress.setName(address.getName());
-
 
1058
        customAddress.setPhoneNumber(address.getPhoneNumber());
-
 
1059
        return customAddress;
-
 
1060
    }
-
 
1061
 
1032
    @Override
1062
    @Override
1033
    @Cacheable(value = "getFofoRetailer", cacheManager = "thirtyMinsTimeOutCacheManager")
1063
    @Cacheable(value = "getFofoRetailer", cacheManager = "thirtyMinsTimeOutCacheManager")
1034
    public CustomRetailer getFofoRetailer(int fofoId) throws ProfitMandiBusinessException {
1064
    public CustomRetailer getFofoRetailer(int fofoId) throws ProfitMandiBusinessException {
1035
        com.spice.profitmandi.dao.entity.user.User saholicUser = userUserRepository.selectById(fofoId);
1065
        com.spice.profitmandi.dao.entity.user.User saholicUser = userUserRepository.selectById(fofoId);
1036
 
-
 
1037
        User dtrUser = userRepository.selectByEmailId(saholicUser.getEmailId());
1066
        User dtrUser = userRepository.selectByEmailId(saholicUser.getEmailId());
1038
        FofoStore store = fofoStoreRepository.selectByRetailerId(fofoId);
1067
        FofoStore store = fofoStoreRepository.selectByRetailerId(fofoId);
1039
        Address address = null;
-
 
1040
        try {
-
 
1041
            address = addressRepository.selectById(saholicUser.getAddressId());
1068
        Address address = addressRepository.selectById(saholicUser.getAddressId());
1042
        } catch (ProfitMandiBusinessException e1) {
-
 
1043
            // TODO Auto-generated catch block
-
 
1044
            e1.printStackTrace();
-
 
1045
        }
1069
 
1046
        Map<Integer, CustomRetailer> customRetailersMap = new HashMap<>();
-
 
1047
        CustomRetailer customRetailer = new CustomRetailer();
1070
        CustomRetailer customRetailer = new CustomRetailer();
1048
        customRetailer.setEmail(saholicUser.getEmailId());
1071
        customRetailer.setEmail(saholicUser.getEmailId());
1049
        customRetailer.setBusinessName(address.getName());
1072
        customRetailer.setBusinessName(address.getName());
1050
        customRetailer.setMobileNumber(address.getPhoneNumber());
1073
        customRetailer.setMobileNumber(address.getPhoneNumber());
1051
        customRetailer.setFirstName(dtrUser.getFirstName());
1074
        customRetailer.setFirstName(dtrUser.getFirstName());
1052
        customRetailer.setLastName(dtrUser.getLastName());
1075
        customRetailer.setLastName(dtrUser.getLastName());
-
 
1076
        customRetailer.setCartId(saholicUser.getActiveCartId());
-
 
1077
        customRetailer.setPartnerId(address.getRetaierId());
-
 
1078
        customRetailer.setCode(store.getCode());
-
 
1079
        customRetailer.setAstId(store.getAstId());
-
 
1080
        customRetailer.setActivationType(store.getActivationType());
-
 
1081
        customRetailer.setWarehouseId(store.getWarehouseId());
-
 
1082
 
-
 
1083
        // Get GST number
1053
        try {
1084
        try {
1054
            customRetailer.setCartId(saholicUser.getActiveCartId());
-
 
1055
            PrivateDealUser pdu = privateDealUserRepository.selectById(address.getRetaierId());
1085
            PrivateDealUser pdu = privateDealUserRepository.selectById(address.getRetaierId());
-
 
1086
            if (pdu != null && pdu.getCounterId() != null) {
1056
            Counter counter = counterRepository.selectById(pdu.getCounterId());
1087
                Counter counter = counterRepository.selectById(pdu.getCounterId());
1057
            customRetailer.setGstNumber(counter.getGstin());
1088
                customRetailer.setGstNumber(counter != null ? counter.getGstin() : null);
-
 
1089
            }
1058
        } catch (Exception e) {
1090
        } catch (Exception e) {
1059
            customRetailer.setGstNumber(null);
1091
            customRetailer.setGstNumber(null);
1060
        }
1092
        }
-
 
1093
 
1061
        CustomAddress address1 = new CustomAddress();
1094
        // Reuse helper method for CustomAddress
1062
        address1.setCity(address.getCity());
1095
        CustomAddress customAddress = buildCustomAddress(address);
1063
        address1.setState(address.getState());
-
 
1064
        address1.setLine1(address.getLine1());
-
 
1065
        address1.setLine2(address.getLine2());
-
 
1066
        address1.setPinCode(address.getPinCode());
1096
        customRetailer.setAddress(customAddress);
1067
        address1.setPhoneNumber(address.getPhoneNumber());
-
 
1068
        address1.setName(address.getName());
-
 
1069
        customRetailer.setDisplayName(address.getName() + " - " + address.getCity());
1097
        customRetailer.setDisplayName(address.getName() + " - " + address.getCity());
1070
        customRetailer.setAddress(address1);
-
 
1071
        customRetailer.setPartnerId(address.getRetaierId());
-
 
1072
        customRetailer.setCode(store.getCode());
-
 
1073
        customRetailer.setAstId(store.getAstId());
-
 
1074
        customRetailer.setActivationType(store.getActivationType());
-
 
1075
        customRetailer.setWarehouseId(store.getWarehouseId());
-
 
1076
        customRetailersMap.put(address.getRetaierId(), customRetailer);
-
 
1077
        return customRetailer;
-
 
1078
 
1098
 
-
 
1099
        return customRetailer;
1079
    }
1100
    }
1080
 
1101
 
1081
    @Override
1102
    @Override
1082
    public Map<Integer, CustomRetailer> getFofoRetailerUserId(List<Integer> fofoIds) throws ProfitMandiBusinessException {
1103
    public Map<Integer, CustomRetailer> getFofoRetailerUserId(List<Integer> fofoIds) throws ProfitMandiBusinessException {
-
 
1104
        if (fofoIds == null || fofoIds.isEmpty()) {
-
 
1105
            return new HashMap<>();
-
 
1106
        }
-
 
1107
 
1083
        List<com.spice.profitmandi.dao.entity.user.User> saholicUsers = userUserRepository.selectByIds(fofoIds);
1108
        List<com.spice.profitmandi.dao.entity.user.User> saholicUsers = userUserRepository.selectByIds(fofoIds);
1084
        Map<Integer, com.spice.profitmandi.dao.entity.user.User> userAddressMap = saholicUsers.stream().filter(x -> x.getAddressId() != null).collect(Collectors.toMap(x -> x.getAddressId(), x -> x));
1109
        Map<Integer, com.spice.profitmandi.dao.entity.user.User> userAddressMap = saholicUsers.stream()
-
 
1110
                .filter(x -> x.getAddressId() != null)
-
 
1111
                .collect(Collectors.toMap(com.spice.profitmandi.dao.entity.user.User::getAddressId, x -> x));
1085
        List<Address> addresses = addressRepository.selectByIds(new ArrayList<>(userAddressMap.keySet()));
1112
        List<Address> addresses = addressRepository.selectByIds(new ArrayList<>(userAddressMap.keySet()));
-
 
1113
 
-
 
1114
        // Batch fetch all retailerIds to userIds mapping to avoid N+1
-
 
1115
        Set<Integer> retailerIds = addresses.stream().map(Address::getRetaierId).collect(Collectors.toSet());
-
 
1116
        Map<Integer, Integer> retailerIdToUserIdMap = getUserIdRetailerIdMapReverse(retailerIds);
-
 
1117
 
-
 
1118
        // Batch fetch all PrivateDealUsers to avoid N+1 queries
-
 
1119
        Map<Integer, PrivateDealUser> privateDealUserMap = privateDealUserRepository.selectByIds(new ArrayList<>(retailerIds))
-
 
1120
                .stream().collect(Collectors.toMap(PrivateDealUser::getId, pdu -> pdu));
-
 
1121
 
-
 
1122
        // Batch fetch all Counters to avoid N+1 queries
-
 
1123
        Set<Integer> counterIds = privateDealUserMap.values().stream()
-
 
1124
                .filter(pdu -> pdu.getCounterId() != null)
-
 
1125
                .map(PrivateDealUser::getCounterId)
-
 
1126
                .collect(Collectors.toSet());
-
 
1127
        Map<Integer, Counter> counterMap = counterIds.isEmpty() ? Collections.emptyMap()
-
 
1128
                : counterRepository.selectByIds(new ArrayList<>(counterIds))
-
 
1129
                        .stream().collect(Collectors.toMap(Counter::getId, c -> c));
-
 
1130
 
1086
        Map<Integer, CustomRetailer> customRetailersMap = new HashMap<>();
1131
        Map<Integer, CustomRetailer> customRetailersMap = new HashMap<>();
1087
        for (Address address : addresses) {
1132
        for (Address address : addresses) {
1088
            com.spice.profitmandi.dao.entity.user.User user = userAddressMap.get(address.getId());
1133
            com.spice.profitmandi.dao.entity.user.User user = userAddressMap.get(address.getId());
-
 
1134
            Integer userId = retailerIdToUserIdMap.get(user.getId());
-
 
1135
            if (userId == null) {
-
 
1136
                continue;
-
 
1137
            }
-
 
1138
 
1089
            CustomRetailer customRetailer = new CustomRetailer();
1139
            CustomRetailer customRetailer = new CustomRetailer();
1090
            customRetailer.setEmail(user.getEmailId());
1140
            customRetailer.setEmail(user.getEmailId());
1091
            customRetailer.setBusinessName(address.getName());
1141
            customRetailer.setBusinessName(address.getName());
1092
            customRetailer.setMobileNumber(address.getPhoneNumber());
1142
            customRetailer.setMobileNumber(address.getPhoneNumber());
1093
            int userId = 0;
-
 
1094
            try {
-
 
1095
                userId = userAccountRepository.selectUserIdByRetailerId(user.getId());
-
 
1096
            } catch (ProfitMandiBusinessException e) {
-
 
1097
                continue;
-
 
1098
            }
-
 
1099
            try {
-
 
1100
                customRetailer.setCartId(user.getActiveCartId());
1143
            customRetailer.setCartId(user.getActiveCartId());
1101
                PrivateDealUser pdu = privateDealUserRepository.selectById(address.getRetaierId());
-
 
1102
                Counter counter = counterRepository.selectById(pdu.getCounterId());
-
 
1103
                customRetailer.setGstNumber(counter.getGstin());
-
 
1104
            } catch (Exception e) {
-
 
1105
                customRetailer.setGstNumber(null);
-
 
1106
            }
-
 
1107
            CustomAddress address1 = new CustomAddress();
-
 
1108
            address1.setCity(address.getCity());
-
 
1109
            address1.setState(address.getState());
-
 
1110
            address1.setLine1(address.getLine1());
-
 
1111
            address1.setLine2(address.getLine2());
-
 
1112
            address1.setPinCode(address.getPinCode());
-
 
1113
            address1.setName(address.getName());
-
 
1114
            customRetailer.setAddress(address1);
-
 
1115
            customRetailer.setPartnerId(address.getRetaierId());
1144
            customRetailer.setPartnerId(address.getRetaierId());
-
 
1145
 
-
 
1146
            // Get GST number from pre-fetched maps
-
 
1147
            PrivateDealUser pdu = privateDealUserMap.get(address.getRetaierId());
-
 
1148
            if (pdu != null && pdu.getCounterId() != null) {
-
 
1149
                Counter counter = counterMap.get(pdu.getCounterId());
-
 
1150
                customRetailer.setGstNumber(counter != null ? counter.getGstin() : null);
-
 
1151
            }
-
 
1152
 
-
 
1153
            CustomAddress customAddress = buildCustomAddress(address);
-
 
1154
            customRetailer.setAddress(customAddress);
1116
            customRetailer.setDisplayName(customRetailer.getBusinessName() + "-" + address1.getCity());
1155
            customRetailer.setDisplayName(customRetailer.getBusinessName() + "-" + customAddress.getCity());
1117
            customRetailersMap.put(userId, customRetailer);
1156
            customRetailersMap.put(userId, customRetailer);
1118
        }
1157
        }
1119
        return customRetailersMap;
1158
        return customRetailersMap;
1120
    }
1159
    }
1121
 
1160
 
-
 
1161
    /**
-
 
1162
     * Helper method to get retailerId to userId mapping
-
 
1163
     */
-
 
1164
    private Map<Integer, Integer> getUserIdRetailerIdMapReverse(Set<Integer> retailerIds) {
-
 
1165
        List<UserAccount> userAccounts = userAccountRepository.selectAllSaholicByRetailerIds(retailerIds);
-
 
1166
        return userAccounts.stream().collect(Collectors.toMap(UserAccount::getAccountKey, UserAccount::getUserId));
-
 
1167
    }
-
 
1168
 
1122
    @Override
1169
    @Override
1123
    public Map<Integer, String> getAllFofoRetailerIdEmailIdMap() throws ProfitMandiBusinessException {
1170
    public Map<Integer, String> getAllFofoRetailerIdEmailIdMap() throws ProfitMandiBusinessException {
1124
        Role roleFofo = null;
1171
        Role roleFofo = null;
1125
        try {
1172
        try {
1126
            roleFofo = roleRepository.selectByName(RoleType.FOFO.toString());
1173
            roleFofo = roleRepository.selectByName(RoleType.FOFO.toString());
Line 1214... Line 1261...
1214
    }
1261
    }
1215
 
1262
 
1216
    @Override
1263
    @Override
1217
    @Cacheable(value = "retailerNames", cacheManager = "thirtyMinsTimeOutCacheManager")
1264
    @Cacheable(value = "retailerNames", cacheManager = "thirtyMinsTimeOutCacheManager")
1218
    public Map<Integer, CustomRetailer> getFofoRetailers(boolean activeOnly) throws ProfitMandiBusinessException {
1265
    public Map<Integer, CustomRetailer> getFofoRetailers(boolean activeOnly) throws ProfitMandiBusinessException {
1219
        // TODO Auto-generated method stub
1266
        Stream<FofoStore> storeStream = fofoStoreRepository.selectAll().stream()
1220
        Stream<FofoStore> storeStream = fofoStoreRepository.selectAll().stream().filter(x -> x.getFofoType().equals(FofoType.FRANCHISE));
1267
                .filter(fs -> fs.getFofoType().equals(FofoType.FRANCHISE));
1221
        if (activeOnly) {
1268
        if (activeOnly) {
1222
            storeStream = storeStream.filter(x -> x.isActive());
1269
            storeStream = storeStream.filter(FofoStore::isActive);
1223
        }
1270
        }
1224
        List<Integer> storeIds = storeStream.map(x -> x.getId()).collect(Collectors.toList());
1271
        List<Integer> storeIds = storeStream.map(FofoStore::getId).collect(Collectors.toList());
1225
        return this.getFofoRetailers(storeIds);
1272
        return this.getFofoRetailers(storeIds);
1226
    }
1273
    }
1227
 
1274
 
1228
    @Override
1275
    @Override
1229
    @Cacheable(value = "retailerNames", cacheManager = "thirtyMinsTimeOutCacheManager")
1276
    @Cacheable(value = "retailerNames", cacheManager = "thirtyMinsTimeOutCacheManager")
1230
    public Map<Integer, CustomRetailer> getFofoRetailers(boolean activeOnly, String pinCode) throws ProfitMandiBusinessException {
1277
    public Map<Integer, CustomRetailer> getFofoRetailers(boolean activeOnly, String pinCode) throws ProfitMandiBusinessException {
1231
        List<PincodePartner> pincodePartners = pincodePartnerRepository.selectPartnersByPincode(pinCode);
1278
        List<PincodePartner> pincodePartners = pincodePartnerRepository.selectPartnersByPincode(pinCode);
1232
        List<Integer> storeIds = new ArrayList<>();
1279
        List<Integer> storeIds = new ArrayList<>();
1233
        if (pincodePartners.size() > 0) {
1280
        if (!pincodePartners.isEmpty()) {
1234
            List<Integer> fofoIds = pincodePartners.stream().map(x -> x.getFofoId()).collect(Collectors.toList());
1281
            List<Integer> fofoIds = pincodePartners.stream().map(PincodePartner::getFofoId).collect(Collectors.toList());
1235
            List<FofoStore> fofoStores = fofoStoreRepository.selectActivePartnersByRetailerIds(fofoIds);
1282
            List<FofoStore> fofoStores = fofoStoreRepository.selectActivePartnersByRetailerIds(fofoIds);
1236
            storeIds = fofoStores.stream().map(x -> x.getId()).collect(Collectors.toList());
1283
            storeIds = fofoStores.stream().map(FofoStore::getId).collect(Collectors.toList());
1237
        }
1284
        }
1238
        return this.getFofoRetailers(storeIds);
1285
        return this.getFofoRetailers(storeIds);
1239
    }
1286
    }
1240
 
1287
 
1241
    @Override
1288
    @Override
1242
    @Cacheable(value = "allFofoRetailers", cacheManager = "thirtyMinsTimeOutCacheManager")
1289
    @Cacheable(value = "allFofoRetailers", cacheManager = "thirtyMinsTimeOutCacheManager")
1243
    public Map<Integer, CustomRetailer> getAllFofoRetailers() throws ProfitMandiBusinessException {
1290
    public Map<Integer, CustomRetailer> getAllFofoRetailers() throws ProfitMandiBusinessException {
-
 
1291
        List<Integer> storeIds = fofoStoreRepository.selectAll().stream()
1244
        // TODO Auto-generated method stub
1292
                .map(FofoStore::getId)
1245
        List<Integer> storeIds = fofoStoreRepository.selectAll().stream().map(x -> x.getId()).collect(Collectors.toList());
1293
                .collect(Collectors.toList());
1246
        return this.getFofoRetailers(storeIds);
1294
        return this.getFofoRetailers(storeIds);
1247
    }
1295
    }
1248
 
1296
 
1249
    @Override
1297
    @Override
1250
    @Cacheable(value = "allFofoRetailersInternalFalse", cacheManager = "thirtyMinsTimeOutCacheManager")
1298
    @Cacheable(value = "allFofoRetailersInternalFalse", cacheManager = "thirtyMinsTimeOutCacheManager")
1251
    public Map<Integer, CustomRetailer> getAllFofoRetailersInternalFalse() throws ProfitMandiBusinessException {
1299
    public Map<Integer, CustomRetailer> getAllFofoRetailersInternalFalse() throws ProfitMandiBusinessException {
1252
        // TODO Auto-generated method stub
1300
        List<Integer> storeIds = fofoStoreRepository.selectAll().stream()
1253
        List<Integer> storeIds = fofoStoreRepository.selectAll().stream().filter(x -> x.getFofoType().equals(FofoType.FRANCHISE)).map(x -> x.getId()).collect(Collectors.toList());
1301
                .filter(fs -> fs.getFofoType().equals(FofoType.FRANCHISE))
-
 
1302
                .map(FofoStore::getId)
-
 
1303
                .collect(Collectors.toList());
1254
        return this.getFofoRetailers(storeIds);
1304
        return this.getFofoRetailers(storeIds);
1255
    }
1305
    }
1256
 
1306
 
1257
    @Override
1307
    @Override
1258
    public LoginRequestResponseModel registerWebUser(LoginRequestResponseModel loginRequestModel) throws
1308
    public LoginRequestResponseModel registerWebUser(LoginRequestResponseModel loginRequestModel) throws
Line 1301... Line 1351...
1301
 
1351
 
1302
    @Override
1352
    @Override
1303
    @Cacheable(value = "fofoIdUrl", cacheManager = "thirtyMinsTimeOutCacheManager")
1353
    @Cacheable(value = "fofoIdUrl", cacheManager = "thirtyMinsTimeOutCacheManager")
1304
    public Map<Integer, String> getAllFofoRetailerIdUrlMap() {
1354
    public Map<Integer, String> getAllFofoRetailerIdUrlMap() {
1305
        Map<Integer, String> fofoRetailerUrlMap = new HashMap<>();
1355
        Map<Integer, String> fofoRetailerUrlMap = new HashMap<>();
1306
        List<FofoStore> stores = fofoStoreRepository.selectAll().stream().filter(x -> x.isActive()).collect(Collectors.toList());
1356
        List<FofoStore> stores = fofoStoreRepository.selectAll().stream()
-
 
1357
                .filter(FofoStore::isActive)
-
 
1358
                .collect(Collectors.toList());
-
 
1359
 
-
 
1360
        // Batch fetch all district masters to avoid N+1 queries
-
 
1361
        List<DistrictMaster> allDistrictMasters = districtMasterRepository.selectAll();
1307
        Map<Integer, com.spice.profitmandi.dao.entity.user.User> userMap = userUserRepository.selectByIds(stores.stream().map(x -> x.getId()).collect(Collectors.toList())).stream().collect(Collectors.toMap(x -> x.getId(), x -> x));
1362
        Map<String, DistrictMaster> districtMasterMap = allDistrictMasters.stream()
-
 
1363
                .collect(Collectors.toMap(
-
 
1364
                        dm -> dm.getStateShortName() + "_" + dm.getShortName(),
-
 
1365
                        dm -> dm,
-
 
1366
                        (existing, replacement) -> existing));
-
 
1367
 
-
 
1368
        // Get fallback district master
-
 
1369
        DistrictMaster fallbackDistrict = districtMasterMap.get("HR_FB");
-
 
1370
 
1308
        for (FofoStore store : stores) {
1371
        for (FofoStore store : stores) {
-
 
1372
            String codeWithoutDigits = store.getCode().replaceAll("\\d+", "");
-
 
1373
            if (codeWithoutDigits.length() < 3) {
1309
            LOGGER.info("Store is {}", store);
1374
                LOGGER.warn("Invalid store code format: {}", store.getCode());
-
 
1375
                continue;
-
 
1376
            }
1310
            String districtShortName = store.getCode().replaceAll("\\d+", "").substring(2);
1377
            String stateShortName = codeWithoutDigits.substring(0, 2);
1311
            String stateShortName = store.getCode().replaceAll("\\d+", "").substring(0, 2);
1378
            String districtShortName = codeWithoutDigits.substring(2);
-
 
1379
 
1312
            DistrictMaster districtMaster = null;
1380
            // Use pre-fetched map instead of N+1 query
1313
            LOGGER.info("Store shortname is {}", districtShortName);
1381
            String key = stateShortName + "_" + districtShortName;
1314
            districtMaster = districtMasterRepository.selectByShortnameNameAndStateShortName(districtShortName, stateShortName);
1382
            DistrictMaster districtMaster = districtMasterMap.getOrDefault(key, fallbackDistrict);
-
 
1383
 
1315
            if (districtMaster == null) {
1384
            if (districtMaster == null) {
1316
                districtMaster = districtMasterRepository.selectByShortnameNameAndStateShortName("FB", "HR");
1385
                LOGGER.warn("No district master found for key: {} and no fallback available", key);
-
 
1386
                continue;
1317
            }
1387
            }
1318
            LOGGER.info("Store id - {}", store.getId());
-
 
1319
            // com.spice.profitmandi.dao.entity.user.User user = userMap.get(store.getId());
-
 
1320
            String urlString = districtMaster.getStateShortName() + "/" + Utils.getHyphenatedString(districtMaster.getName()) + "/" + store.getCode();
-
 
1321
            fofoRetailerUrlMap.put(store.getId(), urlString.toLowerCase());
-
 
1322
            LOGGER.info("Store is {} end", store);
-
 
1323
 
1388
 
-
 
1389
            String urlString = districtMaster.getStateShortName() + "/" +
-
 
1390
                    Utils.getHyphenatedString(districtMaster.getName()) + "/" + store.getCode();
-
 
1391
            fofoRetailerUrlMap.put(store.getId(), urlString.toLowerCase());
1324
        }
1392
        }
1325
        return fofoRetailerUrlMap;
1393
        return fofoRetailerUrlMap;
1326
    }
1394
    }
1327
 
1395
 
1328
    @Override
1396
    @Override