scheduler

mail@pastecode.io avatar
unknown
java
11 days ago
9.0 kB
2
Indexable
Never
package id.co.prismalink.product.scheduler;

import id.co.prismalink.product.AppSingleton;
import id.co.prismalink.product.data.plink.model.*;
import id.co.prismalink.product.data.plink.repo.*;
import id.co.prismalink.product.model.entity.mdo.User;
import id.co.prismalink.product.service.SettlementDataScrapperService;
import id.co.prismalink.product.service.client.DatarepoFeignClient;
import id.co.prismalink.product.service.client.DatarepoMdoFeignClient;
import id.co.prismalink.product.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.hateoas.CollectionModel;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.net.URI;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Component
public class PlMerchantDataSyncScheduler {
    @Autowired
    SettlementDataScrapperService settlementDataScrapperService;
    @Autowired
    MerchantRepository merchantRepository;
    @Autowired
    UsersRepository usersRepository;
    @Autowired
    UserRolesRepository userRolesRepository;
    @Autowired
    UserMerchantRepository userMerchantRepository;
    @Autowired
    TbUserMerchantRoleRepository tbUserMerchantRoleRepository;
    @Autowired
    MerchantKeyRepository merchantKeyRepository;
    @Autowired
    MerchantPaymentmethodRepository merchantPaymentmethodRepository;
    @Autowired
    MerchantVaRepository merchantVaRepository;

    @Value( "${backoffice.config.master.ip}" )
    String backofficeMasterIp;

    String formatDate = "yyyy-MM-ddHH:mm:ss.SSSZ";

//    @Value("#{${application.datarepo}}")
    @Value("#{PropertySplitter.map('${application.datarepo}')}")
    private Map<String, String> applicationDatarepo;

    @Value( "${application.datasync.merchant.maxrow}" )
    private Integer datasyncPltransactionsMaxrow;

// jangan di aktifkan, nanti lewat crontab aja
    @Scheduled(fixedDelayString = "${backoffice.config.timer.scrape-merchantdata-data}")
    public Map run() {
        AppSingleton appSingleton = AppSingleton.getInstance();
        log.info("scheduler MerchantDataSyncScheduler. toggle : {}", appSingleton.getPlTransactionsDataScrapperSchedulerToggle());
        if("ON".equalsIgnoreCase(appSingleton.getPlTransactionsDataScrapperSchedulerToggle())){
            this.scrape();

//            for (Map.Entry<String,String> entry : applicationDatarepo.entrySet()) {
//                System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
//                try{
//
//                }catch (Exception e){
//                    e.printStackTrace();
//                }
//            }

            return null;
        }
        return null;
    }
    public void scrape() {
        final String logId = RandomStringUtils.randomAlphanumeric(6);

        String latestUpdatedDt = "2024-09-26 00:00:00";
        log.info("{}, 1 start getting data, latestUpdatedDt {}", logId, latestUpdatedDt);
        if(latestUpdatedDt != null && !"".equalsIgnoreCase(latestUpdatedDt)) {
            // string split by .
            String[] getMsArr = latestUpdatedDt.split("\\.");

            String ms = (getMsArr.length > 1) ? String.format("%-3s", Integer.parseInt(getMsArr[1])).replace(' ', '0') : "000";
            latestUpdatedDt = getMsArr[0] + "." + ms; // yyyy-MM-dd HH:mm:ss.SSS
            // convert into yyyy-MM-ddHH:mm:ss.SSSZ
            latestUpdatedDt = DateUtils.changeDateFormat(latestUpdatedDt, "yyyy-MM-dd HH:mm:ss.SSS", formatDate);

            this.scrape(latestUpdatedDt, null);
        } else {
            latestUpdatedDt = DateUtils.changeDateFormat("2024-09-01 00:00:00.000", "yyyy-MM-dd HH:mm:ss.SSS", formatDate);
            this.scrape(latestUpdatedDt, null);
        }
    }

    public Map scrape(String startDate, String endDate) {
        final String logId = RandomStringUtils.randomAlphanumeric(6);
        log.info("{}, 2 start getting data, latestUpdatedDt {} endDate {}", logId, startDate, endDate);

        Map resp = new HashMap();
        resp.put("size", 0);

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss.SSSZ");
        Date latestUpdatedDt = null;
        Date parseEndDate = null;

        List<Users> users = null;
        List<UserRoles> userRoles = null;
        List<UserMerchant> userMerchants = null;
        List<TbUserMerchantRole> tbUserMerchantRoles = null;
        List<Merchant> merchants = null;
        List<MerchantKey> merchantKeys = null;
        List<MerchantPaymentmethod> merchantPaymentmethods = null;
        List<MerchantVa> merchantVas = null;

        log.info("{}, fetch plMerchantData begin", logId);
        try {
            latestUpdatedDt = dateFormat.parse(startDate);
            log.info("get all data merchant where update_at > latestUpdatedDt limit datasyncPltransactionsMaxrow");

            users = usersRepository.findUsersByUpdateDtAfterNoLimit(latestUpdatedDt);
            if(users == null){
                log.warn("No users found for synchronization");
                return Collections.emptyMap();
            }

            merchants = merchantRepository.findMerchantByUpdateDtAfterLimit(latestUpdatedDt);
            if(merchants == null){
                log.warn("No merchant found for synchronization");
                return Collections.emptyMap();
            }
            List<Integer> merchantIds = merchants.stream().map(Merchant::getId).collect(Collectors.toList());

            List<Integer> userIds = users.stream().map(Users::getId).collect(Collectors.toList());
            userRoles = userRolesRepository.findByUsersIdIn(userIds);
            if(userRoles == null){
                log.warn("No user roles found for synchronization");
                return Collections.emptyMap();
            }

            userMerchants = userMerchantRepository.findByUsersIdInAndMerchantIdIn(userIds, merchantIds);
            if(userMerchants == null){
                log.warn("No merchant found for synchronization");
                return Collections.emptyMap();
            }

            List<Integer> userMerchantIds = userMerchants.stream().map(UserMerchant::getId).collect(Collectors.toList());
            tbUserMerchantRoles = tbUserMerchantRoleRepository.findByUsersMerchantIdIn(userMerchantIds);
            if(tbUserMerchantRoles == null){
                log.warn("user merchant role not found for synchronization");
                return Collections.emptyMap();
            }

            merchantKeys = merchantKeyRepository.findByMerchantIdIn(merchantIds);
            if(merchantKeys == null){
                log.warn("no merchant key found for synchronization");
                return Collections.emptyMap();
            }

            merchantPaymentmethods = merchantPaymentmethodRepository.findByMerchantIdIn(merchantIds);
            if(merchantPaymentmethods == null){
                log.warn("no merchant payment method found for synchronization");
                return Collections.emptyMap();
            }

            merchantVas = merchantVaRepository.findByMerchantIdIn(merchantIds);
            if(merchantVas == null){
                log.warn("no merchant va found for synchronization");
                return Collections.emptyMap();
            }

        }catch (Exception e) {
            log.info("{}, error when fetch merchant data", logId);
            e.printStackTrace();
            log.info("{}, error when fetch merchant data {}", logId, e.getMessage());
        }
        log.info("{}, fetch merchant data end", logId);
        log.info("{}, users data is exist. size {}", logId, users.size());
        log.info("{}, user roles data is exist. size {}", logId, userRoles.size());
        log.info("{}, merchant data is exist. size {}", logId, merchants.size());
        log.info("{}, user merchant data is exist. size {}", logId, userMerchants.size());
        log.info("{}, user merchant roles is exist. size {}", logId, tbUserMerchantRoles.size());
        log.info("{}, merchant key data is exist. size {}", logId, merchantKeys.size());
        log.info("{}, merchant payment method data is exist. size {}", logId, merchantPaymentmethods.size());
        log.info("{}, merchant va data is exist. size {}", logId, merchantVas.size());

        resp.put("startDate", startDate);
        resp.put("endDate", endDate);
        resp.put("status", "success");
        return resp;
    }
} // end class
Leave a Comment