Subversion Repositories SmartDukaan

Rev

Rev 29477 | Rev 30335 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.smartdukaan.cron.scheduled;

import com.smartdukaan.cron.migrations.RunOnceTasks;
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.dao.entity.fofo.ActivatedImei;
import com.spice.profitmandi.dao.model.ImeiActivationTimestampModel;
import com.spice.profitmandi.dao.repository.fofo.ActivatedImeiRepository;
import com.spice.profitmandi.service.inventory.InventoryService;
import okhttp3.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Component
class VivoImeiActivationService {

        @Autowired
        InventoryService inventoryService;

        @Autowired
        CaptchaService captchaService;

        @Autowired
        ActivatedImeiRepository activatedImeiRepository;

        private static final Logger LOGGER = LogManager.getLogger(RunOnceTasks.class);

        private final Map<String, List<Cookie>> cookieStore = new HashMap<>();

        public void checkImeiActivation() throws ProfitMandiBusinessException, IOException, Exception {

                OkHttpClient okHttpClient = new OkHttpClient.Builder().cookieJar(new CookieJar() {
                        @Override
                        public void saveFromResponse(HttpUrl httpUrl, List<Cookie> list) {
                                LOGGER.info("SAVE fROM - {}", httpUrl.host());
                                cookieStore.put(httpUrl.host(), list);
                        }

                        @Override
                        public List<Cookie> loadForRequest(HttpUrl httpUrl) {
                                List<Cookie> cookies = cookieStore.get(httpUrl.host());
                                LOGGER.info("load from - {}", httpUrl.host());
                                return cookies != null ? cookies : new ArrayList<Cookie>();
                        }
                }).build();

                String captchaBreak = this.getNewCaptcha(okHttpClient);

                LOGGER.info("Captcha code {}", captchaBreak);

                List<ImeiActivationTimestampModel> imeisActivationList = activatedImeiRepository
                                .selectImeiActivationByBrand("Vivo");

                LOGGER.info("Complete list size is - {}", imeisActivationList.size());
                this.getImeiActivation(captchaBreak, okHttpClient, imeisActivationList);

        }

        public String getNewCaptcha(OkHttpClient okHttpClient) throws Exception {

                LOGGER.info("okHttpClient" + okHttpClient);

                HttpUrl vivoSupportUrl = HttpUrl.parse("https://www.vivo.com/in/support/generatingCodes");
                Request request = new Request.Builder().url(vivoSupportUrl).build();

                Response response = okHttpClient.newCall(request).execute();
                LOGGER.info("cookie {}", this.cookieStore.values());

                LOGGER.info("vivoSupportUrl" + vivoSupportUrl);
                LOGGER.info("request" + request);
                LOGGER.info("request" + request);

                String filePath = "/tmp/captcha.avif";
                Files.write(Paths.get(filePath), response.body().bytes());

                // Get all relevent imes from activatedImei and inventoryItem for vivo

                String captchaBreak = captchaService.getCaptchaCode(filePath);

                return captchaBreak;
        }

        public void getImeiActivation(String captchaBreak, OkHttpClient okHttpClient,
                                                                  List<ImeiActivationTimestampModel> imeisActivationList) throws Exception {

                for (ImeiActivationTimestampModel vivoImeiAndActivationTimeStampModel : new HashSet<>(imeisActivationList)) {
                        String imei = vivoImeiAndActivationTimeStampModel.getSerialNumber();
                        RequestBody formBody = new FormBody.Builder().add("imei", imei).add("code", captchaBreak).build();

                        Request request1 = new Request.Builder().url("https://www.vivo.com/in/support/checkCode").post(formBody)
                                        .build();

                        Response response2 = okHttpClient.newCall(request1).execute();
                        JSONObject imeiActivationJson = new JSONObject(response2.body().string());
                        JSONObject data = imeiActivationJson.getJSONObject("data");

                        int status = data.getInt("status");
                        if (status == 0) {
                                captchaBreak = this.getNewCaptcha(okHttpClient);
                                continue;
                        } else if (status == 2) {
                                LOGGER.info("Received status 2 for {}", imei);
                                continue;
                        }

                        JSONObject imeiQueryDto = data.getJSONObject("imeiQueryDto");

                        Object dueTimeObject = imeiQueryDto.get("dueTime");
                        String dueTimeString = dueTimeObject.toString();

                        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
                        String dueTimeSplit = dueTimeString.split(" ")[0];

                        

                        if (!StringUtils.isEmpty(dueTimeSplit)) {
                                LocalDate dateTime = LocalDate.parse(dueTimeSplit, formatter);
                                ActivatedImei activatedImei = activatedImeiRepository.selectBySerialNumber(imei);
                                if (activatedImei == null) {
                                        activatedImei = new ActivatedImei();
                                        activatedImei.setActivationTimestamp(dateTime.atStartOfDay().minusYears(1));
                                        activatedImei.setSerialNumber(imei);
                                        activatedImei.setCreateTimestamp(LocalDateTime.now());
                                        activatedImeiRepository.persist(activatedImei);

                                } else {
                                        activatedImei.setActivationTimestamp(dateTime.atStartOfDay().minusYears(1));

                                }

                                LOGGER.info("activatedImei" + activatedImei);

                                LOGGER.info("vivoImeiStatusModel" + dateTime.atStartOfDay());

                        }

                        
                }
        }
}