<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.kmall.admin.dao.SysNoticeUserDao">

    <resultMap type="com.kmall.admin.entity.SysNoticeUserEntity" id="sysNoticeUserMap">
        <result property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="noticeId" column="notice_id"/>
        <result property="status" column="status"/>
        <result property="remark" column="remark"/>
    </resultMap>

	<select id="queryObject" resultType="com.kmall.admin.entity.SysNoticeUserEntity">
		select
			`id`,
			`user_id`,
			`notice_id`,
			`status`,
			`remark`
		from sys_notice_user
		where id = #{id}
	</select>

	<select id="queryList" resultType="com.kmall.admin.entity.SysNoticeUserEntity">
		select
		sys_notice_user.id,
		sys_notice_user.user_id,
		sys_notice_user.notice_id,
		sys_notice_user.status,
		sys_notice_user.remark,
		sys_user.username user_name,
		sys_notice.context notice_content
		from sys_notice_user
		LEFT JOIN sys_user on sys_notice_user.user_id = sys_user.user_id
		LEFT JOIN sys_notice ON sys_notice_user.notice_id = sys_notice.id
		WHERE 1=1
		<if test="username != null and username.trim() != ''">
			AND sys_user.username LIKE concat('%',#{username},'%')
		</if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
			<otherwise>
                order by id desc
			</otherwise>
        </choose>
		<if test="offset != null and limit != null">
			limit #{offset}, #{limit}
		</if>
	</select>
	
 	<select id="queryTotal" resultType="int">
		select count(*) from sys_notice_user
		LEFT JOIN sys_user on sys_notice_user.user_id = sys_user.user_id
		WHERE 1=1
		<if test="username != null and username.trim() != ''">
			AND sys_user.username LIKE concat('%',#{username},'%')
		</if>
	</select>
	 
	<insert id="save" parameterType="com.kmall.admin.entity.SysNoticeUserEntity">
		insert into sys_notice_user(
			`id`,
			`user_id`,
			`notice_id`,
			`status`,
			`remark`)
		values(
			#{id},
			#{userId},
			#{noticeId},
			#{status},
			#{remark})
	</insert>
	 
	<update id="update" parameterType="com.kmall.admin.entity.SysNoticeUserEntity">
		update sys_notice_user 
		<set>
			<if test="userId != null">`user_id` = #{userId}, </if>
			<if test="noticeId != null">`notice_id` = #{noticeId}, </if>
			<if test="status != null">`status` = #{status}, </if>
			<if test="remark != null">`remark` = #{remark}</if>
		</set>
		where id = #{id}
	</update>
	
	<delete id="delete">
		delete from sys_notice_user where id = #{value}
	</delete>
	
	<delete id="deleteBatch">
		delete from sys_notice_user where id in 
		<foreach item="id" collection="array" open="(" separator="," close=")">
			#{id}
		</foreach>
	</delete>

</mapper>