<?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.interplug.qcast.biz.canvasBasicSetting.CanvasBasicSettingMapper">

    <select id="getCanvasBasicSettingCnt" parameterType="map"
            resultType="com.interplug.qcast.biz.canvasBasicSetting.dto.CanvasBasicSettingInfo">
        /* sqlid : com.interplug.qcast.canvasBasicSetting.getCanvasBasicSettingCnt Canvas Basic Setting 유무 */
        SELECT COUNT(1) AS roofCnt
        FROM TB_CANVAS_BASIC_SETUP
        WHERE object_no = #{objectNo}
          AND plan_no = #{planNo}
    </select>

    <select id="selectCanvasBasicSetting" parameterType="map"
            resultType="com.interplug.qcast.biz.canvasBasicSetting.dto.CanvasBasicSettingResponse">
        /* sqlid : com.interplug.qcast.canvasBasicSetting.selectCanvasBasicSetting Canvas Basic Setting 조회 */
        SELECT cbs.object_no
             , cbs.plan_no
             , cbs.roof_size_set
             , cbs.roof_angle_set
             , crma.roof_apply
             , crma.roof_seq
             , crma.roof_matl_cd
             , crma.roof_width
             , crma.roof_height
             , crma.roof_hajebichi
             , crma.roof_gap
             , crma.roof_layout
             , crma.roof_pitch
             , crma.roof_angle
        FROM TB_CANVAS_BASIC_SETUP cbs
                 LEFT JOIN TB_CANVAS_ROOF_MATERIALS_ADD crma
                           ON cbs.object_no = crma.object_no
                               AND cbs.plan_no = crma.plan_no
        WHERE cbs.object_no = #{objectNo}
          AND cbs.plan_no = #{planNo}
        ORDER BY crma.roof_seq
    </select>

    <insert id="insertCanvasBasicSetting"
            parameterType="com.interplug.qcast.biz.canvasBasicSetting.dto.CanvasBasicSettingInfo">
        /* sqlid : com.interplug.qcast.canvasBasicSetting.insertCanvasBasicSetting Canvas Basic Setting 등록 */
        INSERT INTO TB_CANVAS_BASIC_SETUP
        ( object_no
        , plan_no
        , roof_size_set
        , roof_angle_set
        , regist_datetime
        , last_edit_datetime)
        VALUES ( #{objectNo}
               , #{planNo}
               , #{roofSizeSet}
               , #{roofAngleSet}
               , GETDATE()
               , GETDATE());
    </insert>

    <update id="updateCanvasBasicSetting"
            parameterType="com.interplug.qcast.biz.canvasBasicSetting.dto.CanvasBasicSettingInfo">
        /* sqlid : com.interplug.qcast.canvasBasicSetting.updateCanvasBasicSetting Canvas Basic Setting 수정 */
        UPDATE TB_CANVAS_BASIC_SETUP
        SET roof_size_set      = #{roofSizeSet}
          , roof_angle_set     = #{roofAngleSet}
          , last_edit_datetime = GETDATE()
        WHERE object_no = #{objectNo}
          AND plan_no = #{planNo}
    </update>

    <delete id="deleteCanvasBasicSetting" parameterType="map">
        /* sqlid : com.interplug.qcast.canvasBasicSetting.deleteCanvasBasicSetting Canvas Basic Setting 삭제 */
        DELETE
        FROM TB_CANVAS_BASIC_SETUP
        WHERE object_no = #{objectNo}
          AND plan_no = #{planNo}
    </delete>

    <select id="getRoofMaterialsCnt" parameterType="map"
            resultType="com.interplug.qcast.biz.canvasBasicSetting.dto.RoofMaterialsAddInfo">
        /* sqlid : com.interplug.qcast.canvasBasicSetting.getRoofMaterialsCnt Canvas 지붕재추가 Setting 유무 */
        SELECT COUNT(1) AS roofCnt
        FROM TB_CANVAS_BASIC_SETUP
        WHERE object_no = #{objectNo}
          AND plan_no = #{planNo}
    </select>

    <insert id="insertRoofMaterialsAdd"
            parameterType="com.interplug.qcast.biz.canvasBasicSetting.dto.RoofMaterialsAddInfo">
        /* sqlid : com.interplug.qcast.canvasBasicSetting.insertRoofMaterialsAdd Canvas 지붕재추가 Setting 등록 */
        INSERT INTO TB_CANVAS_ROOF_MATERIALS_ADD
        ( object_no
        , plan_no
        , roof_apply
        , roof_seq
        , roof_matl_cd
        , roof_width
        , roof_height
        , roof_hajebichi
        , roof_gap
        , roof_layout
        , roof_pitch
        , roof_angle
        , regist_datetime
        , last_edit_datetime)
        VALUES ( #{objectNo}
               , #{planNo}
               , #{roofApply}
               , #{roofSeq}
               , #{roofMatlCd}
               , #{roofWidth}
               , #{roofHeight}
               , #{roofHajebichi}
               , #{roofGap}
               , #{roofLayout}
               , #{roofPitch}
               , #{roofAngle}
               , GETDATE()
               , GETDATE());
    </insert>

    <update id="updateRoofMaterialsAdd"
            parameterType="com.interplug.qcast.biz.canvasBasicSetting.dto.RoofMaterialsAddInfo">
        /* sqlid : com.interplug.qcast.canvasBasicSetting.updateRoofMaterialsAdd Canvas 지붕재추가 Setting 수정 */
        MERGE TB_CANVAS_ROOF_MATERIALS_ADD AS target
            USING (SELECT #{objectNo} AS object_no, #{planNo} AS plan_no) AS source
            ON (target.object_no = source.object_no
            AND target.plan_no = source.plan_no
            AND target.roof_seq = #{roofSeq})
            WHEN MATCHED THEN
        UPDATE
            SET roof_apply = #{roofApply}
            , roof_seq = #{roofSeq}
            , roof_matl_cd = #{roofMatlCd}
            , roof_width = #{roofWidth}
            , roof_height = #{roofHeight}
            , roof_hajebichi = #{roofHajebichi}
            , roof_gap = #{roofGap}
            , roof_layout = #{roofLayout}
            , roof_pitch = #{roofPitch}
            , roof_angle = #{roofAngle}
            , last_edit_datetime = GETDATE()
            WHEN NOT MATCHED THEN
        INSERT
        (
        object_no
        ,
        plan_no
        ,
        roof_apply
        ,
        roof_seq
        ,
        roof_matl_cd
        ,
        roof_width
        ,
        roof_height
        ,
        roof_hajebichi
        ,
        roof_gap
        ,
        roof_layout
        ,
        roof_pitch
        ,
        roof_angle
        ,
        regist_datetime
        )
        VALUES (
            #{objectNo}
                , #{planNo}
                , #{roofApply}
                , #{roofSeq}
                , #{roofMatlCd}
                , #{roofWidth}
                , #{roofHeight}
                , #{roofHajebichi}
                , #{roofGap}
                , #{roofLayout}
                , #{roofPitch}
                , #{roofAngle}
                , GETDATE()
            );

    </update>

    <insert id="insertRoofAllocation" parameterType="com.interplug.qcast.biz.canvasBasicSetting.dto.RoofAllocationInfo">
        /* sqlid : com.interplug.qcast.canvasBasicSetting.insertRoofAllocation Canvas 지붕면 할당 Setting 등록 */
        INSERT INTO TB_CANVAS_ROOF_MATERIALS_ADD
        ( object_no
        , plan_no
        , roof_apply
        , roof_seq
        , roof_matl_cd
        , roof_width
        , roof_height
        , roof_hajebichi
        , roof_gap
        , roof_layout
        , roof_pitch
        , roof_angle
        , regist_datetime
        , last_edit_datetime)
        VALUES ( #{objectNo}
               , #{planNo}
               , #{roofApply}
               , #{roofSeq}
               , #{roofMatlCd}
               , #{roofWidth}
               , #{roofHeight}
               , #{roofHajebichi}
               , #{roofGap}
               , #{roofLayout}
               , #{roofPitch}
               , #{roofAngle}
               , GETDATE()
               , GETDATE());
    </insert>

    <delete id="deleteRoofMaterialsAdd" parameterType="map">
        /* sqlid : com.interplug.qcast.canvasBasicSetting.deleteRoofMaterialsAdd Canvas 지붕재추가 Setting 삭제 */
        DELETE
        FROM TB_CANVAS_ROOF_MATERIALS_ADD
        WHERE object_no = #{objectNo}
          AND plan_no = #{planNo}
    </delete>

</mapper>