Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
23819 govind 1
package com.spice.profitmandi.web.controller;
2
 
3
import java.io.ByteArrayInputStream;
4
import java.io.ByteArrayOutputStream;
5
import java.io.InputStream;
6
import java.time.LocalDateTime;
7
import java.util.ArrayList;
8
import java.util.HashMap;
9
import java.util.HashSet;
10
import java.util.LinkedHashMap;
11
import java.util.List;
12
import java.util.Map;
13
import java.util.stream.Collectors;
14
 
15
import javax.servlet.http.HttpServletRequest;
16
import javax.transaction.Transactional;
17
 
18
import org.apache.logging.log4j.LogManager;
19
import org.apache.logging.log4j.Logger;
20
import org.springframework.beans.factory.annotation.Autowired;
21
import org.springframework.beans.factory.annotation.Qualifier;
22
import org.springframework.core.io.InputStreamResource;
23
import org.springframework.http.HttpHeaders;
24
import org.springframework.http.HttpStatus;
25
import org.springframework.http.ResponseEntity;
26
import org.springframework.stereotype.Controller;
27
import org.springframework.ui.Model;
28
import org.springframework.web.bind.annotation.PathVariable;
29
import org.springframework.web.bind.annotation.RequestMapping;
30
import org.springframework.web.bind.annotation.RequestMethod;
31
 
32
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
33
import com.spice.profitmandi.common.util.ExcelUtils;
34
import com.spice.profitmandi.dao.entity.catalog.Item;
35
import com.spice.profitmandi.dao.entity.transaction.LineItemImei;
36
import com.spice.profitmandi.dao.entity.transaction.PriceDrop;
37
import com.spice.profitmandi.dao.entity.transaction.PriceDropIMEI;
38
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
39
import com.spice.profitmandi.dao.repository.transaction.LineItemImeisRepository;
40
import com.spice.profitmandi.dao.repository.transaction.PriceDropIMEIRepository;
41
import com.spice.profitmandi.dao.repository.transaction.PriceDropRepository;
42
import com.spice.profitmandi.service.inventory.InventoryService;
43
import com.spice.profitmandi.service.pricing.PriceDropService;
44
import com.spice.profitmandi.service.wallet.WalletService;
45
import com.spice.profitmandi.web.util.MVCResponseSender;
46
 
47
import in.shop2020.model.v1.order.WalletReferenceType;
48
 
49
@Controller
50
@Transactional
51
public class PriceDropController {
52
 
53
	private static final Logger LOGGER = LogManager.getLogger(PriceDropController.class);
54
 
55
	@Autowired
56
	private PriceDropRepository priceDropRepository;
57
 
58
	@Autowired
59
	private InventoryService inventoryService;
60
 
61
	@Autowired
62
	private PriceDropIMEIRepository priceDropIMEIRepository;
63
 
64
	@Autowired
65
	private PriceDropService priceDropService;
66
 
67
	@Autowired
68
	private MVCResponseSender mvcResponseSender;
69
 
70
	@Autowired
71
	private WalletService walletService;
72
 
73
	@Autowired
74
	private LineItemImeisRepository lineItemImeisRepository;
75
 
76
	@Autowired
77
	@Qualifier("catalogItemRepository")
78
	private ItemRepository itemRepository;
79
 
80
	private List<Integer> getItemIds() {
81
		List<Integer> itemIds = new ArrayList<>();
82
		List<PriceDrop> priceDrops = priceDropRepository.selectAll();
83
		for (PriceDrop priceDrop : priceDrops) {
84
			itemIds.add(priceDrop.getItemId());
85
		}
86
		return itemIds;
87
	}
88
 
89
	@RequestMapping(value = "/getItemDescription", method = RequestMethod.GET)
90
	public String getItemDescription(HttpServletRequest request, Model model) {
91
 
92
		List<Integer> itemIds = this.getItemIds();
93
		Map<Integer, String> itemIdDescriptions = inventoryService
94
				.getItemIdDescriptionMap(new HashSet<Integer>(itemIds));
95
		List<PriceDrop> priceDrops = priceDropRepository.selectAll();
96
		model.addAttribute("itemIdDescriptions", itemIdDescriptions);
97
		model.addAttribute("priceDrops", priceDrops);
98
		return "price-drop";
99
 
100
	}
101
 
102
	@RequestMapping(value = "/downloadPriceDropIMEI/{priceDropId}", method = RequestMethod.GET)
103
	public ResponseEntity<?> downloadPriceDropIMEI(HttpServletRequest request, @PathVariable int priceDropId,
104
			Model model) throws ProfitMandiBusinessException {
105
 
106
		PriceDrop priceDrop = priceDropRepository.selectById(priceDropId);
107
		List<PriceDropIMEI> priceDropIMEIs = priceDropIMEIRepository.selectByPriceDropId(priceDrop.getId());
108
		List<String> imeis = priceDropIMEIs.stream().map(x -> x.getImei()).collect(Collectors.toList());
109
		List<LineItemImei> lineItemImeis = lineItemImeisRepository.selectByIMEI(imeis);
110
		Map<String, String> priceDropIMEIfofoId = priceDropService.getIMEIandRetailerName(lineItemImeis, priceDrop);
111
		if (priceDropIMEIfofoId.size() > 0) {
112
			ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
113
			ExcelUtils.writePriceDrop(priceDropIMEIfofoId, priceDrop.getItemId(), byteArrayOutputStream);
114
			final HttpHeaders headers = new HttpHeaders();
115
			// private static final String CONTENT_TYPE_XLSX =
116
			// "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
117
			headers.set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
118
			// headers.set("Content-Type", "application/vnd.ms-excel");
119
			headers.set("Content-disposition", "inline; filename=priceDrop" + priceDrop.getId() + ".xlsx");
120
			headers.setContentLength(byteArrayOutputStream.toByteArray().length);
121
			final InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
122
			final InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
123
			return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
124
		} else {
125
			throw new ProfitMandiBusinessException("priceDropId", priceDropId, "No IMEI is Eligible For PriceDrop");
126
		}
127
 
128
	}
129
 
130
	@RequestMapping(value = "/paymentAgainstPriceDrop/{priceDropId}/{processedamount}", method = RequestMethod.POST)
131
	public String paymentAgainstPriceDropIMEI(HttpServletRequest request, @PathVariable int priceDropId,
132
			@PathVariable int processedamount, Model model) throws Exception {
133
 
134
 
135
		int noOfIMEIForPartner = 1;
136
		Map<Integer, Integer> noOfPriceDropToPartners = new HashMap<>();
137
		PriceDrop priceDrop = priceDropRepository.selectById(priceDropId);
138
		Item item = itemRepository.selectById(priceDrop.getItemId());
139
		List<PriceDropIMEI> priceDropIMEIs = priceDropIMEIRepository.selectByPriceDropId(priceDrop.getId());
140
		List<String> imeis = priceDropIMEIs.stream().map(x -> x.getImei()).collect(Collectors.toList());
141
		List<LineItemImei> lineItemImeis = lineItemImeisRepository.selectByIMEI(imeis);
142
		Map<String, Integer> priceDropIMEIfofoIds = priceDropService.getIMEIAndfofoIdForPriceDrop(lineItemImeis,
143
				priceDrop);
144
		if (priceDropIMEIfofoIds.size() > 0) {
145
			for (Map.Entry<String, Integer> priceDropIMEIAndFofoId : priceDropIMEIfofoIds.entrySet()) {
146
				int fofoId = priceDropIMEIAndFofoId.getValue();
147
				if (noOfPriceDropToPartners.containsKey(fofoId)) {
148
					noOfPriceDropToPartners.put(fofoId, noOfPriceDropToPartners.get(fofoId) + 1);
149
				} else {
150
					noOfPriceDropToPartners.put(fofoId, noOfIMEIForPartner);
151
				}
152
 
153
			}
154
			for (Map.Entry<Integer, Integer> noOfPriceDropToPartner : noOfPriceDropToPartners.entrySet()) {
155
				String description = String.format("Price Drop amount added for %s, total %d pcs.",
156
						item.getItemDescription(), noOfPriceDropToPartner.getValue());
157
				walletService.addAmountToWallet(noOfPriceDropToPartner.getKey(), priceDrop.getId(),
158
						WalletReferenceType.PRICE_DROP, description,
159
						(noOfPriceDropToPartner.getValue() * processedamount));
160
				LOGGER.info(
161
						"Price Drop amount [{}] has been added with retailerId [{}]'s wallet, referenceId [{}], No Of serialNumber [{}]",
162
						(noOfPriceDropToPartner.getValue() * processedamount), noOfPriceDropToPartner.getKey(),
163
						priceDrop.getId(), noOfPriceDropToPartner.getValue());
164
			}
165
			priceDrop.setProcessTimestamp(LocalDateTime.now());
23823 amit.gupta 166
			priceDrop.setPartnerPayout(processedamount);
23819 govind 167
			priceDropRepository.persist(priceDrop);
168
			model.addAttribute("response", mvcResponseSender.createResponseString(true));
169
			return "response";
170
		} else {
171
			priceDrop.setProcessTimestamp(LocalDateTime.now());
172
			model.addAttribute("response", mvcResponseSender.createResponseString(false));
173
			return "false";
174
		}
175
	}
176
 
177
	@RequestMapping(value = "/downloadtotalPriceDropIMEI/{priceDropId}", method = RequestMethod.GET)
178
	public ResponseEntity<?> downloadTotalPriceDropIMEI(HttpServletRequest request, @PathVariable int priceDropId,
179
			Model model) throws ProfitMandiBusinessException {
180
 
181
		PriceDrop priceDrop = priceDropRepository.selectById(priceDropId);
182
		Map<String, String> priceDropIMEIAndItemId = this.getpriceDropIMEIAndItemDescription(priceDrop);
183
		if (priceDropIMEIAndItemId.size() > 0) {
184
			ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
185
			ExcelUtils.writePriceDropForAllIMEI(priceDropIMEIAndItemId, byteArrayOutputStream);
186
			final HttpHeaders headers = new HttpHeaders();
187
			headers.set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
188
			headers.set("Content-disposition", "inline; filename=totalPriceDropIMEI" + priceDrop.getItemId() + ".xlsx");
189
			headers.setContentLength(byteArrayOutputStream.toByteArray().length);
190
			final InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
191
			final InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
192
			return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
193
		} else {
194
			priceDrop.setProcessTimestamp(LocalDateTime.now());
195
			throw new ProfitMandiBusinessException("IMEI", 1, "IMEI not Found For PriceDrop");
196
		}
197
	}
198
 
199
	private Map<String, String> getpriceDropIMEIAndItemDescription(PriceDrop priceDrop)
200
			throws ProfitMandiBusinessException {
201
		Map<String, String> priceDropIMEIAndItemId = new LinkedHashMap<>();
202
		List<PriceDropIMEI> priceDropIMEIs = priceDropIMEIRepository.selectByPriceDropId(priceDrop.getId());
203
		Item item = itemRepository.selectById(priceDrop.getItemId());
204
		for (PriceDropIMEI priceDropIMEI : priceDropIMEIs) {
205
			priceDropIMEIAndItemId.put(priceDropIMEI.getImei(), item.getItemDescription());
206
		}
207
		return priceDropIMEIAndItemId;
208
	}
209
 
210
}