<?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.sv.mapper.SmsMapper">

    <resultMap id="SmsMap" type="com.common.Sms">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="platform" property="platform" jdbcType="TINYINT"/>
        <result column="status" property="status" jdbcType="TINYINT"/>
        <result column="type" property="type" jdbcType="TINYINT"/>
        <result column="mobile" property="mobile" jdbcType="CHAR"/>
        <result column="content" property="content" jdbcType="VARCHAR"/>
        <result column="created_id" property="createdId" jdbcType="INTEGER"/>
        <result column="modified_id" property="modifiedId" jdbcType="INTEGER"/>
        <result column="created_time" property="createdTime" jdbcType="TIMESTAMP"/>
        <result column="modified_time" property="modifiedTime" jdbcType="TIMESTAMP"/>
        <result column="deleted" property="deleted" jdbcType="TINYINT"/>
    </resultMap>

    <resultMap id="SmsDtoMap" type="com.sv.dto.api.SmsDTO">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="platform" property="platform" jdbcType="TINYINT"/>
        <result column="status" property="status" jdbcType="TINYINT"/>
        <result column="type" property="type" jdbcType="TINYINT"/>
        <result column="mobile" property="mobile" jdbcType="CHAR"/>
        <result column="content" property="content" jdbcType="VARCHAR"/>
        <!--<result column="created_id" property="createdId" jdbcType="INTEGER"/>-->
        <!--<result column="modified_id" property="modifiedId" jdbcType="INTEGER"/>-->
        <result column="created_time" property="createdTime" jdbcType="TIMESTAMP"/>
        <!--<result column="modified_time" property="modifiedTime" jdbcType="TIMESTAMP"/>-->
        <!--<result column="deleted" property="deleted" jdbcType="TINYINT"/>-->
    </resultMap>

    <!--表名 -->
    <sql id="tableName">
        sv_sms
    </sql>

    <!-- 字段 -->
    <sql id="Field">
        id,
        platform,
        status,
        type,
        mobile,
        content,
        created_id,
        modified_id,
        created_time,
        modified_time,
        deleted
    </sql>

    <!-- 字段值 -->
    <sql id="FieldValue">
        #{id, jdbcType=INTEGER},
        #{platform, jdbcType=TINYINT},
        #{status, jdbcType=TINYINT},
        #{type, jdbcType=TINYINT},
        #{mobile, jdbcType=CHAR},
        #{content, jdbcType=VARCHAR},
        #{createdId, jdbcType=INTEGER},
        #{modifiedId, jdbcType=INTEGER},
        #{createdTime, jdbcType=TIMESTAMP},
        #{modifiedTime, jdbcType=TIMESTAMP},
        #{deleted, jdbcType=TINYINT}
    </sql>

    <!-- 查询全部记录 -->
    <select id="findAll" resultMap="SmsMap">
        SELECT
        <include refid="Field"></include>
        FROM
        <include refid="tableName"></include>
        WHERE
        deleted = 0
    </select>

    <!-- 新增-->
    <insert id="insert" parameterType="com.common.Sms" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO
        <include refid="tableName"></include>
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="platform != null">
                platform,
            </if>
            <if test="status != null">
                status,
            </if>
            <if test="type != null">
                type,
            </if>
            <if test="mobile != null">
                mobile,
            </if>
            <if test="content != null">
                content,
            </if>
            <if test="createdId != null">
                created_id,
            </if>
            <if test="modifiedId != null">
                modified_id,
            </if>
            created_time,
            modified_time,
            <if test="deleted != null">
                deleted,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id},
            </if>
            <if test="platform != null">
                #{platform},
            </if>
            <if test="status != null">
                #{status},
            </if>
            <if test="type != null">
                #{type},
            </if>
            <if test="mobile != null">
                #{mobile},
            </if>
            <if test="content != null">
                #{content},
            </if>
            <if test="createdId != null">
                #{createdId},
            </if>
            <if test="modifiedId != null">
                #{modifiedId},
            </if>
            CURRENT_TIMESTAMP,
            CURRENT_TIMESTAMP,
            <if test="deleted != null">
                #{deleted},
            </if>
        </trim>
    </insert>

    <!-- 删除-->
    <delete id="delete" parameterType="java.lang.Integer">
        UPDATE
        <include refid="tableName"></include>
        SET
        deleted = 1
        WHERE
        id = #{id}
    </delete>

    <!-- 批量删除 -->
    <delete id="deleteByIds">
        UPDATE
        <include refid="tableName"></include>
        SET
        deleted = 1
        WHERE
        id IN
        <foreach item="item" index="index" collection="array" open="(" separator="," close=")">
            #{item}
        </foreach>
    </delete>

    <!-- 更新记录 -->
    <update id="update" parameterType="com.common.Sms">
        UPDATE
        <include refid="tableName"></include>
        <set>
            <if test="platform != null">
                platform = #{platform},
            </if>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="type != null">
                type = #{type},
            </if>
            <if test="mobile != null">
                mobile = #{mobile},
            </if>
            <if test="content != null">
                content = #{content},
            </if>
            <if test="createdId != null">
                created_id = #{createdId},
            </if>
            <if test="modifiedId != null">
                modified_id = #{modifiedId},
            </if>

            modified_time = CURRENT_TIMESTAMP,
            <if test="deleted != null">
                deleted = #{deleted},
            </if>
        </set>
        WHERE id = #{id}
    </update>

    <!-- 通过编号查询 -->
    <select id="findById" parameterType="java.lang.Integer" resultMap="SmsMap">
        SELECT
        <include refid="Field"></include>
        FROM
        <include refid="tableName"></include>
        WHERE id = #{id}
    </select>

    <!--更新短信状态-->
    <update id="updateStatus">
        UPDATE
        <include refid="tableName"></include>
        SET
        status = #{status, jdbcType=TINYINT}
        WHERE
        id
        IN
        <foreach collection="ids" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </update>


</mapper>