Subversion Repositories SmartDukaan

Rev

Rev 31702 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
31702 amit.gupta 1
package com.spice.profitmandi.web.controller;
2
 
3
import com.spice.profitmandi.common.util.FileUtil;
4
import com.spice.profitmandi.common.util.FormattingUtils;
5
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
6
import com.spice.profitmandi.dao.entity.fofo.SamsungPCM;
7
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
8
import com.spice.profitmandi.dao.repository.fofo.SamsungPCMRepository;
9
import com.spice.profitmandi.dao.repository.warehouse.BilledImeiModel;
10
import com.spice.profitmandi.dao.repository.warehouse.WarehouseInventoryItemRepository;
11
import com.spice.profitmandi.service.user.RetailerService;
12
import org.apache.logging.log4j.LogManager;
13
import org.apache.logging.log4j.Logger;
14
import org.springframework.beans.factory.annotation.Autowired;
15
import org.springframework.core.io.InputStreamResource;
16
import org.springframework.http.HttpHeaders;
17
import org.springframework.http.HttpStatus;
18
import org.springframework.http.ResponseEntity;
19
import org.springframework.stereotype.Controller;
20
import org.springframework.transaction.annotation.Transactional;
21
import org.springframework.ui.Model;
22
import org.springframework.web.bind.annotation.RequestBody;
23
import org.springframework.web.bind.annotation.RequestMapping;
24
import org.springframework.web.bind.annotation.RequestMethod;
25
import org.springframework.web.bind.annotation.RequestParam;
26
 
27
import java.io.ByteArrayInputStream;
28
import java.io.InputStream;
29
import java.time.LocalDate;
30
import java.util.Arrays;
31
import java.util.List;
32
import java.util.Locale;
33
import java.util.Map;
34
import java.util.stream.Collectors;
35
 
36
@Controller
37
@Transactional(rollbackFor = Throwable.class)
38
public class BrandPCMController {
39
    private static final Logger LOGGER = LogManager.getLogger(AuthUserController.class);
40
 
41
    @Autowired
42
    SamsungPCMRepository samsungPCMRepository;
43
    @Autowired
44
    RetailerService retailerService;
45
 
46
    @Autowired
47
    FofoStoreRepository fofoStoreRepository;
48
 
49
    @Autowired
50
    WarehouseInventoryItemRepository warehouseInventoryItemRepository;
51
 
52
    @RequestMapping(value = "/brand-pcm", method = RequestMethod.GET)
32562 amit.gupta 53
    public String brandPcm(Model model) {
31702 amit.gupta 54
        Map<Integer, LocalDate> samsungPCMMap = samsungPCMRepository.selectAll().stream().collect(Collectors.toMap(x -> x.getFofoId(), x -> x.getPcmDate()));
55
        model.addAttribute("samsungPCMMap", samsungPCMMap);
56
        model.addAttribute("retailerMap", retailerService.getAllFofoRetailers());
57
        return "manage-pcm";
58
    }
59
 
60
    @RequestMapping(value = "/brand-pcm", method = RequestMethod.POST)
32562 amit.gupta 61
    public String addBrandPCM(Model model, @RequestBody BrandPcmDateModel brandPcmDateModel) throws Exception {
31702 amit.gupta 62
 
63
        SamsungPCM samsungPCM = new SamsungPCM();
64
        samsungPCM.setFofoId(brandPcmDateModel.getFofoId());
65
        samsungPCM.setPcmDate(brandPcmDateModel.getPcmDate());
66
        samsungPCMRepository.persist(samsungPCM);
67
 
68
        model.addAttribute("response1", true);
69
        return "response";
70
    }
71
 
72
    @RequestMapping(value = "/brand-pcm/download", method = RequestMethod.GET)
32562 amit.gupta 73
    public ResponseEntity<InputStreamResource> downloadBrandPCM(@RequestParam int fofoId) throws Exception {
31702 amit.gupta 74
 
75
        FofoStore fs = fofoStoreRepository.selectByRetailerId(fofoId);
76
 
77
        List<BilledImeiModel> billedImeiModels = warehouseInventoryItemRepository.findStockByFofoIdBrandToRebill(fofoId, "Samsung");
78
        List<List<?>> rows = billedImeiModels.stream().map(x -> Arrays.asList(x.getFofoId(), x.getStoreCode(), x.getStoreName(), x.getStoreCity(), x.getBrand(), x.getModelName(), x.getModelNumber(), x.getColor(), x.getSerialNumber(),
79
                FormattingUtils.formatDate(x.getPcmDate()), FormattingUtils.formatDate(x.getInvoiceDate())
80
        )).collect(Collectors.toList());
81
 
82
        org.apache.commons.io.output.ByteArrayOutputStream baos = FileUtil.getCSVByteStream(Arrays.asList("Store Id",
83
                "Store Code", "Business Name", "City", "Brand", "Model Name", "Model Number", "Color", "Imei", "Invoice Date", "PCM Date"), rows);
84
 
85
        final HttpHeaders headers = new HttpHeaders();
86
        headers.set("Content-Type", "text/csv");
87
        headers.set("Content-disposition", "inline; filename=pcm-" + fs.getCode().toLowerCase(Locale.ROOT) + ".csv");
88
        headers.setContentLength(baos.toByteArray().length);
89
 
90
        final InputStream inputStream = new ByteArrayInputStream(baos.toByteArray());
91
        final InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
92
 
93
        return new ResponseEntity<>(inputStreamResource, headers, HttpStatus.OK);
94
    }
95
 
96
 
97
}