Subversion Repositories SmartDukaan

Rev

Rev 29677 | Rev 29686 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 29677 Rev 29683
Line 1... Line 1...
1
package com.spice.profitmandi.service.offers;
1
package com.spice.profitmandi.service.offers;
2
 
2
 
-
 
3
import java.io.FileInputStream;
-
 
4
import java.io.IOException;
-
 
5
import java.io.InputStream;
3
import java.time.LocalDate;
6
import java.time.LocalDate;
4
import java.time.LocalDateTime;
-
 
5
import java.time.YearMonth;
7
import java.time.YearMonth;
6
import java.util.ArrayList;
8
import java.util.ArrayList;
7
import java.util.Arrays;
9
import java.util.Arrays;
8
import java.util.Collections;
10
import java.util.Collections;
9
import java.util.Comparator;
11
import java.util.Comparator;
-
 
12
import java.util.HashMap;
-
 
13
import java.util.Iterator;
10
import java.util.List;
14
import java.util.List;
-
 
15
import java.util.Map;
11
import java.util.Set;
16
import java.util.Set;
12
import java.util.stream.Collectors;
17
import java.util.stream.Collectors;
13
 
18
 
14
import org.apache.logging.log4j.LogManager;
19
import org.apache.logging.log4j.LogManager;
15
import org.apache.logging.log4j.Logger;
20
import org.apache.logging.log4j.Logger;
-
 
21
import org.apache.poi.ss.usermodel.Cell;
-
 
22
import org.apache.poi.ss.usermodel.CellType;
-
 
23
import org.apache.poi.ss.usermodel.Row;
-
 
24
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
-
 
25
import org.apache.poi.xssf.usermodel.XSSFRow;
-
 
26
import org.apache.poi.xssf.usermodel.XSSFSheet;
-
 
27
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
16
import org.springframework.beans.factory.annotation.Autowired;
28
import org.springframework.beans.factory.annotation.Autowired;
17
import org.springframework.stereotype.Component;
29
import org.springframework.stereotype.Component;
18
 
30
 
19
import com.google.gson.Gson;
31
import com.google.gson.Gson;
20
import com.spice.profitmandi.common.enumuration.SchemeType;
-
 
21
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
32
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
-
 
33
import com.spice.profitmandi.common.model.PartnerTargetModel;
-
 
34
import com.spice.profitmandi.common.model.ProfitMandiConstants;
22
import com.spice.profitmandi.dao.entity.catalog.ItemCriteria;
35
import com.spice.profitmandi.dao.entity.catalog.ItemCriteria;
23
import com.spice.profitmandi.dao.entity.catalog.Offer;
36
import com.spice.profitmandi.dao.entity.catalog.Offer;
24
import com.spice.profitmandi.dao.entity.catalog.TargetSlab;
37
import com.spice.profitmandi.dao.entity.catalog.TargetSlab;
25
import com.spice.profitmandi.dao.enumuration.catalog.OfferSchemeType;
38
import com.spice.profitmandi.dao.enumuration.catalog.OfferSchemeType;
26
import com.spice.profitmandi.dao.model.CreateOfferRequest;
39
import com.spice.profitmandi.dao.model.CreateOfferRequest;
Line 244... Line 257...
244
			}
257
			}
245
		}
258
		}
246
 
259
 
247
		return createOfferRequests;
260
		return createOfferRequests;
248
	}
261
	}
-
 
262
	
-
 
263
	@Override
-
 
264
	public void createOffers(InputStream fileInputStream) throws Exception {
-
 
265
		Map<CreateOfferRequest, List<PartnerTargetModel>> offerPartnerTargetMap = this.parseFromExcel(fileInputStream);
-
 
266
		for(Map.Entry<CreateOfferRequest, List<PartnerTargetModel>> partnerTargetEntry: offerPartnerTargetMap.entrySet()) {
-
 
267
			CreateOfferRequest createOfferRequest = partnerTargetEntry.getKey();
-
 
268
			Map<List<Integer>, List<Integer>> targetPartnerMap = partnerTargetEntry.getValue().stream().collect(Collectors.groupingBy(PartnerTargetModel::getTargets, Collectors.mapping(PartnerTargetModel::getFofoId, Collectors.toList())));
-
 
269
					for (Map.Entry<List<Integer>, List<Integer>> targetPartnersEntry : targetPartnerMap.entrySet()) {
-
 
270
						List<Integer> fofoIds = createOfferRequest.getPartnerCriteria().getFofoIds();
-
 
271
						List<Integer> filterFofoIds = targetPartnersEntry.getValue().stream().filter(x->!fofoIds.contains(x)).collect(Collectors.toList());
-
 
272
						List<Integer> targets = targetPartnersEntry.getKey();
-
 
273
						
-
 
274
						createOfferRequest.getPartnerCriteria().setFofoIds(filterFofoIds);
-
 
275
						int counter = 0;
-
 
276
						for (com.spice.profitmandi.dao.model.TargetSlab targetSlab : createOfferRequest.getTargetSlabs()) {
-
 
277
							targetSlab.setOnwardsAmount(targets.get(counter));
-
 
278
							counter++;
-
 
279
						}
-
 
280
						this.addOfferService(createOfferRequest);
-
 
281
					}
-
 
282
		}
-
 
283
	}
-
 
284
	
-
 
285
	private Map<CreateOfferRequest, List<PartnerTargetModel>> parseFromExcel(InputStream inputStream) throws ProfitMandiBusinessException {
-
 
286
 
-
 
287
		Map<CreateOfferRequest, List<PartnerTargetModel>> offerPartnerTargetMap =  new HashMap<>();
-
 
288
		XSSFWorkbook myWorkBook = null;
-
 
289
		try {
-
 
290
			myWorkBook = new XSSFWorkbook(inputStream);
-
 
291
			myWorkBook.setMissingCellPolicy(MissingCellPolicy.RETURN_BLANK_AS_NULL);
-
 
292
			for(int i=0; i< myWorkBook.getNumberOfSheets();i++) {
-
 
293
				
-
 
294
				XSSFSheet mySheet = myWorkBook.getSheetAt(i);
-
 
295
				int offerId = Integer.parseInt(mySheet.getSheetName());
-
 
296
				CreateOfferRequest existingOffer = this.getOffer(offerId);
-
 
297
				List<PartnerTargetModel> partnerTargetModels = new ArrayList<>();
-
 
298
				offerPartnerTargetMap.put(existingOffer, partnerTargetModels);
-
 
299
				this.printIterator(mySheet);
-
 
300
				LOGGER.info("rowCellNum {}", mySheet.getLastRowNum());
-
 
301
				for (int rowNumber = 1; rowNumber <= mySheet.getLastRowNum(); rowNumber++) {
-
 
302
					XSSFRow row = mySheet.getRow(rowNumber);
-
 
303
					LOGGER.info("row {}", row);
-
 
304
					PartnerTargetModel partnerTargetModel = new PartnerTargetModel();
-
 
305
 
-
 
306
					if (row.getCell(0) != null && row.getCell(0).getCellTypeEnum() == CellType.NUMERIC) {
-
 
307
						partnerTargetModel.setFofoId((int) row.getCell(0).getNumericCellValue());
-
 
308
					} else {
-
 
309
						ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(
-
 
310
								"FOFO ID", row.getCell(0).toString(), "TGLSTNG_VE_1010");
-
 
311
						LOGGER.error("Excel file parse error : ", profitMandiBusinessException);
-
 
312
						throw profitMandiBusinessException;
-
 
313
					}
-
 
314
					List<Integer> targets = new ArrayList<>();
-
 
315
					for (int cellNumber = 1; cellNumber <= existingOffer.getTargetSlabs().size(); cellNumber++) {
-
 
316
						if (row.getCell(cellNumber) != null
-
 
317
								&& row.getCell(cellNumber).getCellTypeEnum() == CellType.NUMERIC) {
-
 
318
							targets.add((int) row.getCell(cellNumber).getNumericCellValue());
-
 
319
						} else {
-
 
320
							ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(
-
 
321
									"Target Column issue at " + rowNumber + ", " + cellNumber, row.getCell(cellNumber),
-
 
322
									"Invalid target");
-
 
323
							LOGGER.error("Excel file parse error : ", profitMandiBusinessException);
-
 
324
							throw profitMandiBusinessException;
-
 
325
						}
-
 
326
					}
-
 
327
					partnerTargetModel.setTargets(targets);
-
 
328
					partnerTargetModels.add(partnerTargetModel);
-
 
329
				}
-
 
330
			}
-
 
331
			myWorkBook.close();
-
 
332
		} catch ( ProfitMandiBusinessException pbse) {
-
 
333
			throw pbse;
-
 
334
		} catch (IOException ioException) {
-
 
335
			ioException.printStackTrace();
-
 
336
			throw new ProfitMandiBusinessException(ProfitMandiConstants.EXCEL_FILE, ioException.getMessage(),
-
 
337
					"EXL_VE_1000");
-
 
338
		} finally {
-
 
339
			if (myWorkBook != null) {
-
 
340
				try {
-
 
341
					myWorkBook.close();
-
 
342
				} catch (IOException e) {
-
 
343
					// TODO Auto-generated catch block
-
 
344
					e.printStackTrace();
-
 
345
				}
-
 
346
			}
-
 
347
		}
-
 
348
		LOGGER.info("offerPartnerTargetMap {}", offerPartnerTargetMap);
-
 
349
		return offerPartnerTargetMap;
-
 
350
	}
-
 
351
	
-
 
352
	private void printIterator (XSSFSheet sheet) {
-
 
353
		 Iterator<Row> rowIterator = sheet.iterator();
-
 
354
         while (rowIterator.hasNext()) 
-
 
355
         {
-
 
356
             Row row = rowIterator.next();
-
 
357
             //For each row, iterate through all the columns
-
 
358
             Iterator<Cell> cellIterator = row.cellIterator();
-
 
359
              
-
 
360
             while (cellIterator.hasNext()) 
-
 
361
             {
-
 
362
                 Cell cell = cellIterator.next();
-
 
363
                 //Check the cell type and format accordingly
-
 
364
                 switch (cell.getCellType()) 
-
 
365
                 {
-
 
366
                     case Cell.CELL_TYPE_NUMERIC:
-
 
367
                         System.out.print(cell.getNumericCellValue() + "t");
-
 
368
                         break;
-
 
369
                     case Cell.CELL_TYPE_STRING:
-
 
370
                         System.out.print(cell.getStringCellValue() + "t");
-
 
371
                         break;
-
 
372
                 }
-
 
373
             }
-
 
374
             System.out.println("");
-
 
375
         }
-
 
376
	}
-
 
377
 
249
 
378
 
250
}
379
}