package com.fs.core.aspectj;

import cn.hutool.core.util.ObjectUtil;
import com.fs.common.core.redis.RedisUtil;
import com.fs.live.domain.LiveWatchUser;
import com.fs.live.service.ILiveWatchUserService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

@Aspect
@Component
@Slf4j
public class LiveWatchUserAspect {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private ILiveWatchUserService liveWatchUserService;

    @AfterReturning(pointcut = "execution(* com.fs.live.service.impl.LiveWatchUserServiceImpl.insertLiveWatchUser(..)) || " +
            "execution(* com.fs.live.service.impl.LiveWatchUserServiceImpl.updateLiveWatchUser(..)) || " +
            "execution(* com.fs.live.service.impl.LiveWatchUserServiceImpl.deleteLiveWatchUserById(..)) || " +
            "execution(* com.fs.live.service.impl.LiveWatchUserServiceImpl.deleteLiveWatchUserByIds(..))",
            returning = "result")
    public void afterLiveWatchUserOperation(JoinPoint joinPoint, Object result) {
        try {
            String methodName = joinPoint.getSignature().getName();
            Object[] args = joinPoint.getArgs();
            log.info("直播观看用户数据发生变化，方法: {}, 参数: {}", methodName, Arrays.toString(args));
            // 提取liveId并处理缓存更新
            Set<Long> liveIds = extractLiveIds(methodName, args);
            for (Long liveId : liveIds) {
                liveWatchUserService.asyncToCache(liveId);
            }
        } catch (Exception e) {
            log.error("执行直播观看用户变更后逻辑失败", e);
        }
    }

    private Set<Long> extractLiveIds(String methodName, Object[] args) {
        Set<Long> liveIds = new HashSet<>();
        if (args == null || args.length == 0) {
            return liveIds;
        }
        switch (methodName) {
            case "insertLiveWatchUser":
            case "updateLiveWatchUser":
                // 参数是LiveWatchUser对象
                if (args[0] instanceof LiveWatchUser) {
                    LiveWatchUser liveWatchUser = (LiveWatchUser) args[0];
                    if (liveWatchUser.getLiveId() != null) {
                        liveIds.add(liveWatchUser.getLiveId());
                    }
                }
                break;
            case "deleteLiveWatchUserById":
                // 参数是Long类型的id，需要先查询获取liveId
                if (args[0] instanceof Long) {
                    LiveWatchUser liveWatchUser = liveWatchUserService.selectLiveWatchUserById((Long) args[0]);
                    if (ObjectUtil.isNotEmpty(liveWatchUser)) {
                        liveIds.add(liveWatchUser.getLiveId());
                    }
                }
                break;
            case "deleteLiveWatchUserByIds":
                // 参数是Long[]数组
                if (args[0] instanceof Long[]) {
                    Long[] ids = (Long[]) args[0];
                    LiveWatchUser liveWatchUser = liveWatchUserService.selectLiveWatchUserById(ids[0]);
                    if (ObjectUtil.isNotEmpty(liveWatchUser)) {
                        liveIds.add(liveWatchUser.getLiveId());
                    }
                }
                break;
            default:
                log.warn("未处理的方法: {}", methodName);
        }
        return liveIds;
    }


}
