<?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 SUPPLY_COMP_CD       /*공급업체코드*/
		     , SUPPLY_COMP_NM       /*공급업체명*/
		     , BIZ_GB               /*사업자구분*/
		     , BIZ_NO               /*사업자등록번호*/
		     , BIZ_KIND             /*업종*/
		     , BIZ_TYPE             /*업태*/
		     , OWNER_NM             /*대표자명*/
		     , BIZ_ZIPCODE          /*사업장우편번호*/
		     , BIZ_BASE_ADDR        /*사업장기본주소*/
		     , BIZ_DTL_ADDR         /*사업장상세주소*/
		     , MAIN_TELNO           /*대표전화번호*/
		     , MAIN_FAXNO           /*대표팩스번호*/
		     , HOMEPAGE_URL         /*홈페이지URL*/
		     , DISTRIBUTION_GB      /*유통구분*/
		     , SHOT_DELV_YN         /*총알배송여부*/
		     , SUPPLY_STAT          /*입점상태*/
		     , MIN_ORD_AMT          /*무료배송비최소주문금액*/
		     , DELV_FEE             /*배송비*/
		     , SELL_FEE_RATE        /*판매수수료율*/
		     , SETTLE_DAY           /*정산일*/
		     , BANK_CD              /*은행코드*/
		     , ACCOUNT_NO           /*계좌번호*/
		     , DEPOSITOR_NM         /*예금주명*/
		     , CS_CHARGE_NM         /*CS담당자명*/
		     , CS_CHARGE_TELNO      /*CS담당자전화번호*/
		     , SETTLE_CHARGE_NM     /*정산담당자명*/
		     , SETTLE_CHARGE_TELNO  /*정산담당자전화번호*/
		     , SETTLE_CHARGE_EMAIL  /*정산담당자이메일*/
		     , BILL_EMAIL           /*계산서이메일*/
		     , ECONTRACT_YN         /*전자계약여부*/
		     , REMARKS              /*비고*/
		     , USE_YN               /*사용여부*/
		FROM   TB_SUPPLY_COMPANY
		WHERE  1 = 1
		<if test="supplyStat != null and supplyStat != ''">
		AND    SUPPLY_STAT = #{supplyStat}
		</if>
		<if test="searchTxt != null and searchTxt != ''">
		    <if test='searchGb == "NAME"'>
		AND    UPPER(SUPPLY_COMP_NM) LIKE CONCAT('%',UPPER(#{searchTxt}),'%')
		    </if>
		    <if test='searchGb == "OWNER"'>
		AND    UPPER(OWNER_NM) LIKE CONCAT('%',UPPER(#{searchTxt}),'%')
		    </if>
		</if>
		<if test="econtractYn != null and econtractYn != ''">
		AND    ECONTRACT_YN = #{econtractYn}
		</if>
		<if test="supplyCompCd != null and supplyCompCd != ''"> <!-- 브랜드관리 화면에서 사용 -->
		AND    SUPPLY_COMP_CD = #{supplyCompCd}
		</if>
	</select>
	
	<!-- 공급업체 생성 -->
	<insert id="createSupplyCompany" parameterType="SupplyCompany">
		/* TssBusiness.createSupplyCompany */
		INSERT INTO TB_SUPPLY_COMPANY (
		       SUPPLY_COMP_CD
		     , SUPPLY_COMP_NM
		     , PROVIER_NO
		     , BIZ_GB
		     , BIZ_NO
		     , BIZ_KIND
		     , BIZ_TYPE
		     , OWNER_NM
		     , BIZ_ZIPCODE
		     , BIZ_BASE_ADDR
		     , BIZ_DTL_ADDR
		     , MAIN_TELNO
		     , MAIN_FAXNO
		     , HOMEPAGE_URL
		     , DISTRIBUTION_GB
		     , SHOT_DELV_YN
		     , SUPPLY_STAT
		     , MIN_ORD_AMT
		     , DELV_FEE
		     , SELL_FEE_RATE
		     , SETTLE_DAY
		     , BANK_CD
		     , ACCOUNT_NO
		     , DEPOSITOR_NM
		     , CS_CHARGE_NM
		     , CS_CHARGE_TELNO
		     , SETTLE_CHARGE_NM
		     , SETTLE_CHARGE_TELNO
		     , SETTLE_CHARGE_EMAIL
		     , BILL_EMAIL
		     , ECONTRACT_YN
		     , REMARKS
		     , USE_YN
		     , REG_NO
		     , REG_DT
		     , UPD_NO
		     , UPD_DT
		)
		VALUES (
		       (SELECT CONCAT('S',IFNULL(LPAD(SUBSTRING(MAX(SUPPLY_COMP_CD),2) + 1,4,'0'),'0000'))
		        FROM   TB_SUPPLY_COMPANY Z
		        WHERE  SUPPLY_COMP_CD LIKE 'S%'
		       )
		     , #{supplyCompNm}
		     , (SELECT CASE WHEN MAX(PROVIER_NO) IS NULL THEN 40000   /* AS-IS 최대 수보다 크게 수정해야함*/
		               ELSE (MAX(PROVIER_NO) + 1) END
		        FROM   TB_SUPPLY_COMPANY Z
		        WHERE  1 = 1
		        AND PROVIER_NO > 40000                  /* AS-IS 최대 수보다 크게 크게 수정해야함*/
		       )
		     , #{bizGb}
		     , #{bizNo}
		     , #{bizKind}
		     , #{bizType}
		     , #{ownerNm}
		     , #{bizZipcode}
		     , #{bizBaseAddr}
		     , #{bizDtlAddr}
		     , #{mainTelno}
		     , #{mainFaxno}
		     , #{homepageUrl}
		     , #{distributionGb}
		     , #{shotDelvYn}
		     , #{supplyStat}
		     , IFNULL(#{minOrdAmt},0)
		     , IFNULL(#{delvFee},0)
		     , IFNULL(#{sellFeeRate},0.0)
		     , #{settleDay}
		     , #{bankCd}
		     , #{accountNo}
		     , #{depositorNm}
		     , #{csChargeNm}
		     , #{csChargeTelno}
		     , #{settleChargeNm}
		     , #{settleChargeTelno}
		     , #{settleChargeEmail}
		     , #{billEmail}
		     , #{econtractYn}
		     , #{remarks}
		     , #{useYn}
		     , #{regNo}
		     , NOW()
		     , #{updNo}
		     , NOW()
		)
	</insert>
	
	<!-- 공급업체 수정 -->
	<update id="updateSupplyCompany" parameterType="SupplyCompany">
		/* TssBusiness.updateSupplyCompany */
		UPDATE TB_SUPPLY_COMPANY
		SET    SUPPLY_COMP_NM = #{supplyCompNm}
		     , BIZ_GB = #{bizGb}
		     , BIZ_NO = #{bizNo}
		     , BIZ_KIND = #{bizKind}
		     , BIZ_TYPE = #{bizType}
		     , OWNER_NM = #{ownerNm}
		     , BIZ_ZIPCODE = #{bizZipcode}
		     , BIZ_BASE_ADDR = #{bizBaseAddr}
		     , BIZ_DTL_ADDR = #{bizDtlAddr}
		     , MAIN_TELNO = #{mainTelno}
		     , MAIN_FAXNO = #{mainFaxno}
		     , HOMEPAGE_URL = #{homepageUrl}
		     , DISTRIBUTION_GB = #{distributionGb}
		     , SHOT_DELV_YN = #{shotDelvYn}
		     , SUPPLY_STAT = #{supplyStat}
		     , MIN_ORD_AMT = IFNULL(#{minOrdAmt},0)
		     , DELV_FEE = IFNULL(#{delvFee},0)
		     , SELL_FEE_RATE = IFNULL(#{sellFeeRate},0.0)
		     , SETTLE_DAY = #{settleDay}
		     , BANK_CD = #{bankCd}
		     , ACCOUNT_NO = #{accountNo}
		     , DEPOSITOR_NM = #{depositorNm}
		     , CS_CHARGE_NM = #{csChargeNm}
		     , CS_CHARGE_TELNO = #{csChargeTelno}
		     , SETTLE_CHARGE_NM = #{settleChargeNm}
		     , SETTLE_CHARGE_TELNO = #{settleChargeTelno}
		     , SETTLE_CHARGE_EMAIL = #{settleChargeEmail}
		     , BILL_EMAIL = #{billEmail}
		     , ECONTRACT_YN = #{econtractYn}
		     , REMARKS = #{remarks}
		     , USE_YN = #{useYn}
		     , UPD_NO = #{updNo}
		     , UPD_DT = NOW()
		WHERE  SUPPLY_COMP_CD = #{supplyCompCd}
	</update>

	<!-- 브랜드 목록 -->
	<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.DISTRIBUTION_GB             /*유통구분*/
		     , A.SUPPLY_COMP_CD              /*업체코드*/
		     , B.SUPPLY_COMP_NM              /*업체명*/
		     , A.DELV_LOC_CD                 /*출고처코드*/
		     , C.DELV_LOC_NM                 /*출고처명*/
		     , A.ERP_BRAND_CD                /*ERP브랜드코드*/
		     , A.SELF_YN                     /*자사여부*/
		     , A.DELV_FEE                    /*기본배송비*/
		     , A.MIN_ORD_AMT                 /*무료배송비기준*/
		     , A.SELL_FEE_RATE               /*판매수수료율*/
		     , A.USE_YN                      /*사용여부*/
		     , A.PNT_PRATE10                 /*포인트적립율(PC)*/
		     , A.PNT_MRATE10                 /*포인트적립율(모바일)*/
		     , A.PNT_PRATE20                 /*포인트적립율(PC)*/
		     , A.PNT_MRATE20                 /*포인트적립율(모바일)*/
		     , A.DISP_ORD                    /*표시순서*/
		FROM   TB_BRAND A
		INNER JOIN TB_SUPPLY_COMPANY B ON A.SUPPLY_COMP_CD = B.SUPPLY_COMP_CD
		LEFT OUTER JOIN TB_DELIVERY_LOC C ON A.DELV_LOC_CD = C.DELV_LOC_CD
		WHERE  1 = 1
		<if test='supplyCompCd != null and supplyCompCd != ""'>
		AND    A.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    (
		        A.BRAND_CD LIKE CONCAT('%',#{searchTxt},'%')
		        OR
		        A.BRAND_ENM LIKE CONCAT('%',#{searchTxt},'%')
		        OR
		        A.BRAND_KNM LIKE CONCAT('%',#{searchTxt},'%')
		        OR
		        A.BRAND_GRP_NM LIKE CONCAT('%',#{searchTxt},'%')
		       )
		</if>
		<if test='useYn != null and useYn != ""'>
		AND    A.USE_YN = #{useYn}
		</if>
		ORDER  BY A.SELF_YN DESC, A.SUPPLY_COMP_CD, 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>
	
	
	
</mapper>