<?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.i2sema.v2.friendtalk.plus.image.dao.FriendtalkPlusImageDao">

  <parameterMap id="friendtalkPlusImageEntityVo" type="com.i2sema.v2.friendtalk.plus.image.vo.FriendtalkPlusImageEntityVo" />
  <resultMap id="FriendtalkPlusImageVo" type="com.i2sema.v2.friendtalk.plus.common.vo.FriendtalkPlusImageVo" />

  <select id="selectImageCount" parameterType="map" resultType="int">
    <![CDATA[
			SELECT COUNT(*) AS cnt
			FROM			frn_pls_image
			WHERE			use_yn = 'Y'
			  AND     type = #{type}
		]]>
    <if test="@org.springframework.util.StringUtils@hasText(name) == true">
      <![CDATA[ AND name like CONCAT('%', #{name}, '%') ]]>
    </if>
    <if test="@org.springframework.util.StringUtils@hasText(orgId)">
      <![CDATA[ AND org_id = #{orgId} ]]>
    </if>
  </select>

  <select id="selectImageList" parameterType="map" resultType="map">
    <![CDATA[
			SELECT *
			FROM (
			      SELECT 		seq           AS seq,
			                name          AS name,
											url           AS url,
											description   AS description,
											width         AS width,
											height        AS height,
										  created_date  AS createdDate
						FROM			frn_pls_image
						WHERE			use_yn = 'Y'
						  AND     type = #{type}
		]]>
    <if test="@org.springframework.util.StringUtils@hasText(name)">
      <![CDATA[ AND name like CONCAT('%', #{name}, '%') ]]>
    </if>
    <if test="@org.springframework.util.StringUtils@hasText(orgId)">
      <![CDATA[ AND org_id = #{orgId} ]]>
    </if>
    <![CDATA[
		) i
		ORDER BY seq DESC
		LIMIT #{firstIndex}, #{recordCountPerPage}
		]]>
  </select>

  <select id="selectImage" parameterType="map" resultMap="FriendtalkPlusImageVo">
      SELECT
          seq AS imageSeq,
          name AS name,
          url AS imageUrl,
          description AS description,
          width AS width,
          height AS height,
          created_date AS createdDate,
          org_id AS orgId
      FROM
        frn_pls_image
      WHERE
        use_yn = 'Y'
      AND seq = #{seq}
      <if test="@org.springframework.util.StringUtils@hasText(orgId)">
          <![CDATA[ AND org_id = #{orgId} ]]>
      </if>
  </select>

  <select id="selectImageByImageSeq" parameterType="map" resultMap="FriendtalkPlusImageVo">
  <![CDATA[
      /* [ccom/i2sema/v2/friendtalk/plus/image/dao/FriendtalkPlusImageDao.selectImageByImageSeq] 이미지 조회 By ImageSeq */
      SELECT
          seq AS imageSeq,
          name AS name,
          url AS imageUrl,
          description AS description,
          width AS width,
          height AS height,
          created_date AS createdDate,
          org_id AS orgId
      FROM
          frn_pls_image
      WHERE
          use_yn = 'Y'
      AND seq = #{seq}
   ]]>
      <if test="@org.springframework.util.StringUtils@hasText(orgId)">
          <![CDATA[ AND org_id = #{orgId} ]]>
      </if>
  </select>


  <update id="deleteFriendtalkPlusImage" parameterType="map">
    <![CDATA[
        /* [com/i2sema/v2/friendtalk/plus/image/dao/FriendtalkPlusImageDao.deleteFriendtalkPlusImage] 친구톡 이미지 삭제 */
        UPDATE	frn_pls_image
        SET use_yn = 'N'
        WHERE seq in
    ]]>
      <foreach item="item" index="index" collection="imgSeqList" open="("
               separator="," close=")">
          #{item}
      </foreach>
      <if test="@org.springframework.util.StringUtils@hasText(orgId)">
        <![CDATA[ AND org_id = #{orgId}]]>
      </if>
  </update>

  <insert id="insertImageFile" parameterMap="friendtalkPlusImageEntityVo" useGeneratedKeys="true" keyProperty="seq" keyColumn="seq">
    INSERT INTO frn_pls_image (
                      name,
                      url,
                      description,
                      width,
                      height,
                      type,
                      use_yn,
                      org_id,
                      mber_no,
                      created_id
                      ) VALUES (
                      #{name},
                      #{url},
                      #{description},
                      #{width},
                      #{height},
                      #{type},
                      #{useYn},
                      #{orgId},
                      #{mberNo},
                      #{createdId}
                      );
  </insert>
</mapper>
