<?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.ruoyi.demo.mapper.BrandStatisticsDao">
    <resultMap type="com.ruoyi.demo.entity.BrandStatistics" id="BrandStatisticsResult">
        <result property="brandId" column="brand_id"/>
        <result property="brandName" column="brand_name"/>
        <result property="coverProvinceCount" column="cover_province_count"/>
        <result property="coverCityCount" column="cover_city_count"/>
        <result property="coverZoneCount" column="cover_zone_count"/>
        <result property="businessCount" column="business_count"/>
        <result property="pauseBusinessCount" column="pause_business_count"/>
        <result property="noBusinessCount" column="no_business_count"/>
        <result property="total" column="total"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>
    <insert id="insertAll">
        insert into `bl_brand_statistics` values
        <foreach collection="brandStatistics" item="stat" separator=",">
            (#{stat.brandId},#{stat.brandName},#{stat.coverProvinceCount},#{stat.coverCityCount},#{stat.coverZoneCount},#{stat.businessCount},#{stat.pauseBusinessCount},#{stat.noBusinessCount},#{stat.total},#{stat.updateTime})
        </foreach>
    </insert>
    <select id="searchByBrandId" resultMap="BrandStatisticsResult">
        select * from bl_brand_statistics where brand_id = #{brandId}
    </select>
    <select id="searchList" resultMap="BrandStatisticsResult">
        select * from bl_brand_statistics order by total desc;
    </select>
    <select id="searchStated" resultType="java.lang.String">
        SELECT DISTINCT
            brand_id
        FROM
            `bl_brand_statistics`
    </select>
    <select id="searchByStatistics" resultType="com.ruoyi.demo.entity.BrandStatistics">
        SELECT
            cover_province_count,
            cover_city_count,
            cover_zone_count,
            business_count,
            pause_business_count,
            no_business_count
        FROM
            (
                SELECT
                    COUNT(*) cover_province_count
                FROM
                    `bl_brand_province_distribution`
                WHERE
                    brand_id = #{brandId}
            ) province,
            (
                SELECT
                    COUNT(*) cover_city_count
                FROM
                    `bl_brand_city_distribution`
                WHERE
                    brand_id = #{brandId}
            ) city,
            (
                SELECT
                    COUNT(*) cover_zone_count
                FROM
                    `bl_brand_zone_distribution`
                WHERE
                    brand_id = #{brandId}
            ) zone,
	(
		SELECT
			COUNT(*) business_count
		FROM
			`ddt_store_wd`
		WHERE
			business_status = '正常'
		AND brand_id = #{brandId}
	) nomal,
	(
		SELECT
			COUNT(*) pause_business_count
		FROM
			`ddt_store_wd`
		WHERE
			business_status = '暂停'
		AND brand_id = #{brandId}
	) pause,
	(
		SELECT
			COUNT(*) no_business_count
		FROM
			`ddt_store_wd`
		WHERE
			business_status = '尚未营业'
		AND brand_id = #{brandId}
	) noOpen
    </select>
</mapper>
