<?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="ink.wgink.module.sms.dao.sms.ISmsDao">

    <resultMap id="smsDTO" type="ink.wgink.module.sms.pojo.dtos.sms.SmsDTO">
        <id column="sms_id" property="smsId"/>
        <result column="user_id" property="userId"/>
        <result column="phone" property="phone"/>
        <result column="user_name" property="userName"/>
        <result column="content" property="content"/>
        <result column="send_status" property="sendStatus"/>
        <result column="error_message" property="errorMessage"/>
        <result column="gmt_create" property="gmtCreate"/>
    </resultMap>

    <resultMap id="smsPO" type="ink.wgink.module.sms.pojo.pos.sms.SmsPO">
        <id column="sms_id" property="smsId"/>
        <result column="user_id" property="userId"/>
        <result column="phone" property="phone"/>
        <result column="user_name" property="userName"/>
        <result column="content" property="content"/>
        <result column="send_status" property="sendStatus"/>
        <result column="error_message" property="errorMessage"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="creator" property="creator"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="modifier" property="modifier"/>
        <result column="is_delete" property="isDelete"/>
    </resultMap>

    <!-- 建表 -->
    <update id="createTable">
        CREATE TABLE IF NOT EXISTS `sms_sms` (
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增主键',
            `sms_id` char(36) NOT NULL COMMENT '主键',
            `user_id` char(36) DEFAULT NULL COMMENT '用户ID',
            `phone` varchar(255) DEFAULT NULL COMMENT '电话',
            `user_name` varchar(255) DEFAULT NULL COMMENT '昵称',
            `content` varchar(255) DEFAULT NULL COMMENT '发送内容',
            `send_status` int(11) DEFAULT NULL COMMENT '发送状态',
            `error_message` varchar(255) DEFAULT NULL COMMENT '错误信息',
            `creator` char(36) DEFAULT NULL,
            `gmt_create` datetime DEFAULT NULL,
            `modifier` char(36) DEFAULT NULL,
            `gmt_modified` datetime DEFAULT NULL,
            `is_delete` int(1) DEFAULT '0',
            PRIMARY KEY (`id`,`sms_id`),
            KEY `sms_id` (`sms_id`),
            KEY `phone` (`phone`),
            KEY `send_status` (`send_status`),
            KEY `is_delete` (`is_delete`)
        ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
    </update>

    <!-- 新增短信 -->
    <insert id="save" parameterType="map">
        INSERT INTO sms_sms(
            sms_id,
            user_id,
            phone,
            user_name,
            content,
            send_status,
            error_message,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{smsId},
            #{userId},
            #{phone},
            #{userName},
            #{content},
            #{sendStatus},
            #{errorMessage},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

    <!-- 删除短信 -->
    <update id="remove" parameterType="map">
        UPDATE
            sms_sms
        SET
            is_delete = 1,
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            sms_id IN
        <foreach collection="smsIds" index="index" open="(" separator="," close=")">
            #{smsIds[${index}]}
        </foreach>
    </update>

    <!-- 修改短信 -->
    <update id="update" parameterType="map">
        UPDATE
            sms_sms
        SET
        <if test="phone != null and phone != ''">
            phone = #{phone},
        </if>
        <if test="content != null and content != ''">
            content = #{content},
        </if>
        <if test="sendStatus != null">
            send_status = #{sendStatus},
        </if>
        <if test="errorMessage != null and errorMessage != ''">
            error_message = #{errorMessage},
        </if>
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            sms_id = #{smsId}
    </update>

    <!-- 短信详情 -->
    <select id="get" parameterType="map" resultMap="smsDTO">
        SELECT
            t1.user_id,
            t1.phone,
            t1.user_name,
            t1.content,
            t1.send_status,
            t1.error_message,
            t1.sms_id
        FROM
            sms_sms t1
        WHERE
            t1.is_delete = 0
        <if test="smsId != null and smsId != ''">
        AND
            t1.sms_id = #{smsId}
        </if>
    </select>

    <!-- 短信详情 -->
    <select id="getPO" parameterType="map" resultMap="smsPO">
        SELECT
            t1.user_id,
            t1.phone,
            t1.user_name,
            t1.content,
            t1.send_status,
            t1.error_message,
            t1.sms_id
        FROM
            sms_sms t1
        WHERE
            t1.is_delete = 0
        <if test="smsId != null and smsId != ''">
        AND
            t1.sms_id = #{smsId}
        </if>
    </select>

    <!-- 短信列表 -->
    <select id="list" parameterType="map" resultMap="smsDTO">
        SELECT
            t1.user_id,
            t1.phone,
            t1.user_name,
            t1.content,
            t1.send_status,
            t1.error_message,
            LEFT(t1.gmt_create, 19) gmt_create,
            t1.sms_id
        FROM
            sms_sms t1
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND (
            t1.user_name LIKE CONCAT('%', #{keywords}, '%')
            OR
            t1.phone LIKE CONCAT('%', #{keywords}, '%')
            OR
            t1.content LIKE CONCAT('%', #{keywords}, '%')
        )
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="phones != null and phones.size > 0">
        AND
            t1.phone IN
            <foreach collection="phones" index="index" open="(" separator="," close=")">
                #{phones[${index}]}
            </foreach>
        </if>
        <if test="smsIds != null and smsIds.size > 0">
        AND
            t1.sms_id IN
            <foreach collection="smsIds" index="index" open="(" separator="," close=")">
                #{smsIds[${index}]}
            </foreach>
        </if>
    </select>

    <!-- 短信列表 -->
    <select id="listPO" parameterType="map" resultMap="smsPO">
        SELECT
            t1.user_id,
            t1.phone,
            t1.user_name,
            t1.content,
            t1.send_status,
            t1.error_message,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete,
            t1.sms_id
        FROM
            sms_sms t1
        WHERE
            t1.is_delete = 0
        <if test="date != null and date != ''">
        AND
            LEFT(t1.gmt_create, 10) = #{date}
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
    </select>

</mapper>