<?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.BrandEvolveDao">
    <select id="selectByMulti" resultType="com.ruoyi.benyun.entity.vo.BrandEvolveVo">
        SELECT
            brand_id,
            brand_name,
            SUM(count) total,
            DATE_FORMAT(stat_time, '%Y-%m') time
        FROM
            `bl_brand_evolve_statistics`
        <where>
            brand_id = #{brandId}
            <if test="addrCodes != null">
                <foreach collection="addrCodes" item="code" open="AND addr_code IN (" separator="," close=")">
                    #{code,jdbcType=VARCHAR}
                </foreach>
            </if>
            <if test="typeCodes != null">
                <foreach collection="typeCodes" item="code" open="AND type_code_by IN (" separator="," close=")">
                    #{code,jdbcType=VARCHAR}
                </foreach>
            </if>
        </where>
        GROUP BY
            brand_id,
            brand_name,
            time
        ORDER BY
            time;
    </select>
    <insert id="insertAll">
        insert into `bl_brand_evolve_statistics` values
        <foreach collection="evolves" item="e" separator=",">
            (#{e.statId},#{e.brandId},#{e.brandName},#{e.addrCode},#{e.typeCodeBy},#{e.count},#{e.statTime})
        </foreach>
    </insert>
    <select id="selectAllByStatistics" resultType="com.ruoyi.benyun.entity.BrandEvolve">
        SELECT
            brand_id,
            brand_name,
            addr_code,
            type_code_by,
            COUNT(*) count,
	        DATE_FORMAT(collect_time, '%Y-%m-%d') stat_time
        FROM
            `ddt_store_wd` dsw,
            `ddt_wd_info` dwi
        WHERE
            dsw.wd_id = dwi.wd_id
        GROUP BY
            brand_id,
            brand_name,
            addr_code,
            type_code_by,
            stat_time
        ORDER BY
            stat_time;
    </select>
</mapper>
