<?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.cmeeting.mapper.primary.UserAccessRecordMapper">

    <select id="statisticsByDate" resultType="java.util.Map">
        SELECT
            c.user_id as userId,
            s.name,
            sc.dept_id as deptId,
            <foreach collection="dateList" item="d" separator=",">
                cast(MAX(CASE WHEN time = #{d} THEN c.cnt ELSE 0 END) as char) AS `${d}`
            </foreach>
        FROM (
            SELECT
                user_id,
                date_format(create_time, '%Y-%m-%d') as time,
                count(user_id) AS cnt
            FROM
                user_access_record
            WHERE
                date_format(create_time, '%Y-%m-%d') BETWEEN #{startTime} AND #{endTime}
            GROUP BY user_id, date_format(create_time, '%Y-%m-%d')
        ) AS c
        inner join sys_user_sync as s on s.user_id = c.user_id
        <if test="search != null and search != ''">
            and (s.name like concat('%',#{search},'%') or s.user_id like concat('%',#{search},'%'))
        </if>
        inner join sys_user_sync_category as sc on sc.dept_id = s.dept_id
        GROUP BY c.user_id
    </select>
</mapper>