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

    <resultMap type="SpSignture" id="SpSigntureResultMap">
        <id column="id" property="id" />
        <result column="signture" property="signture" />
        <result column="sp_id" property="sp_id" />
        <result column="sp_name" property="sp_name" />
        <result column="create_time" property="create_time" />
        <result column="update_time" property="update_time" />
        <result column="begin_time" property="begin_time" />
        <result column="end_time" property="end_time" />
        <result column="status" property="status" />
        <result column="sp_sign_extend" property="spSignExtend" />
        <result column="fixed_extend" property="fixedExtend" />
        <result column="domain" property="domain" />
        <result column="phones" property="phones" />
        <result column="sms_template" property="smsTemplate" />
    </resultMap>
    <select id="getSpSignture" parameterType="SpSignture"
            resultMap="SpSigntureResultMap">
        SELECT
        s.id,
        s.signture,
        s.sp_id,
        s.create_time,
        s.begin_time,
        s.end_time,
        s.status,
        sp.sp_name sp_name
        FROM
        sms_sp_signture s left join sms_sp_info sp on sp.id = s.sp_id
        <where>
             s.status = 1
            <if test="sp_id!=null and sp_id!=''">
                and sp.id= #{sp_id}
            </if>
            <if test="signture!=null and signture!=''">
                and s.signture = #{signture}
            </if>
            <if test="sp_name!=null and sp_name!=''">
                and sp.sp_name = #{sp_name}
            </if>
            <if test="gid!=null and gid>-1">
            	and s.gid=#{gid}
            </if>
            <if test="spSignExtend!=null and spSignExtend!=''">
                and s.sp_sign_extend=#{spSignExtend}
            </if>
        </where>
        limit 1
    </select>
    <select id="listSpSignture" parameterType="SpSignture"
            resultMap="SpSigntureResultMap">
        SELECT
        s.id,
        s.signture,
        s.sp_id,
        s.create_time,
        s.begin_time,
        s.end_time,
        s.status,
        sp.sp_name sp_name,
        s.gid
        FROM
        sms_sp_signture s,sms_sp_info sp
        <where>
            sp.id = s.sp_id AND s.status = 1
            <if test="sp_id!=null and sp_id!=''">
                and sp.id= #{sp_id}
            </if>
            <if test="signture!=null and signture!=''">
                and s.signture like  '%${signture}%'
            </if>
            <if test="sp_name!=null and sp_name!=''">
                and sp.sp_name like '%${sp_name}%'
            </if>
            <if test="status!=null and status!=''">
                and s.status = #{status}
            </if>
            <if test="gid!=null and gid >-1">
            	and s.gid=#{gid}
            </if>
        </where>
		order by s.create_time desc
    </select>
    <select id="listPageSpSignture" parameterType="SpSignture"
            resultMap="SpSigntureResultMap">
        SELECT
        s.id,
        s.signture,
        s.sp_id,
        s.create_time,
        s.update_time,
        s.begin_time,
        s.end_time,
        s.status,
        sp.sp_name as sp_name,
        s.gid,
        sg.name group_name,
        s.sp_sign_extend,
        s.fixed_extend,
        s.domain,
        s.phones,
        s.sms_template
        FROM
          sms_sp_signture s left join sms_sp_info sp on s.sp_id=sp.id left join sms_sp_sign_group sg on s.gid=sg.id
        <where>
            s.status = 1 and sg.status = 0
            <if test="sp_id!=null and sp_id!=''">
                and sp.id= #{sp_id}
            </if>
            <if test="signture!=null and signture!=''">
                and s.signture like  concat('%',#{signture},'%')
            </if>
            <if test="spSignExtend!=null and spSignExtend!=''">
                and s.sp_sign_extend like concat('%',#{spSignExtend},'%')
            </if>
            <if test="sp_name!=null and sp_name!=''">
                and sp.sp_name like concat('%',#{sp_name},'%')
            </if>
            <if test="status!=null and status!=''">
                and s.status = #{status}
            </if>
            <if test="gid!=null and gid >-1">
            	and s.gid=#{gid}
            </if>
            <if test="domain!=null and domain!=''">
                and s.domain like concat('%',#{domain},'%')
            </if>
            <if test="phones!=null and phones!=''">
                and s.phones like concat('%',#{phones},'%')
            </if>
        </where>
        order by create_time desc, id desc
    </select>
    <update id="deleteSpSignture" parameterType="int">
        update sms_sp_signture set status = 2 where id = #{id}
    </update>

    <insert id="insertSpSignture" useGeneratedKeys="true" keyProperty="id" parameterType="SpSignture">
        INSERT INTO sms_sp_signture (signture,sp_id,create_time,begin_time,end_time,status,gid, sp_sign_extend, fixed_extend, domain, phones, sms_template) VALUES
        (#{signture,jdbcType=VARCHAR},#{sp_id},now(),#{begin_time},#{end_time},#{status},#{gid},#{spSignExtend}, #{fixedExtend}, #{domain}, #{phones}, #{smsTemplate})
    </insert>

    <select id="listPageSpSignGroup" parameterType="SpSignture" resultType="SpSignture">
    	select id,name group_name,create_time,`desc`, status from sms_sp_sign_group
    	<where>
            status = #{status}
    		<if test="group_name!=null and group_name!=''">
    			and name like concat('%',#{group_name},'%')
    		</if>
    		<if test="id!=null and id>-1">
    			and id=#{id}
    		</if>
            <if test="signture != null and signture != ''">
                and id in (select gid from sms_sp_signture sss where sss.status = 1 and  sss.signture like concat('%',#{signture},'%'))
            </if>
            <if test="sp_id != null">
                and find_in_set (id, (select sign_gid from sms_sp_info where id=#{sp_id}))
            </if>
    	</where>
    	order by create_time desc
    </select>

    <select id="listSpSignGroup" parameterType="SpSignture" resultType="SpSignture">
    	select id,name group_name,create_time,`desc` from sms_sp_sign_group
    	<where>
            status = 0
    		<if test="group_name!=null and group_name!=''">
    			and name like concat('%',#{group_name},'%')
    		</if>
    		<if test="id!=null and id>-1">
    			and id=#{id}
    		</if>
    	</where>
    	order by create_time desc
    </select>

    <insert id="insertSpSignGroup" useGeneratedKeys="true" keyProperty="id" parameterType="SpSignture">
    	insert into sms_sp_sign_group(name,create_time) values(#{group_name},now())
    </insert>

    <update id="updateSignGroup" parameterType="SpSignture">
    	update sms_sp_sign_group set name=#{group_name} where id=#{id}
    </update>

    <select id="getSignGroupById" resultType="SpSignture">
    	select id,name group_name,create_time,`desc` from sms_sp_sign_group where id=#{id} limit 1
    </select>

    <delete id="delSignGroup">
    	DELETE from sms_sp_sign_group where id=#{gid}
    </delete>
    <delete id="delSignByGid">
    	 update sms_sp_signture set status = 2 where gid = #{gid}
    </delete>

    <select id="exportSign" parameterType="SpSignture"
        resultType="Map">
        select
        s.id,
        s.signture,
        s.sp_id,
        date_format(s.create_time, '%Y-%m-%d %H:%i:%S') as CREATE_TIME,
        IFNULL(date_format(s.update_time, '%Y-%m-%d %H:%i:%S'), '') as UPDATE_TIME,
        s.begin_time,
        s.end_time,
        s.status,
        sp.sp_name sp_name,
        s.gid,
        sg.name group_name,
        s.sp_sign_extend,
        s.fixed_extend,
        s.domain,
        s.phones,
        s.sms_template
        from
       	sms_sp_signture s left join sms_sp_info sp on s.sp_id=sp.id left join sms_sp_sign_group sg on s.gid=sg.id
        where
      	s.status = 1 and sg.status = 0
            <if test="sp_id!=null and sp_id!=''">
                and sp.id= #{sp_id}
            </if>
            <if test="signture!=null and signture!=''">
                and s.signture like  '%${signture}%'
            </if>
            <if test="sp_name!=null and sp_name!=''">
                and sp.sp_name like '%${sp_name}%'
            </if>
            <if test="gid!=null and gid >-1">
            	and s.gid=#{gid}
            </if>
            <if test="endId != null">
	            and s.id &lt; #{endId}
	        </if>
            <if test="domain != null and domain != ''">
                and s.domain like concat('%',#{domain},'%')
            </if>
            <if test="phones != null and phones != ''">
                and s.phones like concat('%',#{phones},'%')
            </if>
        order by id desc
        <if test="limit != null and limit > 0">
            limit #{limit}
        </if>
    </select>

    <select id="getSpSigntureById" resultType="cn.roadm.bo.SpSignture">
        select id,signture,fixed_extend as fixedExtend, sp_sign_extend as spSignExtend, domain, phones, sms_template as smsTemplate from sms_sp_signture where id = #{id}
    </select>

    <update id="updateSpSigntureInfo">
        update sms_sp_signture set signture = #{signture}, fixed_extend = #{fixedExtend}, sp_sign_extend = #{spSignExtend}, domain = #{domain}, phones = #{phones}, sms_template = #{smsTemplate} where id = #{id}
    </update>

    <!--查询批量删除-->
    <update id="batchDeleteBySearch" parameterType="cn.roadm.bo.SpSignture">
        UPDATE sms_sp_signture s LEFT JOIN sms_sp_info sp  ON s.sp_id=sp.id
        SET s.status = 2
        <where>
            <if test="sp_id!=null and sp_id!=''">
                and sp.id= #{sp_id}
            </if>
            <if test="signture!=null and signture!=''">
                and s.signture LIKE CONCAT('%', #{signture}, '%')
            </if>
            <if test="sp_name!=null and sp_name!=''">
                and sp.sp_name LIKE CONCAT('%', #{sp_name}, '%')
            </if>
            <if test="gid!=null and gid >-1">
                and s.gid=#{gid}
            </if>
            <if test="starttime !=null and starttime !='' ">
                and create_time &gt;= str_to_date(#{starttime},'%Y-%m-%d %H:%i:%S')
            </if>
            <if test="endtime !=null and endtime !='' ">
                and create_time &lt;= str_to_date(#{endtime},'%Y-%m-%d %H:%i:%S')
            </if>
            <if test="domain != null and domain != ''">
                and s.domain like concat('%',#{domain},'%')
            </if>
            <if test="phones != null and phones != ''">
                and s.phones like concat('%',#{phones},'%')
            </if>
        </where>
    </update>

    <update id="updateSignGroupStatus">
        update sms_sp_sign_group set `status` = #{state} where id = #{gid}
    </update>

    <select id="listNoReportSignExtendPool" resultType="cn.roadm.bo.NoReportSignExtendPoolBO">
        select id,
               channel_id                                    as channelId,
               pool_value                                    as poolValue,
               remark,
               DATE_FORMAT(create_time, '%Y-%m-%d %H:%i:%S') as createTime,
               DATE_FORMAT(update_time, '%Y-%m-%d %H:%i:%S') as updateTime
        from sms_no_report_sign_extend_pool
        <where>
            <if test="channelId != null and channelId != ''">
                and channel_id = #{channelId}
            </if>
        </where>
        order by create_time
    </select>

    <select id="getOneNoReportSignExtendPoolConfig" resultType="cn.roadm.bo.NoReportSignExtendPoolBO">
        select id,
               channel_id                                    as channelId,
               pool_value                                    as poolValue,
               remark,
               DATE_FORMAT(create_time, '%Y-%m-%d %H:%i:%S') as createTime,
               DATE_FORMAT(update_time, '%Y-%m-%d %H:%i:%S') as updateTime
        from sms_no_report_sign_extend_pool
        where id = #{id}
    </select>

    <insert id="addRandomExtendPoolValue">
        insert into sms_no_report_sign_extend_pool(channel_id, pool_value, remark, create_time, update_time)
        values (#{channelId}, #{poolValue}, #{remark}, now(), now())
    </insert>

    <update id="updateRandomExtendPoolValue">
        update sms_no_report_sign_extend_pool
        set channel_id = #{channelId},
            pool_value = #{poolValue},
            remark     = #{remark},
            update_time= now()
        where id = #{id}
    </update>

    <delete id="batchDeleteRandomExtendPoolConfig">
        delete from sms_no_report_sign_extend_pool where id in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            ${item}
        </foreach>
    </delete>

    <select id="listPageNoReportSignExtendRecord" resultType="cn.roadm.bo.NoReportSignExtendRecordBO">
        select a.id,
               a.channel_id                                  as channelId,
               a.sign,
               DATE_FORMAT(create_time, '%Y-%m-%d %H:%i:%S') as createTime,
               b.CHANNEL_NAME                                as channelName
        from sms_no_report_sign_extend_record a
                 left join sms_send_channel_info b on a.channel_id = b.ID
        <where>
            a.status = 0
            <if test="channelId != null and channelId != ''">
                and a.channel_id = #{channelId}
            </if>
            <if test="sign != null and sign != ''">
                and a.sign like CONCAT('%', #{sign}, '%')
            </if>
        </where>
        order by a.create_time desc
    </select>

    <update id="deleteNoReportSignExtendRecord">
        update sms_no_report_sign_extend_record set status = 1
        <where>
            status = 0
            <if test="channelId != null and channelId != ''">
                and channel_id = #{channelId}
            </if>
            <if test="sign != null and sign != ''">
                and sign like CONCAT('%', #{sign}, '%')
            </if>
            <if test="idList != null and idList.size() != 0">
                and id in
                <foreach collection="idList" item="item" open="(" separator="," close=")">
                    ${item}
                </foreach>
            </if>
        </where>
    </update>

    <select id="exportQueryNoReportSignExtendRecord" resultType="cn.roadm.bo.NoReportSignExportBO">
        select a.id as id,
               channel_id                                    as channelId,
               CHANNEL_NAME                                  as channelName,
               sign,
               DATE_FORMAT(create_time, '%Y-%m-%d %H:%i:%S') as createTime
        from sms_no_report_sign_extend_record a
                 left join sms_send_channel_info b on a.channel_id = b.ID
        <where>
            a.status = 0
            <if test="channelId != null and channelId != ''">
                and a.channel_id = #{channelId}
            </if>
            <if test="sign != null and sign != ''">
                and a.sign like CONCAT('%', #{sign}, '%')
            </if>
            <if test="lastId != null">
                and a.id &gt; #{lastId}
            </if>
        </where>
    </select>

    <select id="listPageQueryNumberSegmentIntercept" resultType="cn.roadm.bo.NumberSegmentInterceptBO">
        select id, mdnNumber as numberSegment, spId from mdn_segment_intercept_config
        <where>
            <if test="spId != null and spId != ''">
                and spId = #{spId}
            </if>
            <if test="numberSegment != null and numberSegment != ''">
                and mdnNumber like CONCAT('%', #{numberSegment}, '%')
            </if>
        </where>
        order by id
    </select>

    <delete id="deleteNumberSegmentIntercept">
        delete from mdn_segment_intercept_config where id
        in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            ${item}
        </foreach>
    </delete>

    <insert id="addNumberSegmentIntercept">
        insert into mdn_segment_intercept_config (mdnNumber, spId) VALUES
            <foreach collection="numberList" item="item" separator=",">
                (${item}, ${spId})
            </foreach>
    </insert>

    <insert id="addTypeConfig">
        insert into sms_service_type (serviceType) VALUES
            (#{serviceType})
    </insert>

    <select id="listPageTypeConfig" resultType="cn.roadm.bo.NumberSegmentInterceptBO">
        select id, serviceType from sms_service_type
        where status = 1
        <if test="serviceType != null and serviceType != ''">
            and serviceType like CONCAT('%', #{serviceType}, '%')
        </if>
        order by id
    </select>

    <delete id="deleteTypeConfig">
        delete from sms_service_type where id
        in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            ${item}
        </foreach>
    </delete>

    <delete id="batchDeleteByIds">
        UPDATE sms_sp_signture SET status = 2 where id
        in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            ${item}
        </foreach>
    </delete>
</mapper>
