<?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.ho.flow.mapper.SendSmsConfigMapper">

    <sql id="Base_Column_List">
        id
        ,station_id,user_id,username,phone,real_name,dept_name,email,
        sms_type,alarm_level,create_id,update_id,create_time,update_time,is_send_now,delay_data
    </sql>

    <sql id="Insert_Batch_Column_List">
        station_id
        ,user_id,username,phone,real_name,dept_name,email,
        sms_type,alarm_level,create_id,create_time,is_send_now,delay_data
    </sql>

    <select id="selectList" resultType="com.ho.flow.vo.resp.SendSmsConfig.SendSmsConfigRsp">
        select
        <include refid="Base_Column_List"/>
        from send_sms_config
        <where>
            <if test="vo.stationId != null">
                and station_id = #{vo.stationId}
            </if>
            <if test="vo.isSendNow != null">
                and is_send_now = #{vo.isSendNow}
            </if>
            <if test="vo.username != null">
                and username like concat('%',#{vo.username},'%')
            </if>
            <if test="vo.phone != null">
                and phone like concat('%',#{vo.phone},'%')
            </if>
            <if test="vo.smsType != null">
                and sms_type like concat('%',#{vo.smsType},'%')
            </if>
            <if test="vo.alarmLevel != null">
                and alarm_level like concat('%',#{vo.alarmLevel},'%')
            </if>
            <if test="vo.userIds != null and vo.userIds.size > 0">
                and user_id in
                <foreach collection="vo.userIds" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
        order by create_time desc
    </select>

    <insert id="insertList">
        insert into send_sms_config (<include refid="Insert_Batch_Column_List"/>)
        values
        <foreach item="item" collection="list" index="index" separator=",">
            (
            #{item.stationId},#{item.userId},#{item.username},#{item.phone},#{item.realName},
            #{item.deptName},#{item.email},#{item.smsType},#{item.alarmLevel},#{item.createId},now(),#{item.isSendNow},#{item.delayData}
            )
        </foreach>
    </insert>

    <update id="updateById">
        update send_sms_config
        <set>
            <if test="vo.smsType != null">
                sms_type = #{vo.smsType},
            </if>
            <if test="vo.alarmLevel != null">
                alarm_level = #{vo.alarmLevel},
            </if>
            <if test="vo.updateId != null">
                update_id = #{vo.updateId},
            </if>
            <if test="vo.updateTime != null">
                update_time = #{vo.updateTime},
            </if>
            <if test="vo.isSendNow != null">
                is_send_now = #{vo.isSendNow},
            </if>
            <if test="vo.delayData != null">
                delay_data = #{vo.delayData},
            </if>
        </set>
        where id = #{vo.id}
    </update>

    <update id="updateByUserId">
        update send_sms_config
        <set>
            <if test="vo.username != null">
                username = #{vo.username},
            </if>
            <if test="vo.phone != null">
                phone = #{vo.phone},
            </if>
            <if test="vo.realName != null">
                real_name = #{vo.realName},
            </if>
            <if test="vo.deptName != null">
                dept_name = #{vo.deptName},
            </if>
            <if test="vo.email != null">
                email = #{vo.email},
            </if>
            <if test="vo.updateTime != null">
                update_time = #{vo.updateTime},
            </if>
        </set>
        where user_id = #{vo.userId}
    </update>

    <delete id="deleteBatch">
        delete
        from send_sms_config
        <where>
            and id in
            <foreach collection="ids" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
        </where>
    </delete>

</mapper>
