<?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.ChannelAdminMapper">
    <resultMap  id="BaseResultMap" type="ChannelAdmin" >
        <result    column="channel_admin_id"    property="channelAdminId" />
        <result    column="channel_id"    property="channelId" />
        <result    column="channel_type"    property="channelType" />
        <result    column="channel_admin_open_id"    property="channelAdminOpenId" />
        <result    column="channel_admin_status"    property="channelAdminStatus" />
        <result    column="channel_admin_create_time"    property="channelAdminCreateTime" />
        <result    column="channel_admin_update_time"    property="channelAdminUpdateTime" />
    </resultMap>
    <sql    id="Base_List" >
        t.channel_admin_id,
        t.channel_id,
        t.channel_type,
        t.channel_admin_open_id,
        t.channel_admin_status,
        t.channel_admin_create_time,
        t.channel_admin_update_time
    </sql>

    <select id="getChannelAdminList" resultMap="BaseResultMap" parameterType="ChannelAdmin" >
        select
        <include refid="Base_List" />
        from tb_iamberry_channel_admin t
        <where>
            <if test="channelAdminId != null ">
                AND t.channel_admin_id = #{channelAdminId}
            </if >
            <if test="channelId != null ">
                AND t.channel_id = #{channelId}
            </if >
            <if test="channelType != null ">
                AND t.channel_type = #{channelType}
            </if >
            <if test="channelAdminOpenId != null and channelAdminOpenId != ''">
                AND t.channel_admin_open_id = #{channelAdminOpenId}
            </if >
            <if test="channelAdminStatus != null ">
                AND t.channel_admin_status = #{channelAdminStatus}
            </if >
        </where>
    </select>

    <select id="getChannelAdminById" resultMap="BaseResultMap" parameterType="Integer" >
        select
        <include refid="Base_List" />
        from tb_iamberry_channel_admin t
        where t.channel_admin_id= #{channelAdminId}
    </select>

    <select id="listOrderCount" resultType="Integer" parameterType="Order" >
        SELECT
          COUNT(so.sales_orderid)
        FROM
          tb_iamberry_shop_sales_order so
        LEFT JOIN tb_iamberry_main_channel mc ON so.sales_channel_type = 1 AND so.sales_channel_id = mc.main_channel_id
        LEFT JOIN tb_iamberry_child_channel cc ON so.sales_channel_type = 2 AND so.sales_channel_id = cc.child_channel_id
        <where>
            <if test="channelIds != null ">
                (mc.main_channel_id in
                <foreach collection="channelIds" item="item" index="index" separator="," open="(" close=")">
                    #{item}
                </foreach >
                OR cc.main_channel_id in
                <foreach collection="channelIds" item="item" index="index" separator="," open="(" close=")">
                    #{item}
                </foreach >
                )
            </if >
            <if test="salesTransportationType != null ">
                AND so.sales_transportation_type = #{salesTransportationType}
            </if >
            <if test="salesStatus != null ">
                AND so.sales_status = #{salesStatus}
            </if >
            <if test="salesStatus == null ">
                AND so.sales_status IN (2,3,5,6,7,8,9,10,11,12)
            </if >
        </where>
    </select>


    <insert id="save" parameterType="ChannelAdmin" >
        insert into
        tb_iamberry_channel_admin
        (
        channel_id,
        channel_type,
        channel_admin_open_id,
        channel_admin_status
        )
        values
        (
        #{channelId},
        #{channelType},
        #{channelAdminOpenId},
        #{channelAdminStatus}
        )
    </insert>

    <insert id="saveList" parameterType="java.util.List" >
        insert into
        tb_iamberry_channel_admin
        (
        channel_id,
        channel_type,
        channel_admin_open_id,
        channel_admin_status
        )
        values
        <foreach collection="list" item="node" index="index" separator="," open="" close="">
            (
            #{node.channelId},
            #{node.channelType},
            #{node.channelAdminOpenId},
            #{node.channelAdminStatus}
            )
        </foreach >
    </insert>

    <update id="update" parameterType="ChannelAdmin" >
        update
        tb_iamberry_channel_admin
        <set >
            <if test="channelId != null ">
                channel_id = #{channelId},
            </if >
            <if test="channelType != null ">
                channel_type = #{channelType},
            </if >
            <if test="channelAdminOpenId != null and channelAdminOpenId != ''">
                channel_admin_open_id = #{channelAdminOpenId},
            </if >
            <if test="channelAdminStatus != null ">
                channel_admin_status = #{channelAdminStatus},
            </if >
        </set >
        where channel_admin_id= #{channelAdminId}
    </update>

    <delete id="delete" parameterType="Integer" >
        delete FROM
        tb_iamberry_channel_admin
        where channel_admin_id=#{channelAdminId}
    </delete>

    <delete id="deleteList" parameterType="String" >
        delete FROM
        tb_iamberry_channel_admin
        where channel_admin_id  in
        <foreach collection="ids" item="item" index="index" separator="," open="(" close=")">
            #{item}
        </foreach >
    </delete>

    <select id="listOrderPage" resultType="Order" parameterType="Order" >
        SELECT
          so.*
        FROM
          tb_iamberry_shop_sales_order so
        LEFT JOIN tb_iamberry_main_channel mc ON so.sales_channel_type = 1 AND so.sales_channel_id = mc.main_channel_id
        LEFT JOIN tb_iamberry_child_channel cc ON so.sales_channel_type = 2 AND so.sales_channel_id = cc.child_channel_id
        <where>
            <if test="channelIds != null ">
                (mc.main_channel_id in
                    <foreach collection="channelIds" item="item" index="index" separator="," open="(" close=")">
                        #{item}
                    </foreach >
                OR cc.main_channel_id in
                    <foreach collection="channelIds" item="item" index="index" separator="," open="(" close=")">
                        #{item}
                    </foreach >
                )
            </if >
            <if test="salesTransportationType != null ">
                AND so.sales_transportation_type = #{salesTransportationType}
            </if >
            <if test="salesStatus != null ">
                AND so.sales_status = #{salesStatus}
            </if >
            <if test="salesStatus == null ">
                AND so.sales_status IN (2,3,5,6,7,8,9,10,11,12)
            </if >
        </where>
        ORDER BY so.sales_create_date DESC
    </select>

</mapper>