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

import com.rf.psychological.base.repository.BaseRepository;
import com.rf.psychological.dao.model.AnswerEntity;
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 AnswerRepository extends BaseRepository<AnswerEntity, String> {


    /**
     * 根据flag获取全部答案
     *
     * @param flag
     * @return
     */
    @Query(value = "select id,question_no,name,options,question_type,score,score,score_direction,flag from t_answer_info where flag = :flag", nativeQuery = true)
    List<AnswerEntity> getAnswerByFlag(@Param("flag") String flag);

    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "delete from t_answer_info", nativeQuery = true)
    void deleteAll();

    /**
     * 根据id修改指定字段
     *
     * @return
     */
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "UPDATE t_answer_info SET name = :name, score = :score WHERE id = :id ", nativeQuery = true)
    void updateAnswer(@Param("name") String name, @Param("score") String score, @Param("id") String id);

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