package org.dromara.zhishu.aspect;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.dromara.common.json.utils.JsonUtils;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.zhishu.domain.bo.TaskBo;
import org.dromara.zhishu.domain.vo.ProductForm;
import org.dromara.zhishu.service.IRunningLogFileService;
import org.dromara.zhishu.util.MapUtils;
import org.dromara.zhishu.util.RunningLogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;


/**
 * 记录从小程序端入库和发布日志
 */
@Aspect
@Component
public class XcxAspect {

    // 定义 ThreadLocal 用于存储日志上下文 ID 或操作标识
    private static final ThreadLocal<String> LOG_CONTEXT = new InheritableThreadLocal<>();

    @Autowired
    private IRunningLogFileService runningLogFileService;

    //定义文件类型 xcx 小程序
    private final String fileType = "xcx";


//    /**
//     * 小程序扫码提交切点
//     * @param form
//     */
//    // 定义切入点：匹配AdminController中的register方法
//    @Pointcut(value = "execution(* org.dromara.zhishu.service.impl.ZhishuShopGoodsServiceImpl.submitProduct(org.dromara.zhishu.domain.vo.ProductForm)) && args(form)", argNames = "form")
//    public void registerMethod(ProductForm form) {}
//    /**
//     * 小程序版权页提交切点
//     * @param form
//     */
//    @Pointcut(value = "execution(* org.dromara.zhishu.service.impl.ZhishuShopGoodsServiceImpl.submitFromCopyrightPage(org.dromara.zhishu.domain.vo.ProductForm)) && args(form)", argNames = "form")
//    public void submitFromCopyrightPage(ProductForm form) {}
    /**
     * 子方法： 自动发布商品切点
     * @param map
     */
//    @Pointcut(value = "execution(* org.dromara.zhishu.service.impl.ZhishuShopGoodsServiceImpl.goodsAutoAdd(java.util.Map)) && args(map)", argNames = "map")
//    public void registerDetailMethod(Map map){}


//    @Around(value = "registerMethod(form)", argNames = "joinPoint,form")
//    public Object validateAndLog(ProceedingJoinPoint joinPoint, ProductForm form) {
//        String contextId = UUID.randomUUID().toString();
//        LOG_CONTEXT.set(contextId);
//
//        RunningLogUtils.init(runningLogFileService);
//        //写日志
//        RunningLogUtils.runningLog(joinPoint, MapUtils.convertToMap(form),"这里小程序扫码提交",fileType);
//
//        // 继续执行原方法并返回结果
////        try {
////            return joinPoint.proceed();
////        } catch (Throwable throwable) {
////            throw new RuntimeException(throwable);
////        }
//    }

//    @Around(value = "submitFromCopyrightPage(form)", argNames = "joinPoint,form")
//    public Object submitFromCopyrightPage(ProceedingJoinPoint joinPoint, ProductForm form) {
//        String contextId = UUID.randomUUID().toString();
//        LOG_CONTEXT.set(contextId);
//        RunningLogUtils.init(runningLogFileService);
//        //写日志
//        RunningLogUtils.runningLog(joinPoint,MapUtils.convertToMap(form),"这里小程序版权页提交",fileType);
//
//        // 继续执行原方法并返回结果
////        try {
////            return joinPoint.proceed();
////        } catch (Throwable throwable) {
////            throw new RuntimeException(throwable);
////        }
//    }

//    @Around(value = "registerDetailMethod(map)", argNames = "joinPoint,map")
//    public void detailLog(ProceedingJoinPoint joinPoint,Map map){
//        String contextId = LOG_CONTEXT.get();
//        if (contextId == null) {
//            contextId = "NO_PARENT"; // 表示没有外部触发源
//        }
//        //写日志
//        RunningLogUtils.runningLog(joinPoint,map,"这里是商品发布main方法日志",fileType);
//    }


}