package com.fs.app.task;

import com.fs.common.annotation.DataSource;
import com.fs.common.core.redis.RedisCacheT;
import com.fs.common.enums.DataSourceType;
import com.fs.common.utils.PubFun;
import com.fs.qw.result.QwFilterSopCustomersResult;
import com.fs.sop.domain.QwSop;
import com.fs.sop.domain.QwSopTempContent;
import com.fs.sop.domain.QwSopTempDay;
import com.fs.sop.mapper.QwSopMapper;
import com.fs.sop.mapper.QwSopTempContentMapper;
import com.fs.sop.params.QwSopTagsParam;
import com.fs.sop.service.IQwSopTempDayService;
import com.fs.sop.service.IQwSopTempVoiceService;
import com.fs.sop.vo.VoiceVo;
import com.fs.voice.utils.StringUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

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

@Slf4j
@Component
@AllArgsConstructor
public class Task {

    private final IQwSopTempDayService qwSopTempDayService;
    private final QwSopTempContentMapper qwSopTempContentMapper;
    private final IQwSopTempVoiceService qwSopTempVoiceService;
    private final QwSopMapper qwSopMapper;
    private final RedisCacheT<VoiceVo> redisCache;
    public final static String VOICE_CACHE_KEY = "voice:cache:";

    @Scheduled(cron = "0 * * * * *")
    @DataSource(DataSourceType.SOP)
    public void scheduled(){
        List<VoiceVo> list = redisCache.getCacheListByPattern(VOICE_CACHE_KEY + "*");
        if(list.isEmpty()) return;
        VoiceVo vo = list.get(0);
        if(vo.getType() == 0){
            synchronousTemp(Long.parseLong(vo.getId()));
        }
        if(vo.getType() == 1){
            synchronousSop(vo.getId());
        }
        redisCache.deleteObject(VOICE_CACHE_KEY + vo.getType() + ":" + vo.getId());
    }


    @DataSource(DataSourceType.SOP)
    public void synchronousTemp(Long dayId){
        QwSopTempDay day = qwSopTempDayService.getById(dayId);
        List<QwSopTempContent> contents = qwSopTempContentMapper.listByTempAndDay(day.getTempId(), dayId);
        qwSopTempVoiceService.synchronousTemp(contents, day);
    }

    @DataSource(DataSourceType.SOP)
    public void synchronousSop(String sopId){
        QwSop qwSop = qwSopMapper.selectQwSopById(sopId);
        QwSopTagsParam qwSopTagsParam = new QwSopTagsParam();
        //成员筛选
        if (!StringUtil.strIsNullOrEmpty(qwSop.getQwUserIds())){
            qwSopTagsParam.setUserIdsSelectList(Arrays.asList( qwSop.getQwUserIds().split(",")));
        }

        //标过滤类型
        qwSopTagsParam.setFilterType(qwSop.getFilterType());


        //标签
        if (!StringUtil.strIsNullOrEmpty(qwSop.getTags())){
            qwSopTagsParam.setTagsIdsSelectList(Arrays.asList(qwSop.getTags().split(",")));
        }

        //排除标签
        String excludeTags = qwSop.getExcludeTags();
        if (!StringUtil.strIsNullOrEmpty(excludeTags)){
            qwSopTagsParam.setOutTagsIdsSelectList(Arrays.asList(excludeTags.split(",")));
        }
        qwSopTagsParam.setCropId(qwSop.getCorpId());

        // 查询符合条件的客户
//                            List<QwFilterSopCustomersResult> qwFilterSopCustomersResults = qwSopMapper.selectFilterQwSopCustomersTest(qwSopTagsParam);
        List<QwFilterSopCustomersResult> qwFilterSopCustomersResults = qwSopMapper.selectFilterQwSopCustomers(qwSopTagsParam);
        List<List<Long>> companyUserIds = PubFun.listToNewList(qwFilterSopCustomersResults.stream().filter(e -> e.getCuCompanyId() != null && e.getCompanyId() != null).collect(Collectors.toList()), e -> Arrays.asList(e.getCuCompanyUserId(), e.getCuCompanyId()));
        qwSopTempVoiceService.synchronous(sopId, companyUserIds);
    }
}
