//package com.ningdatech.pmapi.meeting.task;
//
//import com.alibaba.fastjson.JSONObject;
//import com.baomidou.mybatisplus.core.toolkit.StringUtils;
//import com.baomidou.mybatisplus.core.toolkit.Wrappers;
//import com.ningdatech.pmapi.meeting.builder.YxtSmsContextBuilder;
//import com.ningdatech.pmapi.meeting.entity.domain.ExpertInviteRule;
//import com.ningdatech.pmapi.meeting.entity.domain.Meeting;
//import com.ningdatech.pmapi.meeting.entity.enumeration.ExpertAttendStatus;
//import com.ningdatech.pmapi.meeting.entity.enumeration.MeetingStatus;
//import com.ningdatech.pmapi.meeting.helper.YxtCallOrSmsHelper;
//import com.ningdatech.pmapi.meeting.service.IExpertInviteRuleService;
//import com.ningdatech.pmapi.meeting.service.IMeetingExpertService;
//import com.ningdatech.pmapi.meeting.service.IMeetingService;
//import lombok.AllArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.PostConstruct;
//import java.time.Duration;
//import java.time.Instant;
//import java.time.temporal.ChronoUnit;
//import java.util.HashSet;
//import java.util.List;
//import java.util.Objects;
//import java.util.Set;
//
///**
// * @author liuxinxin
// * @date 2023/2/2 上午9:57
// */
//
//@Slf4j
//@Component
//@AllArgsConstructor
//public class MeetingInviteCompleteNoticeTask {
//
//    private final RandomInviteProperties randomInviteProperties;
//    private final IMeetingService meetingService;
//    private final IMeetingExpertService meetingExpertService;
//    private final ThreadPoolTaskScheduler scheduler;
//    private final RedisUtils redisUtils;
//    private final static String MEETING_INVITE_COMPLETE_NOTICED_MEETING_ID_CACHE_KEY = "MEETING_INVITE_COMPLETE_NOTICED_MEETING_ID_CACHE_KEY";
//    private final YxtCallOrSmsHelper yxtCallOrSmsHelper;
//    private final IExpertInviteRuleService iExpertInviteRuleService;
//
//    @PostConstruct
//    public void initTask() {
//        Instant startTime = Instant.now().plus(randomInviteProperties.getMeetingInviteCompleteNoticeRate(), ChronoUnit.MINUTES);
//        // 处理电话结果回填
//        scheduler.scheduleAtFixedRate(this::meetingInviteCompleteNotice, startTime, Duration.ofMinutes(randomInviteProperties.getResultRewriteFixedRate()));
//    }
//
//    public void meetingInviteCompleteNotice() {
//        log.info("开始执行抽取完成通知：{}", Thread.currentThread().getName());
//        List<Meeting> meetingIdList = meetingService.list(Wrappers.lambdaQuery(Meeting.class)
//                .eq(Meeting::getStatus, MeetingStatus.Manager.UNCOMPLETED.getCode())
//                .eq(Meeting::getSendMeetingNotice, false)
//                .eq(Meeting::getStopRandomInvite, true));
//
//        Set<Long> completeNoticedMeetingIdList = getCompleteNoticedMeetingIdList();
//        for (Meeting meeting : meetingIdList) {
//            Long meetingId = meeting.getId();
//            if (completeNoticedMeetingIdList.add(meetingId)) {
//                List<ExpertInviteRule> expertInviteRules = iExpertInviteRuleService.listByMeetingId(meetingId);
//                if (expertInviteRules.size() == 0) {
//                    return;
//                }
//                int noticeCount = meetingExpertService.countExpertByStatusAndMeetingId(ExpertAttendStatus.NOTICING, meetingId, null);
//                if (noticeCount == 0) {
//                    redisUtils.set(MEETING_INVITE_COMPLETE_NOTICED_MEETING_ID_CACHE_KEY, JSONObject.toJSONString(completeNoticedMeetingIdList), 24 * 60 * 60);
//                    if (StringUtils.isNotBlank(meeting.getContact()) && StringUtils.isNotBlank(meeting.getConnecter())) {
//                        SendSmsCmd.SendSmsContext context = YxtSmsContextBuilder.meetingInviteCompleteNotice(meeting);
//                        yxtCallOrSmsHelper.sendSms(context);
//                    }
//                }
//            }
//        }
//    }
//
//    private Set<Long> getCompleteNoticedMeetingIdList() {
//        Object result = redisUtils.get(MEETING_INVITE_COMPLETE_NOTICED_MEETING_ID_CACHE_KEY);
//        HashSet<Long> completeNoticedMeetingIdSet = new HashSet<>();
//        if (Objects.nonNull(result)) {
//            String resultStr = result.toString();
//            List<Long> completeNoticedMeetingIdList = JSONObject.parseArray(resultStr, Long.class);
//            completeNoticedMeetingIdSet = new HashSet<>(completeNoticedMeetingIdList);
//        }
//        return completeNoticedMeetingIdSet;
//    }
//
//}