<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.caliverse.admin.domain.dao.admin.LandMapper">
    <resultMap id="LandAuctionResultMap" type="com.caliverse.admin.domain.entity.LandAuction">
        <id property="id" column="id"/>
        <result property="rowNum" column="row_num"/>
        <result property="landId" column="land_id"/>
        <result property="landName" column="land_name"/>
        <result property="landSize" column="land_size"/>
        <result property="landSocket" column="land_socket"/>
        <result property="auctionSeq" column="auction_seq"/>
        <result property="status" column="status"/>
        <result property="resvStartDt" column="resv_start_dt"/>
        <result property="auctionStartDt" column="auction_start_dt"/>
        <result property="auctionEndDt" column="auction_end_dt"/>
        <result property="currencyType" column="currency_type"/>
        <result property="startPrice" column="start_price"/>
        <result property="closeEndDt" column="close_end_dt"/>
        <result property="closePrice" column="close_price"/>
        <result property="bidderGuid" column="bidder_guid"/>
        <result property="bidderNickname" column="bidder_nickname"/>
        <result property="deleted" column="deleted"/>
        <result property="createBy" column="create_by"/>
        <result property="createDt" column="create_dt"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateDt" column="update_dt"/>
    </resultMap>
    <resultMap id="LandOwnerChangeResultMap" type="com.caliverse.admin.domain.entity.LandOwnerChange">
        <id property="id" column="id"/>
        <result property="landId" column="land_id"/>
        <result property="landName" column="land_name"/>
        <result property="buildingId" column="building_id"/>
        <result property="buildingName" column="building_name"/>
        <result property="userGuid" column="user_guid"/>
        <result property="userName" column="user_name"/>
        <result property="isReserve" column="is_reserve"/>
        <result property="reservationDt" column="reservation_dt"/>
        <result property="status" column="status"/>
        <result property="createBy" column="create_by"/>
        <result property="createDt" column="create_dt"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateDt" column="update_dt"/>
    </resultMap>

    <!--이벤트 리스트 조회-->
    <select id="getLandAuctionList" resultMap="LandAuctionResultMap" parameterType="map">
    SELECT *
    FROM(
    SELECT (@row_number:=@row_number + 1) AS row_num , c.* FROM
        (
        SELECT
            a.id
            , a.land_id
            , a.land_name
            , a.land_size
            , a.land_socket
            , a.auction_seq
            , a.status
            , a.resv_start_dt
            , a.auction_start_dt
            , a.auction_end_dt
            , a.currency_type
            , a.start_price
            , a.close_end_dt
            , a.close_price
            , a.bidder_guid
            , a.bidder_nickname
            , (SELECT email FROM admin WHERE id = a.create_by ) AS create_by
            , a.create_dt
            , (SELECT email FROM admin WHERE id = a.update_by ) AS update_by
            , a.update_dt
        FROM land_auction a
        WHERE 1 = 1

        <choose>
            <when test="land_type == 'NAME' or land_type == 'name' ">
                <if test="land_data != null and land_data != ''">
                    AND a.land_name LIKE CONCAT('%',#{land_data},'%')
                </if>
            </when>
            <otherwise>
                <if test="land_data != null and land_data != ''">
                    AND a.land_id = #{land_data}
                </if>
            </otherwise>
        </choose>
        <choose>
            <when test="user_type == 'NAME' or user_type == 'name' ">
                <if test="user_data != null and user_data != ''">
                    AND a.bidder_nickname LIKE CONCAT('%',#{user_data},'%')
                </if>
            </when>
            <otherwise>
                <if test="user_data != null and user_data != ''">
                    AND a.bidder_guid = #{user_data}
                </if>
            </otherwise>
        </choose>
        <if test="land_size != null and land_size != ''">
            <choose>
                <when test="land_size != 'ALL' ">
                    AND a.land_size = #{land_size}
                </when>
            </choose>
        </if>
        <if test="status != null and status != ''">
            <choose>
                <when test="status != 'ALL' ">
                    AND a.status = #{status}
                </when>
            </choose>
        </if>
        <if test="start_dt != null and start_dt != '' and end_dt !=null and end_dt!= ''">
            AND a.auction_start_dt &gt;= #{start_dt, jdbcType=TIMESTAMP}
            AND a.auction_end_dt &lt;= #{end_dt, jdbcType=TIMESTAMP}
        </if>
        GROUP BY a.id
        ORDER BY status
        )c
        , (SELECT @row_number:=0) AS t
        ) Z
        ORDER BY
        CASE WHEN Z.status = 'FAIL' OR Z.status = 'CANCEL' THEN 1 ELSE 0 END
        <if test="orderby != null and orderby != ''">
            ,row_num ${orderby}
        </if>
        <if test="pageSize != null and pageSize != ''">
            LIMIT ${pageSize} OFFSET ${offset}
        </if>
    </select>

    <select id="getTotal" resultType="java.lang.Integer" parameterType="map">
        SELECT count(*) FROM land_auction
    </select>

    <select id="getAllCnt" resultType="java.lang.Integer" parameterType="map">
        SELECT count(*) FROM land_auction a
        WHERE 1 = 1
        <if test="content != null and content != ''">
            <foreach item="content" index="index" collection="content.split(' ')" open="AND (" separator="OR" close=")">
                b.content LIKE CONCAT('%', #{content}, '%')
            </foreach>
        </if>
        <if test="title != null and title != ''">
            AND b.title LIKE CONCAT('%',#{title},'%')
        </if>
        <if test="event_type != null and event_type != ''">
            <choose>
                <when test="event_type != 'ALL' ">
                    AND event_type = #{event_type}
                </when>
            </choose>
        </if>
        <if test="status != null and status != ''">
            <choose>
                <when test="status != 'ALL' ">
                    AND a.status = #{status}
                </when>
            </choose>
        </if>
        <if test="start_dt != null and start_dt != '' and end_dt !=null and end_dt!= ''">
            AND a.auction_start_dt &gt;= #{start_dt, jdbcType=TIMESTAMP}
            AND a.auction_end_dt &lt;= #{end_dt, jdbcType=TIMESTAMP}
        </if>
    </select>

    <select id="getAllLandAuction" resultMap="LandAuctionResultMap" >
        SELECT * FROM land_auction
    </select>

    <select id="getAllLandOwnedChanges" resultMap="LandOwnerChangeResultMap" >
        SELECT * FROM land_ownership_changes
    </select>

    <select id="getMaxLandSeq" parameterType="integer" resultType="integer">
        SELECT COALESCE(MAX(auction_seq), 0)
        FROM land_auction
        WHERE land_id = #{landId}
    </select>

    <select id="getPossibleLand" parameterType="integer" resultType="integer">
        SELECT count(*)
        FROM land_auction
        WHERE land_id = #{landId}
          AND status NOT IN ('AUCTION_END', 'DELETE', 'FAIL', 'CANCEL')
    </select>

    <select id="getPossibleLandOwnerChanges" parameterType="integer" resultType="integer">
        SELECT count(*)
        FROM land_ownership_changes
        WHERE land_id = #{landId}
          AND status = 'WAIT'
    </select>

    <!--이벤트 상세 조회-->
    <select id="getLandAuctionDetail" parameterType="java.lang.Long" resultMap="LandAuctionResultMap" >
        SELECT
            a.id
             , a.land_id
             , a.land_name
             , a.land_size
             , a.land_socket
             , a.auction_seq
             , a.status
             , a.resv_start_dt
             , a.auction_start_dt
             , a.auction_end_dt
             , a.currency_type
             , a.start_price
             , a.close_end_dt
             , a.close_price
             , a.deleted
             , (SELECT email FROM admin WHERE id = a.create_by ) AS create_by
             , a.create_dt
             , (SELECT email FROM admin WHERE id = a.update_by ) AS update_by
             , a.update_dt
        FROM land_auction a
        WHERE a.id = #{id}
    </select>

    <select id="getLandOwnerChangeDetail" parameterType="java.lang.Long" resultMap="LandOwnerChangeResultMap" >
        SELECT
            a.id
             , a.land_id
             , a.land_name
             , a.user_guid
             , a.user_name
             , a.is_reserve
             , a.reservation_dt
             , a.status
             , a.deleted
             , (SELECT email FROM admin WHERE id = a.create_by ) AS create_by
             , a.create_dt
             , (SELECT email FROM admin WHERE id = a.update_by ) AS update_by
             , a.update_dt
        FROM land_ownership_changes a
        WHERE a.id = #{id}
    </select>

    <select id="getLandOwnerChangeInfo" parameterType="java.lang.Integer" resultMap="LandOwnerChangeResultMap" >
        SELECT
        a.id
        , a.land_id
        , a.land_name
        , a.user_guid
        , a.user_name
        , a.is_reserve
        , a.reservation_dt
        , a.status
        FROM land_ownership_changes a
        WHERE a.land_id = #{landId}
    </select>

    <!--랜드경매 저장-->
    <insert id="postLandAuction" parameterType="com.caliverse.admin.domain.request.LandRequest" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO land_auction (land_id, land_name, land_size, land_socket, auction_seq, resv_start_dt, auction_start_dt, auction_end_dt, currency_type, start_price, create_by, update_by)
                  VALUES (#{landId}, #{landName}, #{landSize}, #{landSocket}, #{auctionSeq}, #{resvStartDt}, #{auctionStartDt}, #{auctionEndDt}, #{currencyType}, #{startPrice}, #{createBy}, #{updateBy})
    </insert>

    <insert id="postLandOwnerChange" parameterType="com.caliverse.admin.domain.request.LandRequest" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO land_ownership_changes (land_id, land_name, building_id, building_name, user_guid, user_name, is_reserve, reservation_dt, create_by, update_by)
        VALUES (#{landId}, #{landName}, #{buildingId}, #{buildingName}, #{userGuid}, #{userName}, #{isReserve}, #{reservationDt}, #{createBy}, #{updateBy})
    </insert>

    <!--랜드경매 수정-->
    <update id="updateLandAuction" parameterType="com.caliverse.admin.domain.request.LandRequest">
        UPDATE land_auction SET resv_start_dt = #{resvStartDt}
                        , auction_start_dt = #{auctionStartDt}
                        , auction_end_dt = #{auctionEndDt}
                        , currency_type = #{currencyType}
                        , start_price = #{startPrice}
                        , update_by = #{updateBy}
                        , update_dt = NOW()
        WHERE id = #{id}
    </update>

    <update id="updateLandOwnerChange" parameterType="com.caliverse.admin.domain.request.LandRequest">
        UPDATE land_ownership_changes
        SET user_guid = #{userGuid}
          , reservation_dt = #{reservationDt}
          , update_by = #{updateBy}
          , update_dt = NOW()
        WHERE id = #{id}
    </update>

    <update id="deleteLandAuction" parameterType="map">
        UPDATE land_auction
        SET deleted = 1
            ,status = 'CANCEL'
            , update_by = #{updateBy}
            , update_dt = NOW()
        WHERE id = #{id}
    </update>

    <update id="deleteLandOwnerChanges" parameterType="map">
        UPDATE land_ownership_changes
        SET deleted = 1
          ,status = 'CANCEL'
          , update_by = #{updateBy}
          , update_dt = NOW()
        WHERE id = #{id}
    </update>

    <update id="updateStatusLandAuction" parameterType="map">
        UPDATE land_auction
        SET status = #{status}
        <if test="bidderGuid != null and bidderGuid != ''">
            , bidder_guid = #{bidderGuid}
            , bidder_nickname = #{bidderNickname}
        </if>
        <if test="closeEndDt != null and closeEndDt != ''">
            , close_end_dt = #{closeEndDt, jdbcType=TIMESTAMP}
        </if>
        <if test="closePrice != null and closePrice != ''">
            , close_price = #{closePrice}
        </if>
        where id = #{id}
    </update>

    <update id="updateStatusLandOwnedChange" parameterType="map">
        UPDATE land_ownership_changes
        SET status = #{status}
        where id = #{id}
    </update>

    <select id="getScheduleLandAuctionList" resultMap="LandAuctionResultMap">
        SELECT id
             , land_id
             , auction_seq
             , status
             , resv_start_dt
             , auction_start_dt
             , auction_end_dt
             , currency_type
             , start_price
        FROM land_auction
        WHERE (status = 'WAIT' or status = 'AUCTION_START' or status = 'RESV_START')
          AND deleted = 0
    </select>

    <select id="getScheduleLandOwnedChangeList" resultMap="LandOwnerChangeResultMap">
        SELECT id
             , land_id
             , land_name
             , building_id
             , building_name
             , status
             , user_guid
             , user_name
             , is_reserve
             , reservation_dt
        FROM land_ownership_changes
        WHERE (status = 'WAIT')
          AND deleted = 0
--           AND is_reserve = 1
    </select>

    <update id="initLandAuction" parameterType="java.lang.Long">
        DELETE
        FROM land_auction
        WHERE id = #{id}
    </update>

    <update id="initLandOwnedChanges" parameterType="java.lang.Long">
        DELETE
        FROM land_ownership_changes
        WHERE id = #{id}
    </update>

</mapper>
