Rev 22980 | Rev 23031 | 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.common.model.CustomAddress;import com.spice.profitmandi.common.model.CustomShop;import com.spice.profitmandi.common.model.UpdateRetailerRequest;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;}@SuppressWarnings("unchecked")@Overridepublic Map<String, Object> updateRetailerDetails(UpdateRetailerRequest updateRetailerRequest)throws ProfitMandiBusinessException {Map<String, Object> map = this.getByEmailIdOrMobileNumber(updateRetailerRequest.getEmailIdOrMobileNumber());Retailer retailer = (Retailer)map.get("retailer");this.updateRetailer(retailer, updateRetailerRequest);Address retailerAddress = (Address)map.get("retailerAddress");this.updateAddress(retailerAddress, updateRetailerRequest.getAddress());List<Shop> shops = (List<Shop>)map.get("shops");this.updateRetailerShops(shops, updateRetailerRequest.getShops());return map;}private void updateRetailer(Retailer retailer, UpdateRetailerRequest updateRetailerRequest) throws ProfitMandiBusinessException{retailer.setName(updateRetailerRequest.getName());retailer.setNumber(updateRetailerRequest.getNumber());if(updateRetailerRequest.getDocumentId() > 0){retailer.setDocumentId(updateRetailerRequest.getDocumentId());}retailerRepository.persist(retailer);}private void updateAddress(Address address, CustomAddress customAddress){address.setName(customAddress.getName());address.setLine1(customAddress.getLine1());address.setLine2(customAddress.getLine2());address.setCity(customAddress.getCity());address.setPinCode(customAddress.getPinCode());address.setState(customAddress.getState());addressRepository.persist(address);}private void updateRetailerShops(List<Shop> shops, Set<CustomShop> customShops) throws ProfitMandiBusinessException{//Shop foundShop = null;for(Shop shop : shops){for(CustomShop customShop : customShops){if(shop.getId() == customShop.getShopId()){if(customShop.getDocumentId() > 0){shop.setDocumentId(customShop.getDocumentId());shopRepository.persist(shop);documentRepository.markDocumentAsPersisted(customShop.getDocumentId());}this.updateAddress(shop.getAddress(), customShop.getAddress());}}}}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);}}