Subversion Repositories SmartDukaan

Rev

Rev 35499 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.controller;

import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.common.web.util.ResponseSender;
import com.spice.profitmandi.dao.entity.auth.AuthUser;
import com.spice.profitmandi.dao.entity.fofo.TrialBrandPotential;
import com.spice.profitmandi.dao.entity.fofo.TrialForm;
import com.spice.profitmandi.dao.enumuration.cs.EscalationType;
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
import com.spice.profitmandi.dao.repository.cs.CsService;
import com.spice.profitmandi.dao.repository.fofo.TrialBrandPotentialRepository;
import com.spice.profitmandi.dao.repository.trialOnboarding.TrialFormRepository;
import com.spice.profitmandi.dao.service.TrialService;
import com.spice.profitmandi.web.model.LoginDetails;
import com.spice.profitmandi.web.util.CookiesProcessor;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
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.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

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

    private static final Logger LOGGER = LogManager.getLogger(TrialController.class);
    @Autowired
    TrialBrandPotentialRepository trialBrandPotentialRepository;

    @Autowired
    ResponseSender responseSender;

    @Autowired
    CsService csService;

    @Autowired
    TrialFormRepository trialFormRepository;

    @Autowired
    TrialService trialService;

    @Autowired
    CookiesProcessor cookiesProcessor;

    @Autowired
    AuthRepository authRepository;


    @RequestMapping(value = "/trial/pending", method = RequestMethod.GET)
    public String getTrialPending(HttpServletRequest request, Model model) throws Exception {
        List<TrialForm> trialForms = trialFormRepository.selectAllByStatus(ProfitMandiConstants.Status.PENDING);
        LOGGER.info("trialForms {}", trialForms.size());
        Set<AuthUser> bmAuthUsers = csService.getAuthUserIds(ProfitMandiConstants.TICKET_CATEGORY_SALES, Collections.singletonList(EscalationType.L2)).stream().filter(x -> x.isActive()).collect(Collectors.toSet());
        Set<AuthUser> asmAuthUsers = csService.getAuthUserIds(ProfitMandiConstants.TICKET_CATEGORY_SALES, Collections.singletonList(EscalationType.L1)).stream().filter(x -> x.isActive()).collect(Collectors.toSet());
        LOGGER.info("bm {}", bmAuthUsers);
        LOGGER.info("asm {}", asmAuthUsers);
        asmAuthUsers.addAll(bmAuthUsers);
        model.addAttribute("bm", bmAuthUsers);
        model.addAttribute("asm", asmAuthUsers);
        model.addAttribute("trialForms", trialForms);
        return "trial-form";
    }

    @RequestMapping(value = "/trial/all-trial", method = RequestMethod.GET)
    public String getAllTrialUser(HttpServletRequest request, Model model) throws Exception {
        List<TrialForm> trialForms = trialFormRepository.selectAll();
        LOGGER.info("trialForms {}", trialForms.size());
        model.addAttribute("trialForms", trialForms);
        model.addAttribute("status", ProfitMandiConstants.Status.values());
        return "all-trial-form";
    }

    @RequestMapping(value = "/trial/by-status", method = RequestMethod.GET)
    public String getByStatus(
            @RequestParam("status") ProfitMandiConstants.Status status,
            Model model) {

        List<TrialForm> trialForms = trialFormRepository.selectAllByStatus(status);
        model.addAttribute("trialForms", trialForms);
        model.addAttribute("status", ProfitMandiConstants.Status.values());
        model.addAttribute("selectedStatus", status);
        return "all-trial-form";
    }

    @RequestMapping(value = "/trial/verified", method = RequestMethod.GET)
    public String verifiedTrial(HttpServletRequest request, Model model) throws Exception {
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);

        AuthUser authUser = authRepository.selectByEmailOrMobile(loginDetails.getEmailId());
        List<TrialForm> trialForms = trialFormRepository.selectAllByStatus(ProfitMandiConstants.Status.VERIFIED);
        List<TrialForm> filteredTrialForms = trialForms.stream()
                .filter(trialForm ->
                        (trialForm.getBmId() != null && trialForm.getBmId().equals(authUser.getId())) ||
                                (trialForm.getAsmId() != null && trialForm.getAsmId().equals(authUser.getId()))
                )
                .collect(Collectors.toList());
        LOGGER.info("trialForms {}", trialForms.size());
        LOGGER.info("filtered trialForms {}", filteredTrialForms);
        model.addAttribute("trialForms", filteredTrialForms);
        return "trial-verified";
    }

    @RequestMapping(value = "/trial/asm-bm", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<?> asmBm(HttpServletRequest request) throws Exception {
        int trialFormId = Integer.parseInt(request.getParameter("trialFormId"));
        int asmId = Integer.parseInt(request.getParameter("asmId"));
        int bmId = Integer.parseInt(request.getParameter("bmId"));

        if (trialFormId == 0 || asmId == 0 || bmId == 0) {
            throw new Exception("please select both  bm and asm");
        }
        TrialForm trialForms = trialFormRepository.selectById(trialFormId);
        trialForms.setAsmId(asmId);
        trialForms.setBmId(bmId);
        trialForms.setUpdatedOn(LocalDateTime.now());
        return responseSender.ok("Saved Successfully");
    }

    @RequestMapping(value = "/trial/create/{trialFormId}", method = RequestMethod.GET)
    public String getTrialPending(HttpServletRequest request, Model model, @PathVariable int trialFormId) throws Exception {
        TrialForm trialForm = trialFormRepository.selectById(trialFormId);
        return "trial-form";
    }

    @RequestMapping(value = "/trial/reject", method = RequestMethod.POST)
    public ResponseEntity<?> rejectTrialUser(HttpServletRequest request, @RequestParam int trialFormId,
                                             @RequestParam String remark) throws Exception {
        TrialForm trialForm = trialFormRepository.selectById(trialFormId);
        trialForm.setStatus(ProfitMandiConstants.Status.REJECTED);
        trialForm.setRemark(remark);
        trialService.sentRejectionMailToTrialUser(trialForm);
        return responseSender.ok("Rejected Successfully");
    }

    @RequestMapping(value = "/trial/approve/{trialFormId}", method = RequestMethod.POST)
    public String approveTrialUser(HttpServletRequest request, Model model, @PathVariable int trialFormId) throws Exception {
        TrialForm trialForm = trialFormRepository.selectById(trialFormId);
        trialForm.setStatus(ProfitMandiConstants.Status.APPROVED);
        try {
            trialService.sentMailForStoreCodeCreation(trialForm);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "trial-form";
    }

    @RequestMapping(value = "/trial/convertToFranchise/{trialFormId}", method = RequestMethod.POST)
    public String convertedToFranchise(HttpServletRequest request, Model model, @PathVariable int trialFormId) throws Exception {
        TrialForm trialForm = trialFormRepository.selectById(trialFormId);
        trialForm.setStatus(ProfitMandiConstants.Status.CONVERTED);

        return "trial-form";
    }

    @RequestMapping(value = "/trial/userBrandPotential/{trialFormId}", method = RequestMethod.GET)
    public ResponseEntity<?> userBrandPotential(HttpServletRequest request, @PathVariable int trialFormId) throws Exception {
        List<TrialBrandPotential> trialBrandPotentials = trialBrandPotentialRepository.findAllByTrialFormId(trialFormId);

        return responseSender.ok(trialBrandPotentials);
    }

    @RequestMapping(value = "/trial/brand-Potential", method = RequestMethod.POST)
    public ResponseEntity<?> brandPotential(HttpServletRequest request) throws Exception {

        int trialFormId = Integer.parseInt(request.getParameter("trialFormId"));
        int row = 1;
        while (true) {
            String brand = request.getParameter("brand_" + row);
            String potential = request.getParameter("potential_" + row);

            if (brand == null && potential == null) {
                break;
            }

            if (brand != null && !brand.trim().isEmpty()
                    && potential != null && !potential.trim().isEmpty()) {

                TrialBrandPotential tbp = new TrialBrandPotential();
                tbp.setTrialFormId(trialFormId);
                tbp.setBrandName(brand.trim());
                tbp.setPotential(Integer.parseInt(potential));
                tbp.setCreatedAt(LocalDateTime.now());

                trialBrandPotentialRepository.persist(tbp);
            }

            row++;
        }
        TrialForm trialForm = trialFormRepository.selectById(trialFormId);
        trialForm.setUpdatedOn(LocalDateTime.now());
        trialForm.setStatus(ProfitMandiConstants.Status.VERIFIED);
        try {
            trialService.sentMailForTrialUserToSales(trialForm);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return responseSender.ok("Saved Successfully");
    }


}