Rev 23025 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed
package com.spice.profitmandi.service.user;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Set;import java.util.function.Function;import java.util.stream.Collectors;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;import com.spice.profitmandi.dao.entity.dtr.Retailer;import com.spice.profitmandi.dao.entity.dtr.Shop;import com.spice.profitmandi.dao.entity.dtr.ShopAddress;import com.spice.profitmandi.dao.entity.dtr.User;import com.spice.profitmandi.dao.entity.dtr.UserRole;import com.spice.profitmandi.dao.entity.user.Address;import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;import com.spice.profitmandi.dao.repository.dtr.RetailerRegisteredAddressRepository;import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;import com.spice.profitmandi.dao.repository.dtr.ShopAddressRepository;import com.spice.profitmandi.dao.repository.dtr.ShopRepository;import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;import com.spice.profitmandi.dao.repository.dtr.UserRepository;import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;import com.spice.profitmandi.dao.repository.user.AddressRepository;@Componentpublic class RetailerServiceImpl implements RetailerService {@Autowiredprivate RetailerRepository retailerRepository;@Autowiredprivate UserAccountRepository userAccountRepository;@Autowiredprivate UserRepository userRepository;@Autowiredprivate RetailerRegisteredAddressRepository retailerRegisteredAddressRepository;@Autowiredprivate AddressRepository addressRepository;@Autowiredprivate ShopRepository shopRepository;@Autowiredprivate ShopAddressRepository shopAddressRepository;@Autowiredprivate UserRoleRepository userRoleRepository;@Autowiredprivate DocumentRepository documentRepository;@Overridepublic Map<String, Object> getByEmailIdOrMobileNumber(String emailIdOrMobileNumber)throws ProfitMandiBusinessException {User user = userRepository.selectByEmailIdOrMobileNumber(emailIdOrMobileNumber);int retailerId = userAccountRepository.selectRetailerIdByUserId(user.getId());Retailer retailer = retailerRepository.selectById(retailerId);int retailerAddressId = retailerRegisteredAddressRepository.selectAddressIdByRetailerId(retailer.getId());Address retailerAddress = addressRepository.selectById(retailerAddressId);List<Shop> shops = shopRepository.selectByRetailerId(retailer.getId());this.addAddress(shops);//Map<Integer, Address> shopIdAddressMap = this.getShopAddressesMap(shops);List<UserRole> userRoles = userRoleRepository.selectByUserId(user.getId());Map<String, Object> map = new HashMap<>();map.put("user", user);map.put("retailer", retailer);map.put("retailerAddress", retailerAddress);map.put("shops", shops);map.put("userRoles", this.toString(userRoles));return map;}@Overridepublic Map<String, Object> updateRetailerDocument(String emailIdOrMobileNumber, int documentId)throws ProfitMandiBusinessException {Map<String, Object> map = this.getByEmailIdOrMobileNumber(emailIdOrMobileNumber);Retailer retailer = (Retailer)map.get("retailer");retailer.setDocumentId(documentId);retailerRepository.persist(retailer);documentRepository.markDocumentAsPersisted(documentId);return map;}@SuppressWarnings("unchecked")@Overridepublic Map<String, Object> updateRetailerShopDocument(String emailIdOrMobileNumber, int shopId, int documentId)throws ProfitMandiBusinessException {Map<String, Object> map = this.getByEmailIdOrMobileNumber(emailIdOrMobileNumber);List<Shop> shops = (List<Shop>)map.get("shops");Shop foundShop = null;for(Shop shop : shops){if(shop.getId() == shopId){foundShop = shop;break;}}if(foundShop != null){foundShop.setDocumentId(documentId);shopRepository.persist(foundShop);documentRepository.markDocumentAsPersisted(documentId);}return map;}private void addAddress(List<Shop> shops){Set<Integer> shopIds = this.toShopIds(shops);if(shopIds.isEmpty()){return;}List<ShopAddress> shopAddresses = shopAddressRepository.selectByShopIds(shopIds);Map<Integer, Address> addressIdAddressMap = this.toAddressIdAddressMap(shopAddresses);for(Shop shop : shops){shop.setAddress(addressIdAddressMap.get(shop.getAddressId()));}}private Map<Integer, Address> toAddressIdAddressMap(List<ShopAddress> shopAddresses){Map<Integer, Address> addressIdAddressMap = new HashMap<>();List<Integer> addressIds = this.toAddressIds(shopAddresses);List<Address> addresses = addressRepository.selectByIds(addressIds);for(Address address : addresses){addressIdAddressMap.put(address.getId(), address);}return addressIdAddressMap;}private List<Integer> toAddressIds(List<ShopAddress> shopAddresses){Function<ShopAddress, Integer> shopAddressToAddressIdFunction = new Function<ShopAddress, Integer>(){@Overridepublic Integer apply(ShopAddress shopAddress) {return shopAddress.getAddressId();}};return shopAddresses.stream().map(shopAddressToAddressIdFunction).collect(Collectors.toList());}private Set<Integer> toShopIds(List<Shop> shops){Function<Shop, Integer> shopToAddressIdFunction = new Function<Shop, Integer>(){@Overridepublic Integer apply(Shop shop) {return shop.getId();}};return shops.stream().map(shopToAddressIdFunction).collect(Collectors.toSet());}private String toString(List<UserRole> userRoles){Function<UserRole, String> userRoleToRoleNameFunction = new Function<UserRole, String>(){public String apply(UserRole userRole) {return userRole.getRoleType().toString();};};Set<String> userRoleStrings = userRoles.stream().map(userRoleToRoleNameFunction).collect(Collectors.toSet());return String.join(", ", userRoleStrings);}}