package com.rf.psychological.dao.repository;

import com.rf.psychological.base.repository.BaseRepository;
import com.rf.psychological.dao.model.MemoryCheckEntity;
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 zsy
 * @description:
 * @date 2021/5/14 10:54
 */
public interface MemoryCheckRepository extends BaseRepository<MemoryCheckEntity,String> {

    /**
     * 根据关联记忆id修改既时记忆状态
     * @param correlationMemory
     */
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value="update t_memory_check set status = '0' where id = :correlationMemory ",nativeQuery=true)
    void updateStatus(@Param("correlationMemory")String correlationMemory);

    /**
     * 根据phone获取用户进行中记忆检查
     * @param phone
     */
    @Query(value="select * from t_memory_check where status = '1' and if(:questionNo is not null and :questionNo!='',question_no = :questionNo ,1=1) " +
            "and type = :type and test_type = :testType and binary phone = :phone and institution_no = :institutionNo",nativeQuery=true)
    List<MemoryCheckEntity> getMemoryCheckList(@Param("phone") String phone,@Param("type") String type,@Param("testType") String testType
            ,@Param("questionNo") String questionNo,@Param("institutionNo") String institutionNo);
    /**
     * 根据id获取用户记忆检查信息
     * @param correlationMemory
     */
    @Query(value="select * from t_memory_check where id = :correlationMemory",nativeQuery=true)
    MemoryCheckEntity selectById(@Param("correlationMemory")String correlationMemory);

    /**
     * 查询当前用户第六题信息
     * @param
     */
    @Query(value="select * from t_memory_check where status = '1' and binary phone = :phone and institution_no = :institutionNo and correlation_emory = :correlationMemory",nativeQuery=true)
    MemoryCheckEntity selectMemory(@Param("phone") String phone, @Param("correlationMemory")String correlationMemory, @Param("institutionNo")String institutionNo);

    /**
     * 清除进行中任务
     * @param
     */
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value="update t_memory_check set status = '0' where status = '1' and if(:questionNo is not null and :questionNo!='',question_no = :questionNo ,1=1) " +
            "and type = :type and test_type = :testType and binary phone = :phone and institution_no = :institutionNo",nativeQuery=true)
    void clearMemory(@Param("phone") String phone,@Param("type") String type,@Param("testType") String testType,@Param("questionNo") String questionNo,@Param("institutionNo") String institutionNo);

}
