Subversion Repositories SmartDukaan

Rev

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

Rev 35458 Rev 35690
Line 55... Line 55...
55
 
55
 
56
import java.math.BigDecimal;
56
import java.math.BigDecimal;
57
import java.util.ArrayList;
57
import java.util.ArrayList;
58
import java.util.List;
58
import java.util.List;
59
import java.util.Map;
59
import java.util.Map;
-
 
60
import java.util.Set;
60
import java.util.stream.Collectors;
61
import java.util.stream.Collectors;
61
 
62
 
62
@Service
63
@Service
63
public class BulkOrderService {
64
public class BulkOrderService {
64
    private static final Logger LOGGER = LogManager.getLogger(BulkOrderService.class);
65
    private static final Logger LOGGER = LogManager.getLogger(BulkOrderService.class);
Line 150... Line 151...
150
            boolean hasZeroQuantity = fofoBulkOrderModels.stream().filter(x -> x.getQuantity() <= 0).count() > 0;
151
            boolean hasZeroQuantity = fofoBulkOrderModels.stream().filter(x -> x.getQuantity() <= 0).count() > 0;
151
            if (hasZeroQuantity) {
152
            if (hasZeroQuantity) {
152
                throw new ProfitMandiBusinessException("Item Quantity", "", "Should be greater than 0");
153
                throw new ProfitMandiBusinessException("Item Quantity", "", "Should be greater than 0");
153
            }
154
            }
154
 
155
 
-
 
156
            // Batch-fetch per-fofo data once (instead of per-item)
-
 
157
            FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(fofoId);
-
 
158
            List<String> partnerIneligibleBrands = brandsService.partnerIneligibleBrands(fofoId);
-
 
159
 
-
 
160
            // Batch-fetch items and tag listings for all items in this fofo's order (2 queries instead of 2N)
-
 
161
            Set<Integer> allItemIds = fofoBulkOrderModels.stream().map(BulkOrderModel::getItemId).collect(Collectors.toSet());
-
 
162
            Map<Integer, TagListing> tagListingMap = tagListingRepository.selectByItemIds(allItemIds);
-
 
163
            Map<Integer, Item> itemMap = itemRepository.selectByIds(allItemIds).stream()
-
 
164
                    .collect(Collectors.toMap(Item::getId, item -> item));
-
 
165
 
155
            List<CartItem> cartItems = new ArrayList<>();
166
            List<CartItem> cartItems = new ArrayList<>();
156
            double totalPayableAmount = 0;
167
            double totalPayableAmount = 0;
157
            BigDecimal totalPayableAmountBD = new BigDecimal(0);
168
            BigDecimal totalPayableAmountBD = new BigDecimal(0);
158
            for (BulkOrderModel fofoBulkOrderModel : fofoBulkOrderModels) {
169
            for (BulkOrderModel fofoBulkOrderModel : fofoBulkOrderModels) {
159
                CartItem cartItem = new CartItem();
170
                CartItem cartItem = new CartItem();
160
                cartItem.setQuantity(fofoBulkOrderModel.getQuantity());
171
                cartItem.setQuantity(fofoBulkOrderModel.getQuantity());
161
                cartItem.setItemId(fofoBulkOrderModel.getItemId());
172
                cartItem.setItemId(fofoBulkOrderModel.getItemId());
162
                TagListing tagListing = tagListingRepository.selectByItemId(fofoBulkOrderModel.getItemId());
173
                TagListing tagListing = tagListingMap.get(fofoBulkOrderModel.getItemId());
163
                if (tagListing == null) {
174
                if (tagListing == null) {
164
                    String message = "Pricing Does not exist for " + fofoBulkOrderModel.getItemId() + "(" + fofoBulkOrderModel.getDescription() + ")";
175
                    String message = "Pricing Does not exist for " + fofoBulkOrderModel.getItemId() + "(" + fofoBulkOrderModel.getDescription() + ")";
165
                    throw new ProfitMandiBusinessException(message, message, message);
176
                    throw new ProfitMandiBusinessException(message, message, message);
166
                }
177
                }
167
                List<String> partnerIneligibleBrands = brandsService.partnerIneligibleBrands(fofoId);
178
                Item item = itemMap.get(fofoBulkOrderModel.getItemId());
168
                Item item = itemRepository.selectById(fofoBulkOrderModel.getItemId());
179
                if (item == null) {
169
                FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(fofoId);
180
                    throw new ProfitMandiBusinessException("Item not found", fofoBulkOrderModel.getItemId(), "Item does not exist: " + fofoBulkOrderModel.getItemId());
-
 
181
                }
170
                if (!fofoStore.isInternal()) {
182
                if (!fofoStore.isInternal()) {
171
                    if (partnerIneligibleBrands.contains(item.getBrand())) {
183
                    if (partnerIneligibleBrands.contains(item.getBrand())) {
172
                        throw new ProfitMandiBusinessException("Brand is not allowed", "Brand ( " + item.getBrand() + ") is not allowed for this partner", "");
184
                        throw new ProfitMandiBusinessException("Brand is not allowed", "Brand ( " + item.getBrand() + ") is not allowed for this partner", "");
173
                    }
185
                    }
174
                }
186
                }