Subversion Repositories SmartDukaan

Rev

Rev 33173 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
33172 tejus.loha 1
package com.spice.profitmandi.service.order;
2
 
3
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
4
import com.spice.profitmandi.common.model.BulkOrderModel;
5
import com.spice.profitmandi.common.util.ExcelUtils;
6
import com.spice.profitmandi.dao.cart.CartService;
7
import com.spice.profitmandi.dao.entity.catalog.TagListing;
8
import com.spice.profitmandi.dao.model.CartItem;
9
import com.spice.profitmandi.dao.model.UserCart;
10
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
11
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
12
import com.spice.profitmandi.service.transaction.TransactionService;
13
import com.spice.profitmandi.service.wallet.CommonPaymentService;
14
import com.spice.profitmandi.service.wallet.WalletService;
15
import org.apache.logging.log4j.LogManager;
16
import org.apache.logging.log4j.Logger;
17
import org.apache.poi.ss.usermodel.Row;
18
import org.apache.poi.xssf.usermodel.XSSFRow;
19
import org.apache.poi.xssf.usermodel.XSSFSheet;
20
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
21
import org.springframework.beans.factory.annotation.Autowired;
22
import org.springframework.stereotype.Controller;
23
import org.springframework.web.multipart.MultipartFile;
24
 
25
import java.util.ArrayList;
26
import java.util.List;
27
import java.util.Map;
28
import java.util.stream.Collectors;
29
 
30
@Controller
31
public class BulkOrderService {
32
    private static final Logger LOGGER = LogManager.getLogger(BulkOrderService.class);
33
    @Autowired
34
    CartService cartService;
35
    @Autowired
36
    TransactionService transactionService;
37
    @Autowired
38
    CommonPaymentService commonPaymentService;
39
    @Autowired
40
    TagListingRepository tagListingRepository;
41
    @Autowired
42
    WalletService walletService;
43
    @Autowired
44
    UserRepository userRepository;
45
    @Autowired
46
    ExcelUtils excelUtils;
47
 
48
    public void parseBulkOrders(MultipartFile file) throws Exception {
49
        XSSFWorkbook myWorkBook = new XSSFWorkbook(file.getInputStream());
50
 
51
        myWorkBook.setMissingCellPolicy(Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
52
        // Return first sheet from the XLSX workbook
53
        XSSFSheet mySheet = myWorkBook.getSheetAt(0);
54
        LOGGER.info("rowCellNum {}", mySheet.getLastRowNum());
55
        List<BulkOrderModel> bulkOrderModels = new ArrayList<>();
56
        for (int rowNumber = 1; rowNumber <= mySheet.getLastRowNum(); rowNumber++) {
57
            XSSFRow row = mySheet.getRow(rowNumber);
58
            BulkOrderModel bulkOrderModel = this.createBulkModel(row);
59
            bulkOrderModels.add(bulkOrderModel);
60
        }
61
        Map<Integer, List<BulkOrderModel>> fofoBulkOrdersMap = bulkOrderModels.stream().collect(Collectors.groupingBy(x -> x.getFofoId()));
62
        for (Map.Entry<Integer, List<BulkOrderModel>> fofoBulkOrderEntry : fofoBulkOrdersMap.entrySet()) {
63
            int fofoId = fofoBulkOrderEntry.getKey();
64
            List<BulkOrderModel> fofoBulkOrderModels = fofoBulkOrderEntry.getValue();
65
            Map<Integer, Long> orderItemCountMap = fofoBulkOrderModels.stream().collect(Collectors.groupingBy(x -> x.getItemId(), Collectors.counting()));
66
 
67
            if (orderItemCountMap.entrySet().stream().filter(x -> x.getValue() > 1).count() > 0) {
68
                throw new ProfitMandiBusinessException("Fofo ID", fofoId, "Duplicate in items");
69
            }
70
 
71
            boolean hasZeroQuantity = fofoBulkOrderModels.stream().filter(x -> x.getQuantity() <= 0).count() > 0;
72
            if (hasZeroQuantity) {
73
                throw new ProfitMandiBusinessException("Item Quantity", "", "Should be greater than 0");
74
            }
75
 
76
            List<CartItem> cartItems = new ArrayList<>();
77
            double totalAmount = 0;
78
            for (BulkOrderModel fofoBulkOrderModel : fofoBulkOrderModels) {
79
                CartItem cartItem = new CartItem();
80
                cartItem.setQuantity(fofoBulkOrderModel.getQuantity());
81
                cartItem.setItemId(fofoBulkOrderModel.getItemId());
82
                TagListing tagListing = tagListingRepository.selectByItemId(fofoBulkOrderModel.getItemId());
83
                //TODO:Tejus need to check
84
                cartItem.setSellingPrice(tagListing.getSellingPrice());
85
                totalAmount += cartItem.getSellingPrice() * cartItem.getQuantity();
86
                cartItems.add(cartItem);
87
 
88
 
89
            }
90
            LOGGER.info("totalAmount of item " + totalAmount);
91
            double walletAmount = walletService.getWalletAmount(fofoId);
92
 
93
            if (walletAmount < totalAmount) {
94
                LOGGER.info("Skippin order due to insufficent balance for id - {}", fofoId);
95
                continue;
96
            }
97
            UserCart userCart = cartService.setCartItems(fofoId, cartItems);
98
            int transactionId = transactionService.createTransactionInternally(userCart, totalAmount, 0);
99
            commonPaymentService.payThroughWallet(transactionId);
100
            // Once paid let proceed to process the order further.
101
            transactionService.processTransaction(transactionId);
102
        }
103
    }
104
 
105
    private BulkOrderModel createBulkModel(XSSFRow row) throws ProfitMandiBusinessException {
106
        BulkOrderModel bulkOrderModel = new BulkOrderModel();
107
        int i = 0;
108
        bulkOrderModel.setRowIndex(row.getRowNum());
109
        try {
110
            bulkOrderModel.setName(row.getCell(i++).getStringCellValue());
111
            bulkOrderModel.setDescription(row.getCell(i++).getStringCellValue());
112
            bulkOrderModel.setFofoId((int) row.getCell(i++).getNumericCellValue());
113
            bulkOrderModel.setItemId((int) row.getCell(i++).getNumericCellValue());
114
            bulkOrderModel.setQuantity((int) row.getCell(i++).getNumericCellValue());
115
        } catch (Throwable e) {
116
            LOGGER.info(e.getCause());
117
            throw new ProfitMandiBusinessException("Field", "Field at row - " + row.getRowNum() + ", column - " + (i - 1),
118
                    "Invalid field value at - " + excelUtils.toAlphabet(i - 1) + (row.getRowNum() + 1) + ", " + excelUtils.getCellValue(row.getCell(i - 1)));
119
        }
120
        LOGGER.info(bulkOrderModel);
121
        return bulkOrderModel;
122
    }
123
 
124
 
125
}