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

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

    /**
     * 根据flag获取全部评分规则
     *
     * @param flag
     * @return
     */
    @Query(value = "select id, flag, improvement_suggestions, score_end, score_start, symptom, name, name_explain, reference,standard_deviation,suggestion, is_total_score_explain, scoring_type from t_scale_marks where flag = :flag", nativeQuery = true)
    List<ScaleMarksEntity> getScaleMarksByFlag(@Param("flag") String flag);

    /**
     * 根据id修改量表
     *
     * @return
     */
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "UPDATE t_scale_marks SET symptom = :symptom, improvement_suggestions = :improvementSuggestions WHERE id = :id ", nativeQuery = true)
    void updateById(@Param("id") String id, @Param("symptom") String symptom, @Param("improvementSuggestions") String improvementSuggestions);

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

    /**
     * 根据id修改指定字段
     *
     * @return
     */
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "UPDATE t_scale_marks SET improvement_suggestions = :improvementSuggestions" +
            ", score_end = :scoreEnd" +
            ", score_start = :scoreStart" +
            ", name = :name" +
            ", name_explain = :nameExplain" +
            ", reference = :reference" +
            ", suggestion = :suggestion" +
            ", standard_deviation = :standardDeviation" +
            ", symptom = :symptom WHERE id = :id ", nativeQuery = true)
    void updateMark(@Param("name") String name, @Param("scoreStart") String scoreStart,@Param("scoreEnd")  String scoreEnd,@Param("symptom")  String symptom
            , @Param("improvementSuggestions") String improvementSuggestions, @Param("nameExplain") String nameExplain,@Param("reference") String reference
            ,@Param("suggestion") String suggestion,@Param("standardDeviation") String standardDeviation,@Param("id") String id);

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

    List<ScaleMarksEntity> findScaleMarksByFlagAndName(String flag, String name);
}
