<?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.ApiAuditTaskMapper">
    <select id="getApiAuditTaskPageList" resultType="cn.roadm.admin.dto.SmsApiAuditDO">
        select ak.id,
               ak.sp_id,
               ak.content,
               ak.createtime,
               ak.total,
               ak.allnumber,
               ak.unicomnumber,
               ak.mobilenumber,
               ak.telecomnumber,
               ak.task_type,
               ak.status,
               ak.send_type,
               ak.audit_reason,
               ak.unicommdns,
               ak.mobilemdns,
               ak.telcommdns,
               ak.extendaccessnum,
               s.sp_name,
               s.platform,
               s.sp_desc,
               ak.otype,
               CONCAT(ak.mobilemdns, IF(ak.mobilemdns = '', '', ','), ak.unicommdns, IF(ak.unicommdns = '', '', ','),
                      ak.telcommdns) as mdns
        from sms_api_audit_task ak,
             sms_sp_info s
        <where>
            s.id = ak.sp_id
              and ak.status = 1
            <if test="query.platform != null and query.platform != '' and query.platform > 0">
                and s.platform = #{query.platform}
            </if>
            <if test="query.spName != null and query.spName != ''">
                and s.sp_name like CONCAT('%', #{query.spName}, '%')
            </if>
            <if test="query.spId != null and query.spId > 0">
                and s.id = #{query.spId}
            </if>
            <if test="query.content != null and query.content != ''">
                and ak.content like CONCAT('%', #{query.content}, '%')
            </if>
            <if test="query.sign != null and query.sign != ''">
                and ak.sign = #{query.sign,jdbcType=VARCHAR}
            </if>
            <if test="query.unicomMdn != null and query.unicomMdn != ''">
                and ak.UNICOMMDNS like CONCAT('%', #{query.unicomMdn}, '%')
            </if>
            <if test="query.mobileMdn != null and query.mobileMdn != ''">
                and ak.mobilemdns like CONCAT('%', #{query.mobileMdn}, '%')
            </if>
            <if test="query.telcomMdn != null and query.telcomMdn != ''">
                and ak.telcommdns like CONCAT('%', #{query.telcomMdn}, '%')
            </if>

            <if test="query.sendType != null and query.sendType > 0">
                and ak.send_type = #{query.sendType}
            </if>
            <if test="query.taskId != null and query.taskId > 0">
                and ak.id = #{query.taskId}
            </if>
            <if test="query.startTime != null and query.startTime != ''">
                and ak.createtime &gt;= str_to_date(#{query.startTime}, '%Y-%m-%d %H:%i:%S')
            </if>

            <if test="query.endTime != null and query.endTime != ''">
                and ak.createtime &lt;= str_to_date(#{query.endTime}, '%Y-%m-%d %H:%i:%S')
            </if>
            <if test="query.otype > 0">
                and otype = #{query.otype}
            </if>
        </where>
        order by ak.createtime desc
    </select>

    <select id="getApiAuditTaskById" resultType="cn.roadm.admin.dto.SmsApiAuditDO">
        select ak.id,
               ak.sp_id,
               ak.content,
               ak.createid,
               ak.createtime,
               ak.total,
               ak.allnumber,
               ak.unicomnumber,
               ak.mobilenumber,
               ak.telecomnumber,
               ak.task_type,
               ak.status,
               ak.send_type,
               ak.audit_reason,
               ak.unicommdns,
               ak.mobilemdns,
               ak.telcommdns,
               ak.extendaccessnum,
               s.sp_name,
               s.platform,
               s.sp_desc,
               ak.otype
        from sms_api_audit_task ak,
             sms_sp_info s
        <where>
            s.id = ak.sp_id
              and ak.status = 1
            <if test="taskId != null and taskId != ''">
                and ak.id = #{taskId}
            </if>
            <if test="otype != null and otype > 0">
                and otype = #{otype}
            </if>
        </where>
    </select>

    <select id="getTaskInfoByContent" resultType="cn.roadm.admin.dto.SmsApiAuditDO">
        select tk.id,
               tk.sp_id,
               tk.content,
               tk.createid,
               tk.createtime,
               tk.total,
               tk.allnumber,
               tk.unicomnumber,
               tk.mobilenumber,
               tk.telecomnumber,
               tk.task_type,
               tk.status,
               sp.sp_audit_type,
               tk.send_type
        from sms_api_audit_task tk,
             sms_api_setting sp
        where sp.sp_id = tk.sp_id
          AND tk.status = 1
        <if test="spId != null and spId != ''">
            and tk.sp_id = #{spId}
        </if>
        <if test="content != null and content != null">
            and tk.content = #{content}
        </if>
        <if test="otype != null and otype > 0">
            and tk.otype = #{otype}
        </if>
        limit 0,10000
    </select>

    <update id="updateApiAuditTaskContent">
        update
            sms_api_audit_task set content  =#{content,jdbcType=VARCHAR}
                                 , ltcontent=#{ltcontent,jdbcType=VARCHAR}
                                 , dxcontent=#{dxcontent,jdbcType=VARCHAR}
        <if test="allnumber != null and allnumber > 0">
            , allnumber=#{allnumber}
        </if>
        <if test="unicomnumber != null and unicomnumber > 0">
            , unicomnumber=#{unicomnumber}
        </if>
        <if test="mobilenumber != null and mobilenumber > 0">
            , mobilenumber=#{mobilenumber}
        </if>
        <if test="telecomnumber != null and telecomnumber > 0">
            , telecomnumber=#{telecomnumber}
        </if>
        where id = #{taskId}
        <if test="otype > 0">
            and otype = #{otype}
        </if>
    </update>

    <select id="getApiAuditTaskByChangeTask" resultType="cn.roadm.admin.entity.ChangeChannelTask">
        select *
        from sms_change_channel_task
        where task_id = #{task_id}
        limit 1
    </select>

    <update id="updateChangeTask">
        update sms_change_channel_task
        <set>
            <if test="mobile_pa_id != null and mobile_pa_id != ''">
                mobile_pa_id=#{mobile_pa_id},
                mobile_callmdn=#{mobile_callmdn},
            </if>
            <if test="unicom_pa_id != null and unicom_pa_id != ''">
                unicom_pa_id=#{unicom_pa_id},
                unicom_callmdn=#{unicom_callmdn},
            </if>
            <if test="telecom_pa_id != null and telecom_pa_id != ''">
                telcom_pa_id=#{telecom_pa_id},
                telcom_callmdn=#{telecom_callmdn}
            </if>
        </set>
        where task_id = #{task_id}
    </update>

    <insert id="insertChangeTask">
        insert into sms_change_channel_task
        (task_id, mobile_pa_id, unicom_pa_id, telcom_pa_id, mobile_callmdn, unicom_callmdn, telcom_callmdn, sp_id)
        values (#{task_id}, #{mobile_pa_id}, #{unicom_pa_id}, #{telecom_pa_id}, #{mobile_callmdn,jdbcType=VARCHAR},
                #{unicom_callmdn,jdbcType=VARCHAR}, #{telecom_callmdn,jdbcType=VARCHAR}, #{sp_id})
    </insert>

    <update id="updateApiAuditTaskChangeFlag">
        update sms_api_audit_task
        set changeflag =1
        where id = #{id}
        <if test="otype != null and otype > 0">
            and otype = #{otype}
        </if>
    </update>

    <update id="updateApiAuditTaskById">
        update
            sms_api_audit_task
        set status  =#{status},
            operator=#{operator},
            auditid=#{auditid}
        where id = #{id}
          and status = 1
        <if test="otype != null and otype > 0">
            and otype = #{otype}
        </if>
    </update>

    <select id="getApiAuditIdByTask" resultType="java.lang.Long">
        select auditid
        from sms_api_audit_task
        where id = #{taskId}
        <if test="otype != null and otype > 0">
            and otype = #{otype}
        </if>
        group by id
    </select>

    <update id="updateApiAuditByContent">
        update
            sms_api_audit_task
        set status  =#{status}
          , operator=#{operator}
          , auditid=#{auditid}
        where status = 1
        <if test="spId != null and spId > 0">
            and sp_id = #{spId}
        </if>
        <if test="content != null and content != ''">
            and content = #{content}
        </if>
    </update>

    <update id="updateApiAuditByTaskId">
        update
            sms_api_audit_task
        set status  =#{status},
            operator=#{operator},
            auditid=#{auditid}
        where id = #{id}
          and status = 1
        <if test="otype != null and otype > 0">
            and otype = #{otype}
        </if>
    </update>

    <update id="batchAuditPass">
        update
            sms_api_audit_task tk
        set tk.status=2,
            operator=#{operator},
            auditid=#{auditId}
        where tk.status = 1
          and tk.otype = #{otype}
          and tk.id in
        <foreach item="taskId" index="index" collection="taskIds" open="("
                 separator="," close=")">
            #{taskId}
        </foreach>
    </update>

    <update id="batchAuditReject">
        update
            sms_api_audit_task tk
        set tk.status=3,
            tk.operator=#{operator},
            auditid=#{auditId}
        where otype = #{otype}
          and tk.id in
        <foreach item="item" index="index" collection="taskIds" open="(" separator="," close=")">
            #{item}
        </foreach>
        and tk.status = 1
    </update>

    <update id="updateListAuditSmsStatusForSearch">
        update
            sms_api_audit_task tk
        set tk.status=#{status},
            operator=#{operator},
            auditid=#{auditId}
        where tk.status = 1
        <if test="spId != null and spId != ''">
            and tk.sp_id = #{spId}
        </if>
        <if test="taskId != null and taskId != ''">
            and tk.id = #{taskId}
        </if>
        <if test="isBatch == 1">
            <if test="content != null and content != ''">
                and tk.content = #{content}
            </if>
        </if>
        <if test="isBatch != 1">
            <if test="content != null and content != ''">
                and tk.content like CONCAT('%', #{content}, '%')
            </if>
        </if>
        <if test="sign != null and sign != ''">
            and tk.sign = #{sign,jdbcType=VARCHAR}
        </if>
        <if test="startTime != null and startTime != ''">
            and tk.createtime &gt;= str_to_date(#{startTime}, '%Y-%m-%d %H:%i:%S')
        </if>

        <if test="endTime != null and endTime != ''">
            and tk.createtime &lt;= str_to_date(#{endTime}, '%Y-%m-%d %H:%i:%S')
        </if>
        <if test="unicomNumber != null and unicomNumber != ''">
            and tk.UNICOMMDNS like CONCAT('%', #{unicomNumber}, '%')
        </if>
        <if test="mobileNumber != null and mobileNumber != ''">
            and tk.mobilemdns like CONCAT('%', #{mobileNumber}, '%')
        </if>
        <if test="telecomNumber != null and telecomNumber != ''">
            and tk.telcommdns like CONCAT('%', #{telecomNumber}, '%')
        </if>
        <if test="otype != null and otype > 0">
            and otype = #{otype}
        </if>
        <if test="limitNumber != null and limitNumber > 0">
            limit #{limitNumber}
        </if>
    </update>

    <insert id="saveApiAuditModifyTask">

    </insert>

    <select id="getTaskQueueByContent" resultType="cn.roadm.admin.dto.SmsApiAuditDO">
        select a.*,ss.sp_name from (select content,count(1) co,sp_id,otype from sms_api_audit_task
		<where>
			 sp_id=#{spId} and content=#{content}
			 <if test="otype>0">
			 	and otype=#{otype}
			 </if>
		</where>
		 GROUP BY content,sp_id,otype
		 <if test="otype>0">
		 	,otype
		 </if>
		 ) a ,sms_sp_info ss
		where ss.id=a.sp_id
		ORDER BY a.co desc

    </select>

    <select id="getApiAuditCount" resultType="java.lang.Integer">
        select count(1) from sms_api_audit_task where status = 1
    </select>
</mapper>
