<?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.PlatformAuditMapper">
    <resultMap id="auditTaskMap" type="cn.roadm.admin.dto.SmsPlatformAuditDO">
        <id column="id" property="id"/>
        <result column="sp_id" property="spId"/>
        <result column="sp_name" property="spName"/>
        <result column="content" property="content"/>
        <result column="createtime" property="createtime"/>
        <result column="total" property="total"/>
        <result column="allnumber" property="allnumber"/>
        <result column="unicomnumber" property="unicomnumber"/>
        <result column="mobilenumber" property="mobilenumber"/>
        <result column="telecomnumber" property="telecomnumber"/>
        <result column="task_type" property="taskType"/>
        <result column="status" property="status"/>
        <result column="send_type" property="sendType"/>
        <result column="audit_reason" property="auditReason"/>
        <result column="phone" property="phone"/>
        <result column="platform" property="platform"/>
        <result column="sp_desc" property="spDesc"/>
        <result column="otype" property="otype"/>
        <result column="createid" property="createId"/>
        <result column="endtime" property="endTime"/>
        <result column="sp_audit_type" property="spAuditType"/>
        <result column="user_id" property="userId"/>
    </resultMap>

    <resultMap id="mmsSpConfig" type="cn.roadm.admin.entity.MmsChannelConfigEntity">
        <id column="id" property="id"/>
        <result column="channel_group" property="channelGroup"/>
        <result column="channelId" property="channelId"/>
        <result column="opertor_type" property="operatorType"/>
        <result column="sp_id" property="spId"/>
        <result column="createtime" property="createTime"/>
    </resultMap>

    <resultMap type="cn.roadm.admin.entity.ChannelInfo" id="channelResult">
        <id column="id" property="id"/>
        <result column="channel_name" property="channel_name"/>
        <result column="is_support_unicom" property="is_support_unicom"/>
        <result column="is_support_mobile" property="is_support_mobile"/>
        <result column="is_support_telecom" property="is_support_telecom"/>
        <result column="mobile_prefix" property="mobile_prefix"/>
        <result column="unicom_prefix" property="unicom_prefix"/>
        <result column="telecom_prefix" property="telecom_prefix"/>

        <result column="is_mobile_extend" property="is_mobile_extend"/>
        <result column="is_unicom_extend" property="is_unicom_extend"/>
        <result column="is_telecom_extend" property="is_telecom_extend"/>
        <result column="ch_desc" property="desc"/>
        <result column="is_support_international" property="is_support_international"/>
    </resultMap>

    <resultMap type="cn.roadm.admin.entity.ChannelConfig" id="ChannelConfigResult">
        <id column="id" property="id"/>
        <result column="opertor_type" property="operator_type"/>
        <result column="channelid" property="channelId"/>
        <result column="sp_id" property="sp_id"/>
        <result column="createtime" property="createTime"/>
        <result column="price" property="price"/>
        <result column="channel_name" property="channel_name"/>
    </resultMap>


    <select id="listPageAuditTask" resultMap="auditTaskMap">
        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.phone,
        s.sp_name,
        s.platform,
        s.sp_desc,
        ak.otype,
        ak.user_id
        from sms_send_audit_task ak,
        sms_sp_info s
        where s.id = ak.sp_id
        and ak.status = 1
        and ak.countrylist is null
        <if test="platformAuditListVO.spName != null and platformAuditListVO.spName != ''">
            and s.sp_name like CONCAT('%', #{platformAuditListVO.spName}, '%')
        </if>
        <if test="platformAuditListVO.spId != null and platformAuditListVO.spId != ''">
            and s.id = #{platformAuditListVO.spId}
        </if>
        <if test="platformAuditListVO.content != null and platformAuditListVO.content != ''">
            and ak.content like CONCAT('%', #{platformAuditListVO.content}, '%')
        </if>
        <if test="platformAuditListVO.sign != null and platformAuditListVO.sign != ''">
            and ak.content like CONCAT('', #{platformAuditListVO.sign}, '%')
        </if>
        <if test="platformAuditListVO.startTime != null and platformAuditListVO.startTime != ''">
            and ak.createtime &gt;= str_to_date(#{platformAuditListVO.startTime}, '%Y-%m-%d %H:%i:%S')
        </if>

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

    <select id="getPlatformAuditCount" resultType="java.lang.Integer">
        select count(1)
        from sms_send_audit_task
        where status = 1
    </select>

    <select id="getPlatformAuditTaskById" resultMap="auditTaskMap">
        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,
        tk.endtime,
        sp.sp_audit_type,
        tk.send_type
        from sms_send_task tk,
        sms_api_setting sp
        where sp.sp_id = tk.sp_id
        <if test="spId != null and spId > 0">
            and tk.sp_id = #{spId}
        </if>
        <if test="taskId != null and taskId > 0">
            and tk.id = #{taskId}
        </if>
    </select>

    <select id="queryListAllMmsChannel" resultType="cn.roadm.admin.entity.MmsChannelInfo">
        select ID,
        CHANNEL_NAME,
        STATUS,
        IS_SUPPORT_UNICOM,
        IS_SUPPORT_MOBILE,
        IS_SUPPORT_TELECOM,
        MOBILE_PREFIX,
        UNICOM_PREFIX,
        TELECOM_PREFIX,
        IS_MOBILE_EXTEND,
        IS_UNICOM_EXTEND,
        IS_TELECOM_EXTEND,
        CH_DESC,
        PA_URL,
        PA_LOCATION,
        LOGINNAME,
        PASSWORD,
        accessnumber,
        IP,
        PORT,
        CARRIER_SPEED,
        CARRIER_SP_SIGN,
        PROTOCOL_TYPE,
        http_response,
        access_key,
        `desc`
        from mms_send_channel_info sc
        <where>
            <if test="status != null and status != -1">
                and sc.status = #{status}
            </if>
            <if test="channelName != null and channelName != ''">
                and sc.channel_name LIKE CONCAT('%', #{channelName}, '%')
            </if>
            <if test="isSupportUnicom != null and isSupportUnicom > 0">
                and sc.is_support_unicom = #{isSupportUnicom}
            </if>
            <if test="isSupportMobile != null and isSupportMobile > 0">
                and sc.is_support_mobile = #{isSupportMobile}
            </if>
            <if test="isSupportTelcom != null and isSupportTelcom > 0">
                and sc.is_support_telecom = #{isSupportTelcom}
            </if>
        </where>
    </select>

    <select id="getMmsChannelConfig" resultMap="mmsSpConfig">
        select cg.id,
        cg.opertor_type,
        cg.channelId,
        cg.sp_id,
        cg.createtime,
        cg.channel_group
        from mms_sp_channel_config cg
        <where>
            <if test="id != null and id > 0">
                and cg.id = #{id}
            </if>
            <if test="channelId != null and channelId > 0">
                and cg.channelId = #{channelId}
            </if>
            <if test="spId != null and spId > 0">
                and cg.sp_id = #{spId}
            </if>
            <if test="operatorType != null and operatorType > 0">
                and cg.opertor_type = #{operatorType}
            </if>
            <if test="channelGroup != null and channelGroup > 0">
                and cg.channel_group = #{channelGroup}
            </if>
        </where>
    </select>

    <select id="getChannelInfoList" resultMap="channelResult">
        select sc.id,
        sc.channel_name,
        sc.is_support_unicom,
        sc.is_support_international,
        sc.is_support_mobile,
        sc.is_support_telecom,
        sc.mobile_prefix,
        sc.unicom_prefix,
        sc.telecom_prefix,
        sc.is_mobile_extend,
        sc.is_unicom_extend,
        sc.is_telecom_extend,
        sc.ch_desc
        from sms_send_channel_info sc
        where sc.status = 1
        and sc.protocol_type &lt; 20
        <if test="id != null and id > -1">
            and sc.id = #{id}
        </if>
        <if test="is_support_unicom != null and is_support_unicom > 0">
            and sc.is_support_unicom = #{is_support_unicom}
        </if>
        <if test="is_support_mobile != null and is_support_mobile > 0">
            and sc.is_support_mobile = #{is_support_mobile}
        </if>
        <if test="is_support_telecom != null and is_support_telecom > 0">
            and sc.is_support_telecom = #{is_support_telecom}
        </if>
        <if test="is_support_international != null and is_support_international > 0">
            and sc.is_support_international = #{is_support_international}
        </if>
    </select>

    <select id="getSmsChannelConfig" resultMap="ChannelConfigResult">
        select cg.id,
        cg.opertor_type,
        cg.channelid,
        cg.sp_id,
        cg.createtime
        from sms_chennel_config cg
        <where>
            <if test="sp_id != null and sp_id > 0">
                and cg.sp_id = #{sp_id}
            </if>
            <if test="operator_type != null and operator_type > 0">
                and cg.opertor_type = #{operator_type}
            </if>
        </where>
    </select>

    <select id="getPaGroupConfigList" resultType="cn.roadm.admin.entity.PaGroupConfig">
        select id,
        name,
        network,
        platform,
        biz,
        strategy,
        channels,
        status,
        weight,
        strategytype,
        modifytime,
        overstocks
        from sms_channel_group g
        <where>
            g.status = '1'
            <if test="network != null and network != ''">
                and g.network = #{network}
            </if>
            <if test="name != null and name != ''">
                and g.name like CONCAT('%', #{name}, '%')
            </if>
            <if test="channels != null and channels != ''">
                and CONCAT(',', channels, ',') like CONCAT('%,', #{channels}, ',%')
            </if>
        </where>
        order by g.id desc
    </select>

    <select id="getAuditTaskById" resultType="cn.roadm.admin.dto.SmsPlatformAuditDO">
        select tk.id,
               tk.sp_id,
               tk.content,
               tk.createtime,
               tk.total,
               tk.allnumber,
               tk.unicomnumber,
               tk.mobilenumber,
               tk.telecomnumber,
               tk.task_type,
               tk.status,
               sp.sp_audit_type,
               tk.send_type,
               tk.user_id createid
        from sms_send_audit_task tk,
             sms_api_setting sp
        <where>
            sp.sp_id = tk.sp_id
            <if test="spId != null and spId > 0">
                and tk.sp_id = #{spId}
            </if>
            <if test="taskId != null and taskId > 0">
                and tk.id = #{taskId}
            </if>
            <if test="otype != null and otype > 0">
                and tk.otype = #{otype}
            </if>
        </where>
    </select>

    <delete id="deleteLastChannelChangeRecord">
        delete
        from sms_change_channel_task
        where sp_id = #{spId}
          and task_id = #{taskId}
    </delete>

    <delete id="deleteAuditTaskByIdAndOtype">
        delete
        from sms_send_audit_task where id = #{taskId}
        <if test="otype != null and otype > 0">
            and otype = #{otype}
        </if>
    </delete>

    <insert id="insertAuditResult">
    </insert>


    <update id="setUtf8mb4" parameterType="string">
        set names utf8mb4
    </update>

    <update id="updateAuditTaskById">
        update sms_send_audit_task
        <set>
            total=#{total}
              , allnumber=#{allNumber}
            <if test="unicomNumber > 0">
                , unicomnumber=#{unicomNumber}
            </if>
            <if test="mobileNumber">
                , mobilenumber=#{mobileNumber}
            </if>
            <if test="telecomNumber > 0">
                , telecomnumber=#{telecomNumber}
            </if>
        </set>
        <where>
            id = #{taskId}
            <if test="otype > 0">
                and otype = #{otype}
            </if>
        </where>
    </update>

    <select id="getAuditTaskIdByOnlyId" resultType="java.lang.Long">
        select DISTINCT id
        from sms_send_audit_task
        where id = #{id}
    </select>

    <select id="getChangeTaskByTaskId" resultType="cn.roadm.admin.entity.ChangeChannelTask">
        select task_id,
               mobile_pa_id,
               unicom_pa_id,
               telcom_pa_id   as telecom_pa_id,
               mobile_callmdn,
               telcom_callmdn as telecom_callmdn,
               unicom_callmdn,
               sp_id
        from sms_change_channel_task
        where task_id = #{task_id}
        limit 1
    </select>

    <insert id="insertChangeTask" parameterType="cn.roadm.admin.entity.ChangeChannelTask">
        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="updateChangeTask" parameterType="cn.roadm.admin.entity.ChangeChannelTask">
        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>

    <delete id="deleteAuditTaskById">
        delete
        from sms_send_audit_task where id = #{taskId}
        <if test="otype != null and otype > 0">
            and otype = #{otype}
        </if>
    </delete>

    <update id="updateListAuditSmsStatusForSearch">
		update
		sms_send_audit_task tk set
		tk.status=#{status},tk.audit_reason =#{auditReason}
		where tk.status=1
		<if test="spId != null and spId != ''">
			and tk.sp_id= #{spId}
		</if>
		<if test="taskId!=null and taskId>0">
			and tk.id=#{taskId}
		</if>
		<if test="content != null and content != ''">
			and tk.content like CONCAT('%',#{content},'%')
		</if>
		<if test="sign != null and sign != ''">
			and tk.content like CONCAT('',#{sign},'%')
		</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="otype != null and otype > 0">
        	and tk.otype=#{otype}
        </if>
    </update>

    <insert id="addEditAuditTask">
        INSERT into sms_audit_content_modify
		    (src_content,mobile_content,unicom_content,telcom_content,
		     content_type,createtime,`status`,spids,remark,username,otype,template_content,source_type,task_id)
		    values(#{sourceContent},#{smsContent},#{smsContent},#{smsContent},#{modifyType},#{createTime},
		           #{status},#{spId},#{remark},#{userName},#{otype},#{template},#{sourceType},#{taskId})
    </insert>
</mapper>
