Subversion Repositories SmartDukaan

Rev

Rev 21468 | Rev 21510 | Go to most recent revision | 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.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.thrift.TException;
import org.apache.thrift.transport.TTransportException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
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.ProfitMandiConstants;
import com.spice.profitmandi.common.util.Utils;
import com.spice.profitmandi.dao.entity.UserAccounts;
import com.spice.profitmandi.dao.enumuration.AccountType;
import com.spice.profitmandi.dao.repository.UserAccountRepository;
import com.spice.profitmandi.thrift.clients.TransactionClient;
import com.spice.profitmandi.web.model.ProfitMandiResponse;
import com.spice.profitmandi.web.model.ResponseStatus;
import com.spice.profitmandi.web.res.ConfirmRechargeResponse;
import com.spice.profitmandi.web.util.ResponseSender;

import in.shop2020.model.v1.order.DenominationType;
import in.shop2020.model.v1.order.DeviceNumberInfo;
import in.shop2020.model.v1.order.RechargeDenomination;
import in.shop2020.model.v1.order.RechargeType;
import in.shop2020.model.v1.order.UserWallet;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Controller
public class RechargeController {

        private static final Logger log=LoggerFactory.getLogger(RechargeController.class);

        @Autowired
        UserAccountRepository userAccountRepository;
        
        @RequestMapping(value = ProfitMandiConstants.URL_GET_SERVICE_PROVIDER, method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
        @ApiImplicitParams({
                @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
                                required = true, dataType = "string", paramType = "header")
        })
        @ApiOperation(value = "")
        public ResponseEntity<?> getServiceProvider(HttpServletRequest request, @RequestParam(value="deviceNumber") String deviceNumber, @RequestParam(value="rechargeType") String rechargeType){
                DeviceNumberInfo deviceInfo = null;
                TransactionClient tcl;
                try {
                        tcl = new TransactionClient();
                        deviceInfo = tcl.getClient().getServiceProviderForDevice(RechargeType.valueOf(rechargeType), deviceNumber.substring(0,4));
                } catch (Exception e) {
                        log.error("Unable to get service provider for Device number " + deviceNumber + " and rechargeType : " +  rechargeType, e);
                }
                final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, deviceInfo);
                return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
        }

        @RequestMapping(value = ProfitMandiConstants.URL_GET_ALL_DENOMINATIONS, method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
        @ApiImplicitParams({
                @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
                                required = true, dataType = "string", paramType = "header")
        })
        @ApiOperation(value = "")
        public ResponseEntity<?> getAllDenominations(HttpServletRequest request, @RequestParam(value="operatorId") long operatorId, @RequestParam(value="circleCode") String circleCode, @RequestParam(value="denominationType") String denominationType){
                List<RechargeDenomination> rechargeDenominations = new ArrayList<RechargeDenomination>();
                TransactionClient tcl;
                try {
                        tcl = new TransactionClient();
                        rechargeDenominations =  tcl.getClient().getRechargeDenominations(operatorId, circleCode, DenominationType.valueOf(denominationType));
                } catch (Exception e) {
                        log.error("Unable to get rechargeDenominations for operatorId " + operatorId + " and circleCode : " +  circleCode + " and DenominationType : " + denominationType, e);
                }
                final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, rechargeDenominations);
                return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
        }

        @RequestMapping(value = ProfitMandiConstants.URL_MOBILE_OPERATORS, method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
        @ApiImplicitParams({
                @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
                                required = true, dataType = "string", paramType = "header")
        })
        @ApiOperation(value = "")
        public ResponseEntity<?> getAllMobileOperators(HttpServletRequest request){
                Map<Long, String> mobileProviderMap = Utils.getMobileProvidersMap();
                final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, mobileProviderMap);
                return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
        }

        @RequestMapping(value = ProfitMandiConstants.URL_DTH_OPERATORS, method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
        @ApiImplicitParams({
                @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
                                required = true, dataType = "string", paramType = "header")
        })
        @ApiOperation(value = "")
        public ResponseEntity<?> getAllDTHOperators(HttpServletRequest request){
                Map<Long, String> dthProviderMap = Utils.getDthProvidersMap();
                final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, dthProviderMap);
                return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
        }

        @RequestMapping(value = ProfitMandiConstants.URL_RECHARGE_CONFIRM, method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
        @ApiImplicitParams({
                @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
                                required = true, dataType = "string", paramType = "header")
        })
        @ApiOperation(value = "")
        public ResponseEntity<?> confirmRecharge(HttpServletRequest request, long rechargeAmount){
                int userId = (int)request.getAttribute("userId");
                UserAccounts userAccount = null;
                UserWallet wallet = null;
                if (rechargeAmount <=0){
                        final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.SUCCESS, null);
                        return new ResponseEntity<>(profitMandiResponse,HttpStatus.BAD_REQUEST);
                }
                
                try {
                        userAccount = userAccountRepository.getUserAccountByType(userId, AccountType.saholic);
                } catch (ProfitMandiBusinessException e) {
                        final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.SUCCESS, null);
                        return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
                }
                try {
                        TransactionClient tc = new TransactionClient();
                        wallet = tc.getClient().getUserWallet(Long.valueOf(userAccount.getAccount_key()));
                } catch (NumberFormatException | TException e) {
                        final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.SUCCESS, null);
                        return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
                }
                
                ConfirmRechargeResponse crr = new ConfirmRechargeResponse();
                crr.setWalletAmount(wallet.getAmount());
                crr.setWalletAmountLeft(wallet.getAmount() - rechargeAmount);
                crr.setCanProceed(true);
                if (crr.getWalletAmountLeft() < 0){
                        crr.setCanProceed(false);
                        crr.setReason("You don't have sufficient wallet balance");
                }
                final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, crr);
                return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
        }
        
        @RequestMapping(value = ProfitMandiConstants.URL_CREATE_RECHARGE , method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
        @ApiImplicitParams({
                @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
                                required = true, dataType = "string", paramType = "header")
        })
        public ResponseEntity<?> createRecharge(HttpServletRequest request){
                return null;
        }
        
}