<?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.dbnt.kcgfilemanager.mapper.ModifyRequestMapper">
  <select id="selectModifyRequestList" resultType="ModifyRequest" parameterType="ModifyRequest">
    SELECT A.REQUEST_SEQ AS requestSeq,
      A.REQUEST_TYPE AS requestType,
      A.TITLE AS title,
      A.STATUS AS status,
      B.NAME AS createName,
      A.CREATE_DATE AS createDate
    FROM MODIFY_REQUEST A
      INNER JOIN USER_INFO B ON A.CREATE_ID = B.USER_ID
    <where>
      <if test="userRole == 'USER'">
        AND A.CREATE_ID = #{createId}
      </if>
      <if test="requestType != null and requestType != ''">
        AND A.REQUEST_TYPE = #{requestType}
      </if>
      <if test="title != null and title != ''">
        AND A.TITLE LIKE '%${title}%'
      </if>
      <if test="status != null and status != ''">
        AND A.STATUS = #{status}
      </if>
      <if test="userRole == 'ADMIN'">
        <if test="createName != null and createName != ''">
          AND B.CREATE_NAME LIKE '%${createName}%'
        </if>
      </if>
      <if test="startDate != null and startDate != ''">
        AND A.CREATE_DATE >= CONCAT(#{startDate}, ' 00:00:00')
      </if>
      <if test="endDate != null and endDate != ''">
        AND A.CREATE_DATE &lt;= CONCAT(#{endDate}, ' 23:59:59')
      </if>
    </where>
    ORDER BY A.CREATE_DATE DESC
    LIMIT #{rowCnt} OFFSET #{firstIndex}
  </select>
  <select id="selectModifyRequestListCnt" resultType="int" parameterType="ModifyRequest">
    SELECT COUNT(*)
    FROM MODIFY_REQUEST A
      INNER JOIN USER_INFO B ON A.CREATE_ID = B.USER_ID
    <where>
      <if test="userRole == 'USER'">
        AND CREATE_ID = #{createId}
      </if>
      <if test="requestType != null and requestType != ''">
        AND REQUEST_TYPE LIKE '%${requestType}%'
      </if>
      <if test="title != null and title != ''">
        AND TITLE LIKE '%${title}%'
      </if>
      <if test="status != null and status != ''">
        AND STATUS LIKE '%${status}%'
      </if>
      <if test="userRole == 'ADMIN'">
        <if test="createId != null and createId != ''">
          AND CREATE_ID LIKE '%${createId}%'
        </if>
      </if>
    </where>
  </select>
</mapper>