Đồng bộ KI
unknown
java
2 months ago
7.8 kB
5
Indexable
package vn.com.viettel.services.scheduler; import lombok.RequiredArgsConstructor; import lombok.experimental.FieldDefaults; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import vn.com.viettel.core.utils.StringUtil; import vn.com.viettel.core.utils.StringUtils; import vn.com.viettel.dto.queryresponse.TicketMarkSumQueryResponse; import vn.com.viettel.entities.AgentEntity; import vn.com.viettel.entities.ki.KiAgentEntity; import vn.com.viettel.entities.ki.KiConfigEntity; import vn.com.viettel.repositories.jpa.AgentRepositoryJPA; import vn.com.viettel.repositories.jpa.EvaluationRateRepository; import vn.com.viettel.repositories.jpa.EvaluationRepositoryJPA; import vn.com.viettel.repositories.jpa.TicketMarkRepositoryJPA; import vn.com.viettel.repositories.jpa.ki.KiAgentRepository; import vn.com.viettel.repositories.jpa.ki.KiConfigDetailRepository; import vn.com.viettel.repositories.jpa.ki.KiConfigRepository; import vn.com.viettel.utils.Constants; import vn.com.viettel.utils.DataUtils; import vn.com.viettel.utils.DateUtils; import java.time.LocalDate; import java.time.ZoneId; import java.util.List; import java.util.Map; import java.util.UUID; import java.util.stream.Collectors; @Service @RequiredArgsConstructor @FieldDefaults(level = lombok.AccessLevel.PRIVATE) @Slf4j public class KiConfigServiceScheduler { final KiConfigRepository kiConfigRepository; final KiAgentRepository kiAgentRepository; final AgentRepositoryJPA agentRepositoryJPA; final TicketMarkRepositoryJPA ticketMarkRepositoryJPA; // 0h ngày mồng 1 hàng tháng @Scheduled(cron = "0 0 0 1 * ?") public void updateKiAgent() { // get timestamp of the first day of the previous month long timeStampPrevMonth = DateUtils.getTimeStampFirstDayOfPreviousMonth(1); // get timestamp of the last day of the previous month long timeStampLastDayOfPreviousMonth = DateUtils.getTimeStampLastDayOfPreviousMonth(1); // get timestamp of the first day of the current month long timeStampCurrentMonth = DateUtils.getTimeStampFirstDayOfPreviousMonth(0); List<String> agentListExclude = kiAgentRepository.findAllByMonthCycle(timeStampPrevMonth) .stream() .map(ele -> DataUtils.cleanString(ele.getAgentId())) .distinct() .collect(Collectors.toList()); List<AgentEntity> agentListUpdate = agentRepositoryJPA.findAllByAgentIdIsNotIn(agentListExclude); agentListUpdate = agentListUpdate.stream().peek(ele ->{ ele.setAgentId(DataUtils.cleanString(ele.getAgentId())); }).collect(Collectors.toList()); //check again if agentListUpdate still have agentId in agentListExclude if (!CollectionUtils.isEmpty(agentListUpdate)){ agentListUpdate = agentListUpdate.stream() .filter(ele -> !agentListExclude.contains(ele.getAgentId())) .collect(Collectors.toList()); } if (CollectionUtils.isEmpty(agentListUpdate)){ log.info("AgentListUpdate is empty"); return; } List<TicketMarkSumQueryResponse> ticketMarkSumQueryResponseList = ticketMarkRepositoryJPA .findTotalScoreKiAgentBetween(agentListUpdate.stream().map(AgentEntity::getAgentId).collect(Collectors.toList()), timeStampPrevMonth, timeStampLastDayOfPreviousMonth); Map<String, TicketMarkSumQueryResponse> ticketMarkSumQueryResponseMap = ticketMarkSumQueryResponseList.stream() .collect(Collectors.toMap(TicketMarkSumQueryResponse::getAgentId, ticketMarkSumQueryResponse -> ticketMarkSumQueryResponse)); Map<String, Float> failureRate = ticketMarkSumQueryResponseList.stream() .collect(Collectors.toMap(TicketMarkSumQueryResponse::getAgentId, ele -> { if (ele.getTotalCall() == 0) { return 0f; } return (float) ele.getTotalNotPassCall() / ele.getTotalCall(); })); List<String> realmList = agentListUpdate.stream() .map(AgentEntity::getRealmName) .distinct() .collect(Collectors.toList()); // get all ki config previous month with status active and realName List<KiConfigEntity> kiAgentEntityList = kiConfigRepository.findAllByRealmNameInAndStatusAndFromMonthCycleBefore(realmList, 1, timeStampCurrentMonth); if (CollectionUtils.isEmpty(kiAgentEntityList)){ log.info("KiConfigEntity is empty"); return; } Map<String, KiConfigEntity> kiConfigEntityMap = kiAgentEntityList.stream() .collect(Collectors.toMap(KiConfigEntity::getRealmName, kiConfigEntity -> kiConfigEntity)); List<KiAgentEntity> kiAgentEntityListUpdate = agentListUpdate.stream() .map(agentEntity -> { String agentId = agentEntity.getAgentId(); String realmName = agentEntity.getRealmName(); // ti le diem tru KiConfigEntity kiConfigEntity = kiConfigEntityMap.get(realmName); if (kiConfigEntity == null) { return new KiAgentEntity(); } float ratioDeductionPoint = kiConfigEntity.getDeductionPoint() / kiConfigEntity.getFailureRate(); Float deductionPoint = ratioDeductionPoint * (failureRate.get(agentId) == null ? 0 : failureRate.get(agentId)); Float finalScore = ticketMarkSumQueryResponseMap.get(agentId) == null ? 0 : ticketMarkSumQueryResponseMap.get(agentId).getTotalScore() - deductionPoint; String randomId = UUID.randomUUID().toString(); return KiAgentEntity.builder() .kiAgentId(randomId) .monthCycle(timeStampPrevMonth) .kiConfigId(kiConfigEntity.getKiConfigId()) .kiCode(null) .totalScore(ticketMarkSumQueryResponseMap.get(agentId) == null ? 0 : ticketMarkSumQueryResponseMap.get(agentId).getTotalScore()) .agentId(agentId) .failureRate(failureRate.get(agentId) == null ? 0 : failureRate.get(agentId)) .deductionPoint(String.valueOf(deductionPoint)) .finalScore(finalScore) .realmName(realmName) .kzAccountId(null) .status((int) Constants.STATUS.ACTIVE) .createBy("system") .updateBy("system") .createTime(System.currentTimeMillis()) .updateTime(System.currentTimeMillis()) .build(); }).collect(Collectors.toList()); kiAgentEntityListUpdate = kiAgentEntityListUpdate.stream().filter(ele -> StringUtils.hasText(ele.getKiAgentId())).collect(Collectors.toList()); kiAgentRepository.saveAll(kiAgentEntityListUpdate); log.info("Update ki agent success with size: {}", kiAgentEntityListUpdate.size()); } }
Editor is loading...
Leave a Comment