package com.cirle.scientific.dao;

import com.cirle.scientific.po.QkjVedioPlayRecord;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import java.math.BigDecimal;
import java.util.List;

public interface QkjVedioPlayRecordDao extends PagingAndSortingRepository<QkjVedioPlayRecord, Long>, JpaSpecificationExecutor<QkjVedioPlayRecord> {
    QkjVedioPlayRecord findByMemCardAndObjTypeAndObjId(String memCard, int objType, int objId);


    @Query(value = "select SUM(l.study_percent) from qkj_video_play_record l where l.mem_card =?1 and l.obj_type=?2 and l.pobj_id=?3",nativeQuery = true)
    BigDecimal findTotalPercentByPobjId(String memCard, Integer objType, int intValue);


    @Query(value = "SELECT * from (SELECT l.* from qkj_video_play_record l INNER JOIN qkj_tqcourse qt on qt.id = l.obj_id  where l.mem_card = ?1 and l.obj_type = 7  and qt.status = 1\n" +
            "UNION\n" +
            "SELECT l.* from qkj_video_play_record l INNER JOIN qkj_live ql on ql.id = l.obj_id  where l.mem_card = ?1 and l.obj_type = 2  and ql.status = 1) as a order by opr_time desc limit ?2,?3",nativeQuery = true)
    List<QkjVedioPlayRecord> findUserStudyList(String memCard, int start,int size);


    @Query(value = "select COUNT(DISTINCT l.mem_card) from qkj_video_play_record l where l.obj_type=?1 and l.pobj_id=?2",nativeQuery = true)
    BigDecimal findMemberCountByPobjId(int objType, int pobjId);


    @Query(value = "select * from qkj_video_play_record where mem_card = ?1 and obj_type =?2 and pobj_id = ?3",nativeQuery = true)
    List<QkjVedioPlayRecord> findPlayTimeByMemCardAndObjIdAndObjTypeAndPobjId(String memCard , int objType , int pobjId);
}
