package cn.hnthyy.thmz.mapper.thmz;

import cn.hnthyy.thmz.entity.thmz.Template;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 医疗范文 DAO
 */
public interface TemplateMapper {

    /**
     * 保存医疗范文
     *
     * @param template
     * @return
     */
    @Insert("INSERT INTO t_template(name,icd_text, template_type, template_data, create_id,dept_no,template_status,used_times,create_time,update_id,update_time) VALUES " +
            "(#{name},#{icdText}, #{templateType}, #{templateData}, #{createId},#{deptNo},#{templateStatus},0,#{createTime},#{updateId},#{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int insertTemplate(Template template);

    /**
     * 查询医生自己的，科室和全院的医疗范文 ，不查询内容
     *
     * @param createId
     * @param deptNo
     * @return
     */
    @Select({"<script>",
            "select id,name,icd_text, template_type, create_id,dept_no,template_status,used_times,create_time,update_id,update_time from t_template where template_status =0 and (template_type=2 or create_id=#{createId}",
            "<when test='deptNo!=null'>",
            "or dept_no =#{deptNo}",
            "</when>"
            , ")</script>"})
    List<Template> selectTemplate(@Param("createId") String createId, @Param("deptNo") String deptNo);

    /**
     * 按照id查询
     *
     * @param id
     * @return
     */
    @Select("select id,name,icd_text, template_type,template_data, create_id,dept_no,template_status,used_times,create_time,update_id,update_time from t_template where id=#{id}")
    Template selectById(@Param("id") Long id);

    /**
     * 更新医疗范文
     *
     * @param template
     * @return
     */
    @Update({"<script>",
            "update t_template ",
            "<trim prefix='set' prefixOverrides=',' suffix=' where id =#{id} ' >",
            "<when test='name!=null'>",
            ",name =#{name}",
            "</when>",
            "<when test='icdText!=null'>",
            ",icd_text =#{icdText}",
            "</when>",
            "<when test='templateType!=null'>",
            ",template_type =#{templateType}",
            "</when>",
            "<when test='templateStatus!=null'>",
            ",template_status =#{templateStatus}",
            "</when>",
            "<when test='templateData!=null'>",
            ",template_data =#{templateData}",
            "</when>",
            "<when test='deptNo!=null'>",
            ",dept_no =#{deptNo}",
            "</when>",
            "<when test='usedTimes!=null'>",
            ",used_times =#{usedTimes}",
            "</when>",
            "<when test='updateId!=null'>",
            ",update_id =#{updateId}",
            "</when>",
            "<when test='updateTime!=null'>",
            ",update_time =#{updateTime}",
            "</when>",
            "</trim>",
            "</script>"})
    int updateTemplateByKey(Template template);

}
