Rev 32562 | Blame | Compare with Previous | Last modification | View Log | RSS feed
package com.spice.profitmandi.web.controller;import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;import com.spice.profitmandi.common.util.FileUtil;import com.spice.profitmandi.common.util.FormattingUtils;import com.spice.profitmandi.dao.entity.fofo.FofoStore;import com.spice.profitmandi.dao.entity.fofo.SamsungPCM;import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;import com.spice.profitmandi.dao.repository.fofo.SamsungPCMRepository;import com.spice.profitmandi.dao.repository.warehouse.BilledImeiModel;import com.spice.profitmandi.dao.repository.warehouse.WarehouseInventoryItemRepository;import com.spice.profitmandi.service.user.RetailerService;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.core.io.InputStreamResource;import org.springframework.http.HttpHeaders;import org.springframework.http.HttpStatus;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.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import java.io.ByteArrayInputStream;import java.io.InputStream;import java.time.LocalDate;import java.util.Arrays;import java.util.List;import java.util.Locale;import java.util.Map;import java.util.stream.Collectors;@Controller@Transactional(rollbackFor = Throwable.class)public class BrandPCMController {private static final Logger LOGGER = LogManager.getLogger(AuthUserController.class);@AutowiredSamsungPCMRepository samsungPCMRepository;@AutowiredRetailerService retailerService;@AutowiredFofoStoreRepository fofoStoreRepository;@AutowiredWarehouseInventoryItemRepository warehouseInventoryItemRepository;@RequestMapping(value = "/brand-pcm", method = RequestMethod.GET)public String brandPcm(Model model) throws ProfitMandiBusinessException {Map<Integer, LocalDate> samsungPCMMap = samsungPCMRepository.selectAll().stream().collect(Collectors.toMap(x -> x.getFofoId(), x -> x.getPcmDate()));model.addAttribute("samsungPCMMap", samsungPCMMap);model.addAttribute("retailerMap", retailerService.getAllFofoRetailers());return "manage-pcm";}@RequestMapping(value = "/brand-pcm", method = RequestMethod.POST)public String addBrandPCM(Model model, @RequestBody BrandPcmDateModel brandPcmDateModel) throws Exception {SamsungPCM samsungPCM = new SamsungPCM();samsungPCM.setFofoId(brandPcmDateModel.getFofoId());samsungPCM.setPcmDate(brandPcmDateModel.getPcmDate());samsungPCMRepository.persist(samsungPCM);model.addAttribute("response1", true);return "response";}@RequestMapping(value = "/brand-pcm/download", method = RequestMethod.GET)public ResponseEntity<InputStreamResource> downloadBrandPCM(@RequestParam int fofoId) throws Exception {FofoStore fs = fofoStoreRepository.selectByRetailerId(fofoId);List<BilledImeiModel> billedImeiModels = warehouseInventoryItemRepository.findStockByFofoIdBrandToRebill(fofoId, "Samsung");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(),FormattingUtils.formatDate(x.getPcmDate()), FormattingUtils.formatDate(x.getInvoiceDate()))).collect(Collectors.toList());org.apache.commons.io.output.ByteArrayOutputStream baos = FileUtil.getCSVByteStream(Arrays.asList("Store Id","Store Code", "Business Name", "City", "Brand", "Model Name", "Model Number", "Color", "Imei", "Invoice Date", "PCM Date"), rows);final HttpHeaders headers = new HttpHeaders();headers.set("Content-Type", "text/csv");headers.set("Content-disposition", "inline; filename=pcm-" + fs.getCode().toLowerCase(Locale.ROOT) + ".csv");headers.setContentLength(baos.toByteArray().length);final InputStream inputStream = new ByteArrayInputStream(baos.toByteArray());final InputStreamResource inputStreamResource = new InputStreamResource(inputStream);return new ResponseEntity<>(inputStreamResource, headers, HttpStatus.OK);}}