<?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.benyun.mapper.BrandZoneDao">
    <resultMap type="com.ruoyi.benyun.entity.BrandZone" id="BrandZoneResult">
        <result property="disId" column="dis_id"/>
        <result property="brandId" column="brand_id"/>
<!--        <result property="industryCode" column="industry_code"/>-->
        <result property="addrCode" column="addr_code"/>
        <result property="province" column="province"/>
        <result property="city" column="city"/>
        <result property="zone" column="zone"/>
        <result property="lat" column="lat"/>
        <result property="lng" column="lng"/>
        <result property="disCount" column="dis_count"/>
        <result property="avgScore" column="avg_score"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>
    <resultMap type="com.ruoyi.benyun.entity.BrandProvince" id="ProvinceResult">
        <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>
    <resultMap type="com.ruoyi.benyun.entity.BrandCity" id="CityResult">
        <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_zone_distribution`(dis_id,brand_id,brand_name,addr_code,dis_count,avg_score,update_time) values
        <foreach collection="brandZones" item="brandZone" separator="," >
            (#{brandZone.disId},#{brandZone.brandId},#{brandZone.brandName},#{brandZone.addrCode},#{brandZone.disCount},#{brandZone.avgScore},#{brandZone.updateTime})
        </foreach>
    </insert>
    <select id="searchByBrandId" resultMap="BrandZoneResult">
        select * from bl_brand_zone_distribution where brand_id = #{brandId}
    </select>
    <select id="searchByMulti" resultMap="BrandZoneResult">
        SELECT
            *
        FROM
            `bl_brand_zone_distribution`
        <trim prefix="where" prefixOverrides="and">
            <if test="brandIds != null">
                <foreach collection="brandIds" item="brandId" open="and brand_id IN (" separator="," close=")">
                    #{brandId,jdbcType=VARCHAR}
                </foreach>
            </if>
            <if test="addrCodes != null">
                <foreach collection="addrCodes" item="addrCode" open="and addr_code IN (" separator="," close=")">
                    #{addrCode,jdbcType=VARCHAR}
                </foreach>
            </if>
            <if test="industryCodes != null">
                <foreach collection="industryCodes" item="industryCode" open="and industry_code IN (" separator="," close=")">
                    #{industryCode,jdbcType=VARCHAR}
                </foreach>
            </if>
        </trim>
    </select>
    <select id="searchProvinceCountByBrandId" resultType="java.lang.Integer">
        SELECT
            COUNT(*)
        FROM
            (
                SELECT
                    province
                FROM
                    `bl_brand_zone_distribution`
                WHERE
                    brand_id = #{brandId}
                GROUP BY
                    province
            ) province
    </select>
    <select id="searchCityCountByBrandId" resultType="java.lang.Integer">
        SELECT
            COUNT(*)
        FROM
            (
                SELECT
                    city
                FROM
                    `bl_brand_zone_distribution`
                WHERE
                    brand_id = #{brandId}
                GROUP BY
                    city
            ) city
    </select>
    <select id="searchZoneCountByBrandId" resultType="java.lang.Integer">
        SELECT
            COUNT(*)
        FROM
            `bl_brand_zone_distribution`
        WHERE
            brand_id = #{brandId}
    </select>
    <select id="searchStatedBrandIds" resultType="java.lang.String">
        SELECT DISTINCT
            brand_id
        FROM
            `bl_brand_zone_distribution`
        <where>
            <if test="month != null">
                DATE_FORMAT(update_time, '%Y-%m') = #{month}
            </if>
        </where>
    </select>
    <select id="searchQuicklyStatistics" resultMap="BrandZoneResult">
        SELECT
            brand_id,
            brand_name,
            addr_code,
            COUNT(*) dis_count,
            ROUND(AVG(score), 2) avg_score
        FROM
            `ddt_store_wd` dsw,
            `ddt_wd_info` dwi
        WHERE
            dsw.wd_id = dwi.wd_id
        AND brand_id IS NOT NULL
        AND brand_id <![CDATA[<>]]> ''
        <if test="ins != null">
            <foreach collection="ins" item="in" open="and brand_id in (" separator="," close=")">
                #{in,jdbcType=VARCHAR}
            </foreach>
        </if>
        GROUP BY
            brand_id,
            brand_name,
            addr_code;
    </select>
    <select id="selectIns" resultType="java.lang.String">
        SELECT DISTINCT
            brand_id
        FROM
            `bl_brand_zone_distribution`;
    </select>
</mapper>
