package com.nursinghome.mapper;

import com.nursinghome.entity.Notice;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface NoticeMapper {
    @Insert("INSERT INTO notice(title, content, target_role, target_user_id, created_by, created_at) VALUES(#{title}, #{content}, #{targetRole}, #{targetUserId}, #{createdBy}, NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Notice notice);

    @Select("<script>SELECT * FROM notice WHERE 1=1 <if test='targetUserId != null'> AND (target_user_id = #{targetUserId} OR target_user_id IS NULL)</if> <if test='targetRole != null'> AND (target_role = #{targetRole} OR target_role = 'ALL')</if> ORDER BY created_at DESC</script>")
    List<Notice> listByTarget(@Param("targetRole") String targetRole, @Param("targetUserId") Long targetUserId);
}
