<?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.user.UserMapper">
    <insert id="setStoreSapCdSave" parameterType="com.interplug.qcast.biz.user.dto.StoreRequest" >
        /* sqlid : com.interplug.qcast.user.setStoreSapCdSave */
        WITH SALES_STORE_CTE AS (
            SELECT
                SALE_STORE_ID
                 , SALE_STORE_NAME
                 , SALE_STORE_LEVEL
                 , FIRST_AGENT_ID
                 , PARENT_SALE_AGENT_ID
                 , DEL_FLG
            FROM M_SALES_STORE WITH(NOLOCK)
            WHERE SALE_STORE_ID = #{saleStoreId}
              AND APPROVE_FLG = '2'
            UNION ALL
            SELECT
                A.SALE_STORE_ID
                 , A.SALE_STORE_NAME
                 , A.SALE_STORE_LEVEL
                 , A.FIRST_AGENT_ID
                 , A.PARENT_SALE_AGENT_ID
                 , A.DEL_FLG
            FROM M_SALES_STORE A WITH(NOLOCK)
                        INNER JOIN SALES_STORE_CTE B
            ON A.PARENT_SALE_AGENT_ID = B.SALE_STORE_ID
            WHERE A.APPROVE_FLG = '2'
        )
        MERGE INTO M_SALES_STORE_ID_FOR_SAP AS A
            USING (
                SELECT
                      CASE WHEN SALE_STORE_LEVEL = '1' THEN 'Y' ELSE 'N' END AS FIRST_AGENT_YN
                    , SALE_STORE_ID
                    , SALE_STORE_NAME
                    , SALE_STORE_LEVEL
                    , FIRST_AGENT_ID
                    , PARENT_SALE_AGENT_ID
                    , DEL_FLG
                FROM SALES_STORE_CTE
            ) AS D
            ON A.SALE_STORE_ID = D.SALE_STORE_ID
        WHEN MATCHED AND D.DEL_FLG = '1' THEN
            DELETE
        WHEN MATCHED THEN
            UPDATE SET
                SAP_SALES_STORE_CD = #{sapSalesStoreCd}
        WHEN NOT MATCHED THEN
            INSERT (
                SALE_STORE_ID
              , SAP_SALES_STORE_CD
            ) VALUES (
                D.SALE_STORE_ID
              , #{sapSalesStoreCd}
            );
    </insert>

    <insert id="setStoreSave" parameterType="com.interplug.qcast.biz.user.dto.StoreRequest" >
        /* sqlid : com.interplug.qcast.user.setStoreSave */
        MERGE M_SALES_STORE AS A
            USING
                ( SELECT #{saleStoreId} AS SALE_STORE_ID ) AS D
                ON (A.SALE_STORE_ID = D.SALE_STORE_ID)
        WHEN MATCHED THEN
            UPDATE SET
                  SALE_STORE_NAME = #{saleStoreName}
                , SALE_STORE_NAME_KANA = #{saleStoreNameKana}
                , ZIP_NO = #{zipNo}
                , ADDRESS = #{address}
                , TEL = #{tel}
                , FAX = #{fax}
                , BIZ_NO = #{bizNo}
                , APPLICATION_DATE = #{applicationDate}
                , APPROVAL_DATE = #{approvalDate}
                , APPROVE_FLG = #{approveFlg}
                , PAYMENT_TERMS = #{paymentTerms}
                , FIRST_AGENT_FLG = #{firstAgentFlg}
                , SECOND_AGENT_FLG = #{secondAgentFlg}
                , FIRST_AGENT_ID = #{firstAgentId}
                , PARENT_SALE_AGENT_ID = #{parentSaleAgentId}
                , BUSINESS_CHARGER = #{businessCharger}
                , BUSINESS_CHARGER_CD = #{businessChargerCd}
                , DISP_COMPANY_NAME = #{dispCompanyName}
                , DISP_ZIP_NO = #{dispZipNo}
                , DISP_ADDRESS = #{dispAddress}
                , DISP_TEL = #{dispTel}
                , DISP_FAX = #{dispFax}
                , DISP_MAIL = #{dispMail}
                , SALE_STORE_LEVEL = #{saleStoreLevel}
                , DEL_FLG = #{delFlg}
                , LAST_EDIT_DATETIME = #{lastEditDatetime}
                , LAST_EDIT_USER = #{lastEditUser}
        WHEN NOT MATCHED THEN
            INSERT (
                  SALE_STORE_ID
                , SALE_STORE_NAME
                , SALE_STORE_NAME_KANA
                , ZIP_NO
                , ADDRESS
                , TEL
                , FAX
                , BIZ_NO
                , APPLICATION_DATE
                , APPROVAL_DATE
                , APPROVE_FLG
                , PAYMENT_TERMS
                , FIRST_AGENT_FLG
                , SECOND_AGENT_FLG
                , FIRST_AGENT_ID
                , PARENT_SALE_AGENT_ID
                , OUTPUT_COMP_CLASS -- 출력 대비표 분류
                , INDUSTRIAL_OUTPUT_COMP_CLASS -- 산업출력 대비표 출력
                , FIXED_PRICE_FLG -- 정가 표시
                , QCELLS_FLG -- 패널선택
                , HANWFA_FLG -- 패널선택
                , OUT_STANDARD_FLG -- 비표준대응
                , BUSINESS_CHARGER
                , BUSINESS_CHARGER_CD
                , DISP_COMPANY_NAME
                , DISP_ZIP_NO
                , DISP_ADDRESS
                , DISP_TEL
                , DISP_FAX
                , DISP_MAIL
                , SALE_STORE_LEVEL
                , REGIST_DATETIME
                , DEL_FLG
                , SNOWFALL_FLG -- 적설
                , LAST_EDIT_DATETIME
                , LAST_EDIT_USER
            ) VALUES (
                  #{saleStoreId}
                , #{saleStoreName}
                , #{saleStoreNameKana}
                , #{zipNo}
                , #{address}
                , #{tel}
                , #{fax}
                , #{bizNo}
                , #{applicationDate}
                , #{approvalDate}
                , #{approveFlg}
                , #{paymentTerms}
                , #{firstAgentFlg}
                , #{secondAgentFlg}
                , #{firstAgentId}
                , #{parentSaleAgentId}
                , 0 -- 출력 대비표 분류
                , 0 -- 산업출력 대비표 출력
                , 0 -- 정가 표시
                , 0 -- 패널선택
                , 0 -- 패널선택
                , 0 -- 비표준대응
                , #{businessCharger}
                , #{businessChargerCd}
                , #{dispCompanyName}
                , #{dispZipNo}
                , #{dispAddress}
                , #{dispTel}
                , #{dispFax}
                , #{dispMail}
                , #{saleStoreLevel}
                , #{registDatetime}
                , 0
                , 0 -- 적설
                , #{lastEditDatetime}
                , #{lastEditUser}
            );
    </insert>


    <insert id="setUserSave" parameterType="com.interplug.qcast.biz.user.dto.UserRequest" >
    /* sqlid : com.interplug.qcast.user.setUserSave */
        MERGE M_USER AS A
        USING
            ( SELECT #{userId} AS USER_ID
                , #{delFlg} AS DEL_FLG ) AS D
            ON (A.USER_ID = D.USER_ID)
        WHEN MATCHED AND D.DEL_FLG = '1' THEN
            DELETE
        WHEN MATCHED THEN
            UPDATE SET
                  SALE_STORE_ID = #{saleStoreId}
                , CATEGORY = #{category}
                , NAME = #{name}
                , NAME_KANA = #{nameKana}
                , TEL = #{tel}
                , FAX = #{fax}
                , MAIL = #{mail}
                , LAST_EDIT_DATETIME = #{lastEditDatetime}
                , LAST_EDIT_USER = #{lastEditUser}
         -- , GROUP_ID  -- '70000' , '60000'
         -- , MODULE_SELECT_GROUP_ID -- 모듈 선택 그룹 ('G1','GA' ,'G2')
         -- , VERSION_MANAGEMENT_ID -- 관리ID ('hanbaiten', 'hanwha_eigyo')
         -- , DISP_COST_PRICE -- 구매 값 표시
         -- , DISP_SELLING_PRICE -- 판매 가격 표시
        WHEN NOT MATCHED THEN
            INSERT (
                  USER_ID
                , SALE_STORE_ID
                , PASSWORD
                , CATEGORY
                , NAME
                , NAME_KANA
                , TEL
                , FAX
                , MAIL
                , GROUP_ID
                , DISP_COST_PRICE
                , DISP_SELLING_PRICE
                , REGIST_DATETIME
                , LAST_EDIT_DATETIME
                , LAST_EDIT_USER
            ) VALUES (
                  #{userId}
                , #{saleStoreId}
                , #{userId}
                , #{category}
                , #{name}
                , #{nameKana}
                , #{tel}
                , #{fax}
                , #{mail}
                , #{groupId}
                , 0
                , 0
                , #{registDatetime}
                , #{lastEditDatetime}
                , #{lastEditUser}
            );
    </insert>

    <insert id="setStoreNorthModuleSave" parameterType="com.interplug.qcast.biz.user.dto.StoreRequest" >
        /* sqlid : com.interplug.qcast.user.setStoreNorthModuleSave */
        MERGE INTO M_SALES_STORE_NORTH_MODULE AS A
        USING ( SELECT #{saleStoreId} AS SALE_STORE_ID ) AS D
            ON A.SALE_STORE_ID = D.SALE_STORE_ID
        WHEN MATCHED THEN
            UPDATE SET
            DEL_FLG = #{northModuleFlg}
        WHEN NOT MATCHED THEN
            INSERT (
                  SALE_STORE_ID
                , DEL_FLG
            ) VALUES (
                  #{saleStoreId}
                , #{northModuleFlg}
            );
    </insert>

    <delete id="deleteBusinessChargerSync" parameterType="com.interplug.qcast.biz.user.dto.BusinessChargerSyncResponse" >
        UPDATE M_BUSINESS_CHARGER SET DEL_FLG = '1'
    </delete>

    <insert id="setBusinessChargerSyncSave" parameterType="com.interplug.qcast.biz.user.dto.BusinessChargerSyncResponse" >
        /* sqlid : com.interplug.qcast.user.setBusinessChargerSyncSave */
        MERGE INTO M_BUSINESS_CHARGER AS A
        USING ( SELECT #{businessChargerCd} AS BUSINESS_CHARGER_CD ) AS D
            ON A.BUSINESS_CHARGER_CD = D.BUSINESS_CHARGER_CD
        WHEN MATCHED THEN
            UPDATE SET
                BUSINESS_CHARGER = #{businessCharger}
                , BUSINESS_TEAM_CD = #{businessTeamCd}
                , BUSINESS_CHARGER_TEL = #{businessChargerTel}
                , BUSINESS_CHARGER_MAIL = #{businessChargerMail}
                , DEL_FLG = #{delFlg}
                , LAST_EDIT_DATETIME = GETDATE()
        WHEN NOT MATCHED THEN
            INSERT (
                    BUSINESS_CHARGER_CD
                , BUSINESS_CHARGER
                , BUSINESS_TEAM_CD
                , BUSINESS_CHARGER_TEL
                , BUSINESS_CHARGER_MAIL
                , DEL_FLG
                , LAST_EDIT_DATETIME
            ) VALUES (
                  #{businessChargerCd}
                , #{businessCharger}
                , #{businessTeamCd}
                , #{businessChargerTel}
                , #{businessChargerMail}
                , #{delFlg}
                , GETDATE()
            );
    </insert>

    <delete id="deleteAdminUserSync" parameterType="com.interplug.qcast.biz.user.dto.AdminUserSyncResponse" >
        DELETE FROM M_MANAGER_SITE_ACCESS_USER
        WHERE USER_ID = #{userId}
    </delete>

    <insert id="setAdminUserSyncSave" parameterType="com.interplug.qcast.biz.user.dto.AdminUserSyncResponse" >
        /* sqlid : com.interplug.qcast.user.setAdminUserSyncSave */
        MERGE INTO M_MANAGER_SITE_ACCESS_USER AS A
        USING ( SELECT #{userId} AS USER_ID ) AS D
            ON A.USER_ID = D.USER_ID
        WHEN MATCHED THEN
            UPDATE SET
                COMPANY_NAME = #{companyName}
                , CATEGORY = #{category}
                , NAME = #{name}
                , TEL = #{tel}
                , FAX = #{fax}
                , MAIL = #{mail}
                , GROUP_ID = #{groupId}
        WHEN NOT MATCHED THEN
            INSERT (
                USER_ID
                , PASSWORD
                , COMPANY_NAME
                , CATEGORY
                , NAME
                , TEL
                , FAX
                , MAIL
                , GROUP_ID
            ) VALUES (
                  #{userId}
                , #{userId}
                , #{companyName}
                , #{category}
                , #{name}
                , #{tel}
                , #{fax}
                , #{mail}
                , #{groupId}
            );
    </insert>

</mapper>