package com.cirle.scientific.dao;

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

import java.util.List;

public interface QkjTqcourseDao extends PagingAndSortingRepository<QkjTqcourse, Long>, JpaSpecificationExecutor<QkjTqcourse> {


    @Query(value = "select DISTINCT qt.* from qkj_tqcourse qt inner join qkj_catalogs_detail qcd on qcd.obj_id = qt.id where qcd.catalogs_id in ?1 and qcd.obj_type = 2 and qt.status = 1 and qt.is_visiable = 1  order by qt.top_status desc, qt.create_time desc limit ?2,?3", nativeQuery = true)
    List<QkjTqcourse> getIndexData(List cataId, Integer start, Integer size);

    @Query(value = "select DISTINCT qt.* from qkj_tqcourse qt inner join qkj_catalogs_detail qcd on qcd.obj_id = qt.id where qcd.catalogs_id = ?1 and qcd.obj_type = 2 and qt.status = 1 and qt.is_visiable = 1 ", nativeQuery = true)
    List<QkjTqcourse> getTqByCataIds(Integer cataId);

    @Query(value = "select qzt.tqcoursezq_id from qkj_tqcourse qt inner join qkj_zq_tqcourse_detail qzt on qzt.tqcourse_id = qt.id where tqcourse_id = ?1", nativeQuery = true)
    List<Integer> getTqcoursezqIdByTqcourseId(Long TqcourseId);

    @Query(value = "select count(*) from (select DISTINCT qt.* from qkj_tqcourse qt inner join qkj_catalogs_detail qcd on qcd.obj_id = qt.id where qcd.catalogs_id in ?1 and qcd.obj_type = 2 and qt.status = 1 and qt.is_visiable = 1) a", nativeQuery = true)
    Integer getIndexDataCounts(List cataId);

    @Query(value = "select * from qkj_tqcourse where status = 1 and is_visiable = 1 and m_or_pc in (?3) order by top_status desc, create_time desc , sort_num desc limit ?1,?2", nativeQuery = true)
    List<QkjTqcourse> getIndexDataBysortNum(Integer start, Integer size, Integer[] mOrPc);

    @Query(value = "select * from qkj_tqcourse where status = 1 and is_visiable = 1 and m_or_pc in (?3) order by top_status desc, create_time desc limit ?1,?2", nativeQuery = true)
    List<QkjTqcourse> getIndexNewestData(Integer start, Integer size, Integer[] mOrPc);

    @Query(value = "select * from qkj_tqcourse where status = 1 and is_visiable = 1 order by top_status desc, create_time desc limit ?1,?2", nativeQuery = true)
    List<QkjTqcourse> getIndexDataAll(Integer page, Integer size);

    @Query(value = "select count(*) from qkj_tqcourse where status = 1 and is_visiable = 1 order by top_status desc, create_time desc", nativeQuery = true)
    Integer getIndexDataAllSize();


    @Query(value = "select * from qkj_tqcourse where status = 1 and is_visiable = 1 and m_or_pc in (?3) order by study_num desc limit ?1,?2", nativeQuery = true)
    List<QkjTqcourse> getIndexHottestData(Integer start, Integer size, Integer[] mOrPc);

    @Query(value = "select count(*) from qkj_tqcourse where status = 1 and is_visiable = 1 order by study_num desc", nativeQuery = true)
    Integer getIndexDataCounts();


    @Query(value = "select count(*) from qkj_tqcourse where status = 1 and is_visiable = 1 and m_or_pc in (?1) order by study_num desc", nativeQuery = true)
    Integer getIndexDataCounts(Integer[] mOrPc);

    @Query(value = "select qt.* from qkj_tqcourse qt inner join qkj_zq_tqcourse_detail qzt on qzt.tqcourse_id = qt.id where qt.status = 1 and qt.is_visiable = 1 and qzt.tqcoursezq_id = ?1 order by qt.create_time desc,qzt.sort desc limit ?2,?3", nativeQuery = true)
    List<QkjTqcourse> findTqListForZq(Integer zqId, int start, Integer size);

    @Query(value = "select qt.* from qkj_tqcourse qt inner join qkj_zq_tqcourse_detail qzt on qzt.tqcourse_id = qt.id where qt.status = 1 and qt.is_visiable = 1 and qzt.tqcoursezq_id = ?1 order by qzt.sort asc limit ?2,?3", nativeQuery = true)
    List<QkjTqcourse> findTqListForZq1(Integer zqId, int start, Integer size);


    @Query(value = "select qt.* from qkj_tqcourse qt inner join qkj_zq_tqcourse_detail qzt on qzt.tqcourse_id = qt.id where qt.status = 1 and qt.is_visiable = 1 and qzt.tqcoursezq_id = ?1 order by qt.create_time desc,qzt.sort desc", nativeQuery = true)
    List<QkjTqcourse> findTqListForZq(Integer zqId);

    @Query(value = "select qt.* from qkj_tqcourse qt inner join qkj_zq_tqcourse_detail qzt on qzt.tqcoursezq_id = qt.id where qt.status = 1 and qt.is_visiable = 1 and qzt.tqcourse_id = ?1 order by qt.create_time desc,qzt.sort desc", nativeQuery = true)
    List<QkjTqcourse> findTqListFortqId(Integer tqId);

    @Query(value = "select qt.* from qkj_catalogs_detail qcd inner join qkj_tqcourse qt on qt.id = qcd.obj_id where qcd.catalogs_id in (?1) and qt.id != ?2 and qcd.obj_type = 2 and qt.status = 1 order by qt.create_time desc,qcd.sqn desc limit 0,10", nativeQuery = true)
    List<QkjTqcourse> findTqListForCataList(String ids, Long id);

    @Query(value = "SELECT DISTINCT qt.* from qkj_tqcourse qt left JOIN qkj_tqcourse_course qtc on qtc.tqcourse_id = qt.id left JOIN qkj_tags_detail qtd on qtd.obj_id = qtc.id where qtd.tag_id in (1,2) and qt.status = 1", nativeQuery = true)
    List<QkjTqcourse> findTqListByTags(String tagIds);


    @Query(value = "SELECT * from qkj_tqcourse qt  where qt.tag_ids like %?1% and qt.status = 1 and qt.id != ?2", nativeQuery = true)
    List<QkjTqcourse> findTqListByTagIds(String tagIds, Long id);

}
