<?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.BrandDao">
    <resultMap type="com.ruoyi.benyun.entity.Brand" id="BrandResult">
        <result property="brandId" column="brand_id"/>
        <result property="brandImg" column="brand_img"/>
        <result property="brandName" column="brand_name"/>
        <result property="industryCode" column="industry_code"/>
        <result property="enterpriseUsci" column="enterprise_usci"/>
        <result property="perCapitaConsumption" column="per_capita_consumption"/>
        <result property="commentCount" column="comment_count"/>
        <result property="score" column="score"/>
        <result property="menuInfo" column="menu_info"/>
        <result property="indexScore" column="index_score"/>
        <result property="createDate" column="create_date"/>
        <result property="audit" column="audit"/>
        <result property="collectPerson" column="collect_person"/>
        <result property="collectTime" column="collect_time"/>
        <result property="enterTime" column="enter_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="hash" column="hash"/>
    </resultMap>
    <select id="searchAll" resultMap="BrandResult">
        select * from ddt_brand
    </select>
    <update id="updatePerCapitaConsumptionByBrandId">
        UPDATE `ddt_brand`
        SET per_capita_consumption = (
            SELECT
                ROUND(
                            SUM(per_capita_consumption) / COUNT(*),
                            2
                    )
            FROM
                `ddt_store_wd`
            WHERE
                brand_id = #{brandId}
        )
        WHERE
            brand_id = #{brandId};
    </update>
    <update id="updateScoreByBrandId">
        UPDATE `ddt_brand`
        SET score = (
            SELECT
                ROUND(SUM(score) / COUNT(*), 2)
            FROM
                `ddt_store_wd`
            WHERE
                brand_id = #{brandId}
        )
        WHERE
            brand_id = #{brandId};
    </update>
    <update id="updateCommentCountByBrandId">
        UPDATE `ddt_brand`
        SET comment_count = (
            SELECT
                FLOOR(
                        SUM(comment_count) / COUNT(*)
                    )
            FROM
                `ddt_store_wd`
            WHERE
                brand_id = #{brandId}
        )
        WHERE
            brand_id = #{brandId};
    </update>
    <update id="updateIndexScoreByBrandId">
        UPDATE `ddt_brand`
        SET index_score = (
            SELECT
                ROUND(
                            SUM(index_score) / COUNT(*),
                            2
                    )
            FROM
                `ddt_store_wd`
            WHERE
                brand_id = #{brandId}
        )
        WHERE
            brand_id = #{brandId};
    </update>
    <select id="searchList" resultMap="BrandResult">
        select * from ddt_brand where brand_name like concat("%",#{text},"%")
    </select>
    <select id="searchById" resultMap="BrandResult">
        select * from ddt_brand where brand_id = #{brandId}
    </select>
    <select id="searchByIndustryCodeList" resultMap="BrandResult">
        select * from ddt_brand where industry_code = #{industryCode} and brand_id <![CDATA[<>]]> #{brandId}
    </select>
    <select id="searchNotIns" resultMap="BrandResult">
        SELECT
            *
        FROM
            `ddt_brand`
        <where>
            <if test="notIns != null">
                <foreach collection="notIns" item="ontIn" open="brand_id NOT IN (" separator="," close=")">
                    #{ontIn,jdbcType=VARCHAR}
                </foreach>
            </if>
        </where>
    </select>
    <select id="selectAllId" resultType="java.lang.String">
        SELECT DISTINCT
            brand_id
        FROM
            `ddt_brand`
        WHERE show_delete = 0;
    </select>
</mapper>
