<?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.style24.scm.biz.dao.TssBusinessDao">

	<!-- 공급업체 목록 -->
	<select id="getSupplyCompanyList" parameterType="SupplyCompany" resultType="SupplyCompany">
		/* TssBusiness.getSupplyCompanyList */
		SELECT SC.SUPPLY_COMP_CD                                       /*공급업체코드*/
		     , SC.SUPPLY_COMP_NM                                       /*공급업체명*/
		     , SC.SUPPLY_VENDOR_CD                                     /*공급벤더코드*/
		     , SV.SUPPLY_VENDOR_NM                                     /*공급벤더명*/
		     , SV.BIZ_GB                                               /*사업자구분*/
		     , SV.BIZ_NO                                               /*사업자등록번호*/
		     , SV.BIZ_KIND                                             /*업종*/
		     , SV.BIZ_TYPE                                             /*업태*/
		     , SV.OWNER_NM                                             /*대표자명*/
		     , SV.BIZ_ZIPCODE                                          /*사업장우편번호*/
		     , SV.BIZ_BASE_ADDR                                        /*사업장기본주소*/
		     , SV.BIZ_DTL_ADDR                                         /*사업장상세주소*/
		     , SV.MAIN_TELNO                                           /*대표전화번호*/
		     , SV.MAIN_FAXNO                                           /*대표팩스번호*/
		     , SV.HOMEPAGE_URL                                         /*홈페이지URL*/
		     , SC.DISTRIBUTION_GB                                      /*유통구분*/
		     , SC.SHOT_DELV_YN                                         /*총알배송여부*/
		     , SC.SUPPLY_STAT                                          /*입점상태*/
		     , FN_GET_CODE_NM('G010',SC.SUPPLY_STAT) AS SUPPLY_STAT_NM /*입점상태명*/
		     , SC.SELL_FEE_RATE                                        /*판매수수료율*/
		     , SC.SETTLE_DAY                                           /*정산일*/
		     , SC.BANK_CD                                              /*은행코드*/
		     , SC.ACCOUNT_NO                                           /*계좌번호*/
		     , SC.DEPOSITOR_NM                                         /*예금주명*/
		     , SC.CS_CHARGE_NM                                         /*CS담당자명*/
		     , SC.CS_CHARGE_TELNO                                      /*CS담당자전화번호*/
		     , SC.SETTLE_CHARGE_NM                                     /*정산담당자명*/
		     , SC.SETTLE_CHARGE_TELNO                                  /*정산담당자전화번호*/
		     , SC.SETTLE_CHARGE_EMAIL                                  /*정산담당자이메일*/
		     , SC.BILL_EMAIL                                           /*계산서이메일*/
		     , SC.ECONTRACT_YN                                         /*전자계약여부*/
		     , SC.REMARKS                                              /*비고*/
		     , SC.NOTE                                                 /*배송안내*/
		     , SC.USE_YN                                               /*사용여부*/
		FROM   TB_SUPPLY_COMPANY SC
		     , TB_SUPPLY_VENDOR SV
		WHERE  SC.SUPPLY_VENDOR_CD = SV.SUPPLY_VENDOR_CD
		AND    SC.SUPPLY_COMP_CD = #{supplyCompCd}
		<if test="supplyStat != null and supplyStat != ''">
		AND    SC.SUPPLY_STAT = #{supplyStat}
		</if>
		<if test="ownerNm != null and ownerNm != ''">
		AND    UPPER(SV.OWNER_NM) LIKE CONCAT('%',UPPER(#{ownerNm}),'%')
		</if>
	</select>
	
	<!-- 공급업체 수정 -->
	<update id="updateSupplyCompany" parameterType="SupplyCompany">
		/* TssBusiness.updateSupplyCompany */
		UPDATE TB_SUPPLY_COMPANY
		SET    HOMEPAGE_URL = #{homepageUrl}
		     , CS_CHARGE_NM = #{csChargeNm}
		     , CS_CHARGE_TELNO = #{csChargeTelno}
		     , SETTLE_CHARGE_NM = #{settleChargeNm}
		     , SETTLE_CHARGE_TELNO = #{settleChargeTelno}
		     , SETTLE_CHARGE_EMAIL = #{settleChargeEmail}
		     , BILL_EMAIL = #{billEmail}
		     , NOTE = #{note}
		     , UPD_NO = #{updNo}
		     , UPD_DT = NOW()
		WHERE  SUPPLY_COMP_CD = #{supplyCompCd}
	</update>
	
	<!-- 배송비정책 목록 -->
	<select id="getDeliveryFeePolicyList" parameterType="DelvFeePolicy" resultType="DelvFeePolicy">
		/* TssBusiness.getDeliveryFeePolicyList */
		SELECT DELV_FEE_CD    /*배송비정책코드*/
		     , SUPPLY_COMP_CD /*공급업체코드*/
		     , DELV_FEE_CRITE /*배송비부과기준*/
		     , MIN_ORD_AMT    /*무료배송비최소주문금액*/
		     , DELV_FEE       /*배송비*/
		     , RTN_DELV_FEE   /*반품배송비*/
		     , USE_YN         /*사용여부*/
		FROM   TB_DELV_FEE_POLICY
		WHERE  SUPPLY_COMP_CD = #{supplyCompCd}
		<if test="delvFeeCd != null and delvFeeCd != ''">
		AND    DELV_FEE_CD = #{delvFeeCd}
		</if>
		<if test="useYn != null and useYn != ''">
		AND    USE_YN = #{useYn}
		</if>
		ORDER  BY DELV_FEE_CD
	</select>
	
	<!-- 배송비정책 생성 -->
	<insert id="createDeliveryFeePolicy" parameterType="DelvFeePolicy">
		/* TssBusiness.createDeliveryFeePolicy */
		INSERT INTO TB_DELV_FEE_POLICY (
		       DELV_FEE_CD
		     , SUPPLY_COMP_CD
		     , DELV_FEE_CRITE
		     , MIN_ORD_AMT
		     , DELV_FEE
		     , RTN_DELV_FEE
		     , USE_YN
		     , REG_NO
		     , REG_DT
		     , UPD_NO
		     , UPD_DT
		)
		VALUES (
		       (SELECT CONCAT(IFNULL(SUPPLY_COMP_CD,#{supplyCompCd}),'_',IFNULL(LPAD(SUBSTRING(MAX(DELV_FEE_CD),INSTR(MAX(DELV_FEE_CD),'_') + 1) + 1,2,'0'),'01'))
		        FROM   TB_DELV_FEE_POLICY Z
		        WHERE  SUPPLY_COMP_CD LIKE CONCAT(#{supplyCompCd},'%')
		        GROUP BY SUPPLY_COMP_CD)
		     , #{supplyCompCd}
		     , #{delvFeeCrite}
		     , IFNULL(#{minOrdAmt},0)
		     , IFNULL(#{delvFee},0)
		     , IFNULL(#{rtnDelvFee},0)
		     , #{useYn}
		     , #{regNo}
		     , NOW()
		     , #{updNo}
		     , NOW()
		)
	</insert>
	
	<!-- 배송비정책 수정 -->
	<update id="updateDeliveryFeePolicy" parameterType="DelvFeePolicy">
		/* TssBusiness.updateDeliveryFeePolicy */
		UPDATE TB_DELV_FEE_POLICY
		SET    DELV_FEE_CRITE = #{delvFeeCrite}
		     , MIN_ORD_AMT = IFNULL(#{minOrdAmt},0)
		     , DELV_FEE = IFNULL(#{delvFee},0)
		     , RTN_DELV_FEE = IFNULL(#{rtnDelvFee},0)
		     , USE_YN = #{useYn}
		     , UPD_NO = #{updNo}
		     , UPD_DT = NOW()
		WHERE  1 = 1
		<choose>
		    <when test="distributionGb == 'G065_20'"> <!-- 입점-위탁 -->
		AND    DELV_FEE_CD = #{delvFeeCd}
		AND    SUPPLY_COMP_CD = #{supplyCompCd}
		    </when>
		    <otherwise>
		AND    SUPPLY_COMP_CD IN (SELECT SUPPLY_COMP_CD
		                          FROM   TB_SUPPLY_COMPANY
		                          WHERE  DISTRIBUTION_GB != 'G065_20' /*자사*/
		                         )
		    </otherwise>
		</choose>
	</update>
	
	<!-- 출고처 목록 -->
	<select id="getDeliveryLocList" parameterType="DeliveryLoc" resultType="DeliveryLoc">
		/* TssBusiness.getDeliveryLocList */
		SELECT DL.DELV_LOC_CD        /*출고처코드*/
		     , DL.DELV_LOC_NM        /*출고처명*/
		     , SC.SUPPLY_COMP_NM     /*공급업체명*/
		     , DL.DELV_LOC_ZIPCODE   /*출고처우편번호*/
		     , DL.DELV_LOC_BASE_ADDR /*출고처기본주소*/
		     , DL.DELV_LOC_DTL_ADDR  /*출고처상세주소*/
		     , DL.RTN_LOC_ZIPCODE    /*반품처우편번호*/
		     , DL.RTN_LOC_BASE_ADDR  /*반품처기본주소*/
		     , DL.RTN_LOC_DTL_ADDR   /*반품처상세주소*/
		     , DL.RTN_LOC_NM         /*반품처명*/
		     , DL.USE_YN             /*사용여부*/
		FROM   TB_DELIVERY_LOC DL
		     , TB_SUPPLY_COMPANY SC
		WHERE  DL.SUPPLY_COMP_CD = SC.SUPPLY_COMP_CD
		AND    DL.SUPPLY_COMP_CD = #{supplyCompCd}
		<if test="delvLocNm != null and delvLocNm !=''">
		AND    LOWER(DL.DELV_LOC_NM) LIKE CONCAT('%',LOWER(#{delvLocNm}),'%')
		</if>
		<if test="useYn != null and useYn != ''">
		AND    DL.USE_YN = #{useYn}
		</if>
		ORDER  BY DL.DELV_LOC_CD
	</select>

	<!-- 출고처 저장 -->
	<insert id="saveDeliveryLoc" parameterType="DeliveryLoc">
		/*  TssBusiness.saveDeliveryInfo */
		INSERT INTO TB_DELIVERY_LOC (
		       DELV_LOC_CD
		     , DELV_LOC_NM
		     , SUPPLY_COMP_CD
		     , DELV_LOC_CLSF
		     , DELV_LOC_ZIPCODE
		     , DELV_LOC_BASE_ADDR
		     , DELV_LOC_DTL_ADDR
		     , RTN_LOC_ZIPCODE
		     , RTN_LOC_BASE_ADDR
		     , RTN_LOC_DTL_ADDR
		     , RTN_LOC_NM
		     , USE_YN
		     , REG_NO
		     , REG_DT
		     , UPD_NO
		     , UPD_DT
		)
		VALUES (
		       CASE WHEN #{delvLocCd} IS NULL THEN
		                (SELECT CONCAT('DL',LPAD(SUBSTRING(IFNULL(MAX(DELV_LOC_CD),'DL000'),3) + 1,3,'0'))
		                 FROM   TB_DELIVERY_LOC Z
		                 WHERE  DELV_LOC_CD LIKE 'DL%'
		                )
		            ELSE
		                #{delvLocCd}
		       END
		     , #{delvLocNm}
		     , #{supplyCompCd}
		     , 'G024_30'
		     , #{delvLocZipcode}
		     , #{delvLocBaseAddr}
		     , #{delvLocDtlAddr}
		     , #{rtnLocZipcode}
		     , #{rtnLocBaseAddr}
		     , #{rtnLocDtlAddr}
		     , #{rtnLocNm}
		     , 'Y'
		     , #{regNo}
		     , NOW()
		     , #{updNo}
		     , NOW()
		)
		ON DUPLICATE KEY UPDATE
		       DELV_LOC_NM = #{delvLocNm}
		     , DELV_LOC_ZIPCODE = #{delvLocZipcode}
		     , DELV_LOC_BASE_ADDR = #{delvLocBaseAddr}
		     , DELV_LOC_DTL_ADDR = #{delvLocDtlAddr}
		     , RTN_LOC_ZIPCODE = #{rtnLocZipcode}
		     , RTN_LOC_BASE_ADDR = #{rtnLocBaseAddr}
		     , RTN_LOC_DTL_ADDR = #{rtnLocDtlAddr}
		     , RTN_LOC_NM = #{rtnLocNm}
		     , UPD_NO = #{updNo}
		     , UPD_DT = NOW()
	</insert>

	<!-- 브랜드 목록 -->
	<select id="getBrandList" parameterType="Brand" resultType="Brand">
		/* TssBusiness.getBrandList */
		SELECT CASE WHEN A.SELF_YN = 'Y' THEN 'S'
		            ELSE 'E'
		       END               AS BRAND_GB /*브랜드구분*/
		     , A.BRAND_CD                    /*브랜드코드*/
		     , A.BRAND_ENM                   /*브랜드영문명*/
		     , A.BRAND_KNM                   /*브랜드한글명*/
		     , A.BRAND_GRP_NM                /*브랜드그룹명*/
		     , A.DISP_NM_LANG                /*노출명언어*/
		     , A.RGB_CD                      /*RGB코드*/
		     , A.DISTRIBUTION_GB             /*유통구분*/
		     -- , B.SUPPLY_COMP_NM              /*업체명*/
		     , A.ERP_BRAND_CD                /*ERP브랜드코드*/
		     , A.SELF_YN                     /*자사여부*/
		     , A.USE_YN                      /*사용여부*/
		     , A.PNT_PRATE10                 /*포인트적립율(PC)*/
		     , A.PNT_MRATE10                 /*포인트적립율(모바일)*/
		     , A.PNT_PRATE20                 /*포인트적립율(PC)*/
		     , A.PNT_MRATE20                 /*포인트적립율(모바일)*/
		     , A.LOGO_FILE_NM                 /*로고파일명*/
		     , A.DISP_ORD                    /*표시순서*/
		     , (CASE WHEN D.DISP_NM_LANG = 'EN' THEN D.BRAND_GROUP_ENM ELSE D.BRAND_GROUP_KNM END) AS BRAND_GROUP_NM
		     , A.BRAND_GROUP_NO
		FROM   TB_BRAND A
		INNER JOIN TB_BRAND_GROUP D ON A.BRAND_GROUP_NO = D.BRAND_GROUP_NO
		                            AND D.USE_YN = 'Y'
		<if test='supplyCompCd != null and supplyCompCd != ""'>
		INNER JOIN TB_BRAND_SUPPLY C ON A.BRAND_CD = C.BRAND_CD
		INNER JOIN TB_SUPPLY_COMPANY B ON C.SUPPLY_COMP_CD = B.SUPPLY_COMP_CD
		</if>
		WHERE  1 = 1
		<if test='supplyCompCd != null and supplyCompCd != ""'>
		AND    C.SUPPLY_COMP_CD = #{supplyCompCd}
		</if>
		<if test='distributionGb != null and distributionGb != ""'>
		AND    A.DISTRIBUTION_GB = #{distributionGb}
		</if>
		<if test='erpBrandCd != null and erpBrandCd != ""'>
		AND    A.ERP_BRAND_CD =  #{erpBrandCd}
		</if>
		<if test='brandCd != null and brandCd != ""'>
		AND    A.BRAND_CD = #{brandCd}
		</if>
		<if test="searchTxt != null and searchTxt != ''">
		AND    (
		        UPPER(A.BRAND_ENM) LIKE CONCAT('%',UPPER(#{searchTxt}),'%')
		        OR
		        UPPER(A.BRAND_KNM) LIKE CONCAT('%',UPPER(#{searchTxt}),'%')
		        OR
		        UPPER(A.BRAND_CD) LIKE CONCAT('%',UPPER(#{searchTxt}),'%')
		        OR
		        UPPER(D.BRAND_GROUP_KNM) LIKE CONCAT('%',UPPER(#{searchTxt}),'%')
		        OR
		        UPPER(D.BRAND_GROUP_ENM) LIKE CONCAT('%',UPPER(#{searchTxt}),'%')
		)
		</if>
		<if test="multiBrandCd != null and multiBrandCd.length > 0">
		    AND    A.BRAND_CD IN
		    <foreach collection="multiBrandCd" item="item" index="index"  open="(" close=")" separator=",">
		        UPPER(#{item})
		    </foreach>
		</if>
		<if test="multiBrandNm != null and multiBrandNm.length > 0">
		    AND (
		    <foreach collection="multiBrandNm" item="item" index="index" separator="or">
		        (
		        UPPER(A.BRAND_ENM) LIKE CONCAT('%',UPPER(#{item}),'%')
		        OR
		        UPPER(A.BRAND_KNM) LIKE CONCAT('%',UPPER(#{item}),'%')
		        )
		    </foreach>
		    )
		</if>
		<if test='useYn != null and useYn != ""'>
		AND    A.USE_YN = #{useYn}
		</if>
		<if test='selfYn != null and selfYn != ""'>
		AND    A.SELF_YN = #{selfYn}
		</if>
		ORDER  BY A.SELF_YN DESC,  A.DISP_ORD
	</select>
	
	<!-- 담당MD 목록 -->
	<select id="getMdList" parameterType="String" resultType="BrandMd">
		/* TssBusiness.getMdList */
		SELECT USER_NO AS MD_NO
		     , USER_NM AS MD_NM
		FROM   TB_USER
		WHERE  1 = 1
		/* AND ROLE_CD = 'G001_A101'  */  /*MD권한*/
		<if test="mdNm != null and mdNm != ''">
		AND    LOWER(USER_NM) LIKE CONCAT('%',LOWER(#{mdNm}),'%')
		</if>
		AND    USE_YN  = 'Y'
		ORDER  BY USER_NM
	</select>
	
	<!-- 브랜드담당MD 목록 -->
	<select id="getBrandMdList" parameterType="String" resultType="BrandMd">
		/* TssBusiness.getBrandMdList */
		SELECT A.FORMAL_GB           /*정상이월구분*/
		     , A.MD_NO               /*담당MD번호*/
		     , B.USER_NM    AS MD_NM /*담당MD명*/
		FROM   TB_BRAND_MD A
		     , TB_USER B
		WHERE  A.MD_NO = B.USER_NO
		AND    A.BRAND_CD = #{brandCd}
	</select>
	
	<!-- 브랜드담당MD 삭제 -->
	<delete id="deleteBrandMd" parameterType="String">
		/* TssBusiness.deleteBrandMd */
		DELETE
		FROM   TB_BRAND_MD
		WHERE  BRAND_CD = #{brandCd}
	</delete>

	<!-- 브랜드담당MD 등록 -->
	<insert id="createBrandMd" parameterType="BrandMd">
		/* TssBusiness.createBrandMd */
		INSERT INTO TB_BRAND_MD (
		       BRAND_CD
		     , FORMAL_GB
		     , MD_NO
		     , REG_NO
		     , REG_DT
		     , UPD_NO
		     , UPD_DT
		)
		SELECT #{brandCd}  AS BRAND_CD
		     , CD          AS FORMAL_GB
		     , #{mdNo}     AS MD_NO
		     , #{regNo}    AS REG_NO
		     , NOW()       AS REG_DT
		     , #{updNo}    AS UPD_NO
		     , NOW()       AS UPD_DT
		FROM   TB_COMMON_CODE
		WHERE  CD_GB = 'G009' /*정상이월구분*/
		AND    CD IN ('G009_10','G009_20') /*정상,이월*/
		AND    USE_YN = 'Y'
	</insert>
	
	<!-- 브랜드 공급업체 목록 -->
	<select id="getBrandSupplyList" parameterType="String" resultType="Brand">
		/* TsaBusiness.getBrandSupplyList */
		SELECT A.BRAND_CD
		     , A.SUPPLY_COMP_CD
		     , B.SUPPLY_COMP_NM
		     , A.REG_NO
		     , FN_GET_USER_NM(A.REG_NO) AS REG_NM
		     , DATE_FORMAT(A.REG_DT, '%Y%m%d%H%i%S') AS REG_DT
		FROM   TB_BRAND_SUPPLY A
		INNER JOIN TB_SUPPLY_COMPANY B ON A.SUPPLY_COMP_CD = B.SUPPLY_COMP_CD
		WHERE  BRAND_CD = #{brandCd}
	</select>
</mapper>