package com.wzj.soopin.content.mapper;

import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzj.soopin.content.domain.bo.VlogBO;
import com.wzj.soopin.content.domain.po.Vlog;
import com.wzj.soopin.content.domain.vo.CommentVO;
import com.wzj.soopin.content.domain.vo.IndexVlogVO;
import org.apache.ibatis.annotations.Param;

import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

//@Repository
//public interface VlogMapper extends MyMapper<Vlog> {
//}
@InterceptorIgnore(tenantLine = "true")
@Repository
public interface VlogMapper extends BaseMapper<Vlog> {
//    public List<IndexVlogVO> updateVlogStatus(@Param("paramMap") Map<String, Object> map);
    /**
     * 通用动态更新方法
     * @param primaryKey 主键字段名
     * @param idValue 主键值
     * @param updateFields 待更新的字段Map（key=字段名, value=字段值）
     * @return 影响的行数
     */
    int dynamicUpdate(
        @Param("primaryKey") String primaryKey,
        @Param("idValue") Object idValue,
        @Param("updateFields") Map<String, Object> updateFields
    );

    /**
     * Vlog表多条件动态更新
     * @param updateFields 需要更新的字段及值（Map结构）
     * @param conditions 更新条件（Map结构）
     * @return 受影响的行数
     */
    int updateVlogByConditions(
        @Param("updates") Map<String, Object> updateFields,
        @Param("conditions") Map<String, Object> conditions
    );

    /**
     * 单条插入vlog记录
     * @param vlogData 插入数据的键值对(key=列名, value=列值)
     * @return 影响的行数
     */
    int dynamicInsert(@Param("vlogData") Map<String, Object> vlogData);

    /**
     * 批量插入vlog记录
     * @param vlogList 插入数据列表(每个Map代表一行数据)
     * @return 影响的行数
     */
    int dynamicBatchInsert(@Param("vlogList") List<Map<String, Object>> vlogList);

    public List<IndexVlogVO> selectMyPublic(@Param("paramMap")Map<String, Object> map);
    public List<IndexVlogVO> getVlogDetailFromId(@Param("paramMap")Map<String, Object> map);

    /**
     * 更新视频审核状态
     * @param params 包含vlogId、status、reason和auditTime的Map
     * @return 影响的行数
     */
    int updateVlogStatus(@Param("params") Map<String, Object> params);

    /**
     * 根据fileId查询Vlog信息
     * @param fileId 云点播视频唯一标识
     * @return Vlog对象或null
     */
    Vlog selectByFileId(@Param("fileId") String fileId);

    /**
     * 分页查询视频列表
     * @param page 分页对象
     * @param params 查询参数
     * @return 分页结果
     */
    IPage<Map<String, Object>> selectVlogList(Page<Map<String, Object>> page, @Param("params") Map<String, Object> params);

    IPage<CommentVO> selectByMobileAndVlogId(Page<?> page, @Param("mobile") String mobile, @Param("vlogId") String vlogId);

    /**
     * 分页查询视频列表，包含点赞数和评论数，并支持按这些聚合字段进行全局排序。
     * @param page 分页对象
     * @param vlogBO 查询和排序参数
     * @return 分页结果，包含视频信息及统计数据
     */
    IPage<Map<String, Object>> selectVlogListWithAggregatedData(Page<Map<String, Object>> page, @Param("vlogBO") VlogBO vlogBO);

    @Select("SELECT COUNT(*) FROM cont_vlog where status = 0")
    Object countVlog();


    @Select("SELECT " +
        "    DATE_FORMAT(create_time, '%Y-%m') AS month, " +
        "    COUNT(*) AS vlog_count " +
        "FROM " +
        "    cont_vlog " +
        "WHERE " +
        "    status = 1 " +
        "GROUP BY " +
        "    DATE_FORMAT(create_time, '%Y-%m') " +
        "ORDER BY " +
        "    DATE_FORMAT(create_time, '%Y-%m')")
    List<Map<String, Object>> getMonthlyVlog();

    /**
     * 查询所有公开的视频列表（用于从Redis获取点赞数）
     * @return 视频信息列表
     */
    @Select("SELECT " +
        "    v.id, " +
        "    v.vloger_id, " +
        "    v.url, " +
        "    v.cover, " +
        "    v.title, " +
        "    v.width, " +
        "    v.height, " +
        "    v.like_counts, " +
        "    v.comments_counts, " +
        "    v.is_private, " +
        "    v.create_time, " +
        "    v.update_time, " +
        "    v.status, " +
        "    v.file_id, " +
        "    v.reason, " +
        "city_code,"+
        "    v.first_frame_img " +
        "FROM cont_vlog v " +
        "WHERE v.status = 1 AND v.is_private = 0 " +
        "ORDER BY v.create_time DESC")
    List<Map<String, Object>> selectAllPublicVlogs();

    /**
     * 随机查询指定数量的公开视频
     * @param limit 查询数量限制
     * @return 随机视频列表
     */
    @Select("SELECT " +
        "    v.id, " +
        "    v.vloger_id, " +
        "    v.url, " +
        "    v.cover, " +
        "    v.title, " +
        "    v.width, " +
        "    v.height, " +
        "    v.like_counts, " +
        "    v.comments_counts, " +
        "    v.is_private, " +
        "    v.create_time, " +
        "    v.update_time, " +
        "    v.status, " +
        "    v.file_id, " +
        "    v.reason, " +
        "    v.city_code, " +
        "    v.first_frame_img " +
        "FROM cont_vlog v " +
        "WHERE v.status = 1 AND v.is_private = 0 " +
        "ORDER BY RAND() " +
        "LIMIT #{limit}")
    List<Map<String, Object>> selectRandomVlogs(@Param("limit") int limit);


    IPage<String> getVlogForUser(Page<IndexVlogVO> page, @Param("memberId") Long memberId);
    int readVlog(@Param("memberId") Long memberId, @Param("vlogId") String vlogId);
}
