<?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.cf.imes.module.system.dal.mysql.user.AdminUserMapper">

    <select id="selectListByTerms" resultType="com.cf.imes.module.system.dal.dataobject.user.AdminUserDO"
            parameterType="map">
        SELECT id,
               username,
               password,
               nickname,
               remark,
               dept_id,
               post_ids,
               email,
               mobile,
               sex,
               avatar,
               status,
               login_ip,
               login_date,
               pinyin_initial,
               pinyin_full,
               organ_id,
               create_time,
               update_time,
               creator,
               updater,
               deleted
        FROM system_users
        WHERE deleted = 0
          AND organ_id = #{organId}
          AND status = #{status}
          AND (
                    nickname LIKE CONCAT('%', #{name}, '%')
                OR pinyin_full LIKE CONCAT('%', #{namePyAll}, '%')
                OR pinyin_initial LIKE CONCAT('%', #{namePyFirstChar}, '%')
            )
        ORDER BY
            id DESC
    </select>


    <select id="selectUserDataSource" resultType="java.util.Map">

        select ${filed}
        from system_users
        where organ_id =  #{organId}
          and deleted = false
          and nickname in
        <foreach collection="nameList" item="nameList" open="(" close=")" separator=",">
            #{nameList}
        </foreach>


    </select>

    <sql id="dateUpdate">
        <if test="req.unit != null and req.unit == @com.cf.imes.framework.common.enums.StatisticsUnit@QUARTER.getValue()">
            ,CONCAT(YEAR(o.update_time), '-', QUARTER(o.update_time)) as date
        </if>
        <if test="req.unit != null and req.unit == @com.cf.imes.framework.common.enums.StatisticsUnit@MONTH.getValue()">
            ,CONCAT(YEAR(o.update_time), '-', MONTH(o.update_time)) as date
        </if>
        <if test="req.unit != null and req.unit == @com.cf.imes.framework.common.enums.StatisticsUnit@WEEK.getValue()">
            ,CONCAT(YEAR(o.update_time), '-', MONTH(o.update_time), '-', FLOOR((DayOfMonth(o.update_time)-1)/7)+1) AS date
        </if>
        <if test="req.unit != null and req.unit == @com.cf.imes.framework.common.enums.StatisticsUnit@DAY.getValue()">
            ,CONCAT(YEAR(o.update_time), '-', MONTH(o.update_time), '-', DAY(o.update_time)) as date
        </if>
    </sql>

    <sql id="dateCreate">
        <if test="req.unit != null and req.unit == @com.cf.imes.framework.common.enums.StatisticsUnit@QUARTER.getValue()">
            ,CONCAT(YEAR(o.create_time), '-', QUARTER(o.create_time)) as date
        </if>
        <if test="req.unit != null and req.unit == @com.cf.imes.framework.common.enums.StatisticsUnit@MONTH.getValue()">
            ,CONCAT(YEAR(o.create_time), '-', MONTH(o.create_time)) as date
        </if>
        <if test="req.unit != null and req.unit == @com.cf.imes.framework.common.enums.StatisticsUnit@WEEK.getValue()">
            ,CONCAT(YEAR(o.create_time), '-', MONTH(o.create_time), '-', FLOOR((DayOfMonth(o.create_time)-1)/7)+1) AS
            date
        </if>
        <if test="req.unit != null and req.unit == @com.cf.imes.framework.common.enums.StatisticsUnit@DAY.getValue()">
            ,CONCAT(YEAR(o.create_time), '-', MONTH(o.create_time), '-', DAY(o.create_time)) as date
        </if>
    </sql>

    <select id="selectUserStatusGroupStatistics"
            resultType="com.cf.imes.module.system.dal.dataobject.user.UserStatusGroupStatisticsDO">
        select count(o.id) as userCount
        <if test="deleted">
            <include refid="dateUpdate"/>
        </if>
        <if test="!deleted">
            <include refid="dateCreate"/>
        </if>
        from system_users o
        where
        <if test="deleted">
            o.update_time between #{req.createTimeForQry[0]} and #{req.createTimeForQry[1]}
        </if>
        <if test="!deleted">
            o.create_time between #{req.createTimeForQry[0]} and #{req.createTimeForQry[1]}
        </if>
        and o.deleted = #{deleted}
        group by date;
    </select>
</mapper>