package com.rj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rj.entity.VideoImageAnalysis;
import com.rj.mapper.VideoImageAnalysisMapper;
import com.rj.service.IVideoImageAnalysisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * AI视频图像分析记录Service实现类
 * 
 * @author 系统生成
 * @date 2025/1/30
 */
@Slf4j
@Service
public class VideoImageAnalysisServiceImpl extends ServiceImpl<VideoImageAnalysisMapper, VideoImageAnalysis> 
        implements IVideoImageAnalysisService {

    @Override
    public boolean saveVideoImageAnalysis(VideoImageAnalysis analysis) {
        try {
            log.info("保存AI视频图像分析记录开始，参数：{}", analysis);
            boolean result = this.save(analysis);
            if (result) {
                log.info("AI视频图像分析记录保存成功，ID: {}", analysis.getId());
            } else {
                log.error("AI视频图像分析记录保存失败");
            }
            return result;
        } catch (Exception e) {
            log.error("保存AI视频图像分析记录异常", e);
            return false;
        }
    }

    @Override
    public VideoImageAnalysis getByUserName(String userName) {
        try {
            LambdaQueryWrapper<VideoImageAnalysis> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(VideoImageAnalysis::getUserName, userName)
                       .eq(VideoImageAnalysis::getStatus, 1)
                       .orderByDesc(VideoImageAnalysis::getCreatedAt)
                       .last("LIMIT 1");
            
            return this.getOne(queryWrapper);
        } catch (Exception e) {
            log.error("根据用户姓名查询AI视频图像分析记录异常", e);
            return null;
        }
    }

    @Override
    public VideoImageAnalysis getByUserPhone(String userPhone) {
        try {
            LambdaQueryWrapper<VideoImageAnalysis> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(VideoImageAnalysis::getUserPhone, userPhone)
                       .eq(VideoImageAnalysis::getStatus, 1)
                       .orderByDesc(VideoImageAnalysis::getCreatedAt)
                       .last("LIMIT 1");
            
            return this.getOne(queryWrapper);
        } catch (Exception e) {
            log.error("根据用户电话查询AI视频图像分析记录异常", e);
            return null;
        }
    }

    @Override
    public VideoImageAnalysis getBySessionName(String sessionName) {
        try {
            LambdaQueryWrapper<VideoImageAnalysis> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(VideoImageAnalysis::getSessionName, sessionName)
                       .eq(VideoImageAnalysis::getStatus, 1)
                       .orderByDesc(VideoImageAnalysis::getCreatedAt)
                       .last("LIMIT 1");
            
            return this.getOne(queryWrapper);
        } catch (Exception e) {
            log.error("根据会话名称查询AI视频图像分析记录异常", e);
            return null;
        }
    }

    @Override
    public VideoImageAnalysis getByModel(String model) {
        try {
            LambdaQueryWrapper<VideoImageAnalysis> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(VideoImageAnalysis::getModel, model)
                       .eq(VideoImageAnalysis::getStatus, 1)
                       .orderByDesc(VideoImageAnalysis::getCreatedAt)
                       .last("LIMIT 1");
            
            return this.getOne(queryWrapper);
        } catch (Exception e) {
            log.error("根据模型查询AI视频图像分析记录异常", e);
            return null;
        }
    }
}
