package org.dromara.zhishu.aspect;

import com.pdd.pop.sdk.common.util.JsonUtil;
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.core.domain.model.LoginUser;
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.HashMap;
import java.util.Map;

/**
 * 中心书库发布任务日志
 */
@Aspect
@Component
public class CenterAspect {

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

    @Autowired
    private IRunningLogFileService runningLogFileService;

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


    /**
     * 中心书库发布数据接口
     * @param jsonStr
     */
    @Pointcut(value = "execution(* org.dromara.zhishu.controller.TaskController.centerBooks(String)) && args(jsonStr)", argNames = "jsonStr")
    public void centerBooks(String jsonStr){}

    /**
     * 子方法： 自动发布商品切点
     * @param map
     */
    @Pointcut(value = "execution(* org.dromara.zhishu.service.impl.TaskServiceImpl.addGoods(java.util.Map,org.dromara.zhishu.domain.bo.TaskBo,Long)) && args(map,taskBo,userId)", argNames = "map,taskBo,userId")
    public void addGoods(Map map, TaskBo taskBo, Long userId){}


    @Around(value = "centerBooks(jsonStr)", argNames = "joinPoint,jsonStr")
    public Object repeatBook(ProceedingJoinPoint joinPoint, String jsonStr) {
        RunningLogUtils.init(runningLogFileService);
        //接受数据
        Map dataMap = JsonUtil.transferToObj(jsonStr, HashMap.class);
        //任务名称
        String fileName = dataMap.get("taskId").toString();
        /**
         * 从任务名称中解析出用户id
         */
        //删除文本 ”中心书库发布“
        fileName = fileName.replace("中心书库发布","");
        //根据分号分割
        String[] split = fileName.split(";");
        //遍历split在根据分割后的字符串根据下划线进行分割获取userId
        for (String s : split) {
            String[] split1 = s.split("_");
            if(split1[0].equals("userId")){
                //将userId的值放入dataMap里
                dataMap.put("userId", split1[1]);
                break;
            }
        }
        //写日志
        return RunningLogUtils.runningLog(joinPoint, dataMap,"中心书库发布商品",fileType);
    }


    @Around(value = "addGoods(map,taskBo,userId)", argNames = "joinPoint,map,taskBo,userId")
    public Object addGoods(ProceedingJoinPoint joinPoint,Map map, TaskBo taskBo, Long userId){
        RunningLogUtils.init(runningLogFileService);
        Map dataMap = new HashMap();
        dataMap.put("map", map);
        dataMap.put("taskBo", taskBo);
        dataMap.put("userId", userId);
        //写日志
        return RunningLogUtils.runningLog(joinPoint, dataMap,"中心书库发布商品发布方法addGoods",fileType);
    }
}
