<?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.qs.mp.channel.mapper.ChannelMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.qs.mp.channel.domain.Channel">
        <id column="channel_id" property="channelId" />
        <result column="channel_no" property="channelNo" />
        <result column="name" property="name" />
        <result column="parent_id" property="parentId" />
        <result column="level" property="level" />
        <result column="site_type" property="siteType" />
        <result column="site_name" property="siteName" />
        <result column="comm_rate" property="commRate" />
        <result column="province" property="province" />
        <result column="province_id" property="provinceId" />
        <result column="city" property="city" />
        <result column="city_id" property="cityId" />
        <result column="area" property="area" />
        <result column="area_id" property="areaId" />
        <result column="address" property="address" />
        <result column="contact" property="contact" />
        <result column="mobile" property="mobile" />
        <result column="user_id" property="userId" />
        <result column="money" property="money" />
        <result column="frozen_money" property="frozenMoney" />
        <result column="certify_status" property="certifyStatus" />
        <result column="verify_status" property="verifyStatus" />
        <result column="biz_license_pic" property="bizLicensePic" />
        <result column="door_pic" property="doorPic" />
        <result column="memo" property="memo" />
        <result column="invite_pic" property="invitePic" />
        <result column="status" property="status" />
        <result column="created_time" property="createdTime" />
        <result column="updated_time" property="updatedTime" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        channel_id, channel_no, name, parent_id, level, site_type, site_name, comm_rate, province, province_id, city, city_id, area, area_id, address, contact, mobile, user_id, money, frozen_money, certify_status, verify_status, biz_license_pic, door_pic, memo, invite_pic, status, created_time, updated_time
    </sql>

    <!-- 查询子渠道信息 -->
    <select id="selectChannelVoList" resultType="com.qs.mp.channel.domain.vo.ChannelVO">
		select t1.* , t2.name parentName
		from mp_channel t1
		left join mp_channel t2 on t1.parent_id = t2.channel_id
		${ew.customSqlSegment}
	</select>

	<!-- 查询渠道及其子渠道经销商数总和 -->
    <select id="getChannelSiteCnt" resultType="integer">
        select count(t1.channel_id) as siteCnt
		from mp_channel t1
		where t1.`level`= 0
		and (t1.channel_no like concat(#{channelNo}, '.%') or t1.channel_no = #{channelNo})
    </select>

  <!-- 查询子渠道信息 -->
  <select id="selectChannelList" resultType="com.qs.mp.channel.domain.vo.ChannelVO">
    select t1.* , t2.name parentName, t3.site_cnt, t4.child_cnt, t5.user_cnt
    from mp_channel t1
           left join mp_channel t2 on t1.parent_id = t2.channel_id
           left join (select q1.channel_id, count(q2.channel_id) as site_cnt
                      from mp_channel q1
                             left join mp_channel q2 on q2.channel_no like concat(q1.channel_no, '.%')
                      where q1.level > 0 and q2.level= 0
                      group by q1.channel_id) t3 on t3.channel_id = t1.channel_id
           left join (select q1.channel_id, count(q2.channel_id) as child_cnt
                      from mp_channel q1
                             left join mp_channel q2 on q2.channel_no like concat(q1.channel_no, '.%')
                      where q1.level > 0 and q2.level> 0
                      group by q1.channel_id) t4 on t4.channel_id = t1.channel_id
           left join (select q1.channel_id,sum(q2.user_cnt) as user_cnt
                      from mp_channel q1
                             left join (select b.channel_no, a.channel_id, count(a.id) AS user_cnt
                                        from mp_channel_user_rel a
                                               left join mp_channel b on a.channel_id=b.channel_id
                                        group by a.channel_id, b.channel_no) q2 on q2.channel_no like concat(q1.channel_no, '.%')
                      group by q1.channel_id
                    ) t5 on t5.channel_id=t1.channel_id
      ${ew.customSqlSegment}
  </select>

     <!-- 查询经销商列表信息 -->
    <select id="selectSaleSiteList" resultType="com.qs.mp.channel.domain.vo.ChannelVO">
      select t1.channel_id as channelId, t1.channel_no, t1.name, t1.parent_id, t1.level, t1.site_type, t1.site_name, t1.comm_rate as commRate, t1.province, t1.province_id, t1.city, t1.city_id, t1.area, t1.area_id, t1.address, t1.contact, t1.mobile, t1.user_id, t1.certify_status, t1.verify_status, t1.status, t1.created_time, t1.updated_time ,
             t2.off_line_qty_cnt, t2.off_line_sale_cnt, t4.sale_qty-t2.off_line_sale_cnt as on_line_sale_cnt, t3.user_cnt
      from mp_channel t1
             left join (
                select channel_id, SUM(  b.pkg_unit - b.sale_qty ) AS off_line_qty_cnt,
                       SUM(  b.sale_qty  ) AS off_line_sale_cnt
                from mp_channel_order_detail a
                       left join mp_ticket_package b on a.pkg_id=b.pkg_id
                group by a.channel_id) t2 on t1.channel_id = t2.channel_id
             left join (
                select channel_id, count(*) AS user_cnt
                from mp_channel_user_rel
                group by channel_id) t3 on t1.channel_id = t3.channel_id
             left join (
                select channel_id, sum(ticket_num) AS sale_qty
                from mp_user_ticket_order
                where status=1
                group by channel_id) t4 on t1.channel_id = t4.channel_id
        ${ew.customSqlSegment}
	</select>


	<!-- 统计新增经销商数列表 -->
    <select id="selectIndexSiteIncreaseList" resultType="com.qs.mp.admin.domain.vo.IndexVO">
		select
          DATE_FORMAT(t1.created_time, '%Y%m%d') AS time,
          COUNT(t1.channel_id) AS newSiteCnt
	     from `mp_channel` t1
	left join mp_channel t2 on t1.channel_id = t2.channel_id
		${ew.customSqlSegment}
		GROUP BY time
	</select>

</mapper>
