package thyyxxk.webserver.dao.his.dictionary;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.*;
import thyyxxk.webserver.entity.zhuyuanyisheng.emr.EmrDataExtract;
import thyyxxk.webserver.entity.zhuyuanyisheng.emr.EmrDataMaintenance;

import java.util.List;
import java.util.Map;

@Mapper
public interface EmrDataMaintenanceDao {

    @Select("select * from emr_data_maintenance with(nolock) where del_flag = 0 and data_type in (0,1) ")
    IPage<EmrDataMaintenance> getElectronicMedicalRecordDataElementSql(IPage<EmrDataMaintenance> page);

    @Select("${sql}")
    Map<String, Object> testSql(@Param("sql") String sql);


    @Select("${sql}")
    List<Map<String, Object>> testSqlList(@Param("sql") String sql);

    @Insert("insert into emr_data_maintenance (name, sql_sentence, input_id,data_type) " +
            "values (#{name}, #{sqlSentence}, #{inputId},#{dataType})")
    void addData(@Param("name") String name,
                 @Param("sqlSentence") String sqlSentence,
                 @Param("inputId") String inputId,
                 @Param("dataType") Integer dataType);

    @Update("update emr_data_maintenance set del_flag = 1 , modified_by = #{userId} ,modified_date = getdate() " +
            "where id = #{id}")
    void deleteById(@Param("id") Integer id,
                    @Param("userId") String userId);

    @Update("update emr_data_maintenance " +
            "set name = #{param.name} , sql_sentence = #{param.sqlSentence} , modified_by = #{userId}" +
            " , modified_date = getdate()," +
            "   data_type = #{param.dataType}," +
            "   group_name = #{param.groupName} " +
            "where id = #{param.id} ")
    void updateData(@Param("param") EmrDataMaintenance emrDataMaintenance,
                    @Param("userId") String userId);


    @Select("select id, " +
            "       name, " +
            "       sql_sentence, " +
            "       del_flag, " +
            "       input_id, " +
            "       input_date, " +
            "       modified_by, " +
            "       modified_date," +
            "       data_type," +
            "       group_name " +
            "from emr_data_maintenance where del_flag = 0 and data_type in (0,1) ")
    List<EmrDataMaintenance> getEmrInpatientData();


    @Select("select * from emr_data_extract")
    List<EmrDataExtract> getExtractDataSource();

    @Update("UPDATE emr_data_extract SET emr_category_code = #{param.emrCategoryCode}," +
            "data_extract =#{param.dataExtract},name = #{param.name} WHERE emr_category_code = #{param.oldEmrCategoryCode} ")
    void updateExtractDataSource(@Param("param") EmrDataExtract param);

    @Delete("delete emr_data_extract where emr_category_code = #{code}")
    void deleteExtractDataSource(String code);

    @Select("insert into emr_data_extract (emr_category_code, data_extract, name) values (#{param.emrCategoryCode},#{param.dataExtract},#{param.name})")
    void insertExtractDataSource(@Param("param") EmrDataExtract param);


}
