package com.rf.psychological.group.dao.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.group.dao.model.GroupAuthEntity;
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 26/10/2021下午4:35
 */
public interface GroupAuthRepository extends BaseRepository<GroupAuthEntity, String> {

    /**
     * 删除组的所有权限
     *
     * @param groupId
     */
    @Transactional
    @Modifying(clearAutomatically = true)
    void deleteAllByGroupId(String groupId);

    @Modifying
    @Transactional
    @Query(value = "delete from GroupAuthEntity where id in (?1)")
    void deleteGroupAuthBath(List<String> ids);

    /**
     * 当机构删除某一权限时，同样删除组权限内的含有这一权限
     * @param institutionNo
     * @param ids
     */
    @Modifying
    @Transactional
    @Query(value = "delete t_group_auth\n" +
            "from t_group_auth join t_group_info on t_group_auth.group_id=t_group_info.id\n" +
            "where institution_no=?1 and b_id in (?2)", nativeQuery = true)
    void deleteGroupAuthBecauseInstitutionDelete(String institutionNo, List<String> ids);


    /**
     * 删除机构下组里面的全部权限
     * @param institutionNo
     * @param type
     */
    @Modifying
    @Transactional
    @Query(value = "delete t_group_auth\n" +
            "from t_group_auth join t_group_info on t_group_auth.group_id=t_group_info.id\n" +
            "where institution_no=?1 and t_group_auth.type in (?2)", nativeQuery = true)
    void deleteGroupAuthByInstitutionNoAndType(String institutionNo, String type);


    @Query(value = "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,icon_info iconInfo\n" +
            "from t_institution_auth tia\n" +
            "         join t_subject_info tsi on tia.b_id = tsi.id\n" +
            "where\n" +
            "      if( ?3 is not null and  ?3 != '', (tsi.name like CONCAT('%',  ?3, '%')), 1 = 1) and\n" +
            " institution_no = ?1 and b_id not in (select b_id from t_group_auth where group_id = ?2)\n",
            countQuery = "select count(tia.id)\n" +
                    "from t_institution_auth tia\n" +
                    "         join t_subject_info tsi on tia.b_id = tsi.id\n" +
                    "where\n" +
                    "      if( ?3 is not null and  ?3 != '', (tsi.name like CONCAT('%',  ?3, '%')), 1 = 1) and\n" +
                    " institution_no = ?1 and b_id not in (select b_id from t_group_auth where group_id = ?2)\n", nativeQuery = true)
    Page<InstitutionAuthSubjectNameDto> findGroupNoAuthList(String institutionNo, String groupId, String searchKey, 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 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( ?3 is not null and  ?3!='',(tct.name like CONCAT('%', ?3,'%')), 1=1)" +
            "and institution_no =?1  and b_id not in (select b_id from t_group_auth where group_id = ?2) and status='0'\n",
            countQuery = "select count(tia.id) " +
                    "from t_institution_auth tia join t_cognitive_task tct on tia.b_id = tct.id where " +
                    "if( ?3 is not null and  ?3!='',(tct.name like CONCAT('%', ?3,'%')), 1=1)" +
                    "and institution_no =?1  and b_id not in (select b_id from t_group_auth where group_id = ?2) and status='0'\n", nativeQuery = true)
    Page<InstitutionAuthConTaskDto> findGroupNoAuthCoTaskList(String institutionNo, String groupId, String searchKey, Pageable pageable);

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