<?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.railway.business.push.mapper.BusPushMsgMapper">

    <resultMap id="BaseResultMap" type="com.railway.business.push.domain.BusPushMsg">
        <result column="id" property="id"/>
        <result column="user_id" property="userId"/>
        <result column="client_id" property="clientId"/>
        <result column="client_type" property="clientType"/>
        <result column="title" property="title"/>
        <result column="content" property="content"/>
        <result column="ios_alert_msg" property="iosAlertMsg"/>
        <result column="create_time" property="createTime"/>
        <result column="push_time" property="pushTime"/>
        <result column="push_status" property="pushStatus"/>
        <result column="push_result_desc" property="pushResultDesc"/>
        <result column="push_loop" property="pushLoop"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,
                user_id,
                client_id,
                client_type,
                title,
                content,
                ios_alert_msg,
                create_time,
                push_time,
                push_status,
                push_result_desc,
                push_loop
    </sql>

    <insert id="insert" parameterType="com.railway.business.push.domain.BusPushMsg">
        <selectKey keyProperty="id" order="AFTER" resultType="Long">
            select @@IDENTITY as id
        </selectKey>
        INSERT INTO bus_push_msg
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test ='null != userId'>
                user_id,
            </if>
            <if test ='null != clientId'>
                client_id,
            </if>
            <if test ='null != clientType'>
                client_type,
            </if>
            <if test ='null != title'>
                title,
            </if>
            <if test ='null != content'>
                content,
            </if>
            <if test ='null != iosAlertMsg'>
                ios_alert_msg,
            </if>
            <if test ='null != createTime'>
                create_time,
            </if>
            <if test ='null != pushTime'>
                push_time,
            </if>
            <if test ='null != pushStatus'>
                push_status,
            </if>
            <if test ='null != pushResultDesc'>
                push_result_desc,
            </if>
            <if test ='null != pushLoop'>
                push_loop
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test ='null != userId'>
                #{userId},
            </if>
            <if test ='null != clientId'>
                #{clientId},
            </if>
            <if test ='null != clientType'>
                #{clientType},
            </if>
            <if test ='null != title'>
                #{title},
            </if>
            <if test ='null != content'>
                #{content},
            </if>
            <if test ='null != iosAlertMsg'>
                #{iosAlertMsg},
            </if>
            <if test ='null != createTime'>
                #{createTime},
            </if>
            <if test ='null != pushTime'>
                #{pushTime},
            </if>
            <if test ='null != pushStatus'>
                #{pushStatus},
            </if>
            <if test ='null != pushResultDesc'>
                #{pushResultDesc},
            </if>
            <if test ='null != pushLoop'>
                #{pushLoop}
            </if>
        </trim>
    </insert>

    <delete id="delete">
        DELETE FROM bus_push_msg
        WHERE id = #{id}
    </delete>

    <update id="update" parameterType="com.railway.business.push.domain.BusPushMsg">
        UPDATE bus_push_msg
        <set>
            <if test ='null != userId'>user_id = #{userId},</if>
            <if test ='null != clientId'>client_id = #{clientId},</if>
            <if test ='null != clientType'>client_type = #{clientType},</if>
            <if test ='null != title'>title = #{title},</if>
            <if test ='null != content'>content = #{content},</if>
            <if test ='null != iosAlertMsg'>ios_alert_msg = #{iosAlertMsg},</if>
            <if test ='null != createTime'>create_time = #{createTime},</if>
            <if test ='null != pushTime'>push_time = #{pushTime},</if>
            <if test ='null != pushStatus'>push_status = #{pushStatus},</if>
            <if test ='null != pushResultDesc'>push_result_desc = #{pushResultDesc},</if>
            <if test ='null != pushLoop'>push_loop = #{pushLoop}</if>
        </set>
        WHERE id = #{id}
    </update>


    <select id="getInfo" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM bus_push_msg
        WHERE id = #{id}

    </select>

    <select id="getList" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM bus_push_msg
        <where>
            <if test="userId!=null and userId!=''">
                and user_id=#{userId}
            </if>
            <if test="clientId!=null and clientId!=''">
                and client_id=#{clientId}
            </if>
            <if test="clientType!=null and clientType!=''">
                and client_type=#{clientType}
            </if>
            <if test="title!=null and title!=''">
                and title=#{title}
            </if>
            <if test="content!=null and content!=''">
                and content=#{content}
            </if>
            <if test="iosAlertMsg!=null and iosAlertMsg!=''">
                and ios_alert_msg=#{iosAlertMsg}
            </if>
            <if test="createTime!=null">
                and DATE_FORMAT(t.create_time, '%Y-%m-%d') = #{createTime}
            </if>
            <if test="pushTime!=null">
                and DATE_FORMAT(t.push_time, '%Y-%m-%d') = #{pushTime}
            </if>
            <if test="pushStatus!=null and pushStatus!=''">
                and push_status=#{pushStatus}
            </if>
            <if test="pushResultDesc!=null and pushResultDesc!=''">
                and push_result_desc=#{pushResultDesc}
            </if>
            <if test="pushLoop!=null and pushLoop!=''">
                and push_loop=#{pushLoop}
            </if>
        </where>
    </select>

    <select id="selectAppMsg" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM bus_push_msg
        where (push_status = '0' or push_status = '2')
        <![CDATA[
            and push_loop < 6
        ]]>
    </select>

    <select id="getListTop5" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM bus_push_msg
        <where>
            <if test="userId!=null and userId!=''">
                and user_id=#{userId}
            </if>
            <if test="pushStatus!=null and pushStatus!=''">
                and push_status=#{pushStatus}
            </if>
        </where>
        ORDER BY push_time DESC
        LIMIT 5
    </select>

</mapper>