<?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.MmsTemplateMapper">

    <resultMap type="VideoSms" id="VideoSmsResultMap">
        <id column="id" property="id" />
        <result column="spid" property="spid" />
        <result column="sp_name" property="sp_name" />
        <result column="createtime" property="createtime" />
         <result column="num" property="num" />
        <result column="uchannelid" property="uchannelid" />
        <result column="mchannelid" property="mchannelid" />
        <result column="tchannelid" property="tchannelid" />
        <result column="sign" property="sign"/>
        <result column="reason" property="reason"/>
        <collection property="remoteTemplate" ofType="String"
                    select="queryRemoteTemplate" column="id">
        </collection>
    </resultMap>

 <resultMap type="ChannelConfig" id="ChannelConfigResult">
        <id column="id" property="id"/>
        <result column="opertor_type" property="opertor_type"/>
        <result column="channelid" property="channelId"/>
        <result column="sp_id" property="sp_id"/>
        <result column="createtime" property="createtime"/>
    </resultMap>
    <select id="queryRemoteTemplate" resultType="String">
        select concat(ifnull(channelid, '-'), '，', ifnull(remote_templateid, '-'), '，', CASE status
                                                                                            WHEN 1 THEN '待审核'
                                                                                            WHEN 2 THEN '审核通过'
                                                                                            ELSE '驳回'
            END,
                      case when status not in (1, 2) then concat('，', ifnull(reason, '')) else '' end
                   ) remoteTemplate
        from template_remote
        where local_template = #{id}
    </select>

    <resultMap id="YueXinTemplateMap" type="cn.roadm.bo.YueXinTemplateBO">
        <id column="id" property="id"/>
        <result column="spid" property="spId"/>
        <result column="sp_name" property="spName"/>
        <result column="content" property="content"/>
        <result column="createtime" property="createTime"/>
        <result column="status" property="status"/>
        <result column="name" property="name"/>
        <result column="source" property="source"/>
        <result column="factoryInfo" property="factoryInfo"/>
        <result column="reason" property="remark"/>
        <result column="relModeId" property="relModeId"/>
        <result column="uid" property="uid"/>
        <result column="operator" property="operator"/>
        <result column="relText" property="relText"/>
        <result column="customUrl" property="customUrl"/>
        <result column="modifyTime" property="modifyTime"/>
        <result column="mobile_channel" property="mobileChannel"/>
        <result column="unicom_channel" property="unicomChannel"/>
        <result column="telecom_channel" property="telecomChannel"/>
        <collection property="remoteTemplate" ofType="String"
                    select="queryYueXinRemoteTemplate" column="id">
        </collection>
    </resultMap>
    <select id="queryYueXinRemoteTemplate" resultType="String" parameterType="string">
        select concat(ifnull(channel_id,'-') ,'，',ifnull(remote_id,'-') , '，', CASE audit
                                                                                   WHEN 0 THEN '未审核'
                                                                                   WHEN 1 THEN '审核通过'
                                                                                   WHEN 2 THEN '审核失败'
                                                                                   ELSE ''
            END) remoteTemplate
        from kp_template_remote
        where local_id = #{id}
    </select>

    <resultMap id="YueXinChannelTemplateList" type="cn.roadm.bo.YueXinChannelTemplateBO">
        <id column="id" property="id"/>
        <result column="channel_id" property="channelId"/>
        <result column="remote_id" property="remoteId"/>
        <result column="local_id" property="localId"/>
        <result column="local_name" property="localName"/>
        <result column="reason" property="reason"/>
        <result column="audit" property="status"/>
        <result column="create_time" property="createTime"/>
        <result column="factory" property="factory"/>
        <result column="factory_info" property="factoryInfo"/>
    </resultMap>

    <select id="listPageSpMmsTemplate" parameterType="VideoSms"
        resultMap="VideoSmsResultMap">
        select sp.id spid,
        tl.id,
        tl.status,
        tl.name,
        tl.type,
        tl.subject,
        tl.content,
        tl.uid,
        tl.remark,
        DATE_FORMAT(tl.createtime, '%Y-%m-%d %H:%i:%s') createtime,
        tl.source,
        sp.sp_name,
        tcc.uchannelid,
		tcc.mchannelid,
		tcc.tchannelid,
		stl.num,
        tl.sign, tl.reason
        from  template_local tl left join  sms_sp_info sp
        on tl.spid = sp.id left join template_channel_config tcc on
		tl.id=tcc.templateid left join (select tlz.id id,count(trz.remote_templateid) num from template_local tlz left join template_remote trz on tlz.id=trz.local_template group by tlz.id) stl
		on stl.id=tl.id
        <where>
        <if test="name!=null and name!=''">
            tl.name like CONCAT('%',#{name},'%')
        </if>
        <if test="spid!=null and spid>0">
            and tl.spid=#{spid}
        </if>
        <if test="sp_name != null and sp_name != ''">
            and sp.sp_name like CONCAT('%',#{sp_name},'%')
        </if>
        <if test="sign != null and sign != ''">
            and tl.sign like CONCAT('%',#{sign},'%')
        </if>
        <if test="id!=null and id>0">
            and tl.id=#{id}
        </if>
            <if test="keyWord != null and keyWord != ''">
                and (tl.content->'$**.body' like CONCAT('%',#{keyWord},'%') OR tl.content->'$.subject' like CONCAT('%',#{keyWord},'%'))
            </if>
            <if test="statusList !=null and statusList.size()>0">
                and tl.status in
                <foreach item="item" index="index" collection="statusList" open="(" separator="," close=")">
                    ${item}
                </foreach>
            </if>
        <if test="createTime!=null and createTime!=''">
            and tl.createtime&gt;=str_to_date(#{createTime},'%Y-%m-%d')
        </if>
        <if test="createTime1!=null and createTime1!=''">
            and
            tl.createtime&lt;DATE_ADD(str_to_date(#{createTime1},'%Y-%m-%d'),INTERVAL 1 DAY)
        </if>
        <if test="uchannelid!=null and uchannelid!='-1' and uchannelid!=''">
        	and
        	tcc.uchannelid=#{uchannelid}
        </if>
        <if test="mchannelid!=null and mchannelid!='-1' and mchannelid!=''">
        	and
        	tcc.mchannelid=#{mchannelid}
        </if>
        <if test="tchannelid!=null and tchannelid!='-1' and tchannelid!=''">
        	and
        	tcc.tchannelid=#{tchannelid}
        </if>
        </where>
        order by tl.createtime desc
    </select>
	<select id="getMmsTemplateById" parameterType="VideoSms"
        resultType="VideoSms">
		resultMap="VideoSmsResultMap">
        select sp.id spid,
        tl.id,
        tl.status,
        tl.name,
        tl.type,
        tl.subject,
        tl.content,
        tl.uid,
        tl.remark,
        tl.createtime,
        tl.source,
        sp.sp_name
        from  template_local tl left join  sms_sp_info sp
        on tl.spid = sp.id
        where 1=1
        <if test="id!=null and id>0">
            and tl.id=#{id}
        </if>
        limit 1
	</select>
  <select id="listPageSpMmsChannnelTemplate" parameterType="VideoSms"
        resultMap="VideoSmsResultMap">
        select
        sp.channel_name,
        tr.id,
        tr.channelid,
        tr.createtime,
        tr.status,
        tr.remote_templateid,
        tr.local_template,
        tl.name,
        tr.reason

        from  template_remote tr left join  mms_send_channel_info sp
        on tr.channelid = sp.id
        left join template_local tl on tl.id = tr.local_template
        where 1=1

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

        <if test="channelid !=null and channelid >=0">
            and tr.channelid=#{channelid}
        </if>
        <if test="local_template !=null and local_template >0">
            and tl.id=#{local_template}
        </if>
        <if test="remote_templateid !=null and remote_templateid != ''">
            and tr.remote_templateid=#{remote_templateid}
        </if>
        order by tr.id desc

    </select>

    <update id="updateListAuditMmsTemStatus"
		parameterType="Map">
		update
		template_local set
		status=#{status} ,operator=#{operator}, reason=#{reason}
		where status=0 and id in
		<foreach item="list" index="index" collection="list" open="("
			separator="," close=")">
			#{list}
		</foreach>
	</update>


 	<select id="getSpMmsChannnelTemplate" parameterType="VideoSms"
        resultMap="VideoSmsResultMap">
        select
        sp.channel_name,
        tr.id,
        tr.channelid,
        tr.createtime,
        tr.status,
        tr.remote_templateid,
        tr.local_template,
        tl.name

        from  template_remote tr left join  mms_send_channel_info sp
        on tr.channelid = sp.id
        left join template_local tl on tl.id = tr.local_template
        where 1=1


        <if test="channelid !=null and channelid >=0">
            and tr.channelid=#{channelid}
        </if>
        <if test="local_template !=null and local_template >0">
            and tl.id=#{local_template}
        </if>
        limit 1

    </select>
    <delete id="deletemmsView" parameterType="string">
    delete from  template_remote where id=#{id}
  </delete>

	<insert id="insertMmsChannnelTemplate"
		parameterType="VideoSms">
		insert into
		template_local(channelid,createtime,status,remote_templateid,local_template)

		values(#{channelid},sysdate(),#{status},#{remote_templateid},#{local_template})

	</insert>


	<select id="getSpMmsTemplateChannnelConfig" parameterType="VideoSms"
        resultMap="VideoSmsResultMap">
        select
        tr.id,
        tr.templateid,
        tr.createtime,
        tr.uchannelid,
        tr.mchannelid,
        tr.tchannelid
        from  template_channel_config tr
        where  tr.templateid = #{templateid}
        limit 1
    </select>


   <select id="getMmsChannelConfigInfo" parameterType="ChannelConfig" resultMap="ChannelConfigResult">
	    select
	    cg.id,
	    cg.opertor_type,
	    cg.channelid,
	    cg.sp_id,
	    cg.createtime
	    from mms_sp_channel_config cg
	         where 1=1
	         and cg.sp_id in (select channeilid from template_remote)
	        <if test="sp_id!=null and sp_id>0">
	        and cg.sp_id = #{sp_id}
	        </if>
	        <if test="opertor_type!=null and opertor_type>0">
	        and cg.opertor_type = #{opertor_type}
	        </if>
    </select>

	<insert id="insertMmsTemplateChannnelConfig"
		parameterType="VideoSms">
		insert into
		template_channel_config(templateid,createtime,uchannelid,mchannelid,tchannelid)

		values(#{templateid},sysdate(),#{uchannelid},#{mchannelid},#{tchannelid})

	</insert>

	<update id="updateMmsTemplateChannnelConfig"
		parameterType="VideoSms">
		update template_channel_config
		set   createtime=sysdate(),uchannelid=#{uchannelid},mchannelid=#{mchannelid} , tchannelid=#{tchannelid}
		where 	templateid =#{templateid}
	</update>

	<insert id="insertTemplateRemote" parameterType="VideoSms" useGeneratedKeys="true" keyProperty="id">
		insert into template_remote(channelid,createtime,status,remote_templateid,local_template)
		values(#{channelid},now(),#{status},#{remote_templateid},#{local_template});
	</insert>

	<select id="getIsAlready" parameterType="VideoSms" resultType="VideoSms">
		select channelid from template_remote where channelid=#{channelid}
		 and remote_templateid=#{remote_templateid} and local_template=#{id}
	</select>

	<select id="getMmsContent" parameterType="VideoSms" resultType="VideoSms">
		select id,`subject`,content,`name`,spid from template_local WHERE id=#{id}
	</select>

    <select id="getMmsContentList" parameterType="VideoSms" resultType="VideoSms">
        select id,`subject`,content,`name`,spid from template_local
        <where>
            <if test="content!=null and content!=''">
                and content like concat('%',#{content},'%')
            </if>
            <if test="id!=null and id!=''">
                and id=#{id}
            </if>
            <if test="status!=null and status!=''">
                and status=#{status}
            </if>
            <if test="spid!=null and spid!=''">
                and spid=#{spid}
            </if>
            <if test="startTime != null and startTime != ''">
                and createtime &gt;= str_to_date(#{startTime},'%Y-%m-%d')
            </if>
            <if test="endTime != null and endTime != ''">
                and createtime &lt;= DATE_ADD(str_to_date(#{endTime},'%Y-%m-%d'),INTERVAL 1 DAY)
            </if>

    </where>
    </select>

	<update id="updateTemplateRemote" parameterType="VideoSms">
		update template_remote set remote_templateid=#{remote_templateid} where local_template=#{local_template} and channelid=#{channelid}
	</update>

	<insert id="copyTemplate" parameterType="VideoSms" useGeneratedKeys="true" keyProperty="id">
		insert into template_local
		select  #{id} `id`,type,content,`subject`,now() as `createtime`,1 as `status`,remark,#{spid} `spid`,uid, #{name} `name`,source,notifyUrl,
		#{operator} `operator`,sign,context, operType, modifyTime, reason, param_index from template_local where id=#{local_template}
	</insert>

    <update id="updateMmsTempContentText">
        update template_local
        <set>
        	<if test="content!=null and content!=''">
        		content = #{content},
        	</if>
            <if test="sign!=null and sign!=''">
                sign = #{sign},
            </if>
        	name=#{name},subject=#{subject}
        </set>
          where id = #{id}
    </update>

    <select id="queryMmsTemplate" resultType="cn.roadm.bo.MmsTemplateTestParams">
        SELECT
                a.channelid as channelId, a.remote_templateid as remoteId, a.local_template as localId,
                b.content, b.name, b.spid
        FROM
                template_remote a
                        LEFT JOIN template_local b ON b.id = a.local_template
        WHERE
                local_template = #{tid} limit 1
    </select>

    <update id="delTemplateByStatus">
        update template_local set status = 3 where id = #{tid}
    </update>

    <select id="listPageQueryAuditYueXinTemplate" resultMap="YueXinTemplateMap">
        select tp.id, content, tp.createtime, tp.status, tp.spid, sp.sp_name, name, source from aim_template_local tp
        left join sms_sp_info sp on tp.spid = sp.id
        <where>
            tp.status = 1
            <if test="id != null and id != ''">
                and tp.id = #{id}
            </if>
            <if test="spId != null and spId != ''">
                and tp.spid = #{spId}
            </if>
            <if test="spName != null and spName != ''">
                and sp.sp_name like concat('%',#{spName},'%')
            </if>
            <if test="keyWord != null and keyWord != ''">
                and tp.name like concat('%',#{keyWord},'%')
            </if>
            <if test="startTime != null and startTime != ''">
                and tp.createtime &gt;= STR_TO_DATE(#{startTime}, '%Y-%m-%d')
            </if>
            <if test="endTime != null and endTime != ''">
                and tp.createtime &lt;= DATE_ADD(STR_TO_DATE(#{endTime}, '%Y-%m-%d'), INTERVAL 1 DAY)
            </if>
        </where>
        order by tp.createtime desc
    </select>

    <select id="getYueXinTemplateInfoById" resultMap="YueXinTemplateMap">
        select id, content, relModeId, DATE_FORMAT(createtime, '%Y-%m-%d %H:%i:%s') as createtime, status, reason, spid, uid, name, source, operator, relText, customUrl, factoryInfo, modifyTime, mobile_channel, unicom_channel, telecom_channel from aim_template_local tp
        where tp.id = #{id}
    </select>

    <update id="batchUpdateYueXinTemplateSuccess">
        update aim_template_local set status = #{status}, operator = #{userName} where status = 1 and id in (
        <foreach collection="tid" item="item" separator=",">
            #{item}
        </foreach>
        )
    </update>

    <update id="batchUpdateYueXinTemplateFail">
        update aim_template_local set status = #{status}, reason = #{reason}, operator = #{userName} where status = 1 and id in (
        <foreach collection="idList" item="item" separator=",">
            #{item}
        </foreach>
        )
    </update>

    <update id="updateYueXinTemplateNameAndPages">
        update aim_template_local set name = #{name}
        <if test="content != null and content != ''">
            , content = #{content}
        </if>
        where id = #{id}
    </update>

    <select id="getYueXinChannelTemplateConfig" resultType="java.lang.String">
        select channelTemplatevar from aim_http_channel_template_config where channelid = #{channelId}
    </select>

    <select id="checkYueXinTemplateRemoteIsExist" resultType="java.lang.Integer">
        select count(1) from kp_template_remote where channel_id = #{channelId} and  factory = #{factory} and local_id = #{localTemplate}
    </select>

    <insert id="insertYueXinTemplateRemoteData">
        insert into kp_template_remote (factory, result, reason, tplMask, create_time, local_id, remote_id, remote_name, variable, send_package_size, content, factory_info, channel_id)
        value (#{factory}, null, '', '', now(), #{localTemplates}, '',  #{name}, '', null, null, '000000', #{channelId})
    </insert>

    <select id="listPageQueryLocalYueXinTemplate" resultMap="YueXinTemplateMap">
        select tp.id, content, tp.createtime, tp.status, tp.spid, sp.sp_name, name, source from aim_template_local tp
        left join sms_sp_info sp on tp.spid = sp.id
        <where>
            tp.status != 1
            <if test="status != null">
                and tp.status = #{status}
            </if>
            <if test="id != null and id != ''">
                and tp.id = #{id}
            </if>
            <if test="spId != null and spId != ''">
                and tp.spid = #{spId}
            </if>
            <if test="spName != null and spName != ''">
                and sp.sp_name like concat('%',#{spName},'%')
            </if>
            <if test="keyWord != null and keyWord != ''">
                and tp.name like concat('%',#{keyWord},'%')
            </if>
            <if test="startTime != null and startTime != ''">
                and tp.createtime &gt;= STR_TO_DATE(#{startTime}, '%Y-%m-%d')
            </if>
            <if test="endTime != null and endTime != ''">
                and tp.createtime &lt;= DATE_ADD(STR_TO_DATE(#{endTime}, '%Y-%m-%d'), interval 1 day)
            </if>
        </where>
        order by tp.createtime desc
    </select>

    <insert id="insertYueXinOfflineTemplateData">
        insert into kp_template_remote (factory, result, audit, reason, tplMask, create_time, local_id, remote_id, remote_name, variable, send_package_size, content, factory_info)
        values (#{factory}, '',  1, '', '', now(), #{localId}, #{remoteId}, #{name}, '', null, '', #{mobileFactory})
    </insert>

    <select id="selectSpListOptions" resultType="cn.roadm.bo.PageSelectOptionsVO">
        select id as value, sp_name as label from sms_sp_info where status = 1
    </select>

    <select id="selectThisYueXinTemplateCountBySpId" resultType="java.lang.Integer">
        select count(1) from aim_template_local where spid = #{spId} and id = #{localId}
    </select>

    <insert id="copyYueXinTemplate">
        insert into  aim_template_local (id, content, relModeId, createtime, status, reason, spid, uid, name, source, operator, relText, customUrl, factoryInfo, modifyTime, use_factory)
        values (#{id}, #{content}, #{relModeId}, now(), #{status}, '', #{spId}, #{uid}, #{name}, #{source}, #{operator}, #{relText}, #{customUrl}, #{factoryInfo}, #{modifyTime}, #{useFactory})
    </insert>

    <select id="getYueXinTemplateFactoryOptions" resultType="cn.roadm.bo.PageSelectOptionsVO">
        select distinct factory as value, factory as label from kp_template_remote where local_id = #{id}
    </select>

    <update id="updateYueXinTemplateChannel">
        update aim_template_local set mobile_channel = #{mobileChannel}, unicom_channel = #{unicomChannel}, telecom_channel = #{telecomChannel} where id = #{id}
    </update>

    <update id="deleteYueXinTemplate">
        update aim_template_local set status = 4 where id = #{id}
    </update>

    <select id="listPageYueXinChannelTemplateInfo" resultMap="YueXinChannelTemplateList">
        select tr.id, tr.channel_id, tr.factory, tr.remote_id, tr.local_id as local_id, tl.name as local_name, tr.reason, tr.audit, DATE_FORMAT(tr.create_time, '%Y-%m-%d %H:%i:%S') as create_time, tr.factory_info  from kp_template_remote tr left join aim_template_local tl on tr.local_id = tl.id
        <where>
            tr.local_id is not null
            <if test="channelId != null and channelId != ''">
                and tr.channel_id = #{channelId}
            </if>
            <if test="localId != null and localId != ''">
                and tr.local_id = #{localId}
            </if>
            <if test="localName != null and localName != ''">
                and tl.name like concat('%',#{localName},'%')
            </if>
            <if test="remoteId != null and remoteId != ''">
                and tr.remote_id like concat('%',#{remoteId},'%')
            </if>
        </where>
        order by tr.create_time desc
    </select>

    <delete id="deleteYueXinChannelTemplate">
        delete from kp_template_remote where id = #{id}
    </delete>

    <select id="listAimResourceReport" resultType="cn.roadm.bo.AimResourceReportBO">
        select id, local_id as localId, channel_id as channelId, original_url as originalUrl, report_id as reportId, resource_id as resourceId,
        resource_url as resourceUrl, status, reason, DATE_FORMAT(create_time, '%Y-%m-%d %H:%i:%S') as createTime, DATE_FORMAT(result_time, '%Y-%m-%d %H:%i:%S') as resultTime
        from aim_resource_report where local_id = #{localId, jdbcType=VARCHAR} and channel_id = #{channelId, jdbcType=VARCHAR}
    </select>

    <insert id="insertAimSyncResourceRecord">
        insert into aim_resource_report (local_id, channel_id, original_url, original_id, `status`, create_time) values
            (#{localId}, #{channelId}, #{originalUrl}, #{originalId}, #{status}, #{createTime})
    </insert>

    <select id="getResourceStatusOptions" resultType="cn.roadm.bo.PageSelectOptionsVO">
        select CONCAT(original_url, '-', channel_id) as label, `status` as value from aim_resource_report where local_id = #{localId}
    </select>

    <select id="getThisResourceIsExist" resultType="java.lang.Integer">
        select count(1) from aim_resource_report where local_id = #{localId} and channel_id = #{channelId} and original_id = #{resourceId}
    </select>

    <select id="getHttpChannelOptions" resultType="cn.roadm.bo.PageSelectOptionsVO">
        select aim_account as label, id as value from kp_aim_account where status = 0
    </select>

    <select id="listPageQueryYueXinChannelList" resultType="cn.roadm.bo.YueXinChannelListDO">
        select id as channelId, channel_name as channelName, status, PROTOCOL_TYPE as protocolType, is_support_unicom as isSupportUnicom,
        is_support_mobile as isSupportMobile, is_support_telecom as isSupportTelecom, `desc` channelDesc from aim_send_channel_info sc
        <where>
            sc.PROTOCOL_TYPE = '4'
            <if test="channelName != null and channelName != ''">
                and sc.CHANNEL_NAME like concat('%',#{channelName},'%')
            </if>
            <if test="status != null and status != -1">
                and sc.STATUS = #{status}
            </if>
        </where>
        order by id desc
    </select>

    <select id="listHttpAimChannelTemplate" resultType="cn.roadm.bo.HttpChannelTemplate">
        select a.id,a.templateCode,a.templateName,a.templateDefaultVar,a.templateVar,a.supplier,a.version from aim_http_channel_template a
        where a.status = 0
    </select>

    <select id="getYueXinChannelConfig" resultType="cn.roadm.bo.PaConfig">
         select a.id, a.is_support_unicom,a.is_support_telecom,a.is_support_mobile,  a.is_support_international,  a.channel_name name, a.mobile_prefix accessnumber,
               a.status,
                a.pa_url,a.protocol_type,a.batchsize,a.access_key,a.http_response,
                p.templateCode,p.channelTemplatevar,b.templateDefaultVar ,`desc`,a.price
        from aim_send_channel_info a left join aim_http_channel_template_config p  on a.id = p.channelid
        left join aim_http_channel_template b on p.templateCode = b.templateCode
        where  a.id = #{id}
               limit 1
    </select>

    <update id="updateAimChannelPaConfig">
        update aim_send_channel_info
		<set>
            <if test = "name != null">
                channel_name = #{name, jdbcType=VARCHAR},
            </if>
                is_support_unicom = #{is_support_unicom, jdbcType=VARCHAR},
                is_support_mobile = #{is_support_mobile, jdbcType=VARCHAR},
                is_support_telecom = #{is_support_telecom, jdbcType=VARCHAR},
            <if test = "ch_desc != null">
                ch_desc = #{ch_desc, jdbcType=VARCHAR},
            </if>
            <if test = "pa_url != null">
                pa_url = #{pa_url, jdbcType=VARCHAR},
            </if>
            <if test = "status != null">
                status = #{status, jdbcType=INTEGER},
            </if>
            <if test = "protocol_type != null">
                protocol_type = #{protocol_type, jdbcType=INTEGER},
            </if>
             <if test = "access_key != null">
                access_key= #{access_key, jdbcType=VARCHAR},
            </if>
            <if test = "batchsize != null">
                batchsize = #{batchsize, jdbcType=INTEGER},
            </if>
            <if test = "http_response != null">
                http_response= #{http_response, jdbcType=VARCHAR},
            </if>
            <if test = "accessnumber != null">
                accessnumber= #{accessnumber, jdbcType=VARCHAR},
            </if>
            <if test="desc!=null and desc!=''">
            	`desc`=#{desc,jdbcType=VARCHAR},
            </if>
		</set>
		where id = #{id, jdbcType=BIGINT}
    </update>

    <select id="getAimPaHttpChannelConfigByChannelId" resultType="cn.roadm.bo.PaConfig">
         select p.templateCode,p.channelTemplatevar from aim_http_channel_template_config p where  p.channelid=#{id} limit 1
    </select>

    <insert id="insertAimHttpTemplateConfig">
        insert into aim_http_channel_template_config (channelid, templateCode, channelTemplatevar, createtime)
        values (#{id,jdbcType=INTEGER}, #{templateCode,jdbcType=VARCHAR}, #{channelTemplatevar,jdbcType=VARCHAR},
                sysdate())
    </insert>

    <update id="updateAimHttpTemplateConfig">
         update aim_http_channel_template_config set templateCode=#{templateCode},channelTemplatevar=#{channelTemplatevar} where channelid=#{id}
    </update>

    <select id="getAimPaHttpConfigById" resultType="cn.roadm.bo.PaConfig">
        select a.id,
               a.is_support_unicom,
               a.is_support_telecom,
               a.is_support_mobile,
               a.is_support_international,
               a.channel_name  name,
               a.mobile_prefix accessnumber,
               a.status,
               a.pa_url,
               a.protocol_type,
               a.batchsize,
               a.access_key,
               a.http_response,
               p.templateCode,
               p.channelTemplatevar,
               b.templateDefaultVar,
               `desc`,
               a.price
        from aim_send_channel_info a
                 left join aim_http_channel_template_config p on a.id = p.channelid
                 left join aim_http_channel_template b on p.templateCode = b.templateCode
        where a.id = #{id}
        limit 1
    </select>

    <insert id="insertAimChannelPaConfig" useGeneratedKeys="true" keyProperty="id">
        insert into aim_send_channel_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="name != null">
                channel_name,
            </if>
            <if test="is_support_unicom != null">
                is_support_unicom,
            </if>
            <if test="is_support_mobile != null">
                is_support_mobile,
            </if>
            <if test="is_support_telecom != null">
                is_support_telecom,
            </if>
            <if test="is_support_international != null">
                is_support_international,
            </if>
            <if test="pa_url != null">
                pa_url,
            </if>
            <if test="status != null">
                status,
            </if>
            <if test="protocol_type != null">
                protocol_type,
            </if>
            <if test="access_key != null">
                access_key,
            </if>
            <if test="batchsize != null">
                batchsize,
            </if>
            <if test="http_response != null">
                http_response,
            </if>
            <if test="accessnumber != null">
                accessnumber,
            </if>
            <if test="desc != null">
                `desc`,
            </if>
            <if test="price != null">
                `price`,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id},
            </if>
            <if test="name != null">
                #{name, jdbcType=VARCHAR},
            </if>
            <if test="is_support_unicom != null">
                #{is_support_unicom, jdbcType=VARCHAR},
            </if>
            <if test="is_support_mobile != null">
                #{is_support_mobile, jdbcType=VARCHAR},
            </if>
            <if test="is_support_telecom != null">
                #{is_support_telecom, jdbcType=VARCHAR},
            </if>
            <if test="is_support_international != null">
                #{is_support_international, jdbcType=VARCHAR},
            </if>
            <if test="pa_url != null">
                #{pa_url, jdbcType=VARCHAR},
            </if>
            <if test="status != null">
                #{status, jdbcType=INTEGER},
            </if>
            <if test="protocol_type != null">
                #{protocol_type, jdbcType=INTEGER},
            </if>
            <if test="access_key != null">
                #{access_key, jdbcType=VARCHAR},
            </if>
            <if test="batchsize != null">
                #{batchsize, jdbcType=INTEGER},
            </if>
            <if test="http_response != null">
                #{http_response, jdbcType=VARCHAR},
            </if>
            <if test="accessnumber != null">
                #{accessnumber, jdbcType=VARCHAR},
            </if>
            <if test="desc != null">
                #{desc, jdbcType=VARCHAR},
            </if>
            <if test="price != null">
                #{price, jdbcType=VARCHAR},
            </if>
        </trim>
    </insert>

    <update id="updateAimByPrimaryKey">
        update aim_send_channel_info set STATUS = #{status,jdbcType=VARCHAR} where ID = #{id,jdbcType=INTEGER}
    </update>

    <select id="listAllYueXinChannelTemplate" resultType="cn.roadm.bo.PageSelectOptionsVO">
        select id as value, CHANNEL_NAME as label from aim_send_channel_info where STATUS = '1'
    </select>

    <select id="listAllYueXinChannels" resultType="cn.roadm.bo.ChannelInfo">
        select id, channel_name from aim_send_channel_info where STATUS = 1
    </select>

    <select id="getYueXinRemoteTemplateByRemoteId" resultType="java.lang.String">
        select factory_info from kp_template_remote where remote_id = #{remoteId}
    </select>

    <select id="getHttpChannelTemplateAim" resultType="cn.roadm.bo.HttpChannelTemplate">
        select a.id,a.templateCode,a.templateName,a.templateDefaultVar,a.templateVar,a.supplier,a.version from aim_http_channel_template a
        where a.templateCode =#{templateCode}
    </select>
    <select id="getYueXinSubjectJsonById" resultType="java.lang.String">
        select content from aim_template_local where id=#{id}
    </select>

    <update id="selectInsertYueXinRemoteTemplate">
        INSERT INTO kp_template_remote (`factory`, `result`, `state`, `audit`, `reason`, `tplMask`, `create_type`,
                                        `create_time`, `local_id`, `remote_id`, `remote_name`, `variable`,
                                        `send_package_size`, `content`, `factory_info`, `channel_id`)
        SELECT `factory`,`result`, `state`, `audit`, `reason`, `tplMask`, `create_type`, now(), #{id},
               `remote_id`, `remote_name`, `variable`, `send_package_size`, `content`,
               `factory_info`, `channel_id`
        FROM kp_template_remote
        WHERE local_id = #{oldId}
    </update>

    <select id="selectMmsRemoteTemplateOfChannelId" resultType="cn.roadm.bo.PageSelectOptionsVO">
        select tr.remote_templateid as value, tl.`name` as label from template_remote tr left join template_local tl on tr.local_template = tl.id where channelid = #{channelId}
    </select>

    <update id="resetAimSyncResourceRecord">
        update aim_resource_report set status = 0 where local_id = #{localId} and channel_id = #{channelId} and original_id = #{originalId}
    </update>
</mapper>
