Subversion Repositories SmartDukaan

Rev

Rev 24739 | Rev 24990 | 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.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
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.ProfitMandiConstants;
import com.spice.profitmandi.dao.entity.dtr.UserAccount;
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 = LogManager.getLogger(WalletServiceImpl.class);

        @Autowired
        private UserAccountRepository userAccountRepository;

        @Autowired
        private UserWalletRepository userWalletRepository;

        @Autowired
        private UserWalletHistoryRepository userWalletHistoryRepository;

        @Override
        public void addAmountToWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
                        String description, float amount) throws ProfitMandiBusinessException {
                if (amount == 0)
                        return;
                UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
                userWallet.setAmount(userWallet.getAmount() + Math.round(amount));
                userWalletRepository.persist(userWallet);
                this.createUserWalletHistory(Math.round(amount), userWallet.getId(), referenceId, referenceType, description);
        }

        @Override
        public void consumeAmountFromWallet(int retailerId, int referenceId, WalletReferenceType referenceType,
                        String description, float amount) throws ProfitMandiBusinessException {
                if (amount == 0)
                        return;
                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() - Math.round(amount));
                userWalletRepository.persist(userWallet);
                this.createUserWalletHistory(-Math.round(amount), userWallet.getId(), referenceId, referenceType, description);
        }

        @Override
        public void rollbackAmountFromWallet(int retailerId, float amountToRollback, int rollbackReference,
                        WalletReferenceType walletReferenceType, String rollbackReason) {
                if (amountToRollback == 0)
                        return;
                UserWallet userWallet = this.getUserWallet(retailerId);
                userWallet.setAmount(userWallet.getAmount() - Math.round(amountToRollback));
                userWalletRepository.persist(userWallet);
                this.createUserWalletHistory(-Math.round(amountToRollback), userWallet.getId(), rollbackReference,
                                walletReferenceType, rollbackReason);

        }

        private void createUserWalletHistory(float amount, int walletId, int referenceId, WalletReferenceType referenceType,
                        String description) {
                if (amount == 0)
                        return;
                UserWalletHistory userWalletHistory = new UserWalletHistory();
                userWalletHistory.setWalletId(walletId);
                userWalletHistory.setAmount(Math.round(amount));
                userWalletHistory.setReference(referenceId);
                userWalletHistory.setReferenceType(referenceType);
                userWalletHistory.setTimestamp(LocalDateTime.now());
                userWalletHistory.setDescription(description);
                userWalletHistoryRepository.persit(userWalletHistory);
        }

        @Override
        public UserWallet getUserWalletByUserId(int userId) throws ProfitMandiBusinessException {
                UserAccount userAccount = userAccountRepository.selectByUserIdType(userId, AccountType.saholic);
                return userWalletRepository.selectByRetailerId(Integer.valueOf(userAccount.getAccountKey()));
        }

        @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);
        }

        @Override
        public Map<Integer, UserWallet> getRetailerIdUserWalletMap(Set<Integer> retailerIds) {
                List<UserWallet> userWallets = userWalletRepository.selectByRetailerIds(retailerIds);
                Map<Integer, UserWallet> retailerIdUserWalletMap = new HashMap<>();
                for (UserWallet userWallet : userWallets) {
                        retailerIdUserWalletMap.put(userWallet.getUserId(), userWallet);
                }
                return retailerIdUserWalletMap;
        }

        @Override
        public boolean isExistWalletHistory(int retailerId, int referenceId, WalletReferenceType referenceType)
                        throws ProfitMandiBusinessException {
                UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);
                return userWalletHistoryRepository.isExist(userWallet.getId(), referenceType, referenceId);
        }

        @Override
        public UserWallet getUserWallet(int retailerId) {
                try {
                        return userWalletRepository.selectByRetailerId(retailerId);
                } catch (Exception e) {
                        UserWallet uw = new UserWallet();
                        uw.setAmount(0);
                        uw.setRefundableAmount(0);
                        uw.setUserId(retailerId);
                        userWalletRepository.persist(uw);
                        return uw;
                }
        }

        @Override
        public float getOpeningTill(int fofoId, LocalDateTime date) throws ProfitMandiBusinessException {
                UserWallet wallet = userWalletRepository.selectByRetailerId(fofoId);
                return userWalletHistoryRepository.getSumTillDate(wallet.getId(), date);
        }

        @Override
        public void refundToWallet(Integer retailerId, Float amountToRefund, Integer transactionId,
                        WalletReferenceType walletReferenceType, String description) throws ProfitMandiBusinessException {

                List<UserWalletHistory> all_entries = userWalletHistoryRepository
                                .selectAllByreferenceIdandreferenceType(transactionId, WalletReferenceType.PURCHASE);
                UserWallet userWallet = userWalletRepository.selectByRetailerId(retailerId);

                LOGGER.info("userWallet" + userWallet);
                int creditable_amount_for_reference = 0;

                for (UserWalletHistory history : all_entries) {
                        creditable_amount_for_reference -= history.getAmount();

                        LOGGER.info("creditable_amount_for_reference" + creditable_amount_for_reference);

                        LOGGER.info("walletAmount" + amountToRefund);
                        if (creditable_amount_for_reference < amountToRefund) {

                                LOGGER.info("order" + "Cant be credited back to wallet as most of it has been already credited");
                                throw new ProfitMandiBusinessException(ProfitMandiConstants.USER_ID,userWallet.getUserId(), "Cant be credited back to wallet as most of it has been already credited");
                        }

                        userWallet.setAmount(userWallet.getAmount() + Math.round(amountToRefund));

                        LOGGER.info("userWallet" + userWallet);

                }
                UserWalletHistory userWalletHistory = new UserWalletHistory();
                userWalletHistory.setAmount(Math.round(amountToRefund));
                userWalletHistory.setReference(transactionId);
                userWalletHistory.setReferenceType(WalletReferenceType.PURCHASE);
                userWalletHistory.setWalletId(userWallet.getId());
                userWalletHistory.setTimestamp(LocalDateTime.now());
                userWalletHistory.setDescription(description);

                LOGGER.info("all_entries" + userWalletHistory);
                userWalletHistoryRepository.persit(userWalletHistory);

                
        }

}