package com.rf.psychological.institution.repository;

import com.rf.psychological.base.repository.BaseRepository;
import com.rf.psychological.dao.dto.InstitutionAuthConTaskDto;
import com.rf.psychological.dao.dto.InstitutionAuthSubjectNameDto;
import com.rf.psychological.institution.model.InstitutionAuthEntity;
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;

public interface InstitutionAuthRepository extends BaseRepository<InstitutionAuthEntity, String> {

    /**
     * 根据机构编号删除数据权限信息
     *
     * @return
     */
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "delete from t_institution_auth  where institution_no = :institutionNo ", nativeQuery = true)
    void deleteAuthDataByInstitutionNo(@Param("institutionNo") String institutionNo);

    @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,report_version reportVersion " +
            "from t_institution_auth tia join t_subject_info tsi on tia.b_id = tsi.id where " +
            "if(:searchKey is not null and :searchKey!='',(tsi.name like CONCAT('%',:searchKey,'%')), 1=1)" +
            "and institution_no =:institutionNo " +
            "and if(:warn is not null and :warn!='',(tsi.flag in(SELECT tdi.flag FROM t_dimension_info tdi GROUP BY tdi.flag) " +
            "and tsi.flag not in(SELECT tdo.flag FROM t_dimension_org tdo where tdo.institution_no =:institutionNo GROUP BY tdo.flag)), 1=1) " +
            "and tsi.type =:type order by tsi.test_num desc " +
            " limit :beginNum ,:pageSize", nativeQuery = true)
    List<InstitutionAuthSubjectNameDto> getAllByTableInAuthAndSubInfo(@Param("beginNum") int beginNum, @Param("pageSize") int pageSize, @Param("institutionNo") String institutionNo,
                                                                      @Param("type") String type, @Param("searchKey") String searchKey, @Param("warn") String warn);

    @Query(value = "select count(tia.id)" +
            "from t_institution_auth tia join t_subject_info tsi on tia.b_id = tsi.id where " +
            "if(:searchKey is not null and :searchKey!='',(tsi.name like CONCAT('%',:searchKey,'%')), 1=1)" +
            "and institution_no =:institutionNo " +
            "and if(:warn is not null and :warn!='',(tsi.flag in(SELECT tdi.flag FROM t_dimension_info tdi GROUP BY tdi.flag) " +
            "and tsi.flag not in(SELECT tdo.flag FROM t_dimension_org tdo where tdo.institution_no =:institutionNo GROUP BY tdo.flag)), 1=1) " +
            "and tsi.type =:type"
            , nativeQuery = true)
    int getAllByTableInAuthAndSubInfoNum(@Param("institutionNo") String institutionNo, @Param("type") String type, @Param("searchKey") String searchKey, @Param("warn") String warn);

    @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 taskCode, test_description testDescription, test_explain testExplain, test_num testNum, no," +
            "mobile_description mobileDescription,mobile_page_url mobilePageUrl,mobile_test_description mobileTestDescription,mobile_status mobileStatus " +
            "from t_institution_auth tia join t_cognitive_task tct on tia.b_id = tct.id where " +
            "if(:searchKey is not null and :searchKey!='',(tct.name like CONCAT('%',:searchKey,'%')), 1=1)" +
            "and institution_no =:institutionNo and " +
            "tct.type =:type and status='0' order by tct.test_num desc", nativeQuery = true)
    List<InstitutionAuthConTaskDto> getAllConTask(Pageable pageable, @Param("institutionNo") String institutionNo,
                                                  @Param("type") String type, @Param("searchKey") String searchKey);

    @Query(value = "select count(tia.id)" +
            "from t_institution_auth tia join t_cognitive_task tct on tia.b_id = tct.id where " +
            "if(:searchKey is not null and :searchKey!='',(tct.name like CONCAT('%',:searchKey,'%')), 1=1)" +
            "and institution_no =:institutionNo and " +
            "tct.type =:type and status='0' order by tct.test_num desc "
            , nativeQuery = true)
    int getAllConTaskNum(@Param("institutionNo") String institutionNo, @Param("type") String type, @Param("searchKey") String searchKey);


    @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 taskCode, test_description testDescription, test_explain testExplain, test_num testNum, no," +
            "mobile_description mobileDescription,mobile_page_url mobilePageUrl,mobile_test_description mobileTestDescription,mobile_status mobileStatus " +
            "from t_institution_auth tia join t_cognitive_task tct on tia.b_id = tct.id where " +
            "if(:searchKey is not null and :searchKey!='',(tct.name like CONCAT('%',:searchKey,'%')), 1=1)" +
            "and institution_no =:institutionNo and " +
            "tct.type =:type and status='0' and mobile_status= :mobileStatus order by tct.test_num desc", nativeQuery = true)
    List<InstitutionAuthConTaskDto> listTaskInMobile(Pageable pageable, @Param("institutionNo") String institutionNo,
                                                     @Param("type") String type, @Param("searchKey") String searchKey, String mobileStatus);

    @Query(value = "select count(tia.id)" +
            "from t_institution_auth tia join t_cognitive_task tct on tia.b_id = tct.id where " +
            "if(:searchKey is not null and :searchKey!='',(tct.name like CONCAT('%',:searchKey,'%')), 1=1)" +
            "and institution_no =:institutionNo and " +
            "tct.type =:type and status='0' and mobile_status= :mobileStatus order by tct.test_num desc "
            , nativeQuery = true)
    int countTaskInMobile(@Param("institutionNo") String institutionNo, @Param("type") String type, @Param("searchKey") String searchKey, String mobileStatus);

    /**
     * 根据insittutionNo和type删除权限
     *
     * @param institutionNo
     */
    @Transactional
    @Modifying(clearAutomatically = true)
    void deleteAllByInstitutionNoAndType(@Param("institutionNo") String institutionNo, @Param("type") String type);

    @Transactional
    @Modifying
    @Query(value = "delete from t_institution_auth where b_id in(SELECT id FROM t_subject_info WHERE flag=:flag)  ", nativeQuery = true)
    void deleteInstitutionByFlag(@Param("flag") String flag);
}
