<?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.BrandCityDao">
    <resultMap type="com.ruoyi.demo.entity.BrandCity" id="BrandCityResult">
        <result property="disId" column="dis_id"/>
        <result property="brandId" column="brand_id"/>
        <result property="addrCode" column="addr_code"/>
        <result property="city" column="city"/>
        <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_city_distribution` values
        <foreach collection="cities" item="city" separator=",">
            (#{city.disId},#{city.brandId},#{city.addrCode},#{city.city},#{city.lat},#{city.lng},#{city.disCount},#{city.updateTime})
        </foreach>
    </insert>
    <select id="searchByBrandId" resultMap="BrandCityResult">
        select * from bl_brand_city_distribution where brand_id = #{brandId}
    </select>
    <select id="searchByBrandIdOrderByCity" resultMap="BrandCityResult">
        SELECT
            category.addr_code,
            category.city,
            lat_gd lat,
            lng_gd lng,
            dis_count
        FROM
            `ddt_addr_category` category,
            (
                SELECT
                    city,
                    SUM(dis_count) dis_count
                FROM
                    `bl_brand_zone_distribution`
                WHERE
                    brand_id = #{brandId}
                AND DATE_FORMAT(update_time, '%Y-%m') = #{month}
                GROUP BY
                    city
            ) c
        WHERE
            category.city = c.city
          AND district = ''
          AND c.city <![CDATA[<>]]> ''
    </select>
    <select id="selectIns" resultType="java.lang.String">
        SELECT DISTINCT
            brand_id
        FROM
            `bl_brand_city_distribution`;
    </select>
    <select id="selectCountByBrandId" resultType="java.lang.Integer">
        SELECT
            COUNT(*) cover_city_count
        FROM
            `bl_brand_city_distribution`
        WHERE
            brand_id = #{brandId}
    </select>
</mapper>
