package com.rf.psychological.institution.repository;

import com.rf.psychological.base.repository.BaseRepository;
import com.rf.psychological.institution.model.InstitutionUserEntity;
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 InstitutionUserRepository extends BaseRepository<InstitutionUserEntity, String> {

    /**
     * 根据机构编号
     *
     * @return
     */
    @Query(value = "select id, institution_name, institution_no, password, gender, pet_name, phone, birthday, profession, addition_info, user_status from t_institution_user where institution_no = :institutionNo ", nativeQuery = true)
    InstitutionUserEntity findInstitutionByNo(@Param("institutionNo") String institutionNo);

    /**
     * 根据手机号登录
     *
     * @param phone
     * @param password
     * @return
     */
    InstitutionUserEntity findByPhoneAndPassword(String phone, String password);

    /**
     * 根据手机号修改密码
     *
     * @param phone
     * @param password
     */
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "update t_institution_user set password = :password where phone = :phone and institution_no=:institutionNo", nativeQuery = true)
    void updatePassword(@Param("phone") String phone, @Param("password") String password, @Param("institutionNo") String institutionNo);

    /**
     * 根据手机号
     *
     * @param phone
     * @return
     */
    InstitutionUserEntity findByPhone(String phone);

    InstitutionUserEntity findByInstitutionNoAndPhone(String institutionNo, String phone);

    /**
     * 分页查询所有机构管理员信息
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Query(value = "select id, institution_name, institution_no, password, gender, pet_name, phone, birthday, profession, addition_info, user_status from t_institution_user limit :pageNum ,:pageSize ", nativeQuery = true)
    List<InstitutionUserEntity> findAll(@Param("pageNum") int pageNum, @Param("pageSize") int pageSize);

    /**
     * 可以模糊查询所有机构管理员
     *
     * @param pageNum
     * @param pageSize
     * @param searchKey
     * @return
     */
    @Query(value = "select id, institution_name, institution_no, password, gender, pet_name, phone, birthday, profession, addition_info, user_status from t_institution_user  where phone like %:searchKey% or institution_name like %:searchKey%   limit :pageNum ,:pageSize", nativeQuery = true)
    List<InstitutionUserEntity> findAllByName(@Param("pageNum") int pageNum, @Param("pageSize") int pageSize, @Param("searchKey") String searchKey);

    @Query(value = "select count(1) from t_institution_user  where phone like %:searchKey% or institution_name like %:searchKey%", nativeQuery = true)
    int allNum(@Param("searchKey") String searchKey);

    @Query(value = "select id, institution_name, institution_no, password, gender, pet_name, phone, birthday, profession, addition_info, user_status from t_institution_user  where institution_no =:institutionNo and" +
            " if(:searchKey is not null and :searchKey!='',(pet_name like CONCAT('%',:searchKey,'%') or phone like CONCAT('%',:searchKey,'%') ) ,1=1)  limit :pageNum ,:pageSize", nativeQuery = true)
    List<InstitutionUserEntity> findAllByInNo(@Param("pageNum") int pageNum, @Param("pageSize") int pageSize, @Param("institutionNo") String institutionNo, @Param("searchKey") String searchKey);

    @Query(value = "select count(1) from t_institution_user  where institution_no =:institutionNo and" +
            " if(:searchKey is not null and :searchKey!='',(pet_name like CONCAT('%',:searchKey,'%') or phone like CONCAT('%',:searchKey,'%') ) ,1=1)", nativeQuery = true)
    int numAllByInNo(@Param("institutionNo") String institutionNo, @Param("searchKey") String searchKey);

    /**
     * 根据手机号删除机构用户
     *
     * @param phone
     */
    @Transactional
    @Modifying(clearAutomatically = true)
    void deleteByPhoneAndInstitutionNo(String phone, String institutionNo);


    /**
     * 审核机构用户
     *
     * @param phone
     * @param userStatus
     */
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "update t_institution_user set user_status = :userStatus where phone = :phone and institution_no=:institutionNo", nativeQuery = true)
    void updateUserStatusByPhone(@Param("phone") String phone, @Param("userStatus") String userStatus, @Param("institutionNo") String institutionNo);

    /**
     * 根据机构编号删除
     *
     * @param institutionNo
     */
    @Transactional
    @Modifying(clearAutomatically = true)
    void deleteAllByInstitutionNo(String institutionNo);
}
