<?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.excelDown.ExcelDownMapper">
    <select id="selectQuotPlanExclDownData" parameterType="com.interplug.qcast.biz.excelDown.dto.QuotRequest"
            resultType="com.interplug.qcast.biz.excelDown.dto.QuotPlanResponse">
        /* sqlid : com.interplug.qcast.api.excelDown.selectQuotPlanExclDownData (견적엑셀다운로드 플랜정보 데이터 조회) */
        SELECT
              B.OBJECT_NO                                                       /* 물건번호 物件番号 */
            , A.PLAN_NO                                                         /* 플랜 案件番号 */
            , CONVERT(varchar, A.LAST_EDIT_DATETIME, 120) AS LAST_EDIT_DATETIME /* 플랜정보 최신 갱신일시 プラン情報最新更新日時 */
            , G.SALE_STORE_NAME                                                 /* 판매대리점 販売代理店 */
            , Y.SALE_STORE_NAME AS SOLD_SALE_STORE_NAME                         /* 1차대리점명 一次代理店名 */
            , Y.SALE_STORE_ID AS SOLD_SALE_STORE_ID                             /* 1차대리점ID 一次代理店ID */
            , B.OBJECT_NAME                                                     /* 물건명 物件名 */
            , CASE WHEN A.NORTH_ARRANGEMENT = 1 THEN '有' ELSE '無' END AS NORTH_ARRANGEMENT /* 북면배치 北面配置 */
            , CAST(D.WORK_NO AS varchar) + ' ' + D.WORK_PROGRESS_STATUS_NAME AS WORK_PROGRESS_STATUS_NAME /* 수주예상 受注見込 */
            , B.ZIP_NO                                                          /* 우편번호 郵便番号 */
            , E.PREF_NAME                                                       /* 도도부현 都道府県 */
            , B.ADDRESS                                                         /* 주소 住所 */
            , A.WEATHER_POINT                                                   /* 가상지점 気象地点 */
            , CAST(A.CAPACITY AS varchar) + 'kw' AS CAPACITY                    /* 용량 容量 */
            , B.REMARKS                                                         /* 비고 備考 */
            , CONVERT(varchar, B.ESTIMATE_DETAIL_CREATE_DATE, 120) AS ESTIMATE_DETAIL_CREATE_DATE /* 견적 작성일 見積作成日 */
            , CONVERT(varchar, B.SPECIFICATION_CONFIRM_DATE, 120) AS SPECIFICATION_CONFIRM_DATE /* 사양 확인일 仕様確認日 */
            , COALESCE(L.BUSINESS_CHARGER, '') AS BUSINESS_CHARGER              /* 영업 담당자(판매점 등록) 営業担当者_販売店登録 */
            , COALESCE(M.BUSINESS_TEAM, '') AS BUSINESS_TEAM                    /* 영업팀(판매점 등록) 営業チーム_販売店登録 */
            , (CASE WHEN UPPER(#{langCd}) = 'JA' THEN C1.CODE_JP
                ELSE C1.CODE_NM
                END) AS OBJECT_STATUS                                           /* 주택구분 住宅区分 */
            , CONVERT(varchar, B.MANAGER_LAST_EDIT_DATETIME, 120) AS MANAGER_LAST_EDIT_DATETIME /* 관리자 최종 갱신일시管理者最終更新日時 */
            , X.SALE_STORE_ID                                                   /* 최종갱신판매점ID 最終更新販売店ID */
            , A.LAST_EDIT_USER                                                  /* 최종갱신자 最終更新者 */
            , (CASE WHEN UPPER(#{langCd}) = 'JA' THEN C2.CODE_JP
                ELSE C2.CODE_NM
                END) AS ROF_TYP                                                 /* 지붕종류 屋根種類 */
            , CASE WHEN A.DIFF_ROOF_ENABLED = 1 THEN A.ROOF_MATERIAL_ID_MULTI ELSE COALESCE((CASE WHEN UPPER(#{langCd}) = 'JA' THEN C3.CODE_JP
                                                                                                ELSE C3.CODE_NM
                                                                                                END), '') END AS ROOF_MATERIAL_NM /* 지붕재 屋根材 */
            , CASE WHEN A.DIFF_ROOF_ENABLED = 1 THEN A.SUPPORT_MEAKER_MULTI ELSE A.SUPPORT_MEAKER END AS SUPPORT_MEAKER_NM /* 가대메이커 架台メーカ */
            , CASE WHEN A.DIFF_ROOF_ENABLED = 1 THEN A.SUPPORT_METHOD_ID_MULTI ELSE COALESCE(P.NAME, '') END AS SUPPORT_METHOD_NM /* 지지금구 支持金具 */
            , A.SNOWFALL                                                        /* 적설수준 積雪レベル */
            , (CASE WHEN UPPER(#{langCd}) = 'JA' THEN C4.CODE_JP
                ELSE C4.CODE_NM
                END) AS CNS_LVL                                                 /* 시공레벨 施工レベル */
            , U.HEIGHT                                                          /* 설치높이 設置高さ */
            , W.MENSODO AS CTND_CLSF                                            /* 면조도구분 面粗度区分 */
            , V.CAPACITY AS ATH_CPC                                             /* 인정용량 認定容量 */
            , (CASE
                WHEN G.FIRST_AGENT_FLG = '1' THEN '1'
                WHEN G.SECOND_AGENT_FLG = '1' THEN '2'
                ELSE '' END ) + '次店' AS SALE_STORE_LEVEL                       /* 판매점모녀관계 販売店親子関係 */
            , H.DIRECTION                                                       /* 방위 方位 */
            , H.MODULE_TYPE                                                     /* 모듈종류 モジュール種類 */
            , H.MODULE_NUM                                                      /* 설치매수 設置枚数 */
            , H.ROOF_ANGLE                                                      /* 지붕경사 屋根勾配 */
            , H.PC_TYPE                                                         /* 파워콘종류 パワコン種類 */
            , H.PC_NUM                                                          /* 파워콘대수 パワコン台数 */
            , H.BOX_TYPE                                                        /* 접속상자종류 接続箱種類 */
            , H.BOX_NUM                                                         /* 접속상자수 接続箱数 */
            , H.CABLE_TYPE                                                      /* 케이블종류 ケーブル種類 */
            , H.CABLE_NUM                                                       /* 케이블개수 ケーブル本数 */
            , H.BOOST_TYPE                                                      /* 승압회로종류 昇圧回路種類 */
            , H.BOOST_NUM                                                       /* 승압회로대수 昇圧回路台数 */
            , H.DISPLAY_TYPE                                                    /* 표시유닛종류 表示ユニット種類 */
            , H.DISPLAY_NUM                                                     /* 표시유닛대수 表示ユニット数 */
            , H.COST_ALL                                                        /* 재료구입가격(kW단가) 材料仕入価格 */
            , H.COST_MODULE                                                     /* 모듈구입가격(kW단가) モジュール仕入価格 */
            , H.COST_OTHER                                                      /* 주변기기구입가격 周辺機器仕入価格 */
            , H.COST_STAND                                                      /* 가대구입가격(kW단가)架台仕入価格 */
            , H.SALE_ALL                                                        /* 판매단가(kW단가)販売単価 */
            , H.SALE_MODULE                                                     /* 모듈판매가격(kW단가) モジュール販売価格 */
            , H.SALE_OTHER                                                      /* 주변기기 판매가격(kW단가) 周辺機器販売価格 */
            , H.SALE_STAND                                                      /* 가대판매가격 架台販売価格 */
            <!-- /* 사용안함 */
            , CONVERT(varchar, B.ORDER_DATE, 120) AS ORDER_DATE /* 発注日 */
            , CONVERT(varchar, B.DELIVERY_HOPE_DATE, 120) AS DELIVERY_HOPE_DATE /* 納入希望日 */
            , B.DELIVERY_ZIP_NO /* 納入先郵便番号 */
            , B.DELIVERY_TARGET /* 納入場所 */
            , CONVERT(varchar, F.COOPERATION_DATE, 120) AS COOPERATION_DATE/* 連系日 */
            , CONVERT(varchar, R.SHIPPED_DATE1, 120) AS SHIPPED_DATE1 /* 出荷日1 */
            , CONVERT(varchar, R.SHIPPED_DATE2, 120) AS SHIPPED_DATE2 /* 出荷日2 */
            , CONVERT(varchar, R.SHIPPED_DATE3, 120) AS SHIPPED_DATE3 /* 出荷日3 */
            , CONVERT(varchar, R.SHIPPED_DATE4, 120) AS SHIPPED_DATE4 /* 出荷日4 */
            , CONVERT(varchar, R.SHIPPED_DATE5, 120) AS SHIPPED_DATE5 /* 出荷日5 */
            , CONVERT(varchar, R.SHIPPED_DATE1, 120) AS SHIPPED_DATE555/* 支払日 */
            , CONVERT(varchar, S.APPLICATION_DATE1, 120) AS APPLICATION_DATE1 /* 保証書申請日1 */
            , CONVERT(varchar, S.APPLICATION_DATE2, 120) AS APPLICATION_DATE2 /* 保証書申請日2 */
            , CONVERT(varchar, S.APPLICATION_DATE3, 120) AS APPLICATION_DATE3 /* 保証書申請日3 */
            , CONVERT(varchar, S.APPLICATION_DATE4, 120) AS APPLICATION_DATE4 /* 保証書申請日4 */
            , CONVERT(varchar, S.APPLICATION_DATE5, 120) AS APPLICATION_DATE5 /* 保証書申請日5 */
            , CONVERT(varchar, S.ISSUE_DATE1, 120) AS ISSUE_DATE1 /* 保証書承認発行日1 */
            , CONVERT(varchar, S.ISSUE_DATE2, 120) AS ISSUE_DATE2 /* 保証書承認発行日2 */
            , CONVERT(varchar, S.ISSUE_DATE3, 120) AS ISSUE_DATE3 /* 保証書承認発行日3 */
            , CONVERT(varchar, S.ISSUE_DATE4, 120) AS ISSUE_DATE4 /* 保証書承認発行日4 */
            , CONVERT(varchar, S.ISSUE_DATE5, 120) AS ISSUE_DATE5 /* 保証書承認発行日5 */
            , S.WARRANTY_NO1 /* 保証書番号1 */
            , S.WARRANTY_NO2 /* 保証書番号2 */
            , S.WARRANTY_NO3 /* 保証書番号3 */
            , S.WARRANTY_NO4 /* 保証書番号4 */
            , S.WARRANTY_NO5 /* 保証書番号5 */
            , T.JAN /* 発電量1月 */
            , T.FEB /* 発電量2月 */
            , T.MAR /* 発電量3月 */
            , T.APR /* 発電量4月 */
            , T.MAY /* 発電量5月 */
            , T.JUN /* 発電量6月 */
            , T.JUL /* 発電量7月 */
            , T.AUG /* 発電量8月 */
            , T.SEP /* 発電量9月 */
            , T.OCT /* 発電量10月 */
            , T.NOV /* 発電量11月 */
            , T.DEC /* 発電量12月 */
            , T.TOTAL /* 発電量年間 */
            , CONVERT(varchar, F.GUARANTEE_APPLY_DATE, 120) AS GUARANTEE_APPLY_DATE /* 保証書申請日 */
            , CONVERT(varchar, F.GUARANTEE_APPROVAL_DATE, 120) AS GUARANTEE_APPROVAL_DATE /* 保証書承認日 */
            , CONVERT(varchar, B.SHIPMENT_DATA_GET_DATE, 120) AS SHIPMENT_DATA_GET_DATE /* シェンカーデータ取得日 */
            , CONVERT(varchar, F.GUARANTEE_ISSUE_DATE, 120) AS GUARANTEE_ISSUE_DATE /* 保証書発行日 */
            , CONVERT(varchar, F.GUARANTEE_SEND_DATE, 120) AS GUARANTEE_SEND_DATE /* 保証書送付日 */
            , F.NATURAL_DISASTER_COPENSATE /* 自然保険災害 */
            , B.MANAGER_REWMARKS /* 管理者備考 */
            , CASE WHEN F.STAND_GUARANTEE_NO_TARGET = 1 THEN '有' ELSE '無' END AS STAND_GUARANTEE_NO_TARGET /* 架台保証対象外
            , COALESCE(I.BUSINESS_GROUP, '') AS BUSINESS_GROUP /* 営業グループ */
            , COALESCE(J.BUSINESS_CHARGER, '') AS BUSINESS_CHARGER /* 営業担当者 */
            , COALESCE(K.BUSINESS_TEAM, '') AS BUSINESS_TEAM /* 営業チーム */
            , CASE WHEN F.MAX_PACKAGE = 1 THEN '有' ELSE '無' END AS MAX_PACKAGE /* ＭＡＸパッケージ */
            , CONVERT(varchar, A.LAST_EDIT_DATETIME, 120) AS LAST_EDIT_DATETIME /* 見積最終更新日時 */
            , CONVERT(varchar, A.DRAWING_ESTIMATE_CREATE_DATE, 120) AS DRAWING_ESTIMATE_CREATE_DATE/* プラン見積作成日 */
            -->
        FROM T_OBJECT B
        LEFT OUTER JOIN T_PLAN A
            ON A.OBJECT_NO = B.OBJECT_NO
        LEFT OUTER JOIN M_COMM_L C1
            ON C1.CODE = B.OBJECT_STATUS_ID
            AND C1.HEAD_CD = '201700'
        LEFT OUTER JOIN M_WORK_PROGRESS D
            ON B.WORK_NO = D.WORK_NO
        LEFT OUTER JOIN M_PREFECTURE E
            ON B.PREF_ID = E.PREF_ID
        <!-- LEFT OUTER JOIN T_GUARANTEE F ON A.OBJECT_NO = F.OBJECT_NO AND A.PLAN_NO = F.PLAN_NO -->
        LEFT OUTER JOIN M_SALES_STORE G
            ON B.SALE_STORE_ID = G.SALE_STORE_ID /*본인정보*/
        LEFT OUTER JOIN T_STATISTICS H
            ON A.OBJECT_NO = H.OBJECT_NO
            and A.PLAN_NO = H.PLAN_NO
        <!-- LEFT OUTER JOIN M_BUSINESS_GROUP I ON B.BUSINESS_GROUP_CD = I.BUSINESS_GROUP_CD          -->
        <!-- LEFT OUTER JOIN M_BUSINESS_CHARGER J ON B.BUSINESS_CHARGER_CD = J.BUSINESS_CHARGER_CD    -->
        <!-- LEFT OUTER JOIN M_BUSINESS_TEAM K ON J.BUSINESS_TEAM_CD = K.BUSINESS_TEAM_CD             -->
        LEFT OUTER JOIN M_BUSINESS_CHARGER L
            ON G.BUSINESS_CHARGER_CD = L.BUSINESS_CHARGER_CD
        LEFT OUTER JOIN M_BUSINESS_TEAM M
            ON L.BUSINESS_TEAM_CD = M.BUSINESS_TEAM_CD
        LEFT OUTER JOIN M_COMM_L C2
            ON C2.CODE = A.ROOF_KIND_ID
            AND C2.HEAD_CD = '201800'
        LEFT OUTER JOIN M_COMM_L C3
            ON C3.CODE = A.ROOF_MATERIAL_ID
            AND C3.HEAD_CD = '201900'
        LEFT OUTER JOIN M_SUPPORT_METHOD P
            ON A.SUPPORT_METHOD_ID = P.SUPPORT_METHOD_ID
        LEFT OUTER JOIN M_COMM_L C4
            ON C4.CODE = A.CONSTRUCT_SPECIFICATION
            AND C4.HEAD_CD = '201300'
        <!-- LEFT OUTER JOIN W_SHIPPED_DATE R ON B.OBJECT_NO = R.OBJECT_NO                            -->
        <!-- LEFT OUTER JOIN W_WARRANTY S ON B.OBJECT_NO = S.OBJECT_NO                                -->
        <!-- LEFT OUTER JOIN T_SIMULATION T ON A.OBJECT_NO = T.OBJECT_NO AND A.PLAN_NO = T.PLAN_NO    -->
        LEFT OUTER JOIN T_ROOF_INSTALLED_HEIGHT U
            ON A.OBJECT_NO = U.OBJECT_NO
        LEFT OUTER JOIN T_APPROVAL_CAPATICY V
            ON A.OBJECT_NO = V.OBJECT_NO
        AND A.PLAN_NO = V.PLAN_NO
        LEFT OUTER JOIN T_MENSODO W
            ON A.OBJECT_NO = W.OBJECT_NO
        LEFT OUTER JOIN M_USER X
            ON A.LAST_EDIT_USER = X.USER_ID
        LEFT OUTER JOIN M_SALES_STORE Y
            ON G.FIRST_AGENT_ID = Y.SALE_STORE_ID /*1차점정보*/
        WHERE A.DEL_FLG = 0
            AND B.DEL_FLG = 0
        <if test="sch_startDt != null and sch_startDt != '' and sch_endDt != null and sch_endDt != ''"> <!-- 견적일 -->
            AND B.ESTIMATE_DETAIL_CREATE_DATE <![CDATA[>=]]> #{sch_startDt} + ' 00:00:00'
            AND B.ESTIMATE_DETAIL_CREATE_DATE <![CDATA[<=]]> #{sch_endDt} + ' 23:59:59'
        </if>
        <if test="sch_saleStoreId != null and sch_saleStoreId != ''"> <!-- 판매대리점명/ID -->
            AND B.SALE_STORE_ID = #{sch_saleStoreId}
        </if>
        <if test="sch_businessChargerCd != null and sch_businessChargerCd != ''"> <!-- 영업담당자 -->
            AND G.BUSINESS_CHARGER_CD = #{sch_businessChargerCd}
        </if>
    </select>

    <select id="selectQuotItemExclDownData" parameterType="com.interplug.qcast.biz.excelDown.dto.QuotRequest"
            resultType="com.interplug.qcast.biz.excelDown.dto.QuotItemResponse">
        /* sqlid : com.interplug.qcast.api.excelDown.selectQuotItemExclDownData (견적엑셀다운로드 품목단위 데이터 조회) */
        SELECT
              C.OBJECT_NO                                   /* 물건번호 物件番号 */
            , B.PLAN_NO                                     /* 플랜 案件番号 */
            , C.OBJECT_NAME                                 /* 물건명 案件名 */
            , H.SALE_STORE_NAME AS SOLD_SALE_STORE_NAME     /* 1차 판매점명 一次販売店名 */
            , D.SALE_STORE_NAME                             /* 작성 판매점 作成販売店 */
            , I.PREF_NAME                                   /* 도도부현 エリア */
            , K.BUSINESS_TEAM                               /* 영업팀(판매점등록) 担当課支店 */
            , C.SALE_STORE_ID                               /* 견적서작성 판매점 ID 販売店ID */
            , C.ESTIMATE_DETAIL_CREATE_DATE                 /* 견적작성일 見積作成日*/
            , A.ITEM_NO                                     /* 고객용제품명 品番 */
            , A.ITEM_NAME                                   /* basicmaterial 品名 */
            , A.AMOUNT                                      /* 수량 明細 */
            , (
                SELECT TOP 1 T.SALE_PRICE
                FROM M_SALE_STORE_PRICE T
                WHERE T.SALE_STORE_ID = D.SALE_STORE_ID
                AND T.ITEM_ID = A.ITEM_ID
            ) AS SALE_PRICE                                 /* 판매단가 販売単価2 */
            <!--
            /* 사용안함 */
            , (
                SELECT TOP 1 T.COST_PRICE
                FROM M_SALE_STORE_PRICE T
                WHERE T.SALE_STORE_ID = D.SALE_STORE_ID
                AND T.ITEM_ID = A.ITEM_ID
            ) AS COST_PRICE /* 구매단가 仕入れ単価 */
            , C.SALE_STORE_ID /* 販売代理店 */
            , A.AMOUNT_CHANGE /* 明細_変更 */
            , D.FIRST_AGENT_FLG /* 一次代理店フラグ */
            -->
        FROM T_OBJECT C
        LEFT OUTER JOIN M_SALES_STORE D
            ON C.SALE_STORE_ID = D.SALE_STORE_ID
        LEFT OUTER JOIN T_PLAN B
            ON C.OBJECT_NO = B.OBJECT_NO
        LEFT OUTER JOIN T_PART_ESTIMATE A
            ON A.OBJECT_NO = B.OBJECT_NO
            AND A.PLAN_NO = B.PLAN_NO
        LEFT OUTER JOIN T_STATISTICS E
            ON A.OBJECT_NO = E.OBJECT_NO
            AND A.PLAN_NO = E.PLAN_NO
        LEFT OUTER JOIN M_BUSINESS_GROUP F
            ON C.BUSINESS_GROUP_CD = F.BUSINESS_GROUP_CD
            AND F.DEL_FLG = 0
        LEFT OUTER JOIN M_BUSINESS_CHARGER G
            ON C.BUSINESS_CHARGER_CD = G.BUSINESS_CHARGER_CD
            AND G.DEL_FLG = 0
        LEFT OUTER JOIN M_SALES_STORE H
            ON D.FIRST_AGENT_ID = H.SALE_STORE_ID
            AND H.DEL_FLG = 0
        LEFT OUTER JOIN M_PREFECTURE I
            ON C.PREF_ID = I.PREF_ID
        LEFT OUTER JOIN M_BUSINESS_CHARGER J
            ON D.BUSINESS_CHARGER_CD = J.BUSINESS_CHARGER_CD
            AND J.DEL_FLG = 0
        LEFT OUTER JOIN M_BUSINESS_TEAM K
            ON J.BUSINESS_TEAM_CD = K.BUSINESS_TEAM_CD
            AND K.DEL_FLG = 0
        WHERE C.DEL_FLG = 0
            AND D.DEL_FLG = 0
            AND B.DEL_FLG = 0
        <if test="sch_startDt != null and sch_startDt != '' and sch_endDt != null and sch_endDt != ''"> <!-- 견적일 -->
            AND C.ESTIMATE_DETAIL_CREATE_DATE IS NOT NULL
            AND C.ESTIMATE_DETAIL_CREATE_DATE <![CDATA[>=]]> #{sch_startDt} + ' 00:00:00'
            AND C.ESTIMATE_DETAIL_CREATE_DATE <![CDATA[<=]]> #{sch_endDt} + ' 23:59:59'
        </if>
        <if test="sch_saleStoreId != null and sch_saleStoreId != ''"> <!-- 판매대리점명/ID -->
            AND C.SALE_STORE_ID = #{sch_saleStoreId}
        </if>
        <if test="sch_businessChargerCd != null and sch_businessChargerCd != ''"> <!-- 영업담당자 -->
            AND C.BUSINESS_CHARGER_CD = #{sch_businessChargerCd}
        </if>
    </select>


    <select id="selectNtrCtsCmpExclDownData" parameterType="com.interplug.qcast.biz.excelDown.dto.NtrCtsCmpRequest" resultType="com.interplug.qcast.biz.excelDown.dto.NtrCtsCmpResponse">
        /* sqlid : com.interplug.qcast.api.excelDown.selectNtrCtsCmpExclDownData (자연재해보상입력 엑셀 다운로드 데이터 조회)*/
        SELECT
              A.GUARANTEE_RECEIVE_USER                          /* 구매자 성명購入者 */
            , A.OBJECT_NO                                       /* 물건번호 お客様コード */
            , B.PREF_NAME AS GUARANTEE_RECEIVE_USER_PREF_NAME   /* 구매자 도도부현 都道府県 */
            , A.GUARANTEE_RECEIVE_USER_ZIP_NO                   /* 구매자 우편번호 購入者郵便番号  */
            , A.GUARANTEE_RECEIVE_USER_ZIP_ADDRESS              /* 구매자 주소 購入者住所 */
            , A.SETUP_PLACE_OBJECT_NAME                         /* 견적 작성시 안건명 物件名 */
            , C.PREF_NAME                                       /* 물건 광역자치단체 物件都道府県 */
            , A.SETUP_PLACE_ADDRESS                             /* 물건 주소 物件住所 */
            , A.GUARANTEE_SEND_ZIP_NO                           /* 보증서 송부처 우편번호 保証書送付先郵便番号 */
            , A.GUARANTEE_SEND_ADDRESS                          /* 보증서 송부처 주소 保証書送付先住所 */
            , A.COOPERATION_DATE                                /* 설치일 設置日 */
            , A.GUARANTEE_ISSUE_DATE                            /* 보증서 발급일 保証書発行日 */
            , A.NATURAL_DISASTER_COPENSATE_NOTIFIED_DATE        /* 자연재해보상 통지일 自然災害補償通知日 */
            , A.EQUIP_GUARANTEE_NOTIFIED_DATE                   /* 연장기기보증 통지일 延長機器保証通知日 */
            , ISNULL((SELECT SUM(TG.PNOW_W * TG.AMOUNT)
                        FROM T_PART_GUARANTEE TG
                        INNER JOIN M_ITEM I
                            ON TG.ITEM_ID = I.ITEM_ID
                        WHERE TG.OBJECT_NO = A.OBJECT_NO
                            AND TG.PLAN_NO = A.PLAN_NO
                            AND I.MODULE_FLG = 1), 0
              ) AS TOT_PNOW                                     /* 태양전지용량 太陽電池容量 */
            , STUFF(
                (SELECT CHAR (10) + CAST (TG.AMOUNT AS NVARCHAR(MAX)) + '|' + TG.ITEM_NO
                    FROM T_PART_GUARANTEE TG
                    INNER JOIN  M_ITEM I
                        ON TG.ITEM_ID = I.ITEM_ID
                    WHERE TG.OBJECT_NO = A.OBJECT_NO
                        AND TG.PLAN_NO = A.PLAN_NO
                        AND I.MODULE_FLG = 1
                    ORDER BY TG.DISP_ORDER
                    FOR XML PATH (''), TYPE ).value('.', 'NVARCHAR(MAX)'), 1, 1, ''
              ) AS ITEM                                         /* 판낼수량/판넬형식 パネル数量 */
            , A.OUTPUT_COMP_SALE_STORE_NAME                     /* 판매점명 販売店名 */
            , A.GUARANTEE_REWMARKS                              /* 비고 備考 */
            , CASE
                WHEN D.HOUSE_CLASS_CD = 1 THEN '産業用'
                WHEN D.HOUSE_CLASS_CD = 0 THEN '住宅用'
                ELSE NULL
                END AS HOUSE_CLASS_CD                           /* 주택종류 住宅種別 */
            , CASE
                WHEN A.NATURAL_DISASTER_COPENSATE_APPLY = 1 THEN '有'
                WHEN A.NATURAL_DISASTER_COPENSATE_APPLY = 0 THEN '無'
                ELSE NULL
                END AS NATURAL_DISASTER_COPENSATE_APPLY         /* 자연재해보상가입(신청시) 自然災害補償_申請時 */
            , CASE
                WHEN A.NATURAL_DISASTER_COPENSATE = 1 THEN '有'
                WHEN A.NATURAL_DISASTER_COPENSATE = 0 THEN '無'
                ELSE NULL
                END AS NATURAL_DISASTER_COPENSATE               /* 자연재해보상가입 自然災害補償 */
            , CASE
                WHEN A.NATURAL_DISASTER_COPENSATE_RETROACT = 1 THEN 'TRUE'
                ELSE 'FALSE'
                END AS NATURAL_DISASTER_COPENSATE_RETROACT      /*자연재해보상(소급)  自然災害補償_遡及 */
            , CASE
                WHEN A.MAX_PACKAGE = 1 THEN 'TRUE'
                ELSE 'FALSE'
                END AS MAX_PACKAGE/*ＭＡＸパッケージ*/
            , CASE
                WHEN A.EQUIP_GUARANTEE_YEARS = 10 THEN '10年'
                WHEN A.EQUIP_GUARANTEE_YEARS = 15 THEN '15年'
                WHEN A.EQUIP_GUARANTEE_YEARS = 20 THEN '20年'
                ELSE ''
                END AS EQUIP_GUARANTEE_YEARS                    /* 기기보증연수 機器保証年数 */
            , CASE
                WHEN A.EQUIP_GUARANTEE_YEARS_RETROACT = 1 THEN 'TRUE'
                ELSE 'FALSE'
                END AS EQUIP_GUARANTEE_YEARS_RETROACT           /* 기기보증연수(소급) 機器保証年数_遡及 */
            , CAST(A.SUNSHINE_COPENSATE_YEARS AS NVARCHAR) + '年' AS SUNSHINE_COPENSATE_YEARS /* 일조보상연수 日照補償年数 */
        FROM T_GUARANTEE A
        LEFT OUTER JOIN M_PREFECTURE B
            ON A.GUARANTEE_RECEIVE_USER_PREF_ID = B.PREF_ID
        LEFT OUTER JOIN M_PREFECTURE C
            ON A.SETUP_PLACE_PREF_ID = C.PREF_ID
        LEFT OUTER JOIN T_OBJECT D
            ON A.OBJECT_NO = D.OBJECT_NO
        WHERE D.DEL_FLG = 0
        <if test="sch_dtType != null and sch_dtType != ''">
            <if test="sch_startDt != null and sch_startDt != '' and sch_endDt != null and sch_endDt != ''">
                <choose>
                    <when test='sch_dtType == "NATURAL" '> <!-- 자연재해보상 통지일 -->
                        AND A.NATURAL_DISASTER_COPENSATE_NOTIFIED_DATE IS NOT NULL
                        AND A.NATURAL_DISASTER_COPENSATE_NOTIFIED_DATE <![CDATA[>=]]> #{sch_startDt} + ' 00:00:00'
                        AND A.NATURAL_DISASTER_COPENSATE_NOTIFIED_DATE <![CDATA[<=]]> #{sch_endDt} + ' 23:59:59'
                    </when>
                    <when test='sch_dtType == "GUARANTEE" '> <!-- 보증서 발급일 -->
                        AND A.GUARANTEE_ISSUE_DATE IS NOT NULL
                        AND A.GUARANTEE_ISSUE_DATE <![CDATA[>=]]> #{sch_startDt} + ' 00:00:00'
                        AND A.GUARANTEE_ISSUE_DATE <![CDATA[<=]]> #{sch_endDt} + ' 23:59:59'
                    </when>
                    <when test='sch_dtType == "COOPERATION" '> <!-- 설치일 -->
                        AND A.COOPERATION_DATE IS NOT NULL
                        AND A.COOPERATION_DATE <![CDATA[>=]]> #{sch_startDt} + ' 00:00:00'
                        AND A.COOPERATION_DATE <![CDATA[<=]]> #{sch_endDt} + ' 23:59:59'
                    </when>
                </choose>
            </if>
        </if>
        <if test="sch_objectNo != null and sch_objectNo != ''"> <!--물건번호-->
            AND A.OBJECT_NO LIKE '%' + #{sch_objectNo} + '%'
        </if>
        <if test="sch_objectNm != null and sch_objectNm != ''"> <!-- 물건명 -->
            AND A.SETUP_PLACE_OBJECT_NAME LIKE '%' + #{sch_objectNm} + '%'
        </if>
        <if test="sch_saleStoreId != null and sch_saleStoreId != ''"> <!-- 판매대리점명/ID -->
            AND D.SALE_STORE_ID = #{sch_saleStoreId}
        </if>
    </select>

    <select id="selectWarrantyIssuedCmpExclDownData" parameterType="com.interplug.qcast.biz.excelDown.dto.WrntIsncCmplRequest" resultType="com.interplug.qcast.biz.excelDown.dto.WrntIsncCmplResponse">
        /* sqlid : com.interplug.qcast.api.excelDown.selectWarrantyIssuedCmpExclDownData (보증서발행완료 물건 엑셀 다운로드 데이터 조회)*/
        SELECT
              A.OBJECT_NO                                  /* 부동산 관리 번호 */
            , A.PLAN_NO	                                   /* 계획 */	             
            , A.GUARANTEE_APPLY_DATE	                   /* 보증서 신청일 */            
            , A.GUARANTEE_APPROVAL_DATE	                   /* 보증서 승인일 */	         
            , A.GUARANTEE_ISSUE_DATE	                   /* 보증서 발행일 */	         
            , A.GUARANTEE_SEND_DATE	                       /* 보증서 송부일 */	         
            , CASE
                WHEN A.NATURAL_DISASTER_COPENSATE = 1 THEN '有'
                WHEN A.NATURAL_DISASTER_COPENSATE = 0 THEN '無'
                ELSE NULL END AS NATURAL_DISASTER_COPENSATE               /* 자연재해보상가입 自然災害補償 */
            , A.GUARANTEE_OUTPUTTER_DISPLAY	               /* 보증서 출처 사이트 표시 플래그 */
            , A.MAX_PACKAGE	                               /* MAX 패키지 */	         
            , A.EQUIP_GUARANTEE_YEARS	                   /* 보증 연수 */	         
            , A.SUNSHINE_COPENSATE_YEARS	               /* 일조 보증 연수 */	         
            , A.GUARANTEE_RECEIVE_USER	                   /* 보증서 송부 수취인 */	     
            , A.GUARANTEE_RECEIVE_USER_KANA	               /* 보증서 송부 수취인 후리가나 */	 
            , A.GUARANTEE_RECEIVE_USER_TEL	               /* 보증서 송부 수취인 전화 번호 */	 
            , A.GUARANTEE_RECEIVE_USER_ZIP_NO	           /* 보증서 송부 수취인 우편 번호 */	 
            , A.GUARANTEE_RECEIVE_USER_PREF_ID	           /* 보증서 송부 수취인 도도부현 */	 
            , A.GUARANTEE_RECEIVE_USER_ZIP_ADDRESS	       /* 보증서 송부 수취인 주소 */	 
            , A.SETUP_PLACE_OBJECT_NAME	                   /* 설치 장소 물건명 */	     
            , A.SETUP_PLACE_ZIP_NO	                       /* 설치 장소 우편 번호 */	     
            , A.SETUP_PLACE_PREF_ID	                       /* 설치 장소 도도부현 */	     
            , A.SETUP_PLACE_ADDRESS	                       /* 설치 장소 주소 */	         
            , A.GUARANTEE_SEND_ZIP_NO	                   /* 보증서 송부 우편 번호 */	     
            , A.GUARANTEE_SEND_ADDRESS	                   /* 보증서 송부처 주소 */	     
            , A.GUARANTEE_SEND_STORE                       /* 보증서 송부처 대리점명 */   
            , A.GUARANTEE_SEND_STORE_CHARGER	            /* 보증서 송부처 대리점명 담당자 */	    
            , A.OUTPUT_COMP_SALE_STORE_NAME	                /* 출력 대비표 판매점명 */	        
            , A.GUARANTEE_SALE_STORE_ZIP_NO	                /* 보증서 판매점 우편 번호 */	    
            , A.GUARANTEE_SALE_STORE_ADDRESS	            /* 보증서 판매점 주소 */	        
            , A.GUARANTEE_SALE_STORE_TEL	                /* 보증서 판매점 전화 번호 */	    
            , A.COOPERATION_DATE	                        /* 협력일 */	                
            , A.STAND_GUARANTEE_NO_TARGET	                /* 가대 보증 대상외 */	        
            , A.STAND_GUARANTEE_SET	                        /* 가대 보증 세트 */	            
            , A.NATURAL_DISASTER_COPENSATE_NOTIFIED_DATE	/* 자연재해보상통지일	 */         
            , A.EQUIP_GUARANTEE_NOTIFIED_DATE	            /* 연장기기 보증 통지일 */	        
            , A.CONSTRUCT_DATE	                            /* 시공일 */	                
            , A.CONSTRUCT_ID	                            /* 시공 ID */	            
            , A.CONSTRUCT_ID2	                            /* 시공 IdⅡ */                
            , A.GUARANTEE_PERSON_NAME	                    /* 보증서 송부처 담당자명 */	        
            , A.GUARANTEE_PERSON_MAIL	                    /* 보증서 송부처 e-mail */	    
            , A.GUARANTEE_PERSON_TEL	                    /* 보증서 송부처 전화 번호 */	    
            , A.GUARANTEE_REWMARKS	                        /* 보증서 송부처 */	            
            , A.GUARANTEE_OUTPUT_REWMARKS	                /* 비고 */	                
            , A.LAST_EDIT_USER	                            /* 최종 업데이트자 */	            
            , A.LAST_EDIT_DATETIME				            /* 최종 갱신시 */
        FROM T_GUARANTEE A
        LEFT OUTER JOIN T_OBJECT B
            ON A.OBJECT_NO = B.OBJECT_NO
            AND B.DEL_FLG = 0
        LEFT OUTER JOIN W_WARRANTY C
            ON A.OBJECT_NO = C.OBJECT_NO
        <include refid="querySeachParameter" />
    </select>

    <sql id="querySeachParameter">
        <where>
            <trim prefixOverrides="AND">
                <if test="sch_dtType != null and sch_dtType != ''">
                    <if test="sch_startDt != null and sch_startDt != '' and sch_endDt != null and sch_endDt != ''">
                        <choose>
                            <when test='sch_dtType == "ISSUE" '> <!-- 보증서 발급일 -->
                                AND A.GUARANTEE_ISSUE_DATE IS NOT NULL
                                AND A.GUARANTEE_ISSUE_DATE <![CDATA[>=]]> #{sch_startDt} + ' 00:00:00'
                                AND A.GUARANTEE_ISSUE_DATE <![CDATA[<=]]> #{sch_endDt} + ' 23:59:59'
                            </when>
                            <when test='sch_dtType == "APPLY" '> <!-- 보증서 신청일 -->
                                AND A.GUARANTEE_APPLY_DATE IS NOT NULL
                                AND A.GUARANTEE_APPLY_DATE <![CDATA[>=]]> #{sch_startDt} + ' 00:00:00'
                                AND A.GUARANTEE_APPLY_DATE <![CDATA[<=]]> #{sch_endDt} + ' 23:59:59'
                            </when>
                        </choose>
                    </if>
                </if>
                <if test="sch_objectNo != null and sch_objectNo != ''"> <!--물건번호-->
                    AND A.OBJECT_NO LIKE '%' + #{sch_objectNo} + '%'
                </if>
                <if test="sch_objectNm != null and sch_objectNm != ''"> <!-- 물건명 -->
                    AND A.SETUP_PLACE_OBJECT_NAME LIKE '%' + #{sch_objectNm} + '%'
                </if>
                <if test="sch_saleStoreId != null and sch_saleStoreId != ''"> <!-- 판매대리점명/ID -->
                    AND B.SALE_STORE_ID = #{sch_saleStoreId}
                </if>
                <if test="sch_warrantyNo != null and sch_warrantyNo != ''"> <!-- 보증서 번호 -->
                    AND C.WARRANTY_NO1 LIKE '%' + #{sch_warrantyNo} + '%'
                </if>
            </trim>
        </where>
    </sql>

    <select id="selectWarrantyIssuedCmpFileData" parameterType="com.interplug.qcast.biz.excelDown.dto.WrntIsncCmplRequest" resultType="com.interplug.qcast.biz.excelDown.dto.WrntIsncCmplFileResponse">
    /* sqlid : com.interplug.qcast.api.excelDown.selectWarrantyIssuedCmpFileData (보증서발행완료 물건 파일 데이터 조회)*/
        SELECT
              A.OBJECT_NO   /* 부동산 관리 번호 */
            , A.PLAN_NO     /* 계획 */
            , F.NO
            , F.FAILE_NAME
        FROM T_GUARANTEE A
        LEFT OUTER JOIN T_OBJECT B
            ON A.OBJECT_NO = B.OBJECT_NO
            AND B.DEL_FLG = 0
        LEFT OUTER JOIN W_WARRANTY C
            ON A.OBJECT_NO = C.OBJECT_NO
        INNER JOIN T_UPLOAD F
            ON A.OBJECT_NO = F.OBJECT_NO
            AND F.DEL_FLG = '0'
            AND F.CATEGORY <![CDATA[<]]> 9
            AND F.CATEGORY != 7
        <include refid="querySeachParameter" />
    </select>

</mapper>