<?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.BoardMapper">
  <select id="selectContentList" resultType="Board" parameterType="Board">
    SELECT
      <if test='status == "M"'>
        D.CATEGORY_NAME AS categoryName,
      </if>
      A.CONTENT_SEQ AS contentSeq,
      A.CATEGORY_SEQ AS categorySeq,
      A.TITLE AS title,
      C.FILE_CNT AS fileCnt,
      B.NAME AS createName,
      A.CREATE_DATE AS createDate,
      A.VIEW_CNT AS viewCnt
    FROM BOARD A
      INNER JOIN USER_INFO B
        ON A.CREATE_ID = B.USER_ID
      LEFT OUTER JOIN (SELECT CONTENT_SEQ, MAX(FILE_SEQ) AS FILE_CNT
                  FROM FILE_INFO
                  GROUP BY CONTENT_SEQ ) C
        ON A.CONTENT_SEQ = C.CONTENT_SEQ
    <if test='status == "M"'>
      INNER JOIN CONTENT_POSITION D
        ON A.CATEGORY_SEQ = D.CATEGORY_SEQ
    </if>
    WHERE A.STATUS != 'D'
      <if test="status != null and status != ''">
        AND A.STATUS = #{status}
      </if>
      <if test="categorySeq != null and categorySeq != ''">
        AND A.CATEGORY_SEQ = #{categorySeq}
      </if>
      <if test="title != null and title != ''">
        AND A.TITLE LIKE CONCAT('%', #{title}, '%')
      </if>
      <if test="createName != null and createName != ''">
        AND B.NAME LIKE CONCAT('%', #{createName}, '%')
      </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>
      <if test="hashTagList != null and hashTagList.size != 0">
        AND A.CONTENT_SEQ IN (
          SELECT B.CONTENT_SEQ
          FROM HASH_TAG A
          INNER JOIN HASH_TAG_LINK B ON A.TAG_SEQ = B.TAG_SEQ
          <where>
            <foreach collection="hashTagList" item="hashTag">
              OR A.TAG_NAME LIKE CONCAT('%',#{hashTag.tagName},'%')
            </foreach>
          </where>
        )
      </if>
      <if test="categoryList != null and categoryList.size != 0">
        AND A.CATEGORY_SEQ IN
          <foreach collection="categoryList" item="category" open="(" separator="," close=")">
            #{category.categorySeq}
          </foreach>
      </if>
      <if test="originalName != null and originalName != ''">
        AND A.CONTENT_SEQ IN (
          SELECT DISTINCT CONTENT_SEQ
          FROM FILE_INFO
          WHERE ORIGINAL_NAME LIKE CONCAT('%', #{originalName}, '%')
        )
      </if>
    <if test='status == "M"'>
      ORDER BY A.VIEW_CNT DESC
    </if>
    <if test="status == null or status == ''">
      ORDER BY A.CREATE_DATE DESC
    </if>
    LIMIT #{rowCnt} OFFSET #{firstIndex}
  </select>
  <!--<if test="endDate != null and endDate != ''">
    AND A.CREATE_DATE &lt;= #{endDate}
  </if>-->
  <select id="selectContentListCnt" resultType="int" parameterType="Board">
    SELECT COUNT(*)
    FROM BOARD A
       INNER JOIN USER_INFO B
          ON A.CREATE_ID = B.USER_ID
       LEFT OUTER JOIN (SELECT CONTENT_SEQ, MAX(FILE_SEQ) AS FILE_CNT
                   FROM FILE_INFO
                   GROUP BY CONTENT_SEQ ) C
          ON A.CONTENT_SEQ = C.CONTENT_SEQ
    <if test='status == "M"'>
      INNER JOIN CONTENT_POSITION D
      ON A.CATEGORY_SEQ = D.CATEGORY_SEQ
    </if>
    WHERE A.STATUS != 'D'
      <if test="status != null and status != ''">
        AND A.STATUS = #{status}
      </if>
      <if test="categorySeq != null and categorySeq != ''">
        AND A.CATEGORY_SEQ = #{categorySeq}
      </if>
      <if test="title != null and title != ''">
        AND A.TITLE LIKE CONCAT('%', #{title}, '%')
      </if>
      <if test="createName != null and createName != ''">
        AND B.NAME LIKE CONCAT('%', #{createName}, '%')
      </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>
      <if test="hashTagList != null and hashTagList.size != 0">
        AND A.CONTENT_SEQ IN (
        SELECT B.CONTENT_SEQ
        FROM HASH_TAG A
        INNER JOIN HASH_TAG_LINK B ON A.TAG_SEQ = B.TAG_SEQ
        <where>
          <foreach collection="hashTagList" item="hashTag">
            OR A.TAG_NAME LIKE CONCAT('%',#{hashTag.tagName},'%')
          </foreach>
        </where>
        )
      </if>
      <if test="categoryList != null and categoryList.size != 0">
        AND A.CATEGORY_SEQ IN
        <foreach collection="categoryList" item="category" open="(" separator="," close=")">
          #{category.categorySeq}
        </foreach>
      </if>
      <if test="originalName != null and originalName != ''">
        AND A.CONTENT_SEQ IN (
        SELECT DISTINCT CONTENT_SEQ
        FROM FILE_INFO
        WHERE ORIGINAL_NAME LIKE CONCAT('%', #{originalName}, '%')
        )
      </if>
  </select>

  <select id="selectHashTagListFromContentSeq" resultType="HashTag" parameterType="int">
    SELECT C.*
    FROM BOARD A
           INNER JOIN HASH_TAG_LINK B ON A.CONTENT_SEQ = B.CONTENT_SEQ
           INNER JOIN HASH_TAG C ON B.TAG_SEQ = C.TAG_SEQ
    WHERE A.CONTENT_SEQ = ${categorySeq}
  </select>
  <select id="selectBoardLogFromContentSeq" resultType="BoardLog" parameterType="int">
    SELECT A.LOG_SEQ AS logSeq,
           A.LOG_STATUS AS logStatus,
           A.DESCRIPTION AS description,
           B.NAME AS createName ,
           A.CREATE_DATE AS createDate
    FROM BOARD_LOG A
           INNER JOIN USER_INFO B ON A.CREATE_ID = B.USER_ID
    WHERE A.CONTENT_SEQ = ${categorySeq}
    ORDER BY LOG_SEQ DESC
  </select>
  <select id="selectBoardLogList" resultType="BoardLog" parameterType="BoardLog">
    SELECT C.TITLE AS title,
           C.CATEGORY_SEQ AS categorySeq,
           D.CATEGORY_NAME AS categoryName,
           A.CONTENT_SEQ AS contentSeq,
           A.LOG_SEQ AS logSeq,
           A.LOG_STATUS AS logStatus,
           A.DESCRIPTION AS description,
           B.NAME AS createName ,
           A.CREATE_DATE AS createDate
    FROM BOARD_LOG A
           INNER JOIN USER_INFO B ON A.CREATE_ID = B.USER_ID
           INNER JOIN BOARD C ON A.CONTENT_SEQ  = C.CONTENT_SEQ
           INNER JOIN CONTENT_POSITION D ON C.CATEGORY_SEQ = D.CATEGORY_SEQ
    ORDER BY A.CREATE_DATE DESC, A.LOG_SEQ DESC
    LIMIT #{rowCnt} OFFSET #{firstIndex}
  </select>
  <select id="selectBoardLogListCnt" resultType="int" parameterType="BoardLog">
    SELECT COUNT(*)
    FROM BOARD_LOG A
          INNER JOIN USER_INFO B ON A.CREATE_ID = B.USER_ID
          INNER JOIN BOARD C ON A.CONTENT_SEQ  = C.CONTENT_SEQ
          INNER JOIN CONTENT_POSITION D ON C.CATEGORY_SEQ = D.CATEGORY_SEQ
  </select>
</mapper>