Subversion Repositories SmartDukaan

Rev

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

package com.spice.profitmandi.web.controller;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.CustomRetailer;
import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.common.model.RechargeCredential;
import com.spice.profitmandi.common.util.StringUtils;
import com.spice.profitmandi.dao.entity.dtr.RechargeOperator;
import com.spice.profitmandi.dao.entity.transaction.AddWalletRequest;
import com.spice.profitmandi.dao.entity.transaction.LineItemImei;
import com.spice.profitmandi.dao.entity.transaction.PriceDrop;
import com.spice.profitmandi.dao.entity.transaction.UserWallet;
import com.spice.profitmandi.dao.entity.transaction.UserWalletHistory;
import com.spice.profitmandi.dao.enumuration.transaction.AddWalletRequestStatus;
import com.spice.profitmandi.dao.repository.catalog.AddWalletRequestRepository;
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
import com.spice.profitmandi.service.user.RetailerService;
import com.spice.profitmandi.service.wallet.WalletService;
import com.spice.profitmandi.web.model.LoginDetails;
import com.spice.profitmandi.web.util.CookiesProcessor;
import com.spice.profitmandi.web.util.MVCResponseSender;

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

@Controller
@Transactional(rollbackFor = Throwable.class)
public class WalletController {

        @Autowired
        private CookiesProcessor cookiesProcessor;
        
        @Autowired
        private WalletService walletService;
        
        @Autowired
        private UserWalletRepository userWalletRepository;
        
        @Autowired
        private MVCResponseSender mvcResponseSender;
        
        @Autowired
        private RetailerService retailerService;

        
        @Autowired
        AddWalletRequestRepository addWalletRequestRepository;
        private static final Logger LOGGER = LogManager.getLogger(WalletController.class);
        
        @RequestMapping(value = "/walletDetails", method = RequestMethod.GET)
        public String dashboard(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.START_TIME, required = false) String startTimeString, @RequestParam(name = ProfitMandiConstants.END_TIME, required = false) String endTimeString, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, Model model) throws ProfitMandiBusinessException{
                LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
                
                UserWallet userWallet = null;
                try{
                        userWallet = userWalletRepository.selectByRetailerId(fofoDetails.getFofoId());
                }catch(ProfitMandiBusinessException profitMandiBusinessException){
                        LOGGER.error("UserWallet not found : ", profitMandiBusinessException);
                        //return "error";
                }
                
                LocalDateTime startDateTime = StringUtils.toDateTime(startTimeString);
                LocalDateTime endDateTime = StringUtils.toDateTime(endTimeString);
                
                List<UserWalletHistory> userWalletHistories = new ArrayList<>();
                try{
                        userWalletHistories = walletService.getPaginatedUserWalletHistoryByRetailerId(fofoDetails.getFofoId(), startDateTime, endDateTime, offset, limit);
                }catch(ProfitMandiBusinessException profitMandiBusinessException){
                        LOGGER.error("UserWallet History not found : ", profitMandiBusinessException);
                }
                
                long countItems = 0;
                try{
                        countItems = walletService.getSizeByRetailerId(fofoDetails.getFofoId(), startDateTime, endDateTime);
                }catch(ProfitMandiBusinessException profitMandiBusinessException){
                        LOGGER.error("UserWallet not found : ",profitMandiBusinessException);
                }
                model.addAttribute("userWallet", userWallet);
                model.addAttribute("walletHistories", userWalletHistories);
                model.addAttribute("start", offset + 1);
                model.addAttribute("size",countItems);
                model.addAttribute(ProfitMandiConstants.START_TIME, startTimeString);
                model.addAttribute(ProfitMandiConstants.END_TIME, endTimeString);
                if (userWalletHistories.size() < limit){
                        model.addAttribute("end", offset + userWalletHistories.size());
                }
                else{
                        model.addAttribute("end", offset + limit);
                }
                return "wallet-details";
        }
        
        @RequestMapping(value = "/getPaginatedWalletHistory")
        public String getSaleHistoryPaginated(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.START_TIME, required = false) String startTimeString, @RequestParam(name = ProfitMandiConstants.END_TIME, required = false) String endTimeString, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue="10") int limit, Model model) throws ProfitMandiBusinessException{
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
                
                LocalDateTime startDateTime = StringUtils.toDateTime(startTimeString);
                LocalDateTime endDateTime = StringUtils.toDateTime(endTimeString);

                List<UserWalletHistory> userWalletHistories = new ArrayList<>();
                try{
                        userWalletHistories = walletService.getPaginatedUserWalletHistoryByRetailerId(loginDetails.getFofoId(), startDateTime, endDateTime, offset, limit);
                }catch(ProfitMandiBusinessException profitMandiBusinessException){
                        LOGGER.error("UserWallet History not found : ", profitMandiBusinessException);
                }
                
                model.addAttribute("walletHistories", userWalletHistories);
                return "wallet-history-paginated";
        }
        
        @RequestMapping(value = "/getAddWalletRequest", method = RequestMethod.GET)
        public String getAddwalletRequest(HttpServletRequest request,
                        @RequestParam(name = "offset", defaultValue = "0") int offset,
                        @RequestParam(name = "limit", defaultValue = "10") int limit, Model model)
                        throws ProfitMandiBusinessException {
                List<AddWalletRequest> walletRequest = null;
                long size = 0;
                walletRequest = addWalletRequestRepository.selectAllByStatus(offset, limit,AddWalletRequestStatus.pending);
                LOGGER .info("walletRequest" + walletRequest);
                size = addWalletRequestRepository.selectCountByStatus(AddWalletRequestStatus.pending);
                
                List<Integer> fofoIds=this.getFofoIdsFromWalletRequest(walletRequest);
                Map<Integer,CustomRetailer> fofoIdsAndRetailerName=retailerService.getFofoRetailers(fofoIds);
                
                model.addAttribute("fofoIdsAndRetailerName",fofoIdsAndRetailerName);
                model.addAttribute("walletRequest", walletRequest);
                model.addAttribute("rStatus","pending");
                model.addAttribute("start", offset + 1);
                model.addAttribute("size", size);
                model.addAttribute("url","/getPaginatedWalletRequest");

                if (walletRequest.size() < limit) {
                        model.addAttribute("end", offset + walletRequest.size());
                } else {
                        model.addAttribute("end", offset + limit);
                }

                return "add-wallet-request";
        }

        @RequestMapping(value = "/getPaginatedWalletRequest", method = RequestMethod.GET)
        public String getPaginatedWalletRequest(HttpServletRequest request,
                        @RequestParam(name = "offset", defaultValue = "0") int offset,
                        @RequestParam(name = "limit", defaultValue = "10") int limit, Model model)
                        throws ProfitMandiBusinessException {
                LOGGER .info("requested offset=[{}], limit = [{}]", offset, limit);
                List<AddWalletRequest> walletRequest = null;
                walletRequest = addWalletRequestRepository.selectAllByStatus(offset, limit,AddWalletRequestStatus.pending);
                LOGGER.info("walletRequest" + walletRequest);
                List<Integer> fofoIds=this.getFofoIdsFromWalletRequest(walletRequest);
                Map<Integer,CustomRetailer> fofoIdsAndRetailerName=retailerService.getFofoRetailers(fofoIds);
                
                model.addAttribute("fofoIdsAndRetailerName",fofoIdsAndRetailerName);
                model.addAttribute("walletRequest", walletRequest);
                model.addAttribute("rStatus","pending");
                model.addAttribute("url","/getPaginatedWalletRequest");

                return "add-wallet-request-paginated";
        }
        
        @RequestMapping(value = "/getAddWalletApproved", method = RequestMethod.GET)
        public String getAddwalletRequestApproved(HttpServletRequest request,
                        @RequestParam(name = "offset", defaultValue = "0") int offset,
                        @RequestParam(name = "limit", defaultValue = "10") int limit, Model model)
                        throws ProfitMandiBusinessException {
                List<AddWalletRequest> walletRequest = null;
                
                long size = 0;
                walletRequest = addWalletRequestRepository.selectAllByStatus(offset, limit,AddWalletRequestStatus.approved);
                size = addWalletRequestRepository.selectCountByStatus(AddWalletRequestStatus.approved);
                LOGGER.info("walletRequest" + walletRequest);
                List<Integer> fofoIds=this.getFofoIdsFromWalletRequest(walletRequest);
                Map<Integer,CustomRetailer> fofoIdsAndRetailerName=retailerService.getFofoRetailers(fofoIds);
                
                model.addAttribute("fofoIdsAndRetailerName",fofoIdsAndRetailerName);
                model.addAttribute("walletRequest", walletRequest);
                model.addAttribute("start", offset + 1);
                model.addAttribute("size", size);
                model.addAttribute("url","/getPaginatedWalletApproved");

                if (walletRequest.size() < limit) {
                        model.addAttribute("end", offset + walletRequest.size());
                } else {
                        model.addAttribute("end", offset + limit);
                }

                return "add-wallet-request";
        }

        @RequestMapping(value = "/getPaginatedWalletApproved", method = RequestMethod.GET)
        public String getPaginatedWalletApproved(HttpServletRequest request,
                        @RequestParam(name = "offset", defaultValue = "0") int offset,
                        @RequestParam(name = "limit", defaultValue = "10") int limit, Model model)
                        throws ProfitMandiBusinessException {
                LOGGER .info("requested offset=[{}], limit = [{}]", offset, limit);
                List<AddWalletRequest> walletRequest = null;
                walletRequest = addWalletRequestRepository.selectAllByStatus(offset, limit, AddWalletRequestStatus.approved);
                LOGGER.info("walletRequest" + walletRequest);
                List<Integer> fofoIds=this.getFofoIdsFromWalletRequest(walletRequest);
                Map<Integer,CustomRetailer> fofoIdsAndRetailerName=retailerService.getFofoRetailers(fofoIds);
                
                model.addAttribute("fofoIdsAndRetailerName",fofoIdsAndRetailerName);
                model.addAttribute("walletRequest", walletRequest);
                model.addAttribute("url","/getPaginatedWalletApproved");

                return "add-wallet-request-paginated";
        }
        
        @RequestMapping(value = "/addAmountToWallet", method = RequestMethod.PUT)
        public String addAmountToWallet(HttpServletRequest request,@RequestParam(name = "id", defaultValue = "0")int id,Model model)
                        throws Exception {
                
                AddWalletRequest addWalletRequest= addWalletRequestRepository.selectById(id);
            walletService.addAmountToWallet(addWalletRequest.getRetailerId(), id,WalletReferenceType.ADVANCE_AMOUNT, "ntfs/rgfs",addWalletRequest.getAmount());         
        addWalletRequest.setStatus(AddWalletRequestStatus.approved);
        addWalletRequest.setUpdateTimestamp(LocalDateTime.now());
            addWalletRequestRepository.persist(addWalletRequest);
            model.addAttribute("response", mvcResponseSender.createResponseString(true));
            return "response";
        }
        private List<Integer> getFofoIdsFromWalletRequest(List<AddWalletRequest> walletRequest)
        {
                List<Integer> fofoIds = new ArrayList<>();
                for(AddWalletRequest  walletdetail :walletRequest ) {
                        fofoIds.add(walletdetail.getRetailerId());
                }
                return fofoIds;
        }

}