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

    <resultMap type="FlowTaskInfo" id="FlowTaskInfoResultMap">
        <id column="id" property="taskid" />
        <result column="sp_id" property="spid" />
    </resultMap>
    <resultMap type="FlowChannel" id="FlowChannelResultMap">
        <id column="id" property="channelid" />
    </resultMap>

    <resultMap type="FlowChannelConfig" id="FlowChannelConfigResultMap">
        <id column="id" property="id" />
        <result column="sp_id" property="spid" />
        <result column="sp_name" property="sp_name" />
        <result column="unicom" property="unicom" />
        <result column="mobile" property="mobile" />
        <result column="telcom" property="telcom" />
    </resultMap>
    <resultMap type="FlowChannelConfig" id="FlowChannelInfoResultMap">
        <id column="id" property="id" />
        <result column="type" property="type" />
        <result column="spid" property="spid" />
        <result column="channelid" property="channelid" />
    </resultMap>

    <resultMap type="FlowPackage" id="FlowPackageResultMap">
        <id column="id" property="id" />
    </resultMap>
    <resultMap type="PlatForm" id="PlatFormResultMap">
        <id column="id" property="id" />
        <result column="domain_name" property="domain_name" />
    </resultMap>
    <resultMap type="SpOrderFlowInfo" id="SpOrderFlowResultMap">
        <id column="id" property="id" />
        <result column="sp_id" property="spid" />
    </resultMap>

    <select id="listPageFlowTaskList" parameterType="FlowTaskInfo"
        resultMap="FlowTaskInfoResultMap">
        select sp.sp_name,
        tk.id,
        tk.sp_id,
        tk.mdn_total,
        tk.flow_total,
        tk.status,
        tk.unicom_mdn_total,
        tk.mobile_mdn_total,
        tk.telcom_mdn_total,
        tk.unicom_flow,
        tk.mobile_flow,
        tk.telcom_flow,
        tk.succ,
        tk.fail,
        tk.flow_succ,
        tk.flow_fail,
        tk.unicom_mdn_succ,
        tk.mobile_mdn_succ,
        tk.telcom_mdn_succ,
        tk.isapi,
        tk.createtime,
        tk.endtime,
        tk.begintime,
        tk.unicom_flow_succ,
        tk.mobile_flow_succ,
        tk.telcom_flow_succ
        from
        FLOW_SEND_TASK tk, sms_sp_info sp
        where sp.id = tk.sp_id

        <if test="sp_name!=null and sp_name!=''">
            and sp.sp_name like '%'||#{sp_name}||'%'
        </if>

        <if test="spid!=null and spid>0">
            and tk.sp_id =#{spid}
        </if>
        <if test="begintime!=null and begintime!=''">
            and tk.createtime&gt;=to_date(#{begintime},'YYYY-MM-DD')
        </if>
        <if test="endtime!=null and endtime!=''">
            and tk.createtime&lt;=to_date(#{endtime},'YYYY-MM-DD')+1
        </if>
        order by tk.createtime desc
    </select>


    <select id="listPageFlowPackageList" parameterType="FlowPackage"
        resultMap="FlowPackageResultMap">
        select p.id,
        p.name,
        p.fee,
        p.remark,
        p.status,
        p.operator,
        pm.domain_name,
        p.create_time,
        p.platform,
        p.psize
        from flow_package p,
        im_portal_information pm
        where p.id=pm.id and p.status=0
        <if test="name!=null and name!=''">
            and p.name like '%'||#{name}||'%'
        </if>
        <if test="platform!=null and platform>0">
            and p.platform =#{platform}
        </if>
        order by p.create_time
    </select>

    <select id="listAllPlatForm" parameterType="PlatForm" resultMap="PlatFormResultMap">
        select pm.id,pm.domain_name from im_portal_information pm
    </select>

    <select id="getFlowPackage" parameterType="FlowPackage"
        resultMap="FlowPackageResultMap">
        select p.id,
        p.name,
        p.fee,
        p.remark,
        p.status,
        p.operator,
        p.create_time,
        p.platform,
        p.psize
        from flow_package p
        <where>
            p.id=#{id}
        </where>
    </select>

    <select id="listPageFlowChannelList" parameterType="FlowChannel"
        resultMap="FlowChannelResultMap">
        select fc.id,
        fc.name,
        fc.unicom_support,
        fc.mobile_support,
        fc.telcom_support,
        fc.url,
        fc.submit_speed,
        fc.discount,
        fc.status,
        fc.create_time
        from FLOW_CHANNEL
        fc
        where fc.status=1
        <if test="name!=null and name!=''">
            and fc.name like '%'||#{name}||'%'
        </if>
    </select>

    <select id="listAllFlowChannelList" parameterType="FlowChannel"
        resultMap="FlowChannelResultMap">
        select fc.id,
        fc.name,
        fc.unicom_support,
        fc.mobile_support,
        fc.telcom_support,
        fc.url,
        fc.submit_speed,
        fc.discount,
        fc.status,
        fc.create_time
        from FLOW_CHANNEL
        fc
        where fc.status=1
        <if test="name!=null and name!=''">
            and fc.name like '%'||#{name}||'%'
        </if>
        <if test="unicom_support!=null and unicom_support>0">
            and fc.unicom_support = #{unicom_support}
        </if>
        <if test="mobile_support!=null and mobile_support>0">
            and fc.mobile_support = #{mobile_support}
        </if>
        <if test="telcom_support!=null and telcom_support>0">
            and fc.telcom_support = #{telcom_support}
        </if>

    </select>

    <select id="getChannelConfigInfo" parameterType="FlowChannelConfig"
        resultMap="FlowChannelInfoResultMap">
        select
        cg.id,
        cg.type,
        cg.channelid,
        cg.spid
        from Flow_Sp_Channel_Config cg
        <where>
        <if test="spid!=null and spid>0">
             cg.spid = #{spid}
        </if>
        <if test="type!=null and type>0">
             and cg.type = #{type}
        </if>
        </where>
    </select>

    <select id="getFlowChannelInfo" parameterType="FlowChannel"
        resultMap="FlowChannelResultMap">
        select fc.id,
        fc.name,
        fc.unicom_support,
        fc.mobile_support,
        fc.telcom_support,
        fc.url,
        fc.submit_speed,
        fc.discount,
        fc.status,
        fc.create_time
        from FLOW_CHANNEL fc
        <where>
        <if test="channelid!=null and channelid>0">
             fc.id = #{channelid}
        </if>
        
        </where>
    </select>

    <select id="listPageFlowChannelConfList" parameterType="FlowChannelConfig"
        resultMap="FlowChannelConfigResultMap">
        select a.id, b.sp_id, b.sp_name, a.unicom, c.mobile, d.telcom
        from
        (select sp.id sp_id, sp.sp_name, sp.status
        from sms_sp_info sp
        where
        sp.status = 1) b
        left join (select cc.spid, fc.name unicom, cc.id
        from
        Flow_Sp_Channel_Config cc, flow_channel fc
        where fc.id = cc.channelid
        and cc.type = 1) a
        on b.sp_id = a.spid

        left join (select cc.spid,
        fc.name mobile, cc.id
        from Flow_Sp_Channel_Config cc, flow_channel fc
        where fc.id = cc.channelid
        and cc.type = 2) c
        on b.sp_id = c.spid
        left
        join (select cc.spid, fc.name telcom, cc.id
        from Flow_Sp_Channel_Config
        cc, flow_channel fc
        where fc.id = cc.channelid
        and cc.type = 3) d
        on
        b.sp_id = d.spid
        <where>
            <if test="sp_name!=null and sp_name!=''">
                b.sp_name like '%'||#{sp_name}||'%'
            </if>

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

        </where>
    </select>


    <select id="listPageSpOrderFlowPackageList" parameterType="SpOrderFlowInfo"
        resultMap="SpOrderFlowResultMap">
        select
        fs.id,
        fs.sp_id,
        sp.sp_name,
        fp.psize,
        fs.fee,
        fs.disconnt,
        pm.domain_name,
        fp.operator,
        fp.name,
        fp.platform,
        fp.create_time
        from
        Flow_Sp_Package fs,
        flow_package fp,
        sms_sp_info sp,
        im_portal_information pm
        where sp.id = fs.sp_id
        and fp.id
        = fs.package_id
        and pm.id = fp.platform
        and sp.status=1
        and fp.status=0
        <if test="sp_name!=null and sp_name!=''">
            and sp.sp_name like '%'||#{sp_name}||'%'
        </if>

        <if test="spid!=null and spid>0">
            and fs.sp_id =#{spid}
        </if>
        order by fs.create_time desc
    </select>


    <update id="updateFlowPackageStat" parameterType="FlowPackage">
        update
        flow_package set
        status=1 where id=#{id}
    </update>

    <update id="updateFlowPackageInfo" parameterType="FlowPackage">
        update
        flow_package set
        name=#{name,jdbcType=VARCHAR},
        fee=#{fee},
        remark=#{remark,jdbcType=VARCHAR},
        psize=#{psize},
        platform=#{platform},
        operator=#{operator}
        where id=#{id}
    </update>
    
    <update id="updateFlowChannel" parameterType="FlowChannel">
        update
        flow_channel set
        name=#{name,jdbcType=VARCHAR},
        unicom_support=#{unicom_support},
        mobile_support=#{mobile_support},
        telcom_support=#{telcom_support},
        url=#{url},
        area_type=#{area_type},
        discount=#{discount}
        where id=#{channelid}
    </update>
    
    
    <delete id="deleteSpFlowChannel" parameterType="int">
       delete from  Flow_Sp_Channel_Config where spid=#{spid}
    </delete>
    
    <insert id="insertFlowChannelConfig" parameterType="FlowChannelConfig" >
        <selectKey resultType="int"  order="BEFORE"  keyProperty="id">
          select flow_sp_channel_config_id.nextval from dual
        </selectKey>
        insert into FLOW_SP_CHANNEL_CONFIG  (id,type,channelid,spid) values (#{id},#{type},#{channelid},#{spid})
    </insert>
        
    <insert id="insertFlowChannel" parameterType="FlowChannel" >
        <selectKey resultType="int"  order="BEFORE"  keyProperty="channelid">
          select FLOW_CHANNEL_ID.nextval from dual
        </selectKey>
        insert into flow_channel  (id,name,unicom_support,mobile_support,telcom_support,url,discount,status,area_type,create_time) values (#{channelid},#{name},#{unicom_support},#{mobile_support}
        ,#{telcom_support},#{url},#{discount},1,#{area_type},sysdate)
    </insert>

    <insert id="saveFlowPackageInfo" parameterType="FlowPackage">
        <selectKey resultType="int" order="BEFORE" keyProperty="id">
            select flow_package_id.nextval from dual
        </selectKey>
        insert into flow_package
        (id,name,fee,remark,status,operator,create_time,platform,psize) values
        (#{id},#{name},#{fee},#{remark,jdbcType=VARCHAR},0,#{operator},sysdate,#{platform},#{psize})
    </insert>
</mapper>