/*
package com.tashow.cloud.app.mq.consumer.buriedPoint;
import com.tashow.cloud.app.mq.mapper.BuriedPointMapper;
import com.tashow.cloud.app.mq.message.BuriedMessages;
import com.tashow.cloud.app.mq.model.BuriedPoint;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.Date;
import com.rabbitmq.client.Channel;
*/
/**
 * 埋点消息消费者
 * 将埋点数据存储到数据库
 *//*

@Component
@RabbitListener(queues = BuriedMessages.QUEUE)
@Slf4j
@RequiredArgsConstructor
public class BuriedPointConsumer {

    private final BuriedPointMapper buriedPointMapper;
    
    @Value("${spring.application.name:tashow-app}")
    private String applicationName;

    */
/**
     * 处理埋点消息
     *//*

    @RabbitHandler
    public void onMessage(BuriedMessages message,
                          Channel channel,
                          @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) {
        try {
            log.info("[埋点消费者] 收到埋点消息: {}", message);

            // 确保事件ID不为空
            if (message.getId() == null) {
                message.setId((int)(System.currentTimeMillis() % Integer.MAX_VALUE));
                log.warn("[埋点消费者] 消息中的事件ID为空，已自动生成: {}", message.getId());
            }
            saveToDatabase(message);
            channel.basicAck(deliveryTag, false);
            log.info("[埋点消费者] 消息处理成功，已确认");

        } catch (Exception e) {
            try {
                channel.basicNack(deliveryTag, false, true);
            } catch (IOException ex) {
                log.error("[埋点消费者] 拒绝消息失败", ex);
            }
        }

    }
    

    
    */
/**
     * 将埋点数据保存到数据库
     *//*

    private void saveToDatabase(BuriedMessages message) {
        try {
            log.debug("[埋点消费者] 准备保存埋点数据，事件ID: {}", message.getId());

            // 转换消息为实体
            BuriedPoint buriedPoint = new BuriedPoint();
            
            // 设置必填字段，确保不为空
            buriedPoint.setEventId(message.getId()); 
            buriedPoint.setEventTime(message.getEventTime());
            
            // 获取真实用户ID，避免使用默认anonymous
            String userId = message.getUserId();
            buriedPoint.setUserId(StringUtils.hasText(userId) && !"null".equals(userId) ? userId : "anonymous");

            String eventType = message.getEventType();
            buriedPoint.setEventType(eventType);
            buriedPoint.setService(applicationName);
            
            // 设置method字段，确保获取真实方法名
            buriedPoint.setMethod(message.getMethod());
            buriedPoint.setSessionId(message.getSessionId());
            buriedPoint.setClientIp(message.getClientIp());
            buriedPoint.setServerIp(message.getServerIp());

            // 设置其他字段
            buriedPoint.setPagePath(message.getPagePath());
            buriedPoint.setElementId(message.getElementId());
            buriedPoint.setDuration(message.getDuration());
            
            buriedPoint.setCreateTime(new Date());

            log.debug("[埋点消费者] 埋点实体数据: eventId={}, eventType={}, userId={}, service={}, method={}", 
                    buriedPoint.getEventId(), buriedPoint.getEventType(), 
                    buriedPoint.getUserId(), buriedPoint.getService(), buriedPoint.getMethod());

            int result = buriedPointMapper.insert(buriedPoint);
            
            log.info("[埋点消费者] 埋点数据已保存到数据库, 事件ID: {}, 影响行数: {}", message.getId(), result);
        } catch (Exception e) {
            log.error("[埋点消费者] 保存埋点数据到数据库失败, 事件ID: {}, 错误: {}", 
                    message.getId(), e.getMessage(), e);
        }
    }
} */
