Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
30209 amit.gupta 1
package com.smartdukaan.cron.scheduled;
2
 
3
import com.spice.profitmandi.dao.entity.fofo.ActivatedImei;
4
import com.spice.profitmandi.dao.repository.fofo.ActivatedImeiRepository;
5
import org.apache.logging.log4j.LogManager;
6
import org.apache.logging.log4j.Logger;
7
import org.springframework.beans.factory.annotation.Autowired;
8
import org.springframework.stereotype.Service;
9
import org.springframework.transaction.annotation.Transactional;
10
 
11
import java.time.LocalDate;
12
import java.time.LocalDateTime;
13
import java.util.ArrayList;
14
import java.util.List;
15
import java.util.Map;
16
import java.util.concurrent.ExecutionException;
17
import java.util.concurrent.ExecutorService;
18
import java.util.concurrent.Executors;
19
import java.util.concurrent.Future;
20
 
21
@Service
22
@Transactional(rollbackFor = Throwable.class)
23
public class OppoImeiActivationService {
24
 
25
	private static final Logger LOGGER = LogManager.getLogger(OppoImeiActivationService.class);
26
	@Autowired
27
	ActivatedImeiRepository activatedImeiRepository;
28
 
29
	public void updateActivationDate(List<String> imeis) throws Exception {
30315 amit.gupta 30
		int chromeThreads = 7;
31
		int bucketSize = 4;
30209 amit.gupta 32
		ExecutorService excecutorService = Executors.newFixedThreadPool(chromeThreads);
33
		List<CheckOppoWarrantyTask> tasks = new ArrayList<>();
30315 amit.gupta 34
		for (int i = 0; i < imeis.size() / bucketSize; i++) {
35
			CheckOppoWarrantyTask task = new CheckOppoWarrantyTask(imeis.subList(i * bucketSize, (i + 1) * bucketSize));
30209 amit.gupta 36
			tasks.add(task);
37
		}
38
		List<Future<Map<String, LocalDate>>> futures = excecutorService.invokeAll(tasks);
39
		List<String> foundImeis = new ArrayList<>();
40
		futures.stream().forEach(x -> {
41
			try {
42
				x.get().entrySet().forEach(y -> {
43
					foundImeis.add(y.getKey());
44
					System.out.println("Serial Number " + y.getKey() + "Date " + y.getValue());
45
					if (y.getValue() == null) return;
46
					if (activatedImeiRepository.selectBySerialNumber(y.getKey()) == null) {
47
						ActivatedImei activatedImei = new ActivatedImei();
48
						activatedImei.setSerialNumber(y.getKey());
49
						activatedImei.setActivationTimestamp(y.getValue().atStartOfDay());
50
						activatedImei.setCreateTimestamp(LocalDateTime.now());
51
						activatedImeiRepository.persist(activatedImei);
52
					}
53
				});
54
			} catch (InterruptedException e) {
55
				e.printStackTrace();
56
			} catch (ExecutionException e) {
57
				e.printStackTrace();
58
			}
59
		});
60
		imeis.removeAll(foundImeis);
61
		LOGGER.info("Could not break captcha for imeis - " + foundImeis);
62
	}
63
}