package com.sunyard.ssp.modules.monitor.log.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sunyard.ssp.modules.monitor.log.entity.AuditLog;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author fyc
 * @since 2020-03-18
 */
@Repository
public interface AuditLogMapper extends BaseMapper<AuditLog> {

    /**
     * 分页查询接口
     *
     * @param iPage
     * @param operateContent
     * @param operateType
     * @param requestMethod
     * @param readUserName
     * @param startTime
     * @param endTime
     * @return
     */
    @Select("<script>" +
            "  select *  FROM SC_AUDIT_LOG\n" +
            "        <where>\n" +
            "            <if test=\"operateContent !=null and operateContent != ''\">\n" +
            "                and OPERATE_CONTENT like '%${operateContent}%'\n" +
            "            </if>\n" +
            "            <if test=\"operateType != null and operateType != ''\">\n" +
            "                and OPERATE_TYPE like '%${operateType}%'\n" +
            "            </if>\n" +
            "            <if test=\"requestMethod != null and requestMethod != ''\">\n" +
            "                and REQUEST_MOTHED = #{requestMethod}\n" +
            "            </if>\n" +
            "            <if test=\"readUserName != null and readUserName != ''\">\n" +
            "                and READ_USER_NAME like '%${readUserName}%'\n" +
            "            </if>\n" +
            "           <if test=\"startTime != null and endTime !=null\">\n" +
            "              and CREATE_TIME  between #{startTime} and (#{endTime}+1)\n" +
            "            </if>\n" +
            "and OPERATE_TYPE!='登录'\n" +
            "        </where>\n" +
            "        order by CREATE_TIME desc" +
            "</script>")
    List<AuditLog> queryList(IPage<AuditLog> iPage,
                             @Param("operateContent") String operateContent,
                             @Param("operateType") String operateType,
                             @Param("requestMethod") String requestMethod,
                             @Param("readUserName") String readUserName,
                             @Param("startTime") LocalDate startTime,
                             @Param("endTime") LocalDate endTime);


    /**
     * 分页查询接口
     *
     * @param iPage
     * @param operateContent
     * @param operateType
     * @param requestMethod
     * @param readUserName
     * @param startTime
     * @param endTime
     * @return
     */
    @Select("<script>" +
            "  select *  FROM SC_AUDIT_LOG\n" +
            "        <where>\n" +
            "            <if test=\"operateContent !=null and operateContent != ''\">\n" +
            "                and OPERATE_CONTENT like '%${operateContent}%'\n" +
            "            </if>\n" +
            "            <if test=\"operateType != null and operateType != ''\">\n" +
            "                and OPERATE_TYPE like '%${operateType}%'\n" +
            "            </if>\n" +
            "            <if test=\"requestMethod != null and requestMethod != ''\">\n" +
            "                and REQUEST_MOTHED = #{requestMethod}\n" +
            "            </if>\n" +
            "            <if test=\"readUserName != null and readUserName != ''\">\n" +
            "                and READ_USER_NAME like '%${readUserName}%'\n" +
            "            </if>\n" +
            "           <if test=\"startTime != null and endTime !=null\">\n" +
            "              and CREATE_TIME  between #{startTime} and (#{endTime}+1)\n" +
            "            </if>\n" +
            "        </where>\n" +
            "        order by CREATE_TIME desc" +
            "</script>")
    List<AuditLog> userLogQueryList(IPage<AuditLog> iPage,
                             @Param("operateContent") String operateContent,
                             @Param("operateType") String operateType,
                             @Param("requestMethod") String requestMethod,
                             @Param("readUserName") String readUserName,
                             @Param("startTime") LocalDate startTime,
                             @Param("endTime") LocalDate endTime);

    /**
     * 查询接口
     *
     * @param operateContent
     * @param operateType
     * @param requestMethod
     * @param readUserName
     * @param startTime
     * @param endTime
     * @return
     */
    @Select("<script>" +
            "  select *  FROM SC_AUDIT_LOG\n" +
            "        <where>\n" +
            "            <if test=\"operateContent !=null and operateContent != ''\">\n" +
            "                and OPERATE_CONTENT like '%${operateContent}%'\n" +
            "            </if>\n" +
            "            <if test=\"operateType != null and operateType != ''\">\n" +
            "                and OPERATE_TYPE like '%${operateType}%'\n" +
            "            </if>\n" +
            "            <if test=\"requestMethod != null and requestMethod != ''\">\n" +
            "                and REQUEST_MOTHED = #{requestMethod}\n" +
            "            </if>\n" +
            "            <if test=\"readUserName != null and readUserName != ''\">\n" +
            "                and READ_USER_NAME like '%${readUserName}%'\n" +
            "            </if>\n" +
            "           <if test=\"startTime != null and endTime !=null\">\n" +
            "              and CREATE_TIME  between #{startTime} and (#{endTime}+1)\n" +
            "            </if>\n" +
            "and OPERATE_TYPE!='登录'\n" +
            "        </where>\n" +
            "        order by CREATE_TIME desc" +
            "</script>")
    List<AuditLog> queryList(@Param("operateContent") String operateContent,
                             @Param("operateType") String operateType,
                             @Param("requestMethod") String requestMethod,
                             @Param("readUserName") String readUserName,
                             @Param("startTime") LocalDate startTime,
                             @Param("endTime") LocalDate endTime);

    /**
     * 查询接口
     *
     * @param operateContent
     * @param operateType
     * @param requestMethod
     * @param readUserName
     * @param startTime
     * @param endTime
     * @return
     */
    @Select("<script>" +
            "  select *  FROM SC_AUDIT_LOG\n" +
            "        <where>\n" +
            "            <if test=\"operateContent !=null and operateContent != ''\">\n" +
            "                and OPERATE_CONTENT like '%${operateContent}%'\n" +
            "            </if>\n" +
            "            <if test=\"operateType != null and operateType != ''\">\n" +
            "                and OPERATE_TYPE like '%${operateType}%'\n" +
            "            </if>\n" +
            "            <if test=\"requestMethod != null and requestMethod != ''\">\n" +
            "                and REQUEST_MOTHED = #{requestMethod}\n" +
            "            </if>\n" +
            "            <if test=\"readUserName != null and readUserName != ''\">\n" +
            "                and READ_USER_NAME like '%${readUserName}%'\n" +
            "            </if>\n" +
            "           <if test=\"startTime != null and endTime !=null\">\n" +
            "              and CREATE_TIME  between #{startTime} and (#{endTime}+1)\n" +
            "            </if>\n" +
            "        </where>\n" +
            "        order by CREATE_TIME desc" +
            "</script>")
    List<AuditLog> userLogQueryListWithOutPage(@Param("operateContent") String operateContent,
                             @Param("operateType") String operateType,
                             @Param("requestMethod") String requestMethod,
                             @Param("readUserName") String readUserName,
                             @Param("startTime") LocalDate startTime,
                             @Param("endTime") LocalDate endTime);

    /**
     * 清空表
     *
     * @return
     */
    @Update("<script>" +
            "truncate table SC_AUDIT_LOG" +
            "</script>")
    Integer truncateTable();
}
