<?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.css.simulation.resource.server.infra.db.mysql.mapper.LogMapper" >

    <insert id="insertOperationLog" parameterType="api.common.pojo.po.log.LogOperationPO">
        INSERT INTO log_operation (
          id,
          user_id,
          username,
          ip,
          module,
          operation_type,
          create_time)
        VALUES(
          #{id,jdbcType=VARCHAR},
          #{userId,jdbcType=VARCHAR},
          #{username,jdbcType=VARCHAR},
          #{ip,jdbcType=VARCHAR},
          #{module,jdbcType=VARCHAR},
          #{operationType,jdbcType=VARCHAR},
          #{createTime,jdbcType=TIMESTAMP}
        )
    </insert>

    <select id="getLoginLogPageList" parameterType="api.common.pojo.param.log.LogPageParam" resultType="api.common.pojo.po.log.LogLoginPO">
        SELECT
          user_id,
          username,
          ip,
          state,
          create_time
        FROM log_login
        WHERE 1=1
        <if test="username != null and username != ''">
            and username like CONCAT('%',#{username,jdbcType=VARCHAR},'%')
        </if>
        <if test="createTimeBegin != null and createTimeBegin != ''">
            and DATE(create_time) &gt;= #{createTimeBegin}
        </if>
        <if test="createTimeEnd != null and createTimeBegin != ''">
            and DATE(create_time) &lt;= #{createTimeEnd}
        </if>
        ORDER BY create_time DESC
    </select>

    <select id="getOperationLogPageList" parameterType="api.common.pojo.param.log.LogPageParam" resultType="api.common.pojo.po.log.LogOperationPO">
        select
          id,
          user_id,
          username,
          ip,
          module,
          operation_type,
          create_time
        FROM log_operation
        WHERE 1=1
        <if test="username != null and username != ''">
            and username like CONCAT('%',#{username,jdbcType=VARCHAR},'%')
        </if>
        <if test="module != null and module != ''">
            and module = #{module,jdbcType=VARCHAR}
        </if>
        <if test="operationType != null and operationType != ''">
            and operation_type = #{operationType,jdbcType=VARCHAR}
        </if>
        <if test="createTimeBegin != null and createTimeBegin != ''">
            and DATE(create_time) &gt;= #{createTimeBegin}
        </if>
        <if test="createTimeEnd != null and createTimeBegin != ''">
            and DATE(create_time) &lt;= #{createTimeEnd}
        </if>
        ORDER BY create_time DESC
    </select>

    <select id="getUserOperationLogPageList" parameterType="api.common.pojo.param.log.LogPageParam" resultType="api.common.pojo.po.log.LogOperationPO">
        select
          o.id,
          o.user_id,
          o.username,
          o.ip,
          o.module,
          o.operation_type,
          o.create_time
        FROM log_operation o
        JOIN system_user u ON o.user_id = u.id AND u.create_user_id = #{content,jdbcType=VARCHAR}
        WHERE 1=1
        <if test="username != null and username != ''">
            and o.username like CONCAT('%',#{username,jdbcType=VARCHAR},'%')
        </if>
        <if test="module != null and module != ''">
            and o.module = #{module,jdbcType=VARCHAR}
        </if>
        <if test="operationType != null and operationType != ''">
            and o.operation_type = #{operationType,jdbcType=VARCHAR}
        </if>
        <if test="createTimeBegin != null and createTimeBegin != ''">
            and DATE(o.create_time) &gt;= #{createTimeBegin}
        </if>
        <if test="createTimeEnd != null and createTimeBegin != ''">
            and DATE(o.create_time) &lt;= #{createTimeEnd}
        </if>
        ORDER BY o.create_time DESC
    </select>

    <select id="getSystemLogPageList" parameterType="api.common.pojo.param.log.LogPageParam" resultType="api.common.pojo.po.log.LogSystemPO">
        SELECT
          id,
          user_id,
          username,
          role_code,
          module,
          operation_type,
          content,
          create_time
        FROM log_system
        WHERE 1=1
        <if test="username != null and username != ''">
            and username like CONCAT('%',#{username,jdbcType=VARCHAR},'%')
        </if>
        <if test="module != null and module != ''">
            and module = #{module,jdbcType=VARCHAR}
        </if>
        <if test="operationType != null and operationType != ''">
            and operation_type = #{operationType,jdbcType=VARCHAR}
        </if>
        <if test="content != null and content != ''">
            and content like CONCAT('%',#{content,jdbcType=VARCHAR},'%')
        </if>
        <if test="createTimeBegin != null and createTimeBegin != ''">
            and DATE(create_time) &gt;= #{createTimeBegin}
        </if>
        <if test="createTimeEnd != null and createTimeBegin != ''">
            and DATE(create_time) &lt;= #{createTimeEnd}
        </if>
        ORDER BY create_time DESC
    </select>

    <insert id="insertSystemLog" parameterType="api.common.pojo.po.log.LogSystemPO">
        INSERT INTO log_system(
          id,
          user_id,
          username,
          role_code,
          module,
          operation_type,
          content,
          create_time
        )VALUES(
          #{id,jdbcType=VARCHAR},
          #{userId,jdbcType=VARCHAR},
          #{username,jdbcType=VARCHAR},
          #{roleCode,jdbcType=VARCHAR},
          #{module,jdbcType=VARCHAR},
          #{operationType,jdbcType=VARCHAR},
          #{content,jdbcType=VARCHAR},
          #{createTime,jdbcType=TIMESTAMP})
    </insert>

    <select id="getAccessCount" parameterType="java.lang.String" resultType="api.common.pojo.vo.home.LineChartVO">
        select
          COUNT(DISTINCT(user_id)) as num,
          DATE(create_time) as toDate
        FROM log_login
        WHERE create_time &gt;= #{dateBegin,jdbcType=VARCHAR}
        GROUP BY DATE(create_time)
        ORDER BY toDate ASC
    </select>
</mapper>
