package com.qmth.themis.business.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.themis.business.bean.result.TEAudioResult;
import com.qmth.themis.business.dao.TEAudioMapper;
import com.qmth.themis.business.entity.TEAudio;
import com.qmth.themis.business.service.TEAudioService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 场次音频表 服务实现类
 * </p>
 *
 * @author wangliang
 * @since 2022-08-30
 */
@Service
public class TEAudioServiceImpl extends ServiceImpl<TEAudioMapper, TEAudio> implements TEAudioService {
    private final static Logger log = LoggerFactory.getLogger(TEAudioServiceImpl.class);

    @Resource
    TEAudioMapper teAudioMapper;

//    @Resource
//    TEExamActivityService teExamActivityService;
//
//    @Resource
//    TEExamService teExamService;
//
//    @Resource
//    private MqUtil mqUtil;
//
//    @Resource
//    private MqDtoService mqDtoService;
//
//    @Resource
//    CacheService cacheService;

    /**
     * 根据场次id查询音频信息
     *
     * @param iPage
     * @param activityId
     * @return
     */
    @Override
    public IPage<TEAudio> query(IPage<Map> iPage, Long activityId) {
        return teAudioMapper.query(iPage, activityId);
    }

    /**
     * 根据场次id查询音频信息
     *
     * @param activityId
     * @return
     */
    @Override
    public List<TEAudioResult> query(Long activityId) {
        return teAudioMapper.queryResult(activityId);
    }

//    /**
//     * 发送语音mq消息
//     *
//     * @param teAudio
//     */
//    @Override
//    public void sendAudioMessage(TEAudio teAudio) {
//        TBUser tbUser = (TBUser) ServletUtil.getRequestAccount();
//        ExamActivityCacheBean ac = teExamActivityService.getExamActivityCacheBean(teAudio.getActivityId());
//        ExamCacheBean examCacheBean = teExamService.getExamCacheBean(ac.getExamId());
//        TEExam teExam = teExamService.cacheConvert(examCacheBean);
//        Long start = ac.getStartTime();
//        Long end = ac.getStartTime() + (ac.getOpeningSeconds() * 1000);
//        Long execTime = null, realExecTime = null;
//        if (teAudio.getType() == AudioTypeEnum.BEFORE) {//开考前语音
//            realExecTime = start - (teAudio.getPlayTime() * 1000);
//        } else if (teAudio.getType() == AudioTypeEnum.AFTER) {//考试结束前语音
//            realExecTime = end - (teAudio.getPlayTime() * 1000);
//        }
//        execTime = realExecTime - SystemConstant.EXAM_AUDIO_BEFORE_TIME;
//        //新增quartz任务,发送mq消息start
//        Map<String, Object> prop = new HashMap<>();
//        prop.put(SystemConstant.OPER, SystemConstant.INSERT);
//        prop.put(SystemConstant.EXAM, teExam);
//        prop.put(SystemConstant.EXEC_TIME, execTime);
//        prop.put(SystemConstant.REAL_EXEC_TIME, realExecTime);
//        log.info(SystemConstant.EXEC_TIME + ":{}," + SystemConstant.REAL_EXEC_TIME + ":{}", DateUtil.format(new Date(execTime), Constants.DEFAULT_DATE_PATTERN), DateUtil.format(new Date(realExecTime), Constants.DEFAULT_DATE_PATTERN));
//        if (execTime >= System.currentTimeMillis()) {
//            cacheService.updateExamAudioCache(teAudio.getId());
//            MqDto mqDto = new MqDto(mqUtil.getMqGroupDomain().getTopic(), MqTagEnum.EXAM_AUDIO.name(),
//                    JacksonUtil.parseJson(teAudio), MqTagEnum.EXAM_AUDIO,
//                    String.valueOf(teExam.getId()), prop, tbUser.getName());
//            mqDtoService.assembleSendOneOrderMsg(mqDto);
//        }
//        //新增quartz任务,发送mq消息end
//    }
}
