package com.sonic.frog.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sonic.frog.dao.AdvertiseDao;
import com.sonic.frog.domain.entity.Advertise;
import com.sonic.frog.domain.entity.AiDict;
import com.sonic.frog.domain.output.AdvertiseOutput;
import com.sonic.frog.domain.output.AiCarouselListOutput;
import com.sonic.frog.domain.output.AiUserBaseOutput;
import com.sonic.frog.domain.output.ClassificationListOutput;
import com.sonic.frog.service.AdvertiseService;
import com.sonic.frog.service.AiUserSearchService;
import com.sonic.frog.service.AiUserStatService;
import com.sonic.frog.utils.BeanConver;
import com.sonic.frog.utils.CacheUtils;
import com.sonic.frog.utils.RedisKeyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.sonic.frog.enums.AdvertiseBizType;

/**
 * 广告管理 Service实现类
 */
@Slf4j
@Service
public class AdvertiseServiceImpl extends ServiceImpl<AdvertiseDao, Advertise> implements AdvertiseService {

    @Autowired
    private AiUserSearchService aiUserSearchService;
    @Autowired
    private AiUserStatService aiUserStatService;
    @Autowired
    private CacheUtils cacheUtils;
    @Autowired
    private RedisKeyUtils redisKeyUtils;


    @Override
    public List<AdvertiseOutput> getAdvertiseList(Long userId) {
        List<Advertise> list = list(Wrappers.<Advertise>lambdaQuery().eq(Advertise::getIsDelete, false).orderByAsc(Advertise::getSort));
        return BeanConver.copeList(list, AdvertiseOutput.class);
    }

    @Override
    public List<AiCarouselListOutput> getAiCarouselList(Long userId) {
        //获取ai轮播推荐数据
        Advertise advertise = getOne(Wrappers.<Advertise>lambdaQuery().eq(Advertise::getBizType, AdvertiseBizType.AI_CAROUSEL_RECOMMEND).eq(Advertise::getIsDelete, false).last("limit 1"));
        if (advertise != null) {
            String ext = advertise.getExt();
            List<Long> aiIdList = JSON.parseArray(ext, Long.class);
            if (CollectionUtils.isEmpty(aiIdList)) {
                return Collections.emptyList();
            }
            //缓存1分钟
            String aiCarouselListKey = redisKeyUtils.aiCarouselListKey();
            return cacheUtils.getCacheListAndSet(aiCarouselListKey, AiCarouselListOutput.class, () -> {
                //批量获取ai基础信息Map
                Map<Long, AiUserBaseOutput> aiUserMap = aiUserSearchService.mapByAiIdList(aiIdList);
                //批量获取被喜欢数Map
                Map<Long, Integer> aiLikedCountMap = aiUserStatService.queryAiLikedCount(aiIdList);
                List<AiCarouselListOutput> outputList =  aiIdList.stream().map(aiId -> {
                    AiUserBaseOutput aiUserBaseOutput = aiUserMap.get(aiId);
                    //基础信息赋值
                    AiCarouselListOutput output = BeanConver.copeBean(aiUserBaseOutput, AiCarouselListOutput.class);
                    if (output != null) {
                        //被喜欢数赋值
                        output.setLikedCount(aiLikedCountMap.get(aiId) != null ? aiLikedCountMap.get(aiId) : 0);
                    }
                    return output;
                }).collect(Collectors.toList());
                return JSON.toJSONString(outputList);
            }, 60);
        }
        return Collections.emptyList();
    }
}