<?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.engine.hrmelog.mapper.LocalElogDaoMapper">
    <select id="queryElogList" resultType="java.util.Map">
        select ${columns},DATE_FORMAT(log_date,"%Y-%m-%d %H:%i:%S") createdate from ${tableName}
        where modulename = #{logContent.moduleName} and functionname = #{logContent.functionName}
         and delete_type = 0
        <if test="conditionSql != null">${conditionSql}</if>
        order by log_date desc ,id desc
    </select>
    <select id="queryElogList" resultType="java.util.Map" databaseId="postgresql">
        select ${columns},to_char(log_date::timestamp,'YYYY-MM-DD HH24:MI:SS') createdate from ${tableName}
        where modulename = #{logContent.moduleName} and functionname = #{logContent.functionName}
        
        <if test="conditionSql != null">${conditionSql}</if>
        order by log_date desc ,id desc
    </select>
    <select id="queryElogList" resultType="java.util.Map" databaseId="oracle">
        select d.*,to_char(d.log_date,'yyyy-mm-dd HH24:mi:ss') createdate from ${tableName} d
        where modulename = #{logContent.moduleName} and functionname = #{logContent.functionName}
        
        <if test="conditionSql != null">${conditionSql}</if>
        order by d.log_date desc ,id desc
    </select>
    <select id="queryElogList" resultType="java.util.Map" databaseId="sqlserver">
        select ${columns},convert(nvarchar(19),log_date,120) createdate from ${tableName}
        where modulename = #{logContent.moduleName} and functionname = #{logContent.functionName}
        
        <if test="conditionSql != null">${conditionSql}</if>
        order by log_date desc ,id desc
    </select>

    <select id="queryElogListPapi" resultType="java.util.Map">
        select ${columns},DATE_FORMAT(log_date,"%Y-%m-%d %H:%i:%S") createdate from ${tableName}
        where modulename = #{logContent.moduleName} and functionname = #{logContent.functionName}
        and delete_type = 0
        <if test="conditionSql != null">${conditionSql}</if>
        order by log_date desc ,id desc
    </select>
    <select id="queryElogListPapi" resultType="java.util.Map" databaseId="postgresql">
        select ${columns},to_char(log_date::timestamp,'YYYY-MM-DD HH24:MI:SS') createdate from ${tableName}
        where modulename = #{logContent.moduleName} and functionname = #{logContent.functionName}
        <if test="conditionSql != null">${conditionSql}</if>
        order by log_date desc ,id desc
    </select>
    <select id="queryElogListPapi" resultType="java.util.Map" databaseId="oracle">
        select d.*,to_char(d.log_date,'yyyy-mm-dd HH24:mi:ss') createdate from ${tableName} d
        where modulename = #{logContent.moduleName} and functionname = #{logContent.functionName}
        <if test="conditionSql != null">${conditionSql}</if>
        order by d.log_date desc ,id desc
    </select>
    <select id="queryElogListPapi" resultType="java.util.Map" databaseId="sqlserver">
        select ${columns},convert(nvarchar(19),log_date,120) createdate from ${tableName}
        where modulename = #{logContent.moduleName} and functionname = #{logContent.functionName}
        <if test="conditionSql != null">${conditionSql}</if>
        order by log_date desc ,id desc
    </select>

    <select id="queryCardElogList" resultType="java.util.Map">
        select ${columns}, DATE_FORMAT(log_date,"%Y-%m-%d %H:%i:%S") createdate,
        uuid from ${tableName}
        where modulename = #{logContent.moduleName} and functionname = #{logContent.functionName}
        
        <if test="conditionSql != null">#{conditionSql}</if>
        order by log_date desc ,id desc
    </select>
    <select id="queryCardElogList" resultType="java.util.Map" databaseId="postgresql">
        select ${columns}, to_char(log_date::timestamp,'YYYY-MM-DD HH24:MI:SS') createdate,
        uuid from ${tableName}
        where modulename = #{logContent.moduleName} and functionname = #{logContent.functionName}
        
        <if test="conditionSql != null">#{conditionSql}</if>
        order by log_date desc ,id desc
    </select>
    <select id="queryCardElogList" resultType="java.util.Map" databaseId="oracle">
        select ${columns}, to_char(log_date,'yyyy-mm-dd HH24:mi:ss') createdate,
        uuid from ${tableName}
        where modulename = #{logContent.moduleName} and functionname = #{logContent.functionName}
        
        <if test="conditionSql != null">#{conditionSql}</if>
        order by log_date desc ,id desc
    </select>
    <select id="queryCardElogList" resultType="java.util.Map" databaseId="sqlserver">
        select ${columns}, convert(nvarchar(19),log_date,120) createdate,
        uuid from ${tableName}
        where modulename = #{logContent.moduleName} and functionname = #{logContent.functionName}
        
        <if test="conditionSql != null">#{conditionSql}</if>
        order by log_date desc ,id desc
    </select>


    <select id="elogCountByMorePermission" resultType="java.util.Map">
        select count(1) counts from ${tableName}
        where modulename = #{logContent.moduleName} and functionname = #{logContent.functionName}
        
        <if test="conditionSql != null">${conditionSql}</if>
    </select>

    <select id="elogCount" resultType="java.util.Map">
        select count(1) counts from ${tableName}
        where modulename = #{logContent.moduleName} and functionname = #{logContent.functionName}
        
        <if test="logContent.operator != null and logContent.operator !=''">
            and log_operator = #{logContent.operator}
        </if>
        <if test="conditionSql != null">${conditionSql}</if>
    </select>

    <select id="elogCountPapi" resultType="java.util.Map">
        select count(1) counts from ${tableName}
        where modulename = #{logContent.moduleName} and functionname = #{logContent.functionName}
        and delete_type = 0
        <if test="logContent.operator != null and logContent.operator !=''">
            and log_operator = #{logContent.operator}
        </if>
        <if test="conditionSql != null">${conditionSql}</if>
    </select>


    <select id="queryDetailElogList" resultType="java.util.Map">
        select *,DATE_FORMAT(create_time,"%Y-%m-%d %H:%i:%S") createdate from ${tableName}
        where mainid = #{logContent.uuid}
        
        <if test="conditionSql != null">#{conditionSql}</if>
        order by log_date desc ,id desc
    </select>
    <select id="queryDetailElogList" resultType="java.util.Map" databaseId="postgresql">
        select *,to_char(create_time::timestamp,'YYYY-MM-DD HH24:MI:SS') createdate from ${tableName}
        where mainid = #{logContent.uuid}
        
        <if test="conditionSql != null">#{conditionSql}</if>
        order by log_date desc ,id desc
    </select>
    <select id="queryDetailElogList" resultType="java.util.Map" databaseId="oracle">
        select *,to_char(create_time,'yyyy-mm-dd HH24:mi:ss') createdate from ${tableName}
        where mainid = #{logContent.uuid}
        
        <if test="conditionSql != null">#{conditionSql}</if>
        order by log_date desc ,id desc
    </select>
    <select id="queryDetailElogList" resultType="java.util.Map" databaseId="sqlserver">
        select *,convert(nvarchar(19),create_time,120) createdate from ${tableName}
        where mainid = #{logContent.uuid}
        
        <if test="conditionSql != null">#{conditionSql}</if>
        order by log_date desc ,id desc
    </select>

    <select id="queryAllChanges" resultType="java.util.Map">
        select *
        from ${tableName}
        where mainid = #{mainid}
    </select>

    <select id="queryAllMainData" resultType="java.util.Map">
        select *
        from ${tableName}
        where uuid = #{uuid}
    </select>

    <select id="queryAllMainChanges" resultType="java.util.Map">
        select *
        from ${tableName}
        where mainid = #{mainid}
          and isdetail = '0'
    </select>

    <select id="queryAllDetailChanges" resultType="java.util.Map">
        select *
        from ${tableName}
        where mainid = #{mainid}
          and isdetail = '1'
    </select>

    <select id="elogDetailCount" resultType="java.util.Map">
        select count(1) counts from ${tableName}
        where mainid = #{logContent.uuid}
        
        <if test="conditionSql != null">#{conditionSql}</if>
    </select>

<!--    <select id="queryOperators" resultType="com.weaver.teams.domain.user.SimpleEmployee">-->
<!--        select log_operator id, max(log_date) updateTime-->
<!--        from ${tableName}-->
<!--        where targetid = #{targetId}-->
<!--          and operatetype = #{operateType}-->
<!--        group by log_operator-->
<!--        order by updateTime desc-->
<!--    </select>-->
    <select id="queryReadInfoOperators" resultType="com.engine.hrmelog.entity.dto.ReadInfoEntity">
        select log_operator employeeId, max(log_date) date
        from ${tableName}
        where targetid = #{targetId}
          and operatetype = #{operateType}
        group by log_operator
        order by log_date desc
    </select>

    <select id="queryReadInfoOperators" resultType="com.engine.hrmelog.entity.dto.ReadInfoEntity" databaseId="postgresql">
        select log_operator employeeId, max(log_date) date
        from ${tableName}
        where targetid = #{targetId}
          and operatetype = #{operateType}
        group by log_operator
        order by date desc
    </select>

    <select id="queryReadInfoOperators" resultType="com.engine.hrmelog.entity.dto.ReadInfoEntity" databaseId="oracle">
        select log_operator employeeId, max(log_date) "date"
        from ${tableName}
        where targetid = #{targetId}
          and operatetype = #{operateType}
        group by log_operator
        order by "date" desc
    </select>
    <select id="queryReadInfoOperators" resultType="com.engine.hrmelog.entity.dto.ReadInfoEntity" databaseId="sqlserver">
        select log_operator employeeId, max(log_date) "date"
        from ${tableName}
        where targetid = #{targetId}
          and operatetype = #{operateType}
        group by log_operator
        order by "date" desc
    </select>

    <select id="queryReadInfoDateOperators" resultType="com.engine.hrmelog.entity.dto.ReadInfoEntity">
        select log_operator employeeId, max(log_date) date from ${tableName} where targetid = #{targetId} and
        operatetype = #{operateType}
        <if test="startDate != null">
            AND log_date <![CDATA[ >= ]]> #{startDate}
        </if>
        <if test="endDate != null">
            AND log_date <![CDATA[ <= ]]> #{endDate}
        </if>
        group by log_operator order by log_date desc
    </select>

    <select id="queryReadInfoDateOperators" resultType="com.engine.hrmelog.entity.dto.ReadInfoEntity"
            databaseId="postgresql">
        select log_operator employeeId, max(log_date) date from ${tableName} where targetid = #{targetId} and
        operatetype = #{operateType}
        <if test="startDate != null">
            AND log_date <![CDATA[ >= ]]> #{startDate}
        </if>
        <if test="endDate != null">
            AND log_date <![CDATA[ <= ]]> #{endDate}
        </if>
        group by log_operator order by date desc
    </select>

    <select id="queryReadInfoDateOperators" resultType="com.engine.hrmelog.entity.dto.ReadInfoEntity" databaseId="oracle">
        select log_operator employeeId, max(log_date) "date" from ${tableName} where targetid = #{targetId} and
        operatetype = #{operateType}
        <if test="startDate != null">
            AND log_date <![CDATA[ >= ]]> TO_DATE(#{startDate}, 'SYYYY-MM-DD HH24:MI:SS')
        </if>
        <if test="endDate != null">
            AND log_date <![CDATA[ <= ]]> TO_DATE(#{endDate}, 'SYYYY-MM-DD HH24:MI:SS')
        </if>
        group by log_operator order by "date" desc
    </select>

    <select id="queryReadInfoDateOperators" resultType="com.engine.hrmelog.entity.dto.ReadInfoEntity"
            databaseId="sqlserver">
        select log_operator employeeId, max(log_date) "date" from ${tableName} where targetid = #{targetId} and
        operatetype = #{operateType}
        <if test="startDate != null">
            AND log_date <![CDATA[ >= ]]> #{startDate}
        </if>
        <if test="endDate != null">
            AND log_date <![CDATA[ <= ]]> #{endDate}
        </if>
        group by log_operator order by "date" desc
    </select>


    <select id="queryElogContextById" resultType="java.lang.Integer">
        select 1
        from ${tableName}
        where id = #{id}
    </select>

    <select id="queryElogTraceInfo" resultType="java.util.Map">
        select operatorname,
               targetname,
               modulename,
               operatetypename,
               DATE_FORMAT(log_date, "%Y-%m-%d %H:%i:%S") createdate
        from ${tableName}
        where belongmainid = #{traceId}
        order by log_date desc, id desc LIMIT #{offset}, #{pageSize}
    </select>
    <select id="queryElogTraceInfo" resultType="java.util.Map" databaseId="postgresql">
        select operatorname,
               targetname,
               modulename,
               operatetypename,
               to_char(log_date::timestamp, 'YYYY-MM-DD HH24:MI:SS') createdate
        from ${tableName}
        where belongmainid = #{traceId}
        order by log_date desc, id desc limit #{pageSize}
        offset #{offset}

    </select>
    <select id="queryElogTraceInfo" resultType="java.util.Map" databaseId="oracle">
        SELECT operatorname, targetname, modulename, operatetypename
        FROM (SELECT TMP.*, ROWNUM ROW_ID
              FROM (select t.*, to_char(log_date, 'yyyy-mm-dd HH24:mi:ss') createdate
                    from ${tableName} t
                    where belongmainid = #{traceId}
                    order by log_date desc, id desc) TMP
              WHERE ROWNUM &lt;= (#{pageSize} + #{offset}))
        WHERE ROW_ID &gt; #{offset}


    </select>
    <select id="queryElogTraceInfo" resultType="java.util.Map" databaseId="sqlserver">
        select atmp.operatorname, atmp.targetname, atmp.modulename, atmp.operatetypename
        from (select atmp.*, row_number() over(order by log_date desc ,id desc ) as rownumber
              from (select top(#{pageSize}) atmp.*
                    from (select *, convert(nvarchar(19), log_date, 120) createdate
                          from ${tableName}
                          where belongmainid = #{traceId}) atmp
                    order by log_date desc, id desc) atmp) atmp
        where rownumber &gt; #{offset}
        order by log_date desc, id desc


    </select>

    <select id="queryElogTraceInfoCount" resultType="java.lang.Integer">
        select count(id) id
        from ${tableName}
        where belongmainid = #{traceId}
    </select>

    <select id="queryLogInfoByCustom" resultType="java.util.Map">
        select * from ${tableName}
        <where>${sql}</where>
    </select>

    <select id="queryAllChangesData" resultType="java.util.Map">
        select *
        from ${tableName}
        where mainid = #{mainid}
        order by showorder asc, id asc
    </select>

    <select id="queryTenantKeyOperators" resultType="com.engine.hrmelog.entity.dto.ReadInfoEntity">
        select log_operator employeeId, max(log_date) date
        from ${tableName}
        where tenant_key = #{tenantKey}
          and targetid = #{targetId}
          and operatetype = #{operateType}
        group by log_operator
        order by date desc
    </select>

    <select id="queryTenantKeyOperators" resultType="com.engine.hrmelog.entity.dto.ReadInfoEntity" databaseId="postgresql">
        select log_operator employeeId, max(log_date) date
        from ${tableName}
        where tenant_key = #{tenantKey}
          and targetid = #{targetId}
          and operatetype = #{operateType}
        group by log_operator
        order by date desc
    </select>


    <select id="queryTenantKeyOperators" resultType="com.engine.hrmelog.entity.dto.ReadInfoEntity" databaseId="oracle">
        select log_operator employeeId, max(log_date) "date"
        from ${tableName}
        where tenant_key = #{tenantKey}
          and targetid = #{targetId}
          and operatetype = #{operateType}
        group by log_operator
        order by "date" desc
    </select>
    <select id="queryTenantKeyOperators" resultType="com.engine.hrmelog.entity.dto.ReadInfoEntity" databaseId="sqlserver">
        select log_operator employeeId, max(log_date) "date"
        from ${tableName}
        where tenant_key = #{tenantKey}
          and targetid = #{targetId}
          and operatetype = #{operateType}
        group by log_operator
        order by "date" desc
    </select>

    <select id="queryAllChangesDataPages" resultType="java.util.Map">
        select *
        from ${tableName}
        where mainid = #{mainid}
        order by showorder asc, id asc
    </select>


    <select id="queryAllChangesPageCounts" resultType="java.lang.Integer">
        select count(*)
        from ${tableName}
        where mainid = #{mainid}
    </select>
    <select id="elogCountOnlyNum" resultType="java.lang.Long">
        select count(*) from ${tableName}
        where modulename = #{logContent.moduleName} and functionname = #{logContent.functionName}
         and delete_type = 0
        <if test="logContent.operator != null and logContent.operator !=''">
            and log_operator = #{logContent.operator}
        </if>
        <if test="conditionSql != null">${conditionSql}</if>
    </select>
</mapper>