<?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">
<!--
  UtilityOrderMapper — M4 유틸리티 신청 영속(utility_order, V3). quote 는 jsonb, wiring 은 후속(문서 큐).
  ★backend-dev 추가(db-engineer 계약 §3 밖). 기존 테이블만 사용, 마이그레이션 불변.
-->
<mapper namespace="com.zioinfo.kintex.module.m4.mapper.UtilityOrderMapper">

    <!-- 신청 저장(재제출 시 신규 접수번호로 이력 축적). -->
    <insert id="insertOrder">
        INSERT INTO utility_order (id, event_id, booth_id, status, quote,
                                   location_diagram_url, supply_timing, created_at)
        VALUES (#{params.orderId}, #{params.eventId}, #{params.boothId},
                COALESCE(#{params.status}, 'submitted'),
                CAST(#{params.quoteJson} AS jsonb),
                #{params.locationDiagramUrl}, #{params.supplyTiming}, now())
    </insert>

    <!-- 부스 단위 최신 신청. -->
    <select id="findOrder" resultType="map">
        SELECT id                   AS "orderId",
               status,
               quote::text          AS "quoteJson",
               location_diagram_url AS "locationDiagramUrl",
               supply_timing        AS "supplyTiming"
        FROM utility_order
        WHERE event_id = #{eventId}
          AND booth_id = #{boothId}
        ORDER BY created_at DESC
        LIMIT 1
    </select>

</mapper>
