<?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.BrandProvinceDao">
    <resultMap type="com.ruoyi.demo.entity.BrandProvince" id="BrandProvinceResult">
        <result property="disId" column="dis_id"/>
        <result property="brandId" column="brand_id"/>
        <result property="addrCode" column="addr_code"/>
        <result property="province" column="province"/>
        <result property="lat" column="lat"/>
        <result property="lng" column="lng"/>
        <result property="disCount" column="dis_count"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>
    <insert id="insertAll">
        insert into `bl_brand_province_distribution` values
        <foreach collection="provinces" item="prov" separator=",">
            (#{prov.disId},#{prov.brandId},#{prov.addrCode},#{prov.province},#{prov.lat},#{prov.lng},#{prov.disCount},#{prov.updateTime})
        </foreach>
    </insert>
    <select id="searchByBrandId" resultMap="BrandProvinceResult">
        select * from bl_brand_province_distribution where brand_id = #{brandId}
    </select>
    <select id="searchByBrandIdOrderByProvince" resultMap="BrandProvinceResult">
        SELECT
            category.addr_code,
            category.province,
            lat_gd lat,
            lng_gd lng,
            dis_count
        FROM
            `ddt_addr_category` category,
            (
                SELECT
                    province,
                    SUM(dis_count) dis_count
                FROM
                    `bl_brand_zone_distribution`
                WHERE
                    brand_id = #{brandId}
                AND DATE_FORMAT(update_time, '%Y-%m') = #{month}
                GROUP BY
                    province
            ) p
        WHERE
            category.province = p.province
          AND city = ''
    </select>
    <select id="selectIns" resultType="java.lang.String">
        SELECT DISTINCT
            brand_id
        FROM
            `bl_brand_province_distribution`;
    </select>
    <select id="selectCountByBrandId" resultType="java.lang.Integer">
        SELECT
            COUNT(*) cover_province_count
        FROM
            `bl_brand_province_distribution`
        WHERE
            brand_id = #{brandId}
    </select>
</mapper>
