<?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="yardLayout">

  <sql id="yardLayoutSearchCondition">
    <if test="keyword != null and keyword != ''">
      AND (name ILIKE '%' || #{keyword} || '%')
    </if>
  </sql>

  <select id="getYardLayoutList" parameterType="map" resultType="map">
    SELECT
      yl.*,
      (SELECT COUNT(*) FROM yard_material_placement ymp WHERE ymp.yard_layout_id = yl.id) AS placementCount

    FROM yard_layout yl

    WHERE 1=1
    <include refid="common.companyCodeFilter"/>
    <include refid="yardLayoutSearchCondition"/>

    <include refid="common.dynamicOrderBy"/>
    <include refid="common.pagination"/>
  </select>

  <select id="getYardLayoutListCnt" parameterType="map" resultType="int">
    SELECT COUNT(*)

    FROM yard_layout

    WHERE 1=1
    <include refid="common.companyCodeFilter"/>
    <include refid="yardLayoutSearchCondition"/>
  </select>

  <select id="getYardLayoutInfo" parameterType="map" resultType="map">
    SELECT *

    FROM yard_layout

    WHERE id = #{id}
    <include refid="common.companyCodeFilter"/>
  </select>

  <insert id="insertYardLayout" parameterType="map" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO yard_layout (name, description, created_by, company_code, CREATED_DATE, UPDATED_DATE)
    VALUES (#{name}, #{description}, #{created_by}, #{company_code}, NOW(), NOW())
  </insert>

  <update id="updateYardLayout" parameterType="map">
    UPDATE yard_layout

    SET
      name        = COALESCE(#{name}, name),
      description = COALESCE(#{description}, description),
      UPDATED_DATE  = NOW()

    WHERE id = #{id}
    <include refid="common.companyCodeFilter"/>
  </update>

  <delete id="deleteYardLayout" parameterType="map">
    DELETE FROM yard_layout

    WHERE id = #{id}
    <include refid="common.companyCodeFilter"/>
  </delete>

  <!-- Placement -->
  <select id="getYardPlacementList" parameterType="map" resultType="map">
    SELECT *

    FROM yard_material_placement

    WHERE yard_layout_id = #{yard_layout_id}

    ORDER BY id ASC
  </select>

  <insert id="insertYardPlacement" parameterType="map" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO yard_material_placement (
      yard_layout_id, material_code, material_name, quantity, unit,
      position_x, position_y, position_z,
      size_x, size_y, size_z,
      color, data_source_type, data_source_config,
      data_binding, memo, CREATED_DATE, UPDATED_DATE
    ) VALUES (
      #{yard_layout_id}, #{material_code}, #{material_name}, #{quantity}, #{unit},
      #{position_x}, #{position_y}, #{position_z},
      #{size_x}, #{size_y}, #{size_z},
      #{color}, #{data_source_type}, #{data_source_config},
      #{data_binding}, #{memo}, NOW(), NOW()
    )
  </insert>

  <update id="updateYardPlacement" parameterType="map">
    UPDATE yard_material_placement

    SET
      material_code      = COALESCE(#{material_code},      material_code),
      material_name      = COALESCE(#{material_name},      material_name),
      quantity           = COALESCE(#{quantity},          quantity),
      unit               = COALESCE(#{unit},              unit),
      position_x         = COALESCE(#{position_x},         position_x),
      position_y         = COALESCE(#{position_y},         position_y),
      position_z         = COALESCE(#{position_z},         position_z),
      size_x             = COALESCE(#{size_x},             size_x),
      size_y             = COALESCE(#{size_y},             size_y),
      size_z             = COALESCE(#{size_z},             size_z),
      color              = COALESCE(#{color},             color),
      data_source_type   = COALESCE(#{data_source_type},   data_source_type),
      data_source_config = COALESCE(#{data_source_config}, data_source_config),
      data_binding       = COALESCE(#{data_binding},      data_binding),
      memo               = COALESCE(#{memo},              memo),
      UPDATED_DATE         = NOW()

    WHERE id = #{id}
  </update>

  <delete id="deleteYardPlacement" parameterType="map">
    DELETE FROM yard_material_placement

    WHERE id = #{id}
  </delete>

  <delete id="deleteYardPlacementsByLayoutId" parameterType="map">
    DELETE FROM yard_material_placement

    WHERE yard_layout_id = #{yard_layout_id}
  </delete>

</mapper>
