<?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="cn.roadm.admin.mapper.FixedSignMapper">
    <select id="listPageQueryOneCustomerOneSign" resultType="cn.roadm.admin.dto.SmsSignBypassListPageDTO">
        select ot.id,
               ot.sign,
               ot.createtime,
               ot.mobilechannel as channelId,
               ch.CHANNEL_NAME,
               ot.status,
               ot.mobile_suffix,
               ot.reporting_status,
               ot.batch_no,
               ot.class_id,
               g.name as className,
               ot.extend_type,
               ot.sign_flag,
               ot.domain,
               ot.phones,
               ot.sms_template,
               ot.auto_test,
               ot.fixed_mobile_suffix as fixedSuffix
        from sms_sign_bypass ot
                 left join sms_send_channel_info ch on ot.mobilechannel = ch.ID
                 left join sms_sign_bypass_group g on g.id = ot.class_id
        <where>
            <if test="oneSignReqVO.sign != null and oneSignReqVO.sign != ''">
                and BINARY ot.sign = #{oneSignReqVO.sign}
            </if>
            <if test="oneSignReqVO.channelId != null and oneSignReqVO.channelId != '-1'">
                and ot.mobilechannel = #{oneSignReqVO.channelId}
            </if>
            <if test="oneSignReqVO.extendNo != null and oneSignReqVO.extendNo != ''">
                and ot.mobile_suffix = #{oneSignReqVO.extendNo}
            </if>
            <if test="oneSignReqVO.fixedSuffix != null">
                and ot.fixed_mobile_suffix = #{oneSignReqVO.fixedSuffix}
            </if>
            <if test="oneSignReqVO.reportStatus != null and oneSignReqVO.reportStatus != ''">
                and ot.reporting_status = #{oneSignReqVO.reportStatus}
            </if>
            <if test="oneSignReqVO.batchId != null and oneSignReqVO.batchId != ''">
                and ot.batch_no = #{oneSignReqVO.batchId}
            </if>
            <if test="oneSignReqVO.classId != null">
                and ot.class_id = #{oneSignReqVO.classId}
            </if>
            <if test="oneSignReqVO.extendType != null">
                and ot.extend_type = #{oneSignReqVO.extendType}
            </if>
            <if test="oneSignReqVO.signFlag != null">
                and ot.sign_flag = #{oneSignReqVO.signFlag}
            </if>
            <if test="oneSignReqVO.domain != null and oneSignReqVO.domain != ''">
                and ot.domain like concat('%', #{oneSignReqVO.domain}, '%')
            </if>
            <if test="oneSignReqVO.phones != null and oneSignReqVO.phones != ''">
                and ot.phones like concat('%', #{oneSignReqVO.phones}, '%')
            </if>
            <if test="oneSignReqVO.smsTemplate != null and oneSignReqVO.smsTemplate != ''">
                and ot.sms_template like concat('%', #{oneSignReqVO.smsTemplate}, '%')
            </if>
            <if test="oneSignReqVO.autoTest != null">
                and ot.auto_test = #{oneSignReqVO.autoTest}
            </if>
            <if test="oneSignReqVO.startTime != null and oneSignReqVO.startTime != ''">
                and ot.createtime &gt;= str_to_date(#{oneSignReqVO.startTime}, '%Y-%m-%d')
            </if>
            <if test="oneSignReqVO.endTime != null and oneSignReqVO.endTime != ''">
                and ot.createtime &lt; DATE_ADD(str_to_date(#{oneSignReqVO.endTime}, '%Y-%m-%d'), INTERVAL 1 DAY)
            </if>
        </where>
        order by ot.id desc
    </select>

    <select id="selectOneCustomerOneSignDetail" resultType="cn.roadm.admin.dto.SmsSignBypassListPageDTO">
        select ot.id,
               ot.sign,
               ot.createtime,
               ot.mobilechannel as channelId,
               ot.status,
               ot.mobile_suffix,
               ot.reporting_status,
               ot.batch_no,
               ot.class_id,
               g.name as className,
               ot.extend_type,
               ot.sign_flag,
               ot.domain,
               ot.phones,
               ot.sms_template,
               ot.auto_test,
               ot.fixed_mobile_suffix as fixedSuffix
        from sms_sign_bypass ot
                 left join sms_sign_bypass_group g on g.id = ot.class_id
        where ot.id = #{id}
    </select>

    <insert id="insertOneCustomerOneSign">
        insert into sms_sign_bypass
        (sign, status, mobile_suffix, mobilechannel, reporting_status, fixed_mobile_suffix, class_id, extend_type, sign_flag, domain, phones, sms_template, auto_test, createtime)
        values
            (#{sign}, #{status}, #{extend}, #{channelId}, #{reportStatus}, #{fixedSuffix}, #{classId}, #{extendType}, #{signFlag}, #{domain}, #{phones}, #{smsTemplate}, #{autoTest}, now())
    </insert>

    <update id="updateOneCustomerOneSign">
        update sms_sign_bypass
        set sign=#{sign},
            status=#{status},
            mobile_suffix=#{extend},
            mobilechannel=#{channelId},
            reporting_status=#{reportStatus},
            fixed_mobile_suffix=#{fixedSuffix},
            class_id=#{classId},
            extend_type=#{extendType},
            sign_flag=#{signFlag},
            domain=#{domain},
            phones=#{phones},
            sms_template=#{smsTemplate},
            auto_test=#{autoTest}
        where id = #{id}
    </update>

    <select id="listQueryOneCustomerOneSign" resultType="cn.roadm.admin.dto.SmsSignBypassListPageDTO">
        select ot.id,
               ot.sign,
               ot.createtime,
               ot.description,
               ot.mobilechannel as channelId,
               ot.status,
               ot.mobile_suffix,
               ot.fixed_mobile_suffix as fixedSuffix
        from sms_sign_bypass ot
        <where>
            ot.sign = #{oneSignReqVO.sign}
              and mobilechannel = #{oneSignReqVO.channelId}
            <if test="oneSignReqVO.id != null and oneSignReqVO.id != ''">
                and (id &lt; #{oneSignReqVO.id} or id &gt; #{oneSignReqVO.id})
            </if>
            <if test="oneSignReqVO.fixedSuffix != null and oneSignReqVO.fixedSuffix != ''">
                and ot.fixed_mobile_suffix = #{oneSignReqVO.fixedSuffix}
            </if>
        </where>
    </select>

    <insert id="insertFixedSignChannel">
        insert into sms_sign_bypass
        (sign, CREATETIME, mobilechannel, mobile_suffix, status, fixed_mobile_suffix, reporting_status, batch_no,
         class_id, extend_type, sign_flag, domain, phones, sms_template, auto_test)
        values ( #{sign,jdbcType=VARCHAR}, sysdate(), #{channelId,jdbcType=VARCHAR}, #{extendNo}, #{status}, #{fixedSuffix},
                 #{reportStatus}, #{batchId}, #{classId}, #{extendType}, #{signFlag}, #{domain}, #{phones}, #{smsTemplate}, #{autoTest})
    </insert>

    <update id="updateReportSignList">
        update
            sms_sign_bypass
        set reporting_status = '1'
        <where>
            mobilechannel = #{channelId}
              and sign in
            <foreach collection="vo" item="item" open="(" close=")" separator=",">
                #{item.sign,jdbcType=VARCHAR}
            </foreach>
        </where>
    </update>

    <delete id="batchDeleteSignBySign">
        delete from sms_sign_bypass where mobilechannel = #{channelId} and sign in
        <foreach collection="tempList" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </delete>

    <delete id="batchDeleteSignByExtend">
        delete from sms_sign_bypass where mobilechannel = #{channelId} and mobile_suffix in
        <foreach collection="tempList" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </delete>

    <delete id="searchDeleteSign">
        delete
        from sms_sign_bypass
        <where>
            <if test="sign != null and sign != ''">
                and BINARY sign = #{sign}
            </if>
            <if test="channelId != null and channelId >= 0">
                and mobilechannel = #{channelId}
            </if>
            <if test="extend != null and extend != ''">
                and mobile_suffix = #{extend}
            </if>
            <if test="fixedSuffix != null">
                and fixed_mobile_suffix = #{fixedSuffix}
            </if>
            <if test="startTime != null and startTime != ''">
                and createtime &gt;= str_to_date(#{startTime}, '%Y-%m-%d %H:%i:%S')
            </if>
            <if test="endTime != null and endTime != ''">
                and createtime &lt;= str_to_date(#{endTime}, '%Y-%m-%d %H:%i:%S')
            </if>
            <if test="batchNo != null and batchNo != ''">
                and batch_no = #{batchNo}
            </if>
            <if test="classId != null">
                and class_id = #{classId}
            </if>
            <if test="extendType != null">
                and extend_type = #{extendType}
            </if>
            <if test="signFlag != null">
                and sign_flag = #{signFlag}
            </if>
            <if test="domain != null and domain != ''">
                and domain like concat('%', #{domain}, '%')
            </if>
            <if test="phones != null and phones != ''">
                and phones like concat('%', #{phones}, '%')
            </if>
            <if test="smsTemplate != null and smsTemplate != ''">
                and sms_template like concat('%', #{smsTemplate}, '%')
            </if>
            <if test="autoTest != null">
                and auto_test = #{autoTest}
            </if>
        </where>
    </delete>

    <delete id="deleteOneCustomerOneSign">
        delete from sms_sign_bypass where id = #{id}
    </delete>

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

    <update id="batchUpdateSignStatus">
        update sms_sign_bypass
        set status = #{status}
        where id in
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </update>

    <update id="batchUpdateReportStatus">
        update sms_sign_bypass
        set reporting_status = #{reportStatus}
        where id in
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </update>

    <select id="listByChannelAndSigns" resultType="cn.roadm.admin.dto.SmsSignBypassListPageDTO">
        select ot.id,
               ot.sign,
               ot.createtime,
               ot.mobilechannel as channelId,
               ch.CHANNEL_NAME,
               ot.status,
               ot.mobile_suffix,
               ot.reporting_status,
               ot.batch_no,
               ot.class_id,
               g.name as className,
               ot.extend_type,
               ot.sign_flag,
               ot.domain,
               ot.phones,
               ot.sms_template,
               ot.auto_test,
               ot.fixed_mobile_suffix as fixedSuffix
        from sms_sign_bypass ot
                 left join sms_send_channel_info ch on ot.mobilechannel = ch.ID
                 left join sms_sign_bypass_group g on g.id = ot.class_id
        where ot.mobilechannel = #{channelId}
          and ot.sign in
        <foreach collection="signs" item="sign" open="(" close=")" separator=",">
            #{sign}
        </foreach>
    </select>

    <select id="listQueryOneCustomerOneSignForExport" resultType="cn.roadm.admin.dto.SmsSignBypassListPageDTO">
        select ot.id,
               ot.sign,
               ot.createtime,
               ot.mobilechannel as channelId,
               ch.CHANNEL_NAME,
               ot.status,
               ot.mobile_suffix,
               ot.reporting_status,
               ot.batch_no,
               ot.class_id,
               g.name as className,
               ot.extend_type,
               ot.sign_flag,
               ot.domain,
               ot.phones,
               ot.sms_template,
               ot.auto_test,
               ot.fixed_mobile_suffix as fixedSuffix
        from sms_sign_bypass ot
                 left join sms_send_channel_info ch on ot.mobilechannel = ch.ID
                 left join sms_sign_bypass_group g on g.id = ot.class_id
        <where>
            <if test="oneSignReqVO.sign != null and oneSignReqVO.sign != ''">
                and BINARY ot.sign = #{oneSignReqVO.sign}
            </if>
            <if test="oneSignReqVO.channelId != null and oneSignReqVO.channelId != '-1'">
                and ot.mobilechannel = #{oneSignReqVO.channelId}
            </if>
            <if test="oneSignReqVO.extendNo != null and oneSignReqVO.extendNo != ''">
                and ot.mobile_suffix = #{oneSignReqVO.extendNo}
            </if>
            <if test="oneSignReqVO.fixedSuffix != null">
                and ot.fixed_mobile_suffix = #{oneSignReqVO.fixedSuffix}
            </if>
            <if test="oneSignReqVO.reportStatus != null and oneSignReqVO.reportStatus != ''">
                and ot.reporting_status = #{oneSignReqVO.reportStatus}
            </if>
            <if test="oneSignReqVO.batchId != null and oneSignReqVO.batchId != ''">
                and ot.batch_no = #{oneSignReqVO.batchId}
            </if>
            <if test="oneSignReqVO.classId != null">
                and ot.class_id = #{oneSignReqVO.classId}
            </if>
            <if test="oneSignReqVO.extendType != null">
                and ot.extend_type = #{oneSignReqVO.extendType}
            </if>
            <if test="oneSignReqVO.signFlag != null">
                and ot.sign_flag = #{oneSignReqVO.signFlag}
            </if>
            <if test="oneSignReqVO.domain != null and oneSignReqVO.domain != ''">
                and ot.domain like concat('%', #{oneSignReqVO.domain}, '%')
            </if>
            <if test="oneSignReqVO.phones != null and oneSignReqVO.phones != ''">
                and ot.phones like concat('%', #{oneSignReqVO.phones}, '%')
            </if>
            <if test="oneSignReqVO.smsTemplate != null and oneSignReqVO.smsTemplate != ''">
                and ot.sms_template like concat('%', #{oneSignReqVO.smsTemplate}, '%')
            </if>
            <if test="oneSignReqVO.autoTest != null">
                and ot.auto_test = #{oneSignReqVO.autoTest}
            </if>
            <if test="oneSignReqVO.startTime != null and oneSignReqVO.startTime != ''">
                and ot.createtime &gt;= str_to_date(#{oneSignReqVO.startTime}, '%Y-%m-%d')
            </if>
            <if test="oneSignReqVO.endTime != null and oneSignReqVO.endTime != ''">
                and ot.createtime &lt; DATE_ADD(str_to_date(#{oneSignReqVO.endTime}, '%Y-%m-%d'), INTERVAL 1 DAY)
            </if>
        </where>
        order by ot.id desc
    </select>

    <select id="listSignBypassClassGroupOptions" resultType="cn.roadm.admin.controller.vo.PageSelectOptionsVO">
        select id as value, name as label
        from sms_sign_bypass_group
        where status = 0
        order by id desc
    </select>

    <select id="listPageQuerySignBypassClassGroup" resultType="cn.roadm.admin.dto.SignBypassClassGroupDTO">
        select id,
               name,
               status,
               create_time as createTime
        from sms_sign_bypass_group
        <where>
            status = 0
            <if test="reqVO.name != null and reqVO.name != ''">
                and name like concat('%', #{reqVO.name}, '%')
            </if>
            <if test="reqVO.startTime != null and reqVO.startTime != ''">
                and create_time &gt;= str_to_date(#{reqVO.startTime}, '%Y-%m-%d')
            </if>
            <if test="reqVO.endTime != null and reqVO.endTime != ''">
                and create_time &lt; DATE_ADD(str_to_date(#{reqVO.endTime}, '%Y-%m-%d'), INTERVAL 1 DAY)
            </if>
        </where>
        order by id desc
    </select>

    <select id="getSignBypassClassGroupDetail" resultType="cn.roadm.admin.dto.SignBypassClassGroupDTO">
        select id,
               name,
               status,
               create_time as createTime
        from sms_sign_bypass_group
        where id = #{id}
        limit 1
    </select>

    <insert id="insertSignBypassClassGroup">
        insert into sms_sign_bypass_group(name, status, create_time)
        values (#{name}, 0, now())
    </insert>

    <update id="updateSignBypassClassGroup">
        update sms_sign_bypass_group
        set name = #{name}
        where id = #{id}
          and status = 0
    </update>

    <update id="deleteSignBypassClassGroup">
        update sms_sign_bypass_group
        set status = 1
        where id = #{id}
          and status = 0
    </update>
</mapper>
