package com.fs.app.mq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fs.common.annotation.DataSource;
import com.fs.common.core.redis.RedisCacheT;
import com.fs.common.enums.DataSourceType;
import com.fs.qw.service.IQwUserService;
import com.fs.qw.vo.QwUserVO;
import com.fs.sop.domain.QwSop;
import com.fs.sop.domain.QwSopTempContent;
import com.fs.sop.domain.QwSopTempVoice;
import com.fs.sop.service.IQwSopService;
import com.fs.sop.service.IQwSopTempContentService;
import com.fs.sop.service.IQwSopTempDayService;
import com.fs.sop.service.IQwSopTempVoiceService;
import com.fs.sop.vo.VoiceVo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
@AllArgsConstructor
//@RocketMQMessageListener(topic = "${rocketmq.consumer.topic}", consumerGroup = "${rocketmq.consumer.group}")
public class RocketMQConsumerService implements RocketMQListener<String> {
    private final RedisCacheT<VoiceVo> redisCache;
    public final static String VOICE_CACHE_KEY = "voice:cache:";
    private final IQwSopTempVoiceService qwSopTempVoiceService;
    private final IQwSopTempContentService qwSopTempContentService;
    private final IQwUserService qwUserService;
    private final IQwSopService qwSopService;

    @Override
    @DataSource(DataSourceType.SOP)
    public void onMessage(String message) {
        log.info("消息队列接收到消息：{}",  message);
        VoiceVo vo = JSON.parseObject(message, VoiceVo.class);
        vo.setGenerated(0);
        redisCache.setCacheObject(VOICE_CACHE_KEY + vo.getType() + ":" + vo.getId(), vo);

        /*VoiceVo vo = JSON.parseObject(message, VoiceVo.class);
        if(vo.getId() != null){
            List<QwSopTempContent> contentList = qwSopTempContentService.selectQwSopTempContentByTempId(vo.getId());
            if(contentList != null && !contentList.isEmpty()){
                updateTempVoiceInfo(contentList);
            }
        }*/
    }

    private void updateTempVoiceInfo(List<QwSopTempContent> voiceList) {
        for (QwSopTempContent qwSopTempContent : voiceList) {
            String tempId = qwSopTempContent.getTempId();
            String content = qwSopTempContent.getContent();
            JSONObject jsonObject = JSONObject.parseObject(content);
            String text = jsonObject.getString("value");
            List<QwSop> qwSopList = qwSopService.selectQwSopByTempId(tempId);//通过tempId查询出所有sop
            if(qwSopList != null && !qwSopList.isEmpty()){
                for (QwSop qwSop : qwSopList) {
                    if(qwSop != null && qwSop.getQwUserIds() != null){
                        //查询出所有的tempContent来筛选文字
                        List<QwSopTempContent> qwSopTempContentList = qwSopTempContentService.selectQwSopTempContentByTempId(tempId);
                        if(qwSopTempContentList != null && !qwSopTempContentList.isEmpty()){
                            for (QwSopTempContent qwSopTemp : qwSopTempContentList) {
                                if(qwSopTemp != null && qwSopTemp.getContentType() == 7){
                                    String[] split = qwSop.getQwUserIds().split(",");
                                    Long[] qwUserIds = (Long[]) ConvertUtils.convert(split, Long.class);
                                    List<QwUserVO> qwUserVOS = qwUserService.selectQwUserVOByIds(qwUserIds);
                                    if(qwUserVOS != null){
                                        for (QwUserVO qwUserVO : qwUserVOS) {
                                            Long companyUserId = qwUserVO.getCompanyUserId();
                                            QwSopTempVoice qwSopTempVoice = qwSopTempVoiceService.selectQwSopTempVoiceByCompanyUserIdAndVoiceTxt(companyUserId,text);
                                            if(qwSopTempVoice == null){
                                                if(companyUserId != null && text != null){
                                                    QwSopTempVoice sopTempVoice = new QwSopTempVoice();
                                                    sopTempVoice.setCompanyUserId(companyUserId);
                                                    sopTempVoice.setVoiceTxt(text);
                                                    sopTempVoice.setTempId(tempId);
                                                    sopTempVoice.setRecordType(0);
                                                    qwSopTempVoiceService.insertQwSopTempVoice(sopTempVoice);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        /*if(qwSop != null && qwSop.getQwUserIds() != null){
            String tempId = qwSop.getTempId();
            List<QwSopTempContent> voiceList = qwSopTempContentService.selectQwSopTempContentByTempId(tempId);
            if(voiceList != null && !voiceList.isEmpty()){
                for (QwSopTempContent qwSopTempContent : voiceList) {
                    String content = qwSopTempContent.getContent();
                    JSONObject jsonObject = JSONObject.parseObject(content);
                    String text = jsonObject.getString("value");
                    //查询出所有的tempContent来筛选文字
                    List<QwSopTempContent> qwSopTempContentList = qwSopTempContentService.selectQwSopTempContentByTempId(tempId);
                    if(qwSopTempContentList != null && !qwSopTempContentList.isEmpty()){
                        for (QwSopTempContent qwSopTemp : qwSopTempContentList) {
                            if(qwSopTemp != null && qwSopTemp.getContentType() == 7){
                                String[] split = qwSop.getQwUserIds().split(",");
                                Long[] qwUserIds = Arrays.stream(split)
                                        .filter(s -> !s.isEmpty())
                                        .map(Long::valueOf)
                                        .toArray(Long[]::new);
                                List<QwUserVO> qwUserVOS = qwUserService.selectQwUserVOByIds(qwUserIds);
                                if(qwUserVOS != null){
                                    for (QwUserVO qwUserVO : qwUserVOS) {
                                        Long companyUserId = qwUserVO.getCompanyUserId();
                                        QwSopTempVoice qwSopTempVoice = qwSopTempVoiceService.selectQwSopTempVoiceByCompanyUserIdAndVoiceTxt(companyUserId,text);
                                        if(qwSopTempVoice == null){
                                            QwSopTempVoice sopTempVoice = new QwSopTempVoice();
                                            sopTempVoice.setCompanyUserId(companyUserId);
                                            sopTempVoice.setVoiceTxt(text);
                                            sopTempVoice.setTempId(tempId);
                                            sopTempVoice.setRecordType(0);
                                            qwSopTempVoiceService.insertQwSopTempVoice(sopTempVoice);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }*/
    }
}
