<?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.yeejoin.amos.boot.module.jxiop.api.mapper.StationQrCodeStatisticsMapper">

<select id="getQrCodeCountByStation" resultType="java.util.Map">
    SELECT
    a.date,
    IFNULL( sum( b.red_num ), 0 ) AS redNum,
    IFNULL( sum( b.yellow_num ), 0 ) AS yellowNum,
    IFNULL( sum( b.green_num ), 0 ) AS greenNum
    FROM
    (
    SELECT
    DATE_FORMAT( DATE_ADD(( DATE( DATE_ADD( now(), INTERVAL - 7 DAY ))), INTERVAL @s DAY ), '%Y-%m-%d' ) AS date,
    @s := @s + 1 AS `index`
    FROM
    mysql.help_topic,
    ( SELECT @s := 1 ) temp
    WHERE
    DATEDIFF( now(), DATE( DATE_ADD( now(), INTERVAL - 7 DAY )) ) >= @s ) a
    LEFT JOIN
        ( SELECT * FROM station_qr_code_statistics
            <where>
                <if test="parentCode != null and parentCode != ''">
                    AND code LIKE concat(#{parentCode}, '%')
                </if>
                <if test="dataType != null and dataType != ''">
                    AND `type` = #{dataType}
                </if>
            </where>
        ) b ON a.date = b.record_date
    GROUP BY
    a.date
    </select>
</mapper>
