package com.rf.psychological.plan.dao.repository;

import com.rf.psychological.base.repository.BaseRepository;
import com.rf.psychological.dao.dto.InstitutionAuthSubjectNameDto;
import com.rf.psychological.plan.dao.dto.TestPlanAuthConTaskDto;
import com.rf.psychological.plan.dao.model.TestPlanContendEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lpf
 * @description:
 * @date 2022/1/6 12:42
 */
public interface TestPlanContendRepository extends BaseRepository<TestPlanContendEntity, String> {

    @Transactional
    @Modifying(clearAutomatically = true)
    void deleteById(String id);


    Page<TestPlanContendEntity> findTestPlanContendEntitiesByTestPlanIdAndType(String testPlanId, String type, Pageable pageable);

    @Transactional
    @Modifying(clearAutomatically = true)
    void deleteAllByTestPlanId(String testPlanId);

    @Transactional
    @Modifying(clearAutomatically = true)
    void deleteByTestIdAndTestPlanId(String testId, String testPlanId);

    @Query(value = "select tia.id,tia.b_id bid, tia.institution_no institutionNo, description, ename, flag, image_url imgeUrl, is_hot isHot, is_new isNew, name, test_num testNum, tsi.type, uploader, file_name fileName, scoring_principle scoringPrinciple, status, scale_limit scaleLimit,no,icon_info iconInfo" +
            "            from t_institution_auth tia join t_subject_info tsi on tia.b_id = tsi.id where\n" +
            "        institution_no =:institutionNo and\n" +
            "        tsi.type =:type and\n" +
            " if(:searchKey is not null and :searchKey!='',(tsi.name like CONCAT('%',:searchKey,'%')), 1=1)\n" +
            "and not exists(select 1 from t_test_plan_contend ttpc where ttpc.test_id = tia.b_id and test_plan_id=:testPlanId)\n",
            countQuery = "select count(tia.id)\n" +
                    "            from t_institution_auth tia join t_subject_info tsi on tia.b_id = tsi.id where\n" +
                    "        institution_no =:institutionNo and\n" +
                    "        tsi.type =:type and\n" +
                    " if(:searchKey is not null and :searchKey!='',(tsi.name like CONCAT('%',:searchKey,'%')), 1=1)\n" +
                    "and not exists(select 1 from t_test_plan_contend ttpc where ttpc.test_id = tia.b_id and test_plan_id=:testPlanId)\n",
            nativeQuery = true)
    Page<InstitutionAuthSubjectNameDto> getTestPlanNoSubTask(@Param("institutionNo") String institutionNo,
                                                             @Param("type") String type, @Param("searchKey") String searchKey,
                                                             @Param("testPlanId") String testPlanId, Pageable pageable);

    @Query(value = "select tia.id, b_id bId, institution_no institutionNo, tct.type, description, image_name imageName, image_url imageUrl, name, page_url pageUrl, scale_limit scaleLimit, status, task_code flag, test_description testDescription, test_explain testExplain, test_num testNum, no\n" +
            "from t_institution_auth tia join t_cognitive_task tct on tia.b_id = tct.id where\n" +
            "   institution_no =:institutionNo and\n" +
            " if(:searchKey is not null and :searchKey!='',(tct.name like CONCAT('%',:searchKey,'%')), 1=1)\n" +
            "and tct.type =:type and  not exists(select 1 from t_test_plan_contend ttpc where ttpc.test_id = tia.b_id and test_plan_id=:testPlanId) and status='0'",
            countQuery = "select count(tia.id)\n" +
                    "from t_institution_auth tia join t_cognitive_task tct on tia.b_id = tct.id where\n" +
                    "   institution_no =:institutionNo and\n" +
                    " if(:searchKey is not null and :searchKey!='',(tct.name like CONCAT('%',:searchKey,'%')), 1=1)\n" +
                    "and tct.type =:type and  not exists(select 1 from t_test_plan_contend ttpc where ttpc.test_id = tia.b_id and test_plan_id=:testPlanId) and status='0'",
            nativeQuery = true)
    Page<TestPlanAuthConTaskDto> getTestPlanNoConTask(@Param("institutionNo") String institutionNo,
                                                      @Param("type") String type, @Param("searchKey") String searchKey,
                                                      @Param("testPlanId") String testPlanId, Pageable pageable);


    @Query(value = "select id, create_time, test_id, type, update_time, test_plan_id, flag,name,description\n" +
            "from t_test_plan_contend  ttpc2\n" +
            "where not exists(select 1\n" +
            "                 from t_user_record tur\n" +
            "                          join t_test_plan_contend ttpc on tur.flag = ttpc.flag  and tur.test_plan_id=ttpc.test_plan_id\n" +
            "                 where tur.test_plan_id = ?4\n" +
            "                   and ttpc.flag=ttpc2.flag\n" +
            "                   and phone = ?2\n" +
            "                   and institution_no = ?1\n" +
            "                   and tur.type = ?3)\n" +
            "  and test_plan_id =  ?4 and type=?3",
            countQuery = "select count(id)\n" +
                    "from t_test_plan_contend  ttpc2\n" +
                    "where not exists(select 1\n" +
                    "                 from t_user_record tur\n" +
                    "                          join t_test_plan_contend ttpc on tur.flag = ttpc.flag  and tur.test_plan_id=ttpc.test_plan_id\n" +
                    "                 where tur.test_plan_id = ?4\n" +
                    "                   and ttpc.flag=ttpc2.flag\n" +
                    "                   and phone = ?2\n" +
                    "                   and institution_no = ?1\n" +
                    "                   and tur.type = ?3)\n" +
                    "  and test_plan_id =  ?4 and type=?3", nativeQuery = true)
    Page<TestPlanContendEntity> getUserNoDoTestPlan(String institutionNo, String phone, String type,
                                                    String testPlanId, Pageable pageable);

    @Query(value = "select id, create_time, test_id, type, update_time, test_plan_id, flag, name, description\n" +
            "from t_test_plan_contend  ttpc2\n" +
            "where not exists(select 1\n" +
            "                 from t_user_record tur\n" +
            "                          join t_test_plan_contend ttpc on tur.flag = ttpc.flag  and tur.test_plan_id=ttpc.test_plan_id\n" +
            "                 where tur.test_plan_id = ?3\n" +
            "                   and ttpc.flag=ttpc2.flag\n" +
            "                   and phone = ?2\n" +
            "                   and institution_no = ?1\n" +
            "                   )\n" +
            "  and test_plan_id =  ?3", nativeQuery = true)
    List<TestPlanContendEntity> getUserNoDoTestPlan(String institutionNo, String phone, String testPlanId);


    @Query(value = "select count(id)\n" +
            "from t_test_plan_contend  ttpc2\n" +
            "where not exists(select 1\n" +
            "                 from t_user_record tur\n" +
            "                          join t_test_plan_contend ttpc on tur.flag = ttpc.flag  and tur.test_plan_id=ttpc.test_plan_id\n" +
            "                 where tur.test_plan_id = ?3\n" +
            "                   and ttpc.flag=ttpc2.flag\n" +
            "                   and phone = ?2\n" +
            "                   and institution_no = ?1\n" +
            "                   )\n" +
            "  and test_plan_id =  ?3", nativeQuery = true)
    int getUserNoDoTestPlanNum(String institutionNo, String phone, String testPlanId);

    /*@Query(value = "select ttpc.id, create_time, test_id, ttpc.type, update_time, ttpc.test_plan_id,tur.flag,ttpc.name,ttpc.description\n" +
            "                 from t_user_record tur\n" +
            "                          join t_test_plan_contend ttpc on tur.flag = ttpc.flag  and tur.test_plan_id=ttpc.test_plan_id\n" +
            "                 where tur.test_plan_id = ?3\n" +
            "                   and phone = ?2\n" +
            "                   and institution_no = ?1 group by tur.flag\n", nativeQuery = true)*/
    @Query(value = "SELECT\n" +
            "\t*\n" +
            "FROM\n" +
            "\tt_test_plan_contend ttpc\n" +
            "WHERE\n" +
            "\tttpc.test_plan_id = ?3\n" +
            "AND ttpc.flag IN (\n" +
            "\tSELECT\n" +
            "\t\ttur.flag\n" +
            "\tFROM\n" +
            "\t\tt_user_record tur\n" +
            "\tWHERE\n" +
            "\t\ttur.test_plan_id = ?3\n" +
            "\tAND phone = ?2\n" +
            "\tAND institution_no = ?1\n" +
            "\tGROUP BY\n" +
            "\t\ttur.flag\n" +
            ");", nativeQuery = true)
    List<TestPlanContendEntity> getUserDoTestPlan(String institutionNo, String phone, String testPlanId);

    @Query(value = "select count(ttpc.id)\n" +
            "                 from t_user_record tur\n" +
            "                          join t_test_plan_contend ttpc on tur.flag = ttpc.flag and tur.test_plan_id=ttpc.test_plan_id\n" +
            "                 where tur.test_plan_id = ?3\n" +
            "                   and phone = ?2\n" +
            "                   and institution_no = ?1\n", nativeQuery = true)
    int getUserDoTestPlanNum(String institutionNo, String phone, String testPlanId);


    @Query(value = "select count(ttpc.id)\n" +
            "from t_user_record\n" +
            "         join t_test_plan_contend ttpc on t_user_record.flag = ttpc.flag and t_user_record.test_plan_id=ttpc.test_plan_id\n" +
            "    where ttpc.test_plan_id=?1\n" +
            "    and ttpc.flag=?2\n", nativeQuery = true)
    int countHavingDoByFlag(String testPlanId, String flag);


    @Transactional
    @Modifying
    @Query(value = "delete from t_test_plan_contend where flag=:flag  ", nativeQuery = true)
    void deleteTestPlanByFlag(@Param("flag")String flag);

    @Query(value = "select count(1) from t_test_plan_contend where test_plan_id = :testPlanId",nativeQuery = true)
    int countContent(@Param("testPlanId") String  testPlanId);


    @Query(value = "select nsi.id,\n" +
            "       bid,\n" +
            "       institutionNo,\n" +
            "       description,\n" +
            "       nsi.ename,\n" +
            "       nsi.flag,\n" +
            "       imgeUrl,\n" +
            "       isHot,\n" +
            "       isNew,\n" +
            "       name,\n" +
            "       testNum,\n" +
            "       type,\n" +
            "       uploader,\n" +
            "       fileName,\n" +
            "       scoringPrinciple,\n" +
            "       status,\n" +
            "       scaleLimit,\n" +
            "       no,\n" +
            "       iconInfo\n" +
            "from (\n" +
            "         select tia.id,\n" +
            "                tia.b_id           bid,\n" +
            "                tia.institution_no institutionNo,\n" +
            "                description,\n" +
            "                ename,\n" +
            "                flag,\n" +
            "                image_url          imgeUrl,\n" +
            "                is_hot             isHot,\n" +
            "                is_new             isNew,\n" +
            "                name,\n" +
            "                test_num           testNum,\n" +
            "                tsi.type,\n" +
            "                uploader,\n" +
            "                file_name          fileName,\n" +
            "                scoring_principle  scoringPrinciple,\n" +
            "                status,\n" +
            "                scale_limit        scaleLimit,\n" +
            "                no,\n" +
            "                icon_info          iconInfo\n" +
            "         from t_institution_auth tia\n" +
            "                  join t_subject_info tsi on tia.b_id = tsi.id\n" +
            "         where institution_no = ?1 \n" +
            "           and tsi.type = ?2 and \n" +
            " if(?3 is not null and ?3 !='',(tsi.name like CONCAT('%',?3,'%')), 1=1)\n" +
            "           and not exists(select 1\n" +
            "                          from t_test_plan_contend ttpc\n" +
            "                          where ttpc.test_id = tia.b_id\n" +
            "                            and test_plan_id = ?4)\n" +
            "     ) nsi\n" +
            "         join t_category_subject tcs on nsi.flag = tcs.flag where tcs.ename=?5 ",
            countQuery = "select count(nsi.id)\n" +
                    "from (\n" +
                    "         select tia.id,\n" +
                    "                tia.b_id           bid,\n" +
                    "                tia.institution_no institutionNo,\n" +
                    "                description,\n" +
                    "                ename,\n" +
                    "                flag,\n" +
                    "                image_url          imgeUrl,\n" +
                    "                is_hot             isHot,\n" +
                    "                is_new             isNew,\n" +
                    "                name,\n" +
                    "                test_num           testNum,\n" +
                    "                tsi.type,\n" +
                    "                uploader,\n" +
                    "                file_name          fileName,\n" +
                    "                scoring_principle  scoringPrinciple,\n" +
                    "                status,\n" +
                    "                scale_limit        scaleLimit,\n" +
                    "                no,\n" +
                    "                icon_info          iconInfo\n" +
                    "         from t_institution_auth tia\n" +
                    "                  join t_subject_info tsi on tia.b_id = tsi.id\n" +
                    "         where institution_no = ?1 \n" +
                    "           and tsi.type = ?2 and \n" +
                    " if(?3 is not null and ?3!='',(tsi.name like CONCAT('%',?3,'%')), 1=1)\n" +
                    "           and not exists(select 1\n" +
                    "                          from t_test_plan_contend ttpc\n" +
                    "                          where ttpc.test_id = tia.b_id\n" +
                    "                            and test_plan_id = ?4)\n" +
                    "     ) nsi\n" +
                    "         join t_category_subject tcs on nsi.flag = tcs.flag where tcs.ename=?5 ", nativeQuery = true)
    Page<InstitutionAuthSubjectNameDto> getTestPlanNoSubTaskByCatagoryEnglinshName(String institutionNo, String type, String searchKey, String testPlanId, String categoryEname, Pageable pageable);


    @Query(value = "select nci.id,\n" +
            "       bId,\n" +
            "       institutionNo,\n" +
            "       nci.type,\n" +
            "       description,\n" +
            "       imageName,\n" +
            "       imageUrl,\n" +
            "       name,\n" +
            "       pageUrl,\n" +
            "       scaleLimit,\n" +
            "       status,\n" +
            "       nci.flag,\n" +
            "       testDescription,\n" +
            "       testExplain,\n" +
            "       testNum,\n" +
            "       no\n" +
            "from (select tia.id,\n" +
            "             b_id             bId,\n" +
            "             institution_no   institutionNo,\n" +
            "             tct.type,\n" +
            "             description,\n" +
            "             image_name       imageName,\n" +
            "             image_url        imageUrl,\n" +
            "             name,\n" +
            "             page_url         pageUrl,\n" +
            "             scale_limit      scaleLimit,\n" +
            "             status,\n" +
            "             task_code        flag,\n" +
            "             test_description testDescription,\n" +
            "             test_explain     testExplain,\n" +
            "             test_num         testNum,\n" +
            "             no\n" +
            "      from t_institution_auth tia\n" +
            "               join t_cognitive_task tct on tia.b_id = tct.id\n" +
            "      where institution_no = ?1 \n" +
            "        and if(?3 is not null and ?3 != '', (tct.name like CONCAT('%', ?3, '%')), 1 = 1)\n" +
            "        and tct.type = ?2 \n" +
            "        and not exists(select 1\n" +
            "                       from t_test_plan_contend ttpc\n" +
            "                       where ttpc.test_id = tia.b_id\n" +
            "                         and test_plan_id = ?4)\n" +
            "        and status = '0') nci\n" +
            "         join t_category_subject tcs on nci.flag = tcs.flag\n" +
            "where tcs.ename = ?5 ",
            countQuery = "select count(nci.id)\n" +
                    "from (select tia.id,\n" +
                    "             b_id             bId,\n" +
                    "             institution_no   institutionNo,\n" +
                    "             tct.type,\n" +
                    "             description,\n" +
                    "             image_name       imageName,\n" +
                    "             image_url        imageUrl,\n" +
                    "             name,\n" +
                    "             page_url         pageUrl,\n" +
                    "             scale_limit      scaleLimit,\n" +
                    "             status,\n" +
                    "             task_code        flag,\n" +
                    "             test_description testDescription,\n" +
                    "             test_explain     testExplain,\n" +
                    "             test_num         testNum,\n" +
                    "             no\n" +
                    "      from t_institution_auth tia\n" +
                    "               join t_cognitive_task tct on tia.b_id = tct.id\n" +
                    "      where institution_no = ?1 \n" +
                    "        and if(?3 is not null and ?3 != '', (tct.name like CONCAT('%', ?3, '%')), 1 = 1)\n" +
                    "        and tct.type = ?2 \n" +
                    "        and not exists(select 1\n" +
                    "                       from t_test_plan_contend ttpc\n" +
                    "                       where ttpc.test_id = tia.b_id\n" +
                    "                         and test_plan_id = ?4)\n" +
                    "        and status = '0') nci\n" +
                    "         join t_category_subject tcs on nci.flag = tcs.flag\n" +
                    "where tcs.ename = ?5 ", nativeQuery = true)
    Page<TestPlanAuthConTaskDto> getTestPlanNoConTaskByCatagoryEnglinshName(String institutionNo, String type, String searchKey, String testPlanId, String categoryEname, Pageable pageable);

}
