<?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.AuditSmsMapper">
    <resultMap type="TaskInfo" id="auditTaskMap">
        <id column="id" property="task_id" />
        <result column="content" property="content" />
        <result column="createtime" property="beginTime" />
        <result column="createid" property="createid" />
        <result column="total" property="total" />
        <result column="allnumber" property="allnumber" />
        <result column="mobilenumber" property="mobilenumber" />
        <result column="unicomnumber" property="unicomnumber" />
        <result column="telecomnumber" property="telecomnumber" />
        <result column="task_type" property="task_type" />
        <result column="status" property="status" />
        <result column="send_type" property="send_type" />
        <result column="sp_id" property="sp_id" />
        <result column="sp_name" property="sp_name" />
        <result column="audit_result" property="audit_result" />
        <result column="otype" property="otype" />
        <result column="cron_time" property="cron_time"/>

    </resultMap>

    <resultMap type="AuditResult" id="auditResultMap">
        <id column="id" property="id" />
        <result column="create_time" property="create_time" />
        <result column="audit_result" property="audit_result" />
        <result column="audit_opeator" property="audit_opeator" />
        <result column="audit_desc" property="audit_desc" />
        <result column="createtime" property="beginTime" />
        <result column="taskid" property="taskid" />
        <result column="allnumber" property="allnumber" />
        <result column="mobilenumber" property="mobilenumber" />
        <result column="unicomnumber" property="unicomnumber" />
        <result column="telecomnumber" property="telecomnumber" />
        <result column="send_type" property="send_type" />
        <result column="sp_id" property="sp_id" />
        <result column="sp_name" property="sp_name" />
        <result column="content" property="content" />
        <result column="audit_content" property="audit_content" />
		<result column="api_send_task_json" property="api_send_task_json" />
    </resultMap>

    <resultMap type="ChangeChannelTask" id="ChangeChannelTaskResultMap">
        <result column="task_id" property="task_id" />
        <result column="unicom_pa_id" property="unicom_pa_id" />
        <result column="mobile_pa_id" property="mobile_pa_id" />
        <result column="telcom_pa_id" property="telcom_pa_id" />
        <result column="unicom_callmdn" property="unicom_callmdn" />
        <result column="mobile_callmdn" property="mobile_callmdn" />
        <result column="telcom_callmdn" property="telcom_callmdn" />
        <result column="sp_id" property="sp_id" />
    </resultMap>

	<select id="listPageAuditTask" parameterType="TaskInfo"
		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
		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="platform!=null and platform!='' and platform >0">
			and s.platform =#{platform}
		</if>
		<if test="sp_name!=null and sp_name!=''">
			and s.sp_name like CONCAT('%',#{sp_name},'%')
		</if>
		<if test="sp_id!=null and sp_id>0">
			and s.id =#{sp_id}
		</if>
		<if test="content!=null and content!=''">
			and ak.content like CONCAT('%',#{content},'%')
		</if>
		<if test="sign!=null and sign!=''">
			and ak.content like CONCAT('',#{sign},'%')
		</if>
		<if test="phone!=null and phone!=''">
			and ak.phone =#{phone}
		</if>
		<if test="send_type!=null and send_type>0">
			and ak.send_type =#{send_type}
		</if>
		<if test="task_id!=null and task_id>0">
			and ak.id =#{task_id}
		</if>
		<if test="createTime!=null and createTime!=''">
            and ak.createtime&gt;=str_to_date(#{createTime},'%Y-%m-%d %H:%i:%S')
        </if>

        <if test="createTime1!=null and createTime1!=''">
            and ak.createtime&lt;=str_to_date(#{createTime1},'%Y-%m-%d %H:%i:%S')
        </if>
        <if test="otype>0">
        	and ak.otype=#{otype}
        </if>
		<if test="sp_saler!=null and sp_saler!=''">
			and s.sp_saler = #{sp_saler}
		</if>
		<if test="salerList!=null and salerList.size()>0">
			and s.sp_saler in
			<foreach collection="salerList" index="index"  item="item" open="(" separator="," close=")">
				'${item}'
			</foreach>
		</if>
	</select>

	<select id="listPageCronTask" parameterType="TaskInfo"
		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,
		concat(ak.unicommdns,",",ak.mobilemdns,",",ak.telcommdns) mdns,
		s.sp_name,
		s.platform,
		s.sp_desc,
		ak.otype,
		DATE_FORMAT(ak.cron_time,'%Y-%m-%d %H:%i:%s') cron_time
		from
		sms_api_crontab_task ak, sms_sp_info s
		where s.id=ak.sp_id and ak.status=2
		<if test="platform!=null and platform!='' and platform >0">
			and s.platform =#{platform}
		</if>
		<if test="sp_name!=null and sp_name!=''">
			and s.sp_name like CONCAT('%',#{sp_name},'%')
		</if>
		<if test="sp_id!=null and sp_id>0">
			and s.id =#{sp_id}
		</if>
		<if test="content!=null and content!=''">
			and ak.content like CONCAT('%',#{content},'%')
		</if>
		<if test="sign!=null and sign!=''">
			and ak.content like CONCAT('',#{sign},'%')
		</if>
		<if test="send_type!=null and send_type>0">
			and ak.send_type =#{send_type}
		</if>
		<if test="task_id!=null and task_id>0">
			and ak.id =#{task_id}
		</if>
		<if test="createTime!=null and createTime!=''">
            and ak.createtime&gt;=str_to_date(#{createTime},'%Y-%m-%d %H:%i:%S')
        </if>

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

    <select id="getTaskInfoByContent" parameterType="TaskInfo" resultMap="auditTaskMap">
        select tk.id,
        tk.sp_id,
        tk.content,
        st.createid,
        st.createtime,
        tk.total,
        tk.allnumber,
        tk.unicomnumber,
        tk.mobilenumber,
        tk.telecomnumber,
        tk.task_type,
        tk.status,
        sp.sp_audit_type,
        tk.send_type,
        tk.otype
        from sms_send_audit_task tk,
        sms_api_setting sp,
        sms_send_task st
        where sp.sp_id = tk.sp_id
        AND tk.user_audit_status = 0
        AND tk.id = st.ID
        <if test="sp_id!=null and sp_id>0">
            and tk.sp_id=#{sp_id}
        </if>
        <if test="content!=null and content!=null">
            and tk.content=#{content}
        </if>
        <if test="otype>0">
        	and tk.otype=#{otype}
        </if>
        	limit 0,10000
    </select>

    <select id="listPageAuditResult" parameterType="AuditResult" resultMap="auditResultMap">
        select
        s.sp_name,
        ar.id,
        ar.sp_id,
        ar.taskid,
        ar.audit_opeator,
        ar.create_time,
        ar.audit_result,
		ar.api_send_task_json,
		ar.audit_content,
        ar.audit_desc
        from sms_sp_audit_result ar
		left join sms_sp_info s on s.id = ar.sp_id
		<where>
			<if test="audit_result != null and audit_result != 0">
				and ar.audit_result = #{audit_result}
			</if>

			<if test="sp_name!=null and sp_name!=''">
				and s.sp_name like CONCAT('%',#{sp_name},'%')
			</if>

			<if test="sp_id!=null and sp_id>0">
				and s.id =#{sp_id}
			</if>

			<if test="taskid!=null and taskid>0">
				and ar.taskid =#{taskid}
			</if>

			<if test="createTime!=null and createTime!=''">
				and ar.create_time&gt;=str_to_date(#{createTime},'%Y-%m-%d')
			</if>

			<if test="createTime1!=null and createTime1!=''">
				and ar.create_time&lt;=DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL 1 DAY)
			</if>
		</where>
        order by ar.create_time desc
    </select>

	<select id="auditResultSendTask" parameterType="AuditResult" resultMap="auditResultMap">
		select
			tk.total,
	        tk.content,
	        tk.createtime,
	        tk.allnumber,
	        tk.send_type,
	        tk.unicomnumber,
	        tk.mobilenumber,
	        tk.telecomnumber
	        from sms_send_task tk
	        where tk.id=#{taskid}
	        limit 1
	</select>

    <insert id="insertAuditResult"  useGeneratedKeys="true" keyProperty="id"  parameterType="AuditResult">
        insert into sms_sp_audit_result
        (SP_ID,TASKID,AUDIT_OPEATOR,AUDIT_RESULT,AUDIT_DESC,create_time) values
        (#{sp_id},#{taskid},#{audit_opeator,jdbcType=VARCHAR},#{audit_result},#{audit_desc,jdbcType=VARCHAR},sysdate())
    </insert>

    <delete id="deleteAuditTaskById" parameterType="TaskInfo">
        delete from sms_send_audit_task where id=#{task_id}
        <if test="otype>0">
        	and otype=#{otype}
        </if>
    </delete>

     <update id="updateAuditTaskById" parameterType="TaskInfo">
        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=#{task_id}
	        <if test="otype>0">
	        	and otype=#{otype}
	        </if>
        </where>

    </update>


    <select id="getAuditTaskInfoById" parameterType="TaskInfo" resultType="TaskInfo">
        select tk.id task_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="sp_id!=null and sp_id>0">
	            and tk.sp_id=#{sp_id}
	        </if>
	        <if test="task_id!=null and task_id>0">
	            and tk.id=#{task_id}
	        </if>
	        <if test="otype>0">
	            and tk.otype=#{otype}
	        </if>
        </where>
    </select>

      <select id="getAuditTaskById" parameterType="int" resultType="long">
        select DISTINCT id from sms_send_audit_task where id=#{id}
    </select>
     <select id="getChangeTaskByTaskId" resultType="ChangeChannelTask">
    	select * from sms_change_channel_task where task_id=#{task_id} limit 1
    </select>
    <insert id="insertChangeTask" parameterType="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="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!=''">
    			telecom_pa_id=#{telecom_pa_id}, telcom_callmdn=#{telecom_callmdn}
    		</if>
    	</set>
    	where task_id=#{task_id}
    </update>

    <insert id="insertListChangeTask" parameterType="java.util.ArrayList">
        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
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.task_id,jdbcType=NUMERIC},
            #{item.mobile_pa_id,jdbcType=NUMERIC},
            #{item.unicom_pa_id,jdbcType=NUMERIC},
            #{item.telecom_pa_id,jdbcType=NUMERIC},
            #{item.mobile_callmdn,jdbcType=VARCHAR},
            #{item.unicom_callmdn,jdbcType=VARCHAR},
            #{item.telecom_callmdn,jdbcType=VARCHAR},
            #{item.sp_id,jdbcType=NUMERIC}
            )
        </foreach>

    </insert>
  <!--  sh  -->
   <select id="listAuditResult" parameterType="TaskInfo" resultMap="auditTaskMap">
		select tk.sp_id,tk.id,tk.allnumber,tk.total from
		sms_send_audit_task tk,sms_sp_info si
		where tk.sp_id=si.id
		and tk.id in
		<foreach item="taskids" index="index" collection="taskids" open="(" separator="," close=")">
        	#{taskids}
        </foreach>

	</select>
	<insert id="insertListAuditResult"
		parameterType="java.util.ArrayList">
		insert into sms_sp_audit_result
		(SP_ID,TASKID,AUDIT_OPEATOR,AUDIT_RESULT,AUDIT_DESC,create_time)
		values
		<foreach collection="list" item="item" index="index"
			separator=",">
			(
			#{item.sp_id,jdbcType=NUMERIC},
			#{item.taskid,jdbcType=NUMERIC},
			#{item.audit_opeator,jdbcType=VARCHAR},
			#{item.audit_result,jdbcType=VARCHAR},
			#{item.audit_desc,jdbcType=VARCHAR},
			sysdate()
			)
		</foreach>

	</insert>
	<update id="updateListAuditSmsStatus"
		parameterType="java.util.ArrayList">
		update
		sms_send_audit_task tk set
		tk.status=2
		where tk.id in
		<foreach item="list" index="index" collection="list" open="("
			separator="," close=")">
			#{list}
		</foreach>
	</update>
	<select id="listSearchAuditResult" parameterType="TaskInfo"
		resultMap="auditTaskMap">
		select tk.sp_id,tk.id,si.sp_name,tk.content,tk.allnumber,tk.total,tk.send_type from
		sms_send_audit_task tk,sms_sp_info si
		where tk.sp_id=si.id
		and tk.status=1
		<if test="sp_id!=null and sp_id>0">
			and tk.sp_id=#{sp_id}
		</if>
		<if test="task_id!=null and task_id>0">
			and tk.id=#{task_id}
		</if>
		<if test="sp_name!=null and sp_name>0">
			and si.sp_name=#{sp_name}
		</if>
		<if test="send_type!=null and send_type>0">
			and tk.send_type =#{send_type}
		</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="createTime!=null and createTime!=''">
            and tk.createtime&gt;=str_to_date(#{createTime},'%Y-%m-%d %H:%i:%S')
        </if>

        <if test="createTime1!=null and createTime1!=''">
            and tk.createtime&lt;=str_to_date(#{createTime1},'%Y-%m-%d %H:%i:%S')
        </if>

		limit 0,10000
	</select>

	<update id="updateListAuditSmsStatusForSearch"
		parameterType="TaskInfo">
		update
		sms_send_audit_task tk set
		tk.status=#{status},tk.audit_reason =#{audit_reason}
		where tk.status=1
		<if test="sp_id!=null and sp_id>0">
			and tk.sp_id=#{sp_id}
		</if>
		<if test="task_id!=null and task_id>0">
			and tk.id=#{task_id}
		</if>
		<if test="sp_name!=null and sp_name>0">
			and si.sp_name=#{sp_name}
		</if>
		<if test="send_type!=null and send_type>0">
			and tk.send_type =#{send_type}
		</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="createTime!=null and createTime!=''">
            and tk.createtime&gt;=str_to_date(#{createTime},'%Y-%m-%d %H:%i:%S')
        </if>

        <if test="createTime1!=null and createTime1!=''">
            and tk.createtime&lt;=str_to_date(#{createTime1},'%Y-%m-%d %H:%i:%S')
        </if>
        <if test="otype>0">
        	and tk.otype=#{otype}
        </if>
	</update>
	<delete id="deleteListAuditTaskById" parameterType="Map">
		delete from sms_send_audit_task where id in
		<foreach item="list" index="index" collection="list" open="(" separator="," close=")">
        	#{list}
        </foreach> and otype=#{otype}
	</delete>
	<select id="getListAuditTask" parameterType="int" resultMap="auditTaskMap">
		select tk.sp_id,tk.id,tk.allnumber,tk.total,sp.sp_audit_type,tk.audit_reason,tk.content,tk.send_type,tk.task_type,tk.otype ,tk.user_id createid from
		sms_send_audit_task tk,sms_api_setting sp
		where sp.sp_id = tk.sp_id
		and tk.status=2
		limit 0,10000
	</select>
	<select id="getListAuditTaskFailed" parameterType="int" resultMap="auditTaskMap">
		select tk.sp_id,tk.id,tk.allnumber,tk.total,sp.sp_audit_type,tk.audit_reason
		,tk.content,tk.send_type,tk.task_type,tk.otype,tk.user_id createid
		 from
		sms_send_audit_task tk,sms_api_setting sp
		where sp.sp_id = tk.sp_id
		and tk.status=3
		limit 0,10000
	</select>

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

	<update id="addEditAuditTaskRecord" parameterType="cn.roadm.bo.AuditEditRequestVO">
		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},#{newContent},#{newContent},#{newContent},#{editType},#{createTime},
		           #{status},#{spId},#{remark},#{userName},#{otype},#{template},#{sourceType},#{taskId})
	</update>

	<select id="listPageInterAuditTask" parameterType="TaskInfo"
        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
        from
        sms_send_audit_task ak, sms_sp_info s
        where countrylist is not null and s.id=ak.sp_id and ak.status=1
        <if test="platform!=null and platform!='' and platform >0">
            and s.platform =#{platform}
        </if>
        <if test="sp_name!=null and sp_name!=''">
            and s.sp_name like CONCAT('%',#{sp_name},'%')
        </if>
        <if test="sp_id!=null and sp_id>0">
            and s.id =#{sp_id}
        </if>
        <if test="content!=null and content!=''">
            and ak.content like CONCAT('%',#{content},'%')
        </if>
        <if test="sign!=null and sign!=''">
            and ak.content like CONCAT('',#{sign},'%')
        </if>
        <if test="phone!=null and phone!=''">
            and ak.phone =#{phone}
        </if>
        <if test="send_type!=null and send_type>0">
            and ak.send_type =#{send_type}
        </if>
        <if test="task_id!=null and task_id>0">
            and ak.id =#{task_id}
        </if>
        <if test="createTime!=null and createTime!=''">
            and ak.createtime&gt;=str_to_date(#{createTime},'%Y-%m-%d %H:%i:%S')
        </if>
        <if test="createTime1!=null and createTime1!=''">
            and ak.createtime&lt;=str_to_date(#{createTime1},'%Y-%m-%d %H:%i:%S')
        </if>
    </select>

    <select id="getCountrylistById" parameterType="long" resultType="string">
        select countrylist
        from sms_send_audit_task
        where id = #{id}
    </select>

	<select id="getUserIdById" resultType="string">
		select user_id
		from sms_send_audit_task
		where id = #{id} and sp_id = #{spid} and otype = #{otype}
	</select>

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