package com.rf.psychological.dao.repository;

import com.rf.psychological.base.repository.BaseRepository;
import com.rf.psychological.scale.dao.model.CategoryEntity;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * @author zsy
 * @description:Category
 * @date 2021/6/17 15:55
 */
public interface CategoryRepository extends BaseRepository<CategoryEntity, String> {

    /**
     * 查询  分页
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Query(value = "select id,ename,name,sequence,type from t_category_info order by sequence limit :pageNum ,:pageSize ", nativeQuery = true)
    List<CategoryEntity> findAll(@Param("pageNum") int pageNum, @Param("pageSize") int pageSize);

    /**
     * 总条数
     *
     * @return
     */
    @Query(value = "select count(1) from t_category_info   ", nativeQuery = true)
    int allNum();

    /**
     * 根据名称模糊查询  分页
     *
     * @param pageNum
     * @param pageSize
     * @param searchStr
     * @return
     */
    @Query(value = "select id,ename,name,sequence,type from t_category_info where name like %:searchStr% or ename like %:searchStr% order by sequence limit :pageNum ,:pageSize ", nativeQuery = true)
    List<CategoryEntity> getSubjectListByName(@Param("pageNum") int pageNum, @Param("pageSize") int pageSize, @Param("searchStr") String searchStr);

    /**
     * 根据名称模糊查询总条数
     *
     * @param searchStr
     * @return
     */
    @Query(value = "select count(1) from t_category_info where name like %:searchStr% or ename like %:searchStr%   ", nativeQuery = true)
    int allNumByName(@Param("searchStr") String searchStr);

    /**
     * 效验是否存在
     *
     * @param name
     * @param ename
     * @param sequence
     * @return
     */
    @Query(value = "select id,ename,name,sequence,type from t_category_info where name = :name or ename = :ename or sequence = :sequence", nativeQuery = true)
    List<CategoryEntity> getCategoryByName(@Param("name") String name, @Param("ename") String ename, @Param("sequence") String sequence);

    /**
     * 查询所有类别信息
     *
     * @return
     */
    @Query(value = "select id,ename,name,sequence,type from t_category_info where type = :type and ename in (SELECT a.ename FROM t_category_subject a GROUP BY a.ename) order by sequence", nativeQuery = true)
    List<CategoryEntity> findAllOrder(@Param("type") String type);


    /**
     * 查询机构的认知任务类别信息
     *
     * @return
     */
    @Query(value = "select id, ename, name, sequence, type\n" +
            "from t_category_info\n" +
            "where  ename in (select distinct(tcs.ename)\n" +
            "                from   t_cognitive_task tct\n" +
            "                         join\n" +
            "                     t_institution_auth tia\n" +
            "                         join\n" +
            "                     t_category_subject tcs\n" +
            "                     on tcs.flag = tct.task_code\n" +
            "                         and b_id = tct.id\n" +
            "                where tia.institution_no = ?1  and status='0'\n" +
            "        ) or ename='CALL' order by sequence", nativeQuery = true)
    List<CategoryEntity> findAllCTInstitution(String institutionNo);

    /**
     * 查询组的认知任务类别信息
     *
     * @return
     */
    @Query(value = "select id, ename, name, sequence, type\n" +
            "from t_category_info\n" +
            "where  ename in (select distinct(tcs.ename)\n" +
            "                 from   t_cognitive_task tct\n" +
            "                            join\n" +
            "                        t_group_auth tga\n" +
            "                            join\n" +
            "                        t_category_subject tcs\n" +
            "                        on tcs.flag = tct.task_code\n" +
            "                            and b_id = tct.id\n" +
            "                 where tga.group_id = ?1 and status='0'\n" +
            ") or ename='CALL'\n" +
            "order by sequence\n", nativeQuery = true)
    List<CategoryEntity> findAllCTByGroup(String groupId);

    /**
     * 查询机构的量表类别信息
     *
     * @return
     */
    @Query(value = "select id, ename, name, sequence, type\n" +
            "from t_category_info\n" +
            "where ename in (select distinct(tcs.ename)\n" +
            "                from t_subject_info tsi\n" +
            "                         join\n" +
            "                     t_institution_auth tia\n" +
            "                         join\n" +
            "                     t_category_subject tcs\n" +
            "                     on tcs.flag = tsi.flag\n" +
            "                         and b_id = tsi.id\n" +
            "                where tia.institution_no = ?1 and status='0'\n" +
            ")\n" +
            "   or ename = 'ALL'  order by sequence ", nativeQuery = true)
    List<CategoryEntity> findAllSubjectInstitution(String institutionNO);

    /**
     * 查询组的量表类别信息
     *
     * @return
     */
    @Query(value = "select id, ename, name, sequence, type\n" +
            "from t_category_info\n" +
            "where ename in (select distinct(tcs.ename)\n" +
            "                from t_subject_info tsi\n" +
            "                         join\n" +
            "                     t_group_auth tga \n" +
            "                         join\n" +
            "                     t_category_subject tcs\n" +
            "                     on tcs.flag = tsi.flag\n" +
            "                         and b_id = tsi.id\n" +
            "                where tga.group_id= ?1 and status='0'\n" +
            ")\n" +
            "   or ename = 'ALL' order by sequence ", nativeQuery = true)
    List<CategoryEntity> findAllSubjectGroup(String groupId);
}
