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

    <resultMap type="AuditMasterPlate" id="AuditMasterPlateResultMap">
        <id column="id" property="id" />
        <result column="content" property="content" />
        <result column="sp_id" property="sp_id" />
        <result column="sp_name" property="sp_name" />
        <result column="api_valid" property="api_valid" />
        <result column="platform_valid" property="platform_valid" />
        <result column="type" property="type" />
        <result column="mobilechannel" property="mobilechannel" />
        <result column="unicomchannel" property="unicomchannel" />
        <result column="telecomchannel" property="telecomchannel" />
    </resultMap>

    <select id="listPageAuditMasterPlate" parameterType="AuditMasterPlate"
        resultMap="AuditMasterPlateResultMap">
        select  s.id,s.content,s.sp_id,s.api_valid,s.platform_valid,sp.sp_name,s.remark,s.status,s.type,
        starttime,endtime,mobilechannel,unicomchannel,telecomchannel,DATE_FORMAT(s.createtime,'%Y-%m-%d %H:%i:%s') as createTime from sms_masterplate s left join sms_sp_info sp on sp.id=s.sp_id
        <where>
        	<if test="id!=null and id!=''">
        		and s.id=#{id}
        	</if>
        	<if test="content!=null and content!=''">
	            and s.content like CONCAT('%',#{content},'%') 
	        </if>
	        <if test="sp_id!=null and sp_id>=0">
	            and s.sp_id=#{sp_id} 
	        </if>
	       	<if test="sp_name!=null and sp_name!=''">
	            and sp.sp_name like CONCAT('%',#{sp_name},'%') 
	        </if>
	        <if test="status!=null ">
	            and s.status=#{status} 
	        </if>
	        <if test="mobilechannel!=null and mobilechannel!='-1'">
	        	and mobilechannel=#{mobilechannel}
	        </if>
	        <if test="unicomchannel!=null and unicomchannel!='-1'">
	        	and unicomchannel=#{unicomchannel}
	        </if>
	        <if test="telecomchannel!=null and telecomchannel!='-1'">
	        	and telecomchannel=#{telecomchannel}
	        </if>
        </where>
        
        order by s.id desc

    </select>
    <select id="exportAuditMaster" parameterType="AuditMasterPlate"
        resultType="Map">
        select  s.id,s.content,s.sp_id,s.api_valid,s.platform_valid,sp.sp_name from sms_masterplate s left join sms_sp_info sp on s.sp_id = sp.id
        <where>
            <if test="content!=null and content!=''">
                and s.content like CONCAT('%',#{content},'%')
            </if>
           <if test="sp_id!=null and sp_id>=0">
                and s.sp_id=#{sp_id}
            </if>
           <if test="sp_name!=null and sp_name!=''">
                and sp.sp_name like CONCAT('%',#{sp_name},'%')
            </if>
            <if test = "mobilechannel != null">
                and s.mobilechannel=#{mobilechannel}
            </if>
            <if test = "unicomchannel != null">
                and s.unicomchannel=#{unicomchannel}
            </if>
            <if test = "telecomchannel != null">
                and s.telecomchannel=#{telecomchannel}
            </if>
            <if test="status!=null ">
                and s.status=#{status}
            </if>
        </where>

    </select>

    <update id="updateAuditMaster" parameterType="AuditMasterPlate">
        update sms_masterplate
        <set>
            <if test = "mobilechannel != null">
                mobilechannel=#{mobilechannel},
            </if>
            <if test = "unicomchannel != null">
                unicomchannel=#{unicomchannel},
            </if>
            <if test = "telecomchannel != null">
                telecomchannel=#{telecomchannel},
            </if>
        </set>
        where id=#{id}
    </update>
    <select id="getAuditMasterPlateById" parameterType="int"
        resultMap="AuditMasterPlateResultMap">
    select s.id,s.content,s.sp_id,s.api_valid,s.platform_valid,sp.sp_name, starttime,endtime,s.`type`,s.mobilechannel,s.unicomchannel,s.telecomchannel from
     sms_masterplate s left join sms_sp_info sp on sp.id=s.sp_id where s.id=#{id}
    </select>

    <update id="updateAuditMasterPlateById" parameterType="AuditMasterPlate">
        update
        sms_masterplate set
        sp_id=#{sp_id},
        content=#{content},
        api_valid = #{api_valid},
        platform_valid = #{platform_valid},
        starttime=#{starttime},
        endtime=#{endtime},
        updatetime=#{updatetime},
        `type`=#{type}
        where
        id=#{id}
    </update>


    <insert id="insertAuditMasterPlate"  useGeneratedKeys="true" keyProperty="id" parameterType="AuditMasterPlate">
        insert into sms_masterplate
        (CONTENT,SP_ID,api_valid,platform_valid,starttime,`type`,endtime,createtime) values (#{content,jdbcType=VARCHAR},#{sp_id},#{api_valid},#{platform_valid},#{starttime},#{type},#{endtime},NOW())
    </insert>
    
     <insert id="addMaterPlateBatch"   useGeneratedKeys="true" keyProperty="id" parameterType="java.util.List">  
    insert into sms_masterplate (CONTENT,SP_ID,api_valid,platform_valid,starttime,endtime,mobilechannel,unicomchannel,
    telecomchannel,mobile_suffix,unicom_suffix,telecom_suffix,fixed_telecom_suffix,fixed_unicom_suffix,fixed_mobile_suffix,
    user_name,createtime,`type`)
    VALUES
    <foreach collection="list" item="item" index="index" separator=",">
        (#{item.content},#{item.sp_id},#{item.api_valid},#{item.platform_valid},#{item.starttime},#{item.endtime},#{item.mobilechannel},
        #{item.unicomchannel},#{item.telecomchannel},#{item.mobile_suffix},#{item.unicom_suffix},#{item.telecom_suffix},
        #{item.fixed_telecom_suffix},#{item.fixed_unicom_suffix},#{item.fixed_mobile_suffix},#{item.user_name},NOW(),#{type})
    </foreach>
</insert>  
    

    <delete id="deleteAuditMasterPlateById" parameterType="int">
        delete from sms_masterplate where id=#{id}
    </delete>

    <select id="findSystemConfig" parameterType="String" resultType="cn.roadm.bo.SystemConfig">
           select id,config_key as configKey,value,descp from sms_system_config where config_key like CONCAT('%',#{key})
    </select>

    <update id="updateMdnNumber" parameterType="cn.roadm.bo.SystemConfig">
          update sms_system_config set value = #{value} where id = #{id}
    </update>
    
	<update id="updateListAuditTemStatus"
		parameterType="Map">
		update
		sms_masterplate tk set
		tk.status=#{status} ,tk.api_valid =#{api_valid}
		where tk.status=0 and tk.id in
		<foreach item="list" index="index" collection="list" open="("
			separator="," close=")">
			#{list}
		</foreach>
	</update>
	
	<select id="getAuditBypassById" parameterType="int" resultType="AuditMasterPlate">
		select id,content,mobilechannel,unicomchannel,telecomchannel,mobile_suffix,unicom_suffix,telecom_suffix,
		fixed_telecom_suffix,fixed_unicom_suffix,fixed_mobile_suffix,status
		 from sms_masterplate where id=#{id}
	</select>

	<update id="editAuditMaster" parameterType="AuditMasterPlate">
		update sms_masterplate set mobilechannel=#{mobilechannel},unicomchannel=#{unicomchannel},telecomchannel=#{telecomchannel},
		mobile_suffix=#{mobile_suffix},telecom_suffix=#{telecom_suffix},unicom_suffix=#{unicom_suffix},fixed_mobile_suffix=#{fixed_mobile_suffix},
		fixed_telecom_suffix=#{fixed_telecom_suffix},fixed_unicom_suffix=#{fixed_unicom_suffix} where id =#{id}
	</update>

    <select id="queryAuditTemplateById" resultType="java.lang.String">
        select content from sms_masterplate where id = #{tid}
    </select>
</mapper>