Subversion Repositories SmartDukaan

Rev

Rev 35370 | Rev 35458 | Go to most recent revision | 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.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 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.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

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

@Controller
@Transactional(rollbackOn = 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;

    @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);
        model.addAttribute("bm", bmAuthUsers);
        model.addAttribute("asm", asmAuthUsers);
        model.addAttribute("trialForms", trialForms);
        return "trial-form";
    }

    @RequestMapping(value = "/trial/verified", method = RequestMethod.GET)
    public String verifiedTrial(HttpServletRequest request, Model model) throws Exception {
        List<TrialForm> trialForms = trialFormRepository.selectAllByStatus(ProfitMandiConstants.Status.VERIFIED);
        LOGGER.info("trialForms {}", trialForms.size());
        model.addAttribute("trialForms", trialForms);
        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/{trialFormId}", method = RequestMethod.POST)
    public String rejectTrialUser(HttpServletRequest request, Model model, @PathVariable int trialFormId) throws Exception {
        TrialForm trialForm = trialFormRepository.selectById(trialFormId);
        trialForm.setStatus(ProfitMandiConstants.Status.REJECTED);
        trialService.sentRejectionMailToTrialUser(trialForm);

        return "trial-form";
    }

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


}