<?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.xingxi.business.News.mapper.NewsMapper">
    <resultMap type="News" id="NewsResult">
        <result property="newsId"    column="newsId"    />
        <result property="newsContext"    column="newsContext"    />
        <result property="delFlag"    column="delFlag"    />
        <result property="createUser"    column="createUser"    />
        <result property="createTime"    column="createTime"    />
        <result property="updateUser"    column="updateUser"    />
        <result property="updateTime"    column="updateTime"    />
    </resultMap>

    <sql id="selectNews">
        select newsId, newsContext, delFlag, createUser, createTime, updateUser, updateTime from t_news
    </sql>

    <sql id="selectNewsExt">
        select
            ${tableAlias}.newsId,
            ${tableAlias}.newsContext,
            ${tableAlias}.delFlag,
            ${tableAlias}.createUser,
            ${tableAlias}.createTime,
            ${tableAlias}.updateUser,
            ${tableAlias}.updateTime
        from t_news ${tableAlias}
    </sql>

    <select id="selectNewsList" parameterType="News" resultMap="NewsResult">
        <include refid="selectNews"/>
        <where>  
            <if test="newsContext != null  and newsContext != ''"> and newsContext = #{newsContext}</if>
            <if test="delFlag != null  and delFlag != ''"> and delFlag = #{delFlag}</if>
            <if test="createUser != null  and createUser != ''"> and createUser = #{createUser}</if>
            <if test="createTime != null "> and createTime = #{createTime}</if>
            <if test="updateUser != null  and updateUser != ''"> and updateUser = #{updateUser}</if>
            <if test="updateTime != null "> and updateTime = #{updateTime}</if>
        </where>
    </select>
    
    <select id="selectNewsByNewsId" parameterType="Long" resultMap="NewsResult">
        <include refid="selectNews"/>
        where newsId = #{newsId}
    </select>
        
    <insert id="insertNews" parameterType="News" useGeneratedKeys="true" keyProperty="newsId">
        insert into t_news
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="newsContext != null">newsContext,</if>
            <if test="delFlag != null">delFlag,</if>
            <if test="createUser != null">createUser,</if>
            <if test="createTime != null">createTime,</if>
            <if test="updateUser != null">updateUser,</if>
            <if test="updateTime != null">updateTime,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="newsContext != null">#{newsContext},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createUser != null">#{createUser},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateUser != null">#{updateUser},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <insert id="batchInsertNews">
        insert into t_news( newsContext, delFlag, createUser, createTime, updateUser, updateTime) values
        <foreach item="item" index="index" collection="list" separator=",">
            ( #{item.newsContext}, #{item.delFlag}, #{item.createUser}, #{item.createTime}, #{item.updateUser}, #{item.updateTime})
        </foreach>
    </insert>

    <update id="updateNews" parameterType="News">
        update t_news
        <trim prefix="SET" suffixOverrides=",">
            <if test="newsContext != null">newsContext = #{newsContext},</if>
            <if test="delFlag != null">delFlag = #{delFlag},</if>
            <if test="createUser != null">createUser = #{createUser},</if>
            <if test="createTime != null">createTime = #{createTime},</if>
            <if test="updateUser != null">updateUser = #{updateUser},</if>
            <if test="updateTime != null">updateTime = #{updateTime},</if>
        </trim>
        where newsId = #{newsId}
    </update>

    <delete id="deleteNewsByNewsId" parameterType="Long">
        delete from t_news where newsId = #{newsId}
    </delete>

    <delete id="deleteNewsByNewsIds" parameterType="String">
        delete from t_news where newsId in 
        <foreach item="newsId" collection="array" open="(" separator="," close=")">
            #{newsId}
        </foreach>
    </delete>

    <update id="logicDeleteNewsByNewsId" parameterType="Long">
        update t_news
            set delFlag = '1'
        where newsId = #{newsId}
    </update>

    <update id="logicDeleteNewsByNewsIds" parameterType="String">
        update t_news
        set delFlag = '1'
        where newsId in
        <foreach item="newsId" collection="array" open="(" separator="," close=")">
            #{newsId}
        </foreach>
    </update>

    <update id="logicDeleteNewsByCondition" parameterType="News">
        update t_news
        set delFlag = '1'
        <where>
            <if test="newsContext != null  and newsContext != ''"> and newsContext = #{newsContext}</if>
            <if test="delFlag != null  and delFlag != ''"> and delFlag = #{delFlag}</if>
            <if test="createUser != null  and createUser != ''"> and createUser = #{createUser}</if>
            <if test="createTime != null "> and createTime = #{createTime}</if>
            <if test="updateUser != null  and updateUser != ''"> and updateUser = #{updateUser}</if>
            <if test="updateTime != null "> and updateTime = #{updateTime}</if>
        </where>
    </update>
</mapper>