<?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="com.iamberry.wechat.service.mapper.MainChannelMapper">
    <resultMap  id="BaseResultMap" type="MainChannel" >
        <result    column="main_channel_id"    property="mainChannelId" />
        <result    column="main_channel_open_id"    property="mainChannelOpenId" />
        <result    column="main_channel_name"    property="mainChannelName" />
        <result    column="main_channel_docking_name"    property="mainChannelDockingName" />
        <result    column="main_channel_docking_tel"    property="mainChannelDockingTel" />
        <result    column="main_channel_bank"    property="mainChannelBank" />
        <result    column="main_channel_bank_account"    property="mainChannelBankAccount" />
        <result    column="main_channel_bank_name"    property="mainChannelBankName" />
        <result    column="main_channel_bank_branch"    property="mainChannelBankBranch" />
        <result    column="main_channel_qrcode"    property="mainChannelQrcode" />
        <result    column="main_channel_status"    property="mainChannelStatus" />
        <result    column="main_channel_desc"    property="mainChannelDesc" />
        <result    column="main_channel_create_time"    property="mainChannelCreateTime" />
        <result    column="main_channel_update_time"    property="mainChannelUpdateTime" />
    </resultMap>
    <sql    id="Base_List" >
        t.main_channel_id,
        t.main_channel_open_id,
        t.main_channel_name,
        t.main_channel_docking_name,
        t.main_channel_docking_tel,
        t.main_channel_bank,
        t.main_channel_bank_account,
        t.main_channel_bank_name,
        t.main_channel_bank_branch,
        t.main_channel_qrcode,
        t.main_channel_status,
        t.main_channel_desc,
        t.main_channel_create_time,
        t.main_channel_update_time
    </sql>

    <select id="getMainChannelList" resultType="MainChannel" parameterType="MainChannel" >
        select
        <include refid="Base_List" />,
        (CASE WHEN cr.rebackAmount is NULL THEN 0 ELSE cr.rebackAmount END) rebackAmount
        from tb_iamberry_main_channel t
        LEFT JOIN (
        SELECT
        c.reback_channel_id reback_channel_id,
        SUM(c.reback_amount) rebackAmount
        FROM
        tb_iamberry_channel_rebate c
        WHERE
        c.reback_channel_type = 1
        AND c.reback_status = 2
        ) cr ON t.main_channel_id = cr.reback_channel_id
        <where>
            <if test="mainChannelId != null ">
                AND t.main_channel_id = #{mainChannelId}
            </if >
            <if test="mainChannelOpenId != null and mainChannelOpenId != ''">
                AND t.main_channel_open_id = #{mainChannelOpenId}
            </if >
            <if test="mainChannelName != null and mainChannelName != ''">
                AND t.main_channel_name  like  CONCAT ('%',#{mainChannelName},'%')
            </if >
            <if test="mainChannelDockingName != null and mainChannelDockingName != ''">
                AND t.main_channel_docking_name  like  CONCAT ('%',#{mainChannelDockingName},'%')
            </if >
            <if test="mainChannelDockingTel != null and mainChannelDockingTel != ''">
                AND t.main_channel_docking_tel  like  CONCAT ('%',#{mainChannelDockingTel},'%')
            </if >
            <if test="mainChannelStatus != null ">
                AND t.main_channel_status = #{mainChannelStatus}
            </if >
        </where>
        GROUP BY
        t.main_channel_id
        ORDER BY t.main_channel_id DESC
    </select>
    <select id="getMainChannelById" resultType="MainChannel" parameterType="Integer" >
        select
        <include refid="Base_List" />,
        (CASE WHEN cr.rebackAmount is NULL THEN 0 ELSE cr.rebackAmount END) rebackAmount
        from tb_iamberry_main_channel t
        LEFT JOIN (
        SELECT
        c.reback_channel_id reback_channel_id,
        SUM(c.reback_amount) rebackAmount
        FROM
        tb_iamberry_channel_rebate c
        WHERE
        c.reback_channel_type = 1
        AND c.reback_status = 2
        ) cr
        on t.main_channel_id = cr.reback_channel_id
        where
        t.main_channel_id= #{mainChannelId}
        GROUP BY t.main_channel_id
    </select>
    <insert id="save" parameterType="MainChannel" >
        insert into
        tb_iamberry_main_channel
        (
        main_channel_open_id,
        main_channel_name,
        main_channel_docking_name,
        main_channel_docking_tel,
        main_channel_bank,
        main_channel_bank_account,
        main_channel_bank_name,
        main_channel_bank_branch,
        main_channel_qrcode,
        main_channel_status,
        main_channel_desc
        )
        values
        (
        #{mainChannelOpenId},
        #{mainChannelName},
        #{mainChannelDockingName},
        #{mainChannelDockingTel},
        #{mainChannelBank},
        #{mainChannelBankAccount},
        #{mainChannelBankName},
        #{mainChannelBankBranch},
        #{mainChannelQrcode},
        #{mainChannelStatus},
        #{mainChannelDesc}
        )
    </insert>
    <update id="update" parameterType="MainChannel" >
        update
        tb_iamberry_main_channel
        <set >
            <if test="mainChannelOpenId != null and mainChannelOpenId != ''">
                main_channel_open_id = #{mainChannelOpenId},
            </if >
            <if test="mainChannelName != null and mainChannelName != ''">
                main_channel_name = #{mainChannelName},
            </if >
            <if test="mainChannelDockingName != null and mainChannelDockingName != ''">
                main_channel_docking_name = #{mainChannelDockingName},
            </if >
            <if test="mainChannelDockingTel != null and mainChannelDockingTel != ''">
                main_channel_docking_tel = #{mainChannelDockingTel},
            </if >
            <if test="mainChannelBank != null and mainChannelBank != ''">
                main_channel_bank = #{mainChannelBank},
            </if >
            <if test="mainChannelBankAccount != null and mainChannelBankAccount != ''">
                main_channel_bank_account = #{mainChannelBankAccount},
            </if >
            <if test="mainChannelBankName != null and mainChannelBankName != ''">
                main_channel_bank_name = #{mainChannelBankName},
            </if >
            <if test="mainChannelBankBranch != null and mainChannelBankBranch != ''">
                main_channel_bank_branch = #{mainChannelBankBranch},
            </if >
            <if test="mainChannelQrcode != null and mainChannelQrcode != ''">
                main_channel_qrcode = #{mainChannelQrcode},
            </if >
            <if test="mainChannelStatus != null ">
                main_channel_status = #{mainChannelStatus},
            </if >
            <if test="mainChannelDesc != null and mainChannelDesc != ''">
                main_channel_desc = #{mainChannelDesc},
            </if >
        </set >
        where main_channel_id= #{mainChannelId}
    </update>
</mapper>