package com.vetti.hotake.service.impl;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.format.DatePrinter;
import cn.hutool.core.util.StrUtil;
import com.vetti.common.config.TwilioConfig;
import com.vetti.common.core.domain.entity.SysUser;
import com.vetti.common.core.service.BaseServiceImpl;
import com.vetti.common.enums.FillTypeEnum;
import com.vetti.common.enums.MeetingCalendarStatus;
import com.vetti.common.enums.StageEnum;
import com.vetti.common.exception.ServiceException;
import com.vetti.common.service.verification.VerificationService;
import com.vetti.common.service.verification.impl.VerificationServiceImpl;
import com.vetti.common.utils.SecurityUtils;
import com.vetti.hotake.domain.HotakeMeetingCalendarDetail;
import com.vetti.hotake.domain.HotakeRolesApplyInfo;
import com.vetti.hotake.domain.HotakeRolesInfo;
import com.vetti.hotake.domain.dto.HotakeMeetingCalendarDataDto;
import com.vetti.hotake.domain.vo.HotakeInterviewInvitationVo;
import com.vetti.hotake.domain.vo.HotakeMeetingCalendarInfoVo;
import com.vetti.hotake.domain.vo.HotakeMeetingCalendarVo;
import com.vetti.hotake.mapper.HotakeMeetingCalendarDetailMapper;
import com.vetti.hotake.mapper.HotakeRolesApplyInfoMapper;
import com.vetti.hotake.mapper.HotakeRolesInfoMapper;
import com.vetti.hotake.service.IHotakeMeetingCalendarDetailService;
import com.vetti.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.vetti.hotake.mapper.HotakeMeetingCalendarInfoMapper;
import com.vetti.hotake.domain.HotakeMeetingCalendarInfo;
import com.vetti.hotake.service.IHotakeMeetingCalendarInfoService;
import org.springframework.util.Base64Utils;

import javax.annotation.Resource;

/**
 * 会议日历记录主Service业务层处理
 *
 * @author wangxiangshun
 * @date 2025-11-09
 */
@SuppressWarnings("all")
@Service
public class HotakeMeetingCalendarInfoServiceImpl extends BaseServiceImpl implements IHotakeMeetingCalendarInfoService {

    private final String meetingName = "Interview Invitation";


    @Autowired
    private HotakeMeetingCalendarInfoMapper hotakeMeetingCalendarInfoMapper;

    @Autowired
    private HotakeMeetingCalendarDetailMapper hotakeMeetingCalendarDetailMapper;

    @Autowired
    private IHotakeMeetingCalendarDetailService calendarDetailService;

    @Autowired
    private HotakeRolesInfoMapper hotakeRolesInfoMapper;

    @Autowired
    private HotakeRolesApplyInfoMapper hotakeRolesApplyInfoMapper;

    @Autowired
    private VerificationService verificationService;


    @Autowired
    private ISysUserService userService;

    @Resource
    private TwilioConfig twilioConfig;

    /**
     * 查询会议日历记录主
     *
     * @param id 会议日历记录主主键
     * @return 会议日历记录主
     */
    @Transactional(readOnly = true)
    @Override
    public HotakeMeetingCalendarInfo selectHotakeMeetingCalendarInfoById(Long id) {
        HotakeMeetingCalendarInfo calendarInfo = hotakeMeetingCalendarInfoMapper.selectHotakeMeetingCalendarInfoById(id);

        //查询岗位数据
        HotakeRolesInfo query = new HotakeRolesInfo();
        List<HotakeRolesInfo> rolesInfoList = hotakeRolesInfoMapper.selectHotakeRolesInfoList(query);

        //查询岗位数据
        HotakeRolesApplyInfo queryApply = new HotakeRolesApplyInfo();
        List<HotakeRolesApplyInfo> applyInfoList = hotakeRolesApplyInfoMapper.selectHotakeRolesApplyInfoList(queryApply);

        //获取对应的参会人员数据
        HotakeMeetingCalendarDetail queryDetail = new HotakeMeetingCalendarDetail();
        List<HotakeMeetingCalendarDetail> detailList = calendarDetailService.selectHotakeMeetingCalendarDetailList(queryDetail);
        if (CollectionUtil.isNotEmpty(detailList)) {

            Map<Long, List<HotakeMeetingCalendarDetail>> mapDetailList = detailList.stream().collect(Collectors.groupingBy(HotakeMeetingCalendarDetail::getMeetingId));
            SysUser queryUser = new SysUser();
            List<SysUser> userList = userService.selectUserList(queryUser);
            List<HotakeMeetingCalendarDetail> calendarDetails = mapDetailList.get(calendarInfo.getId());
            for (HotakeMeetingCalendarDetail detail : calendarDetails) {
                List<SysUser> users = userList.stream().filter(e -> e.getUserId() == detail.getCandidateId()).collect(Collectors.toList());
                if (CollectionUtil.isNotEmpty(users)) {
                    detail.setCandidateUser(users.get(0));
                }
            }
            calendarInfo.setCalendarDetails(calendarDetails);
            //返回岗位数据信息
            //返回岗位数据信息
            List<HotakeRolesApplyInfo> applyInfos = applyInfoList.stream().filter(e->e.getId().longValue() == calendarInfo.getRoleApplyId().longValue()).toList();
            if(CollectionUtil.isNotEmpty(applyInfos)) {
                HotakeRolesApplyInfo applyInfo = applyInfos.get(0);
                List<HotakeRolesInfo> rolesInfos = rolesInfoList.stream().filter(e->e.getId().longValue() == applyInfo.getRoleId().longValue()).toList();
                if(CollectionUtil.isNotEmpty(rolesInfos)) {
                    HotakeRolesInfo rolesInfo = rolesInfos.get(0);
                    calendarInfo.setRolesInfo(rolesInfo);
                }
            }
        }
        return calendarInfo;
    }

    /**
     * 查询会议日历记录主列表
     *
     * @param hotakeMeetingCalendarInfo 会议日历记录主
     * @return 会议日历记录主
     */
    @Transactional(readOnly = true)
    @Override
    public List<HotakeMeetingCalendarInfo> selectHotakeMeetingCalendarInfoList(HotakeMeetingCalendarInfo hotakeMeetingCalendarInfo) {
        List<HotakeMeetingCalendarInfo> calendarInfos = hotakeMeetingCalendarInfoMapper.selectHotakeMeetingCalendarInfoList(hotakeMeetingCalendarInfo);
        if (CollectionUtil.isNotEmpty(calendarInfos)) {
            //查询岗位数据
            HotakeRolesInfo query = new HotakeRolesInfo();
            List<HotakeRolesInfo> rolesInfoList = hotakeRolesInfoMapper.selectHotakeRolesInfoList(query);

            //查询岗位数据
            HotakeRolesApplyInfo queryApply = new HotakeRolesApplyInfo();
            List<HotakeRolesApplyInfo> applyInfoList = hotakeRolesApplyInfoMapper.selectHotakeRolesApplyInfoList(queryApply);
            //获取对应的参会人员数据
            HotakeMeetingCalendarDetail queryDetail = new HotakeMeetingCalendarDetail();
            List<HotakeMeetingCalendarDetail> detailList = calendarDetailService.selectHotakeMeetingCalendarDetailList(queryDetail);
            if (CollectionUtil.isNotEmpty(detailList)) {
                Map<Long, List<HotakeMeetingCalendarDetail>> mapDetailList = detailList.stream().collect(Collectors.groupingBy(HotakeMeetingCalendarDetail::getMeetingId));
                SysUser queryUser = new SysUser();
                List<SysUser> userList = userService.selectUserList(queryUser);
                for (HotakeMeetingCalendarInfo calendarInfo : calendarInfos) {
                    List<HotakeMeetingCalendarDetail> calendarDetails = mapDetailList.get(calendarInfo.getId());
                    for (HotakeMeetingCalendarDetail detail : calendarDetails) {
                        List<SysUser> users = userList.stream().filter(e -> e.getUserId() == detail.getCandidateId()).collect(Collectors.toList());
                        if (CollectionUtil.isNotEmpty(users)) {
                            detail.setCandidateUser(users.get(0));
                        }
                    }
                    calendarInfo.setCalendarDetails(calendarDetails);
                    //返回岗位数据信息
                    List<HotakeRolesApplyInfo> applyInfos = applyInfoList.stream().filter(e->e.getId().longValue() == calendarInfo.getRoleApplyId().longValue()).toList();
                    if(CollectionUtil.isNotEmpty(applyInfos)) {
                        HotakeRolesApplyInfo applyInfo = applyInfos.get(0);
                        List<HotakeRolesInfo> rolesInfos = rolesInfoList.stream().filter(e->e.getId().longValue() == applyInfo.getRoleId().longValue()).toList();
                        if(CollectionUtil.isNotEmpty(rolesInfos)) {
                            HotakeRolesInfo rolesInfo = rolesInfos.get(0);
                            calendarInfo.setRolesInfo(rolesInfo);
                        }
                    }
                }
            }
        }
        return calendarInfos;
    }

    /**
     * 查询候选者相关的会议记录数据
     * @param hotakeMeetingCalendarInfo 会议日历记录主
     * @return
     */
    @Override
    public List<HotakeMeetingCalendarInfo> selectHotakeMeetingCalendarInfoCandidateList(HotakeMeetingCalendarInfo hotakeMeetingCalendarInfo) {
        //根据候选者查询会议明细数据
        HotakeMeetingCalendarDetail queryCalendarDetail = new HotakeMeetingCalendarDetail();
        queryCalendarDetail.setCandidateId(SecurityUtils.getUserId());
        List<HotakeMeetingCalendarDetail> calendarDetailList = hotakeMeetingCalendarDetailMapper.selectHotakeMeetingCalendarDetailList(queryCalendarDetail);

        if(CollectionUtil.isNotEmpty(calendarDetailList)) {
            //查询明细中的会议主表ID
            List<Long> meetingIds = calendarDetailList.stream().map(HotakeMeetingCalendarDetail::getMeetingId).collect(Collectors.toList());
            hotakeMeetingCalendarInfo.setMeetingIds(meetingIds);
            List<HotakeMeetingCalendarInfo> calendarInfos = hotakeMeetingCalendarInfoMapper.selectHotakeMeetingCalendarInfoList(hotakeMeetingCalendarInfo);
            if (CollectionUtil.isNotEmpty(calendarInfos)) {
                //查询岗位数据
                HotakeRolesInfo query = new HotakeRolesInfo();
                List<HotakeRolesInfo> rolesInfoList = hotakeRolesInfoMapper.selectHotakeRolesInfoList(query);

                //查询岗位数据
                HotakeRolesApplyInfo queryApply = new HotakeRolesApplyInfo();
                List<HotakeRolesApplyInfo> applyInfoList = hotakeRolesApplyInfoMapper.selectHotakeRolesApplyInfoList(queryApply);
                //获取对应的参会人员数据
                HotakeMeetingCalendarDetail queryDetail = new HotakeMeetingCalendarDetail();
                List<HotakeMeetingCalendarDetail> detailList = calendarDetailService.selectHotakeMeetingCalendarDetailList(queryDetail);
                if (CollectionUtil.isNotEmpty(detailList)) {
                    Map<Long, List<HotakeMeetingCalendarDetail>> mapDetailList = detailList.stream().collect(Collectors.groupingBy(HotakeMeetingCalendarDetail::getMeetingId));
                    SysUser queryUser = new SysUser();
                    List<SysUser> userList = userService.selectUserList(queryUser);
                    for (HotakeMeetingCalendarInfo calendarInfo : calendarInfos) {
                        List<HotakeMeetingCalendarDetail> calendarDetails = mapDetailList.get(calendarInfo.getId());
                        for (HotakeMeetingCalendarDetail detail : calendarDetails) {
                            List<SysUser> users = userList.stream().filter(e -> e.getUserId() == detail.getCandidateId()).collect(Collectors.toList());
                            if (CollectionUtil.isNotEmpty(users)) {
                                detail.setCandidateUser(users.get(0));
                            }
                        }
                        calendarInfo.setCalendarDetails(calendarDetails);
                        //返回岗位数据信息
                        List<HotakeRolesApplyInfo> applyInfos = applyInfoList.stream().filter(e->e.getId().longValue() == calendarInfo.getRoleApplyId().longValue()).toList();
                        if(CollectionUtil.isNotEmpty(applyInfos)) {
                            HotakeRolesApplyInfo applyInfo = applyInfos.get(0);
                            List<HotakeRolesInfo> rolesInfos = rolesInfoList.stream().filter(e->e.getId().longValue() == applyInfo.getRoleId().longValue()).toList();
                            if(CollectionUtil.isNotEmpty(rolesInfos)) {
                                HotakeRolesInfo rolesInfo = rolesInfos.get(0);
                                calendarInfo.setRolesInfo(rolesInfo);
                            }
                        }
                    }
                }
            }
            return calendarInfos;
        }else{
            return new ArrayList<>();
        }



    }

    /**
     * 新增会议日历记录主
     *
     * @param hotakeMeetingCalendarInfo 会议日历记录主
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public HotakeMeetingCalendarInfo insertHotakeMeetingCalendarInfo(HotakeMeetingCalendarInfo hotakeMeetingCalendarInfo) {
        fill(FillTypeEnum.INSERT.getCode(), hotakeMeetingCalendarInfo);
        hotakeMeetingCalendarInfoMapper.insertHotakeMeetingCalendarInfo(hotakeMeetingCalendarInfo);
        //保存参会人员
        //1.1 先删除对应的参会人员
        calendarDetailService.deleteHotakeMeetingCalendarDetailByInfoId(hotakeMeetingCalendarInfo.getId());
        //1.2 保存参会人员数据
        for (HotakeMeetingCalendarDetail detail : hotakeMeetingCalendarInfo.getCalendarDetails()) {
            detail.setMeetingId(hotakeMeetingCalendarInfo.getId());
            fill(FillTypeEnum.INSERT.getCode(), detail);
        }
        calendarDetailService.batchInsertHotakeMeetingCalendarDetail(hotakeMeetingCalendarInfo.getCalendarDetails());

        return hotakeMeetingCalendarInfo;
    }

    /**
     * 修改会议日历记录主
     *
     * @param hotakeMeetingCalendarInfo 会议日历记录主
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public HotakeMeetingCalendarInfo updateHotakeMeetingCalendarInfo(HotakeMeetingCalendarInfo hotakeMeetingCalendarInfo) {
        fill(FillTypeEnum.UPDATE.getCode(), hotakeMeetingCalendarInfo);
        hotakeMeetingCalendarInfoMapper.updateHotakeMeetingCalendarInfo(hotakeMeetingCalendarInfo);
        //保存参会人员
        //1.1 先删除对应的参会人员
        calendarDetailService.deleteHotakeMeetingCalendarDetailByInfoId(hotakeMeetingCalendarInfo.getId());
        //1.2 保存参会人员数据
        for (HotakeMeetingCalendarDetail detail : hotakeMeetingCalendarInfo.getCalendarDetails()) {
            detail.setMeetingId(hotakeMeetingCalendarInfo.getId());
            fill(FillTypeEnum.INSERT.getCode(), detail);
        }
        calendarDetailService.batchInsertHotakeMeetingCalendarDetail(hotakeMeetingCalendarInfo.getCalendarDetails());
        return hotakeMeetingCalendarInfo;
    }

    /**
     * 批量删除会议日历记录主
     *
     * @param ids 需要删除的会议日历记录主主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteHotakeMeetingCalendarInfoByIds(Long[] ids) {
        return hotakeMeetingCalendarInfoMapper.deleteHotakeMeetingCalendarInfoByIds(ids);
    }

    /**
     * 删除会议日历记录主信息
     *
     * @param id 会议日历记录主主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteHotakeMeetingCalendarInfoById(Long id) {
        return hotakeMeetingCalendarInfoMapper.deleteHotakeMeetingCalendarInfoById(id);
    }

    /**
     * 批量新增会议日历记录主
     *
     * @param hotakeMeetingCalendarInfoList 会议日历记录主列表
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int batchInsertHotakeMeetingCalendarInfo(List<HotakeMeetingCalendarInfo> hotakeMeetingCalendarInfoList) {
        return hotakeMeetingCalendarInfoMapper.batchInsertHotakeMeetingCalendarInfo(hotakeMeetingCalendarInfoList);
    }

    /**
     * 保存会议日历记录
     * @param calendarVo 面试邀请信息
     */
    @Override
    public void saveHotakeMeetingCalendarInfo(HotakeMeetingCalendarVo calendarVo) {
        //保存面试邀请数据
        if(CollectionUtil.isNotEmpty(calendarVo.getCalendarInfoVoList())){
            for(HotakeMeetingCalendarInfoVo calendarInfoVo : calendarVo.getCalendarInfoVoList()){
                HotakeMeetingCalendarInfo calendarInfo = new HotakeMeetingCalendarInfo();
                calendarInfo.setRoleApplyId(calendarVo.getRoleApplyId());
                calendarInfo.setRecruiterId(calendarVo.getRecruiterId());
                calendarInfo.setMeetingDate(calendarInfoVo.getMeetingDate());
                calendarInfo.setTimes(calendarInfoVo.getTimes());
                calendarInfo.setMessageVia(calendarInfoVo.getMessageVia());
                calendarInfo.setStatus(MeetingCalendarStatus.NORMAL.getCode());
                calendarInfo.setMeetingName(meetingName);
                //保存主表数据
                fill(FillTypeEnum.INSERT.getCode(), calendarInfo);
                hotakeMeetingCalendarInfoMapper.insertHotakeMeetingCalendarInfo(calendarInfo);
                //保存明细数据
                HotakeMeetingCalendarDetail hotakeMeetingCalendarDetail = new HotakeMeetingCalendarDetail();
                hotakeMeetingCalendarDetail.setMeetingId(calendarInfo.getId());
                hotakeMeetingCalendarDetail.setCandidateId(calendarInfoVo.getCandidateId());
                fill(FillTypeEnum.INSERT.getCode(), hotakeMeetingCalendarDetail);
                hotakeMeetingCalendarDetailMapper.insertHotakeMeetingCalendarDetail(hotakeMeetingCalendarDetail);

                //更新申请岗位状态
                HotakeRolesApplyInfo applyInfo = new HotakeRolesApplyInfo();
                applyInfo.setId(calendarVo.getRoleApplyId());
                applyInfo.setStage(StageEnum.INTERVIEW.getCode());
                hotakeRolesApplyInfoMapper.updateHotakeRolesApplyInfo(applyInfo);

                //发送信息-邮件信息
                try{
                    if(StrUtil.isNotEmpty(calendarInfoVo.getMessageVia())){
                        SysUser user = userService.selectUserById(calendarInfoVo.getCandidateId());

                        HotakeRolesApplyInfo applyInfo1 = hotakeRolesApplyInfoMapper.selectHotakeRolesApplyInfoById(calendarVo.getRoleApplyId());

                        HotakeRolesInfo rolesInfo = hotakeRolesInfoMapper.selectHotakeRolesInfoById(applyInfo1.getRoleId());
                        if (calendarInfoVo.getMessageVia().contains("Email")){
                            HotakeInterviewInvitationVo vo = new HotakeInterviewInvitationVo();
                            vo.setCandidate_name(user.getNickName());
                            vo.setCompany_name(rolesInfo.getCompanyName());
                            vo.setPosition_name(rolesInfo.getRoleName());
                            vo.setInterview_date(calendarInfoVo.getMeetingDate() + " "+calendarInfoVo.getTimes());
                            vo.setCurrent_year(String.valueOf(DateUtil.year(DateUtil.date())));

                            String param = "roleId="+rolesInfo.getId()+"&roleApplyId="+calendarVo.getRoleApplyId()+"&userName="+SecurityUtils.getUsername()+"&userType=candidate&token=";
                            String paramBase64 = Base64Utils.encodeToString(param.getBytes());
                            String webUrl = "https://vetti.hotake.cn/#/interview/link?uuid="+paramBase64;
                            vo.setInterview_now(webUrl);
                            verificationService.sendInterviewTemplate(user.getEmail(),twilioConfig.getTemplateIds().getInterviewTemplateCode(),vo);
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 查询当前候选者会议日历数据
     * @return
     */
    @Override
    public List<HotakeMeetingCalendarDataDto> selectHotakeMeetingCalendarDataDtoList() {
        List<HotakeMeetingCalendarDataDto> dataDtos = new ArrayList<>();
        //根据候选者查询会议明细数据
        HotakeMeetingCalendarDetail queryCalendarDetail = new HotakeMeetingCalendarDetail();
        queryCalendarDetail.setCandidateId(SecurityUtils.getUserId());
        List<HotakeMeetingCalendarDetail> calendarDetailList = hotakeMeetingCalendarDetailMapper.selectHotakeMeetingCalendarDetailList(queryCalendarDetail);
        if(CollectionUtil.isNotEmpty(calendarDetailList)) {
            HotakeMeetingCalendarInfo queryCalendarInfo = new HotakeMeetingCalendarInfo();
            //查询明细中的会议主表ID
            List<Long> meetingIds = calendarDetailList.stream().map(HotakeMeetingCalendarDetail::getMeetingId).collect(Collectors.toList());
            queryCalendarInfo.setMeetingIds(meetingIds);
            List<HotakeMeetingCalendarInfo> calendarInfos = hotakeMeetingCalendarInfoMapper.selectHotakeMeetingCalendarInfoList(queryCalendarInfo);
            if (CollectionUtil.isNotEmpty(calendarInfos)) {
                //获取对应的参会人员数据
                Map<String,List<HotakeMeetingCalendarInfo>> mapCalendarInfo = calendarInfos.stream().collect(Collectors.groupingBy(HotakeMeetingCalendarInfo::getMeetingDate));
                if(mapCalendarInfo != null) {
                    for(String key : mapCalendarInfo.keySet()) {
                        List<HotakeMeetingCalendarInfo> calendarInfoList = mapCalendarInfo.get(key);
                        HotakeMeetingCalendarDataDto dataDto = new HotakeMeetingCalendarDataDto();
                        dataDto.setMeetingDate(key);
                        dataDto.setMeetingFlag("1");
                        dataDto.setNums(calendarInfoList.size());
                        dataDtos.add(dataDto);
                    }
                }
            }
            return dataDtos;
        }else{
            return new ArrayList<>();
        }
    }

    /**
     * 获取当前候选人即将开始的会议信息
     * @return
     */
    @Override
    public HotakeMeetingCalendarInfo selectCandidateStartCalendarInfo() {
        //获取候选人所有的日程信息
        HotakeMeetingCalendarDetail queryDetai = new HotakeMeetingCalendarDetail();
        queryDetai.setCandidateId(SecurityUtils.getUserId());
        List<HotakeMeetingCalendarDetail> calendarDetailList = calendarDetailService.selectHotakeMeetingCalendarDetailList(queryDetai);
        if(CollectionUtil.isEmpty(calendarDetailList)) {
            throw new ServiceException("Currently, you have no upcoming scheduled meetings");
        }
        List<Long> meetingIds = calendarDetailList.stream().map(HotakeMeetingCalendarDetail::getMeetingId).collect(Collectors.toList());
        //查询对应的会议列表数据
        HotakeMeetingCalendarInfo queryCalendarInfo = new HotakeMeetingCalendarInfo();
        queryCalendarInfo.setMeetingIds(meetingIds);
        List<HotakeMeetingCalendarInfo> calendarInfoList = selectHotakeMeetingCalendarInfoList(queryCalendarInfo);
        //获取符合当前时间的会议日程信息
        String day = DateUtil.format(DateUtil.date(), "MM/dd/yyyy");
        Boolean flag = true;
        HotakeMeetingCalendarInfo calendarInfoResultData = null;
        for(HotakeMeetingCalendarInfo calendarInfo : calendarInfoList) {
            if (day.equals(calendarInfo.getMeetingDate())) {
                String times = DateUtil.format(DateUtil.date(), "hh");
                String timesType = DateUtil.isAM(DateUtil.date()) ? "AM" : "PM";
                if(calendarInfo.getTimes().startsWith(times+" "+timesType)){
                    flag = false;
                    calendarInfoResultData =  calendarInfo;
                    break;
                }
            }
        }
        if(flag){
            throw new ServiceException("Currently, you have no upcoming scheduled meetings");
        }
        return calendarInfoResultData;
    }
}
