Rev 23031 | Rev 23046 | 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.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;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.RetailerRegisteredAddress;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.entity.user.Cart;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;import com.spice.profitmandi.dao.repository.user.CartRepository;import in.shop2020.model.v1.user.CartStatus;@Componentpublic class RetailerServiceImpl implements RetailerService {private static final Logger LOGGER = LoggerFactory.getLogger(RetailerServiceImpl.class);@Autowiredprivate RetailerRepository retailerRepository;@Autowiredprivate UserAccountRepository userAccountRepository;@Autowiredprivate UserRepository userRepository;@Autowiredprivate CartRepository cartRepository;@Autowiredprivate RetailerRegisteredAddressRepository retailerRegisteredAddressRepository;@Autowiredprivate AddressRepository addressRepository;@Autowiredprivate ShopRepository shopRepository;@Autowiredprivate ShopAddressRepository shopAddressRepository;@Autowiredprivate UserRoleRepository userRoleRepository;@Autowiredprivate DocumentRepository documentRepository;@Autowired@Qualifier("userUserRepository")private com.spice.profitmandi.dao.repository.user.UserRepository userUserRepository;@Overridepublic Map<String, Object> getByEmailIdOrMobileNumber(String emailIdOrMobileNumber)throws ProfitMandiBusinessException {User user = userRepository.selectByEmailIdOrMobileNumber(emailIdOrMobileNumber);List<UserRole> userRoles = userRoleRepository.selectByUserId(user.getId());Map<String, Object> map = new HashMap<>();map.put("user", user);//map.put("retailer", retailer);map.put("userRoles", this.toString(userRoles));try{int retailerId = userAccountRepository.selectRetailerIdByUserId(user.getId());Retailer retailer = retailerRepository.selectById(retailerId);map.put("retailer", retailer);try{int retailerAddressId = retailerRegisteredAddressRepository.selectAddressIdByRetailerId(retailer.getId());Address retailerAddress = addressRepository.selectById(retailerAddressId);map.put("retailerAddress", retailerAddress);List<Shop> shops = shopRepository.selectByRetailerId(retailer.getId());map.put("shops", shops);this.addAddress(shops);}catch(ProfitMandiBusinessException profitMandiBusinessException){LOGGER.error("Retailer Registered Address not found");}}catch(ProfitMandiBusinessException profitMandiBusinessException){LOGGER.error("Retailer not found");}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());User user = (User)map.get("user");Retailer retailer = (Retailer)map.get("retailer");this.updateRetailer(user.getEmailId(), retailer, updateRetailerRequest);Address retailerAddress = (Address)map.get("retailerAddress");this.updateRetailerAddress(retailerAddress, updateRetailerRequest.getAddress(), retailer.getId());List<Shop> shops = (List<Shop>)map.get("shops");this.updateRetailerShops(shops, updateRetailerRequest.getShops(), retailer.getId());return map;}private int createSaholicUser(String emailId, String retailerName){com.spice.profitmandi.dao.entity.user.User saholicUser = null;try {saholicUser = userUserRepository.selectByEmailId(emailId);}catch (ProfitMandiBusinessException e) {LOGGER.info("User doesnt exist in old system");}if(saholicUser == null){Cart cart = new Cart();cart.setCartStatus(CartStatus.ACTIVE);cartRepository.persist(cart);saholicUser = new com.spice.profitmandi.dao.entity.user.User();saholicUser.setEmailId(emailId);saholicUser.setName(retailerName);saholicUser.setActiveCartId(cart.getId());userUserRepository.persist(saholicUser);}return saholicUser.getId();}private void updateRetailer(String emailId, Retailer retailer, UpdateRetailerRequest updateRetailerRequest) throws ProfitMandiBusinessException{if(retailer == null){int saholicUserId = this.createSaholicUser(emailId, updateRetailerRequest.getName());retailer = new Retailer();retailer.setId(saholicUserId);}retailer.setName(updateRetailerRequest.getName());retailer.setNumber(updateRetailerRequest.getNumber());if(updateRetailerRequest.getDocumentId() > 0){retailer.setDocumentId(updateRetailerRequest.getDocumentId());}retailerRepository.persist(retailer);}private void updateRetailerAddress(Address address, CustomAddress customAddress, int retailerId) throws ProfitMandiBusinessException{if(address == null){address = new Address();address.setRetaierId(retailerId);this.updateAddress(address, customAddress);//addressRepository.persist(addressRetailer);final RetailerRegisteredAddress retailerRegisteredAddress = new RetailerRegisteredAddress();retailerRegisteredAddress.setRetailerId(retailerId);retailerRegisteredAddress.setAddressId(address.getId());retailerRegisteredAddressRepository.persist(retailerRegisteredAddress);}}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, int retailerId) throws ProfitMandiBusinessException{if(shops.isEmpty()){for(CustomShop customShop : customShops){Shop shop = new Shop();this.createOrUpdateShop(shop, customShop, retailerId);shops.add(shop);}}else{for(Shop shop : shops){for(CustomShop customShop : customShops){if(shop.getId() == customShop.getShopId()){this.createOrUpdateShop(shop, customShop, retailerId);}}}for(CustomShop customShop : customShops){if(customShop.getShopId() == 0){Shop shop = new Shop();this.createOrUpdateShop(shop, customShop, retailerId);shops.add(shop);}}}}private void createOrUpdateShop(Shop shop, CustomShop customShop, int retailerId) throws ProfitMandiBusinessException{shop.setRetailerId(retailerId);shop.setName(customShop.getName());if(customShop.getDocumentId() > 0){shop.setDocumentId(customShop.getDocumentId());documentRepository.markDocumentAsPersisted(customShop.getDocumentId());}if(shop.getAddressId() == null){//shop.setDocumentId(customShop.getDocumentId());Address address = new Address();this.updateAddress(address, customShop.getAddress());shop.setAddressId(address.getId());shop.setAddress(address);shopRepository.persist(shop);ShopAddress shopAddress = new ShopAddress();shopAddress.setAddressId(address.getId());shopAddress.setShopId(shop.getId());shopAddressRepository.persist(shopAddress);}else{Address address = addressRepository.selectById(shop.getAddressId());this.updateAddress(address, customShop.getAddress());shop.setAddress(address);shopRepository.persist(shop);}}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);}}