<?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.nantian.cloud.dbcapp.mapper.jly.SysInfoMapper">


    <insert id="renewLoginLog">
        insert into ${dbName}.user_login_log
		select log.id, log.create_by name, log.create_time datetime, log.login_type, log.remote_addr
		from ${dbName}.sys_log log,(
			select log_id from ${dbName}.user_login_log order by log_id desc limit 0,1
		) t where log.id > t.log_id
        <if test="title!=null and title!=''">
            and log.title = #{title}
        </if>
    </insert>
    <insert id="insertAccount">
        insert into ${dbName}.sys_user(username,enterprise_code,user_real_name,dept_id,password,create_time,update_time,tenant_id)
        values(#{enterpriseCode},#{enterpriseCode},#{cmpName},#{regionCode},'$2a$10$6jfXV7QwUoqcfj61kMyUt.51X1LYGdJZzAF/XP5CWkvLT0DfUm9VG',(select DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s')),(select DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s')),1);
        insert into ${dbName}.sys_user_role(user_id,role_id) select user_id,27 from ${dbName}.sys_user where del_flag = 0 and enterprise_code = #{enterpriseCode};
        insert into ${dbName}.sys_user_role(user_id,role_id) select user_id,45 from ${dbName}.sys_user where del_flag = 0 and enterprise_code = #{enterpriseCode};
    </insert>

    <select id="getAccount" resultType="java.util.Map">
        select '累计分配账户' title, count(1) total,  count(case when enterprise_code = '' or enterprise_code is null then 1 end) gov, count(case when enterprise_code != '' and  enterprise_code is not null then 1 end) com
        from ${dbName}.sys_user where del_flag = 0 and user_real_name not like '%测试%' and user_dept not like '%0%' and user_dept not like '%9%'
        <if test="startDate != null and startDate != '' ">
            and left(create_time,10) &gt;= #{startDate}
        </if>
        <if test="startDate != null and startDate != '' ">
            and left(create_time,10) &lt;= #{endDate}
        </if>
        union
        select '累计登录次数' title, count(1) total, count(case when s.enterprise_code = '' or s.enterprise_code is null then 1 end) gov, count(case when s.enterprise_code != '' and s.enterprise_code is not null then 1 end) com
        from ${dbName}.user_login_log g left join ${dbName}.sys_user s on g.name = s.username where 1=1 and s.user_dept not like '%0%' and s.user_dept not like '%9%'
        <if test="startDate != null and startDate != '' ">
            and left(g.datetime,10) &gt;= #{startDate}
        </if>
        <if test="startDate != null and startDate != '' ">
            and left(g.datetime,10) &lt;= #{endDate}
        </if>
        union
        select '累计活跃账号' title, count(1) total, count(case when enterprise_code = '' or enterprise_code is null then 1 end) gov, count(case when enterprise_code != '' and  enterprise_code is not null then 1 end) com
        from ${dbName}.sys_user where username in (
            select name from (select `name`, count(name) num from ${dbName}.user_login_log where 1=1
            <if test="startDate != null and startDate != '' ">
                and left(datetime,10) &gt;= #{startDate}
            </if>
            <if test="startDate != null and startDate != '' ">
                and left(datetime,10) &lt;= #{endDate}
            </if>
            group by name) t where t.num >=3
        ) and user_dept not like '%0%' and user_dept not like '%9%'
    </select>

    <select id="getLoginGroupByDate" resultType="java.util.Map">
        select left(g.datetime,7) month, count(1) total, count(case when s.enterprise_code = '' or s.enterprise_code is null then 1 end) gov, count(case when s.enterprise_code != '' and s.enterprise_code is not null then 1 end) com
        from ${dbName}.user_login_log g left join ${dbName}.sys_user s on g.name = s.username where 1=1 and s.user_dept not like '%0%' and s.user_dept not like '%9%'
        <if test="startDate != null and startDate != '' ">
            and left(g.datetime,10) &gt;= #{startDate}
        </if>
        <if test="startDate != null and startDate != '' ">
            and left(g.datetime,10) &lt;= #{endDate}
        </if>
        group by left(g.datetime,7) order by left(g.datetime,7) asc
    </select>

    <select id="getDeptLoginGroupByDate" resultType="java.util.Map">
        select left(g.datetime,7) month, count(1) total, count(case when s.dept_id='530000' then 1 end) province, count(case when s.dept_id!='530000' and right(s.dept_id,2)='00' then 1 end) city, count(case when right(s.dept_id,2)!='00' or s.dept_id is null or s.dept_id = '' then 1 end) county
        from ${dbName}.user_login_log g left join ${dbName}.sys_user s on g.name = s.username where (s.enterprise_code = '' or s.enterprise_code is null) and s.user_dept not like '%0%' and s.user_dept not like '%9%'
        <if test="startDate != null and startDate != '' ">
            and left(g.datetime,10) &gt;= #{startDate}
        </if>
        <if test="startDate != null and startDate != '' ">
            and left(g.datetime,10) &lt;= #{endDate}
        </if>
        group by left(g.datetime,7) order by left(g.datetime,7) asc
    </select>

    <select id="getActiveAccountGroupByDate" resultType="java.util.Map">
        select date month, count(1) total, count(case when enterprise_code = '' or enterprise_code is null then 1 end) gov, count(case when enterprise_code != '' and enterprise_code is not null then 1 end) com
        from (
            select left(g.datetime,7) date, g.name, count(1) num, user_dept, s.dept_id, s.enterprise_code
            from ${dbName}.user_login_log g left join ${dbName}.sys_user s on g.name = s.username where 1=1 and s.user_dept not like '%0%' and s.user_dept not like '%9%'
            <if test="startDate != null and startDate != '' ">
                and left(g.datetime,10) &gt;= #{startDate}
            </if>
            <if test="startDate != null and startDate != '' ">
                and left(g.datetime,10) &lt;= #{endDate}
            </if>
            group by left(g.datetime,7), g.name having(count(1)>=3)
        ) t group by date order by date
    </select>

    <select id="getDeptActiveAccountGroupByDate" resultType="java.util.Map">
        select date month, count(1) total, count(case when dept_id='530000' then 1 end) province, count(case when dept_id!='530000' and right(dept_id,2)='00' then 1 end) city, count(case when right(dept_id,2)!='00'  or dept_id is null or dept_id = '' then 1 end) county
        from (
            select left(g.datetime,7) date, g.name, count(1) num, user_dept, s.dept_id, s.enterprise_code
            from ${dbName}.user_login_log g left join ${dbName}.sys_user s on g.name = s.username where (enterprise_code = '' or enterprise_code is null) and s.user_dept not like '%0%' and s.user_dept not like '%9%'
            <if test="startDate != null and startDate != '' ">
                and left(g.datetime,10) &gt;= #{startDate}
            </if>
            <if test="startDate != null and startDate != '' ">
                and left(g.datetime,10) &lt;= #{endDate}
            </if>
            group by left(g.datetime,7), g.name having(count(1)>=3)
        ) t group by date order by date
    </select>
    <select id="getEpcByAccount" resultType="java.lang.String">
        select distinct enterprise_code from ${dbName}.sys_user where enterprise_code != '' and enterprise_code is not null and del_flag = '0'
    </select>


</mapper>
