Subversion Repositories SmartDukaan

Rev

Rev 22730 | Rev 22925 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.service.wallet;

import java.time.LocalDateTime;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.UserAccounts;
import com.spice.profitmandi.dao.entity.transaction.UserWallet;
import com.spice.profitmandi.dao.entity.transaction.UserWalletHistory;
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
import com.spice.profitmandi.dao.repository.transaction.UserWalletHistoryRepository;
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;

import in.shop2020.model.v1.order.WalletReferenceType;

@Component
public class WalletServiceImpl implements WalletService {

        private static final Logger LOGGER = LoggerFactory.getLogger(WalletServiceImpl.class);
        
        @Autowired
        UserAccountRepository userAccountRepository;
        
        @Autowired
        UserWalletRepository userWalletRepository;
        
        @Autowired
        UserWalletHistoryRepository userWalletHistoryRepository;
        
        @Override
        public void addAmountToWallet(int retailerId, int referenceId, WalletReferenceType referenceType, String description,
                        float amount) throws ProfitMandiBusinessException{
                UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
                userWallet.setAmount(userWallet.getAmount() + Float.valueOf(amount).intValue());
                userWalletRepository.persist(userWallet);
                this.createUserWalletHistory(amount, userWallet.getId(), referenceId, referenceType, description);
        }
        
        @Override
        public void consumeAmountFromWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
                        String description, float amount) throws ProfitMandiBusinessException {
                UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
                if(amount > userWallet.getAmount()){
                        LOGGER.error("Wallet Balance is insufficient!");
                        throw new ProfitMandiBusinessException("balance", userWallet.getAmount(), "WLT_1000");
                }
                userWallet.setAmount(userWallet.getAmount() - Float.valueOf(amount).intValue());
                userWalletRepository.persist(userWallet);
                this.createUserWalletHistory(amount, userWallet.getId(), referenceId, referenceType, description);
        }
        
        private void createUserWalletHistory(float amount, int walletId, int referenceId, WalletReferenceType referenceType, String description){
                UserWalletHistory userWalletHistory = new UserWalletHistory();
                userWalletHistory.setWalletId(walletId);
                userWalletHistory.setAmount(Float.valueOf(amount).intValue());
                userWalletHistory.setReference(referenceId);
                userWalletHistory.setReferenceType(referenceType);
                userWalletHistory.setTimestamp(LocalDateTime.now());
                userWalletHistory.setDescription(description);
                userWalletHistoryRepository.persit(userWalletHistory);
        }
        
        
        @Override
        public UserWallet getUserWalletByUserId(int userId) throws ProfitMandiBusinessException {
                UserAccounts userAccount = userAccountRepository.getUserAccountByType(userId, AccountType.saholic);
                return userWalletRepository.selectByRetailerId(Integer.valueOf(userAccount.getAccount_key()));
        }

        @Override
        public List<UserWalletHistory> getUserWalletHistoryByUserId(int userId) throws ProfitMandiBusinessException {
                UserWallet userWallet = this.getUserWalletByUserId(userId);
                List<UserWalletHistory> userWalletHistories = userWalletHistoryRepository.selectByWalletId(userWallet.getId());
                return userWalletHistories;
        }
        
        @Override
        public List<UserWalletHistory> getUserWalletHistoryByRetailerId(int retailerId)
                        throws ProfitMandiBusinessException {
                UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
                return userWalletHistoryRepository.selectByWalletId(userWallet.getId());
        }
        
        @Override
        public long getSizeByRetailerId(int retailerId, LocalDateTime startDateTime, LocalDateTime endDateTime) throws ProfitMandiBusinessException{
                UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
                return userWalletHistoryRepository.selectCountByWalletId(userWallet.getId(), startDateTime, endDateTime);
        }
        
        @Override
        public List<UserWalletHistory> getPaginatedUserWalletHistoryByRetailerId(int retailerId,
                        LocalDateTime startDateTime, LocalDateTime endDateTime, int offset, int limit)
                        throws ProfitMandiBusinessException {
                UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
                return userWalletHistoryRepository.selectPaginatedByWalletId(userWallet.getId(), startDateTime, endDateTime, offset, limit);
        }

}