package com.fs.company.aspectj;

import cn.hutool.core.util.ObjectUtil;
import com.fs.live.domain.Live;
import com.fs.live.service.ILiveService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LiveControllerAspect {

    private static final Logger logger = LoggerFactory.getLogger(LiveControllerAspect.class);

    @Autowired
    private ILiveService liveService;

    @AfterReturning(pointcut = "execution(* com.fs.company.controller.live.LiveController.add(..)) || " +
            "execution(* com.fs.company.controller.live.LiveController.finishLive(..)) || " +
            "execution(* com.fs.company.controller.live.LiveController.copyLive(..)) || " +
            "execution(* com.fs.company.controller.live.LiveController.handleShelfOrUn(..)) || " +
            "execution(* com.fs.company.controller.live.LiveController.handleDeleteSelected(..)) || " +
            "execution(* com.fs.company.controller.live.LiveController.edit(..)) || " +
            "execution(* com.fs.company.controller.live.LiveController.remove(..)) || " +
            "execution(* com.fs.company.controller.live.LiveController.startLive(..))",
            returning = "result")
    public void afterLiveControllerMethodExecution(JoinPoint joinPoint,Object result) {
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        liveService.asyncToCache();
        if (args[0] instanceof Live) {
            Live live = (Live) args[0];
            if (ObjectUtil.isNotEmpty(live) && ObjectUtil.isNotEmpty(live.getLiveId())){
                liveService.asyncToCacheLiveDetail(live.getLiveId());
            }
        }
        logger.info("后台直播间列表变更");

    }
}
