package com.rf.psychological.module.index.repository;

import com.rf.psychological.base.repository.BaseRepository;
import com.rf.psychological.scale.dao.model.CategoryEntity;
import com.rf.psychological.module.index.dao.dto.IndexNumMonthDTO;
import com.rf.psychological.module.index.dao.dto.IndexRecordsNumSortDTO;
import com.rf.psychological.module.index.dao.dto.IndexSortNumDTO;
import org.springframework.data.jpa.repository.Query;
import com.rf.psychological.module.index.dao.dto.IndexNumDayDTO;

import java.util.List;

/**
 * @author lpf
 * @description:
 * @date 14/3/2022 下午4:46
 */
public interface IndexRepository extends BaseRepository<CategoryEntity, String> {


    @Query(value = "select count(*) count\n" +
            "from t_group_info\n" +
            "where if(?1 !='10006',institution_no = ?1 ,1=1) ", nativeQuery = true)
    int findGroupCount(String institutionNo);

    @Query(value = "select count(*) count\n" +
            "from t_user_info\n" +
            "where if(?1 !='10006',institution_no = ?1 and role_type=2 ,1=1 and role_type=2)", nativeQuery = true)
    int findInstitutionAdminCount(String institutionNo);

    @Query(value = "select count(*) count\n" +
            "from t_user_info\n" +
            "where if(?1 !='10006',institution_no = ?1 and role_type=1 ,1=1 and role_type=1)", nativeQuery = true)
    int findUserCount(String institutionNo);

    /**
     * 查询认知任务分类数量
     *
     * @param institutionNo
     * @return
     */
    @Query(value = "select tcs.ename, ANY_VALUE(tci.name) as name, count(*) count\n" +
            "from t_cognitive_task tct\n" +
            "         join t_institution_auth tia\n" +
            "         join t_category_subject tcs\n" +
            "         join t_category_info tci on tcs.ename = tci.ename\n" +
            "    and tcs.flag = tct.task_code\n" +
            "    and b_id = tct.id\n" +
            "where tia.institution_no = ?1\n" +
            "  and status = '0'\n" +
            "  and tci.ename != 'CALL'\n" +
            "group by tcs.ename order by count desc", nativeQuery = true)
    List<IndexSortNumDTO> findIndexCgSort(String institutionNo);

    /**
     * 查找所有认知任务分类数量
     *
     * @return
     */
    @Query(value = "select tcs.ename, ANY_VALUE(tci.name) as name, count(*) count\n" +
            "from t_cognitive_task tct\n" +
            "         join t_category_subject tcs\n" +
            "         join t_category_info tci on tcs.ename = tci.ename\n" +
            "    and tcs.flag = tct.task_code\n" +
            "where  status = '0'\n" +
            "  and tci.ename != 'CALL'\n" +
            "group by tcs.ename order by count desc", nativeQuery = true)
    List<IndexSortNumDTO> findIndexCgSort();

    /**
     * 查询量表类别分类数
     *
     * @param institutionNo
     * @return
     */
    @Query(value = "select tcs.ename, ANY_VALUE(tci.name) as name, count(*) count\n" +
            "from t_subject_info tsi\n" +
            "         join t_institution_auth tia\n" +
            "         join t_category_subject tcs\n" +
            "         join t_category_info tci on tsi.flag = tcs.flag and b_id = tsi.id and tci.ename = tcs.ename\n" +
            "where tia.institution_no = ?1\n" +
            "  and status = '0'\n" +
            "  and tia.type = '0'\n" +
            "group by ename order by count desc", nativeQuery = true)
    List<IndexSortNumDTO> findIndexSjSort(String institutionNo);

    /**
     * 查询所有量表类别分类数
     *
     * @return
     */
    @Query(value = "select tcs.ename, ANY_VALUE(tci.name) as name, count(*) count\n" +
            "from t_subject_info tsi\n" +
            "         join t_category_subject tcs\n" +
            "         join t_category_info tci on tsi.flag = tcs.flag and tci.ename = tcs.ename\n" +
            "where status = '0'\n" +
            "  and tsi.type = '0'\n" +
            "group by ename order by count desc", nativeQuery = true)
    List<IndexSortNumDTO> findIndexSjSort();

    @Query(value = "select any_value(tsi.name) name, tsi.flag flag,count(*) count\n" +
            "from t_subject_info tsi\n" +
            "         join t_user_record tur on tsi.flag = tur.flag\n" +
            "where if(?1 !='10006',institution_no = ?1 ,1=1)\n" +
            "group by tur.flag\n" +
            "order by count desc\n" +
            "limit ?2\n", nativeQuery = true)
    List<IndexRecordsNumSortDTO> findIndexSubjectLeaderboard(String institutionNo, int limit);

    @Query(value = "select any_value(tct.name) name, tct.task_code flag,count(*) count\n" +
            "from t_cognitive_task tct \n" +
            "         join t_user_record tur on tct.task_code = tur.flag\n" +
            "where if(?1 !='10006',institution_no = ?1 ,1=1)\n" +
            "group by tur.flag\n" +
            "order by count desc\n" +
            "limit ?2\n", nativeQuery = true)
    List<IndexRecordsNumSortDTO> findIndexCognitiveLeaderboard(String institutionNo, int limit);

    /**
     * 查看过去12个月的，每个月做量表和认知任务的数量
     *
     * @param type          0--量表，1--认知任务
     * @param institutionNo 机构名称
     * @return
     */
    @Query(value = "select a.month, ifnull(ANY_VALUE(b.count), 0) count\n" +
            "from (\n" +
            "         SELECT DATE_FORMAT(CURDATE(), '%Y-%m') AS `month`\n" +
            "         UNION\n" +
            "         SELECT DATE_FORMAT((CURDATE() - INTERVAL 1 MONTH), '%Y-%m') AS `month`\n" +
            "         UNION\n" +
            "         SELECT DATE_FORMAT((CURDATE() - INTERVAL 2 MONTH), '%Y-%m') AS `month`\n" +
            "         UNION\n" +
            "         SELECT DATE_FORMAT((CURDATE() - INTERVAL 3 MONTH), '%Y-%m') AS `month`\n" +
            "         UNION\n" +
            "         SELECT DATE_FORMAT((CURDATE() - INTERVAL 4 MONTH), '%Y-%m') AS `month`\n" +
            "         UNION\n" +
            "         SELECT DATE_FORMAT((CURDATE() - INTERVAL 5 MONTH), '%Y-%m') AS `month`\n" +
            "         UNION\n" +
            "         SELECT DATE_FORMAT((CURDATE() - INTERVAL 6 MONTH), '%Y-%m') AS `month`\n" +
            "         UNION\n" +
            "         SELECT DATE_FORMAT((CURDATE() - INTERVAL 7 MONTH), '%Y-%m') AS `month`\n" +
            "         UNION\n" +
            "         SELECT DATE_FORMAT((CURDATE() - INTERVAL 8 MONTH), '%Y-%m') AS `month`\n" +
            "         UNION\n" +
            "         SELECT DATE_FORMAT((CURDATE() - INTERVAL 9 MONTH), '%Y-%m') AS `month`\n" +
            "         UNION\n" +
            "         SELECT DATE_FORMAT((CURDATE() - INTERVAL 10 MONTH), '%Y-%m') AS `month`\n" +
            "         UNION\n" +
            "         SELECT DATE_FORMAT((CURDATE() - INTERVAL 11 MONTH), '%Y-%m') AS `month`\n" +
            "     ) a\n" +
            "         left join (\n" +
            "    select left(str_to_date(test_date, '%Y年%m月%d日%H时%i分%s秒'), 7) as 'month',\n" +
            "           count(*)                                              as count\n" +
            "    from t_user_record as a\n" +
            "    where DATE_FORMAT(str_to_date(test_date, '%Y年%m月%d日%H时%i分%s秒'), '%Y-%m') >\n" +
            "          DATE_FORMAT(date_sub(CURDATE(), interval 12 month), '%Y-%m')\n" +
            "      and type = ?1\n" +
            "      and   if(?2 !='10006',institution_no = ?2 ,1=1)\n" +
            "    GROUP BY month) b on a.month = b.month\n" +
            "group by a.month", nativeQuery = true)
    List<IndexNumMonthDTO> finIndexNumMonth(String type, String institutionNo);


    @Query(value = "select date, ifnull(ANY_VALUE(b.count), 0) count\n" +
            "from (\n" +
            "         SELECT @cdate \\:= date_add(@cdate, INTERVAL - 1 DAY) as date\n" +
            "         FROM (SELECT @cdate \\:= date_add(CURDATE(), INTERVAL + 1 DAY)\n" +
            "               FROM t_user_record\n" +
            "              ) d1\n" +
            "         ORDER BY date desc\n" +
            "         limit 15\n" +
            "     ) a\n" +
            "         left join (select left(str_to_date(test_date, '%Y年%m月%d日%H时%i分%s秒'), 10) as 'day',\n" +
            "                           count(*)                                               as count\n" +
            "                    from t_user_record as a\n" +
            "                    where DATE_FORMAT(str_to_date(test_date, '%Y年%m月%d日%H时%i分%s秒'), '%Y-%m-%d') >\n" +
            "                          DATE_FORMAT(date_sub(CURDATE(), interval 12 month), '%Y-%m')\n" +
            "                      and type = ?1\n" +
            "                      and  if(?2 !='10006',institution_no = ?2 ,1=1)\n" +
            "                    GROUP BY day) b on a.date = b.day\n" +
            "order by date desc",
            nativeQuery = true)
    List<IndexNumDayDTO> findIndexNumberByDays(String type, String institutionNo);

}
