<?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.TssCounselDao">

	<!-- 상품문의 목록 -->
	<select id="getGoodsQnaList" parameterType="Counsel" resultType="Counsel">
		/* TssCounsel.getGoodsQnaList */
		SELECT A.COUNSEL_SQ                                                        /*상담일련번호*/
		     , A.SITE_CD                                                           /*사이트코드*/
		     , DATE_FORMAT(A.QUEST_DT,'%Y-%m-%d %H:%i:%S')     AS QUEST_DT         /*문의일시*/
		     , A.SECRET_YN                                                         /*비밀글여부*/
		     , A.CUST_NO                                                           /*고객번호*/
		     , B.CUST_ID                                                           /*고객ID*/
		     , B.CUST_NM                                                           /*고객명*/
		     , A.CELL_PHNNO                                                        /*휴대전화번호*/
		     , A.SMS_REQ_YN                                                        /*SMS요청여부*/
		     , A.SMS_SEND_YN                                                       /*SMS발송여부*/
		     , A.EMAIL                                                             /*이메일*/
		     , A.REL_GOODS_CD                                                      /*관련상품코드*/
		     , G.GOODS_NM                                                          /*상품명*/
		     , (SELECT SYS_IMG_NM
		        FROM   TB_GOODS_IMG
		        WHERE  GOODS_CD = G.GOODS_CD
		        AND    COLOR_CD = G.MAIN_COLOR_CD
		        AND    DEFAULT_IMG_YN = 'Y' /*디폴트이미지*/
		        LIMIT 1
		       )                                               AS GOODS_IMG        /*상품이미지*/
		     , A.ANS_STAT                                                          /*답변상태*/
		     , A.ANS_TRANS_YN                                                      /*답변의뢰여부*/
		     , DATE_FORMAT(A.ANS_TRANS_DT,'%Y-%m-%d %H:%i:%S') AS ANS_TRANS_DT     /*답변의뢰일시*/
		     , A.ANS_TRANS_NO                                                      /*답변의뢰자번호*/
		     , C.USER_NM                                       AS ANS_TRANS_NM     /*답변의뢰자명*/
		     , DATE_FORMAT(A.ANS_DT, '%Y-%m-%d %H:%i:%S')      AS ANS_DT           /*답변일시*/
		     , A.ANS_NO                                                            /*답변자번호*/
		     , D.USER_NM                                       AS ANS_NM           /*답변자명*/
		FROM   TB_COUNSEL A
		INNER JOIN TB_CUSTOMER B ON A.CUST_NO = B.CUST_NO
		INNER JOIN TB_GOODS G ON A.REL_GOODS_CD = G.GOODS_CD
		LEFT OUTER JOIN TB_USER C ON A.ANS_TRANS_NO = C.USER_NO
		LEFT OUTER JOIN TB_USER D ON A.ANS_NO = D.USER_NO
		WHERE  COUNSEL_TYPE = 'G' /*상담유형(상품문의)*/
		<if test="siteCd != null and siteCd != ''">
		AND    A.SITE_CD = #{siteCd}
		</if>
		<if test="counselClsf != null and counselClsf != ''">
		AND    A.COUNSEL_CLSF = #{counselClsf}
		</if>
		<if test="counselDclsf != null and counselDclsf != ''">
		AND    A.COUNSEL_DCLSF = #{counselDclsf}
		</if>
		<if test="delYn != null and delYn != ''">
		AND    A.DEL_YN = #{delYn}
		</if>
		<if test="condition != null and condition != ''">
		    <if test="custGb == 'custId'">
		AND    B.CUST_ID LIKE #{condition}||'%'
		    </if>
		    <if test="custGb == 'custNm'">
		AND    B.CUST_NM LIKE #{condition}||'%'
		    </if>
		    <if test="custGb == 'email'">
		AND    A.EMAIL LIKE #{condition}||'%'
		    </if>
		</if>
		<if test="termStdt != null and termStdt != '' and termEddt != null and termEddt != ''">
		    <if test="termGb == 'regDt'">
		AND    A.QUEST_DT <![CDATA[>=]]> STR_TO_DATE(#{termStdt},'%Y-%m-%d')
		AND    A.QUEST_DT <![CDATA[<]]> DATE_ADD(STR_TO_DATE(#{termEddt},'%Y-%m-%d'),INTERVAL 1 DAY)
		    </if>
		    <if test="termGb == 'ansDt'">
		AND    A.ANS_DT <![CDATA[>=]]> STR_TO_DATE(#{termStdt},'%Y-%m-%d')
		AND    A.ANS_DT <![CDATA[<]]> DATE_ADD(STR_TO_DATE(#{termEddt},'%Y-%m-%d'),INTERVAL 1 DAY)
		    </if>
		</if>
		AND    A.ANS_COMP_CD = #{ansCompCd}
		AND    A.ANS_TRANS_YN = 'Y' /*답변의뢰*/
		<if test="ansStat != null and ansStat != ''">
		AND    A.ANS_STAT = #{ansStat}
		</if>
		ORDER  BY A.QUEST_DT DESC
	</select>
	
	<!-- 상품문의 상세 -->
	<select id="getGoodsQna" parameterType="Counsel" resultType="Counsel">
		/* TssCounsel.getGoodsQna */
		SELECT A.COUNSEL_SQ                                                        /*상담일련번호*/
		     , FN_GET_CODE_NM('G000',A.SITE_CD)                AS SITE_NM          /*사이트명*/
		     , A.CUST_NO                                                           /*고객번호*/
		     , B.CUST_ID                                                           /*고객ID*/
		     , B.CUST_NM                                                           /*고객명*/
		     , A.CELL_PHNNO                                                        /*휴대전화번호*/
		     , A.SMS_REQ_YN                                                        /*SMS요청여부*/
		     , A.SMS_SEND_YN                                                       /*SMS발송여부*/
		     , A.EMAIL                                                             /*이메일*/
		     , A.REL_GOODS_CD                                                      /*관련상품코드*/
		     , G.GOODS_NM                                                          /*상품명*/
		     , (SELECT SYS_IMG_NM
		        FROM   TB_GOODS_IMG
		        WHERE  GOODS_CD = G.GOODS_CD
		        AND    COLOR_CD = G.MAIN_COLOR_CD
		        AND    DEFAULT_IMG_YN = 'Y' /*디폴트이미지*/
		        LIMIT 1
		       )                                               AS GOODS_IMG        /*상품이미지*/
		     , G.SELF_GOODS_YN                                                     /*자사상품여부*/
		     , DATE_FORMAT(A.QUEST_DT,'%Y-%m-%d %H:%i:%S')     AS QUEST_DT         /*문의일시*/
		     , A.SECRET_YN                                                         /*비밀글여부*/
		     , A.ANS_NO                                                            /*답변자번호*/
		     , D.USER_NM                                       AS ANS_NM           /*답변자명*/
		     , DATE_FORMAT(A.ANS_DT,'%Y-%m-%d %H:%i:%S')       AS ANS_DT           /*답변일시*/
		     , A.QUEST_TITLE                                                       /*질문제목*/
		     , A.QUEST_CONTENT                                                     /*질문내용*/
		     , A.ANS_TITLE                                                         /*답변제목*/
		     , A.ANS_CONTENT                                                       /*답변내용*/
		     , A.ANS_STAT                                                          /*답변상태코드*/
		     , FN_GET_CODE_NM('G060',A.ANS_STAT)               AS ANS_STAT_NM      /*답변상태명*/
		     , A.ANS_TRANS_YN                                                      /*답변의뢰여부*/
		     , A.ANS_COMP_CD                                                       /*답변업체코드*/
		     , FN_GET_SUPPLY_COMP_NM(A.ANS_COMP_CD)            AS ANS_COMP_NM      /*답변업체명*/
		     , A.ANS_TRANS_NO                                                      /*답변의뢰자번호*/
		     , C.USER_NM                                       AS ANS_TRANS_NM     /*답변의뢰자명*/
		     , DATE_FORMAT(A.ANS_TRANS_DT,'%Y-%m-%d %H:%i:%S') AS ANS_TRANS_DT     /*답변의뢰일시*/
		FROM   TB_COUNSEL A
		INNER JOIN TB_CUSTOMER B ON A.CUST_NO = B.CUST_NO
		INNER JOIN TB_GOODS G ON A.REL_GOODS_CD = G.GOODS_CD
		LEFT OUTER JOIN TB_USER C ON A.ANS_TRANS_NO = C.USER_NO
		LEFT OUTER JOIN TB_USER D ON A.ANS_NO = D.USER_NO
		WHERE  A.COUNSEL_SQ = #{counselSq}
		AND    A.COUNSEL_TYPE = 'G' /*상담유형(상품문의)*/
		AND    A.ANS_COMP_CD = #{ansCompCd}
		AND    A.ANS_TRANS_YN = 'Y' /*답변의뢰*/
	</select>

	<!--상품문의 답변 저장 -->
	<update id="updateGoodsQnaAnswer" parameterType="Counsel">
		/* TssCounsel.updateGoodsQnaAnswer */
		UPDATE TB_COUNSEL
		SET    SMS_SEND_YN = SMS_REQ_YN /*SMS답변수신 값에 따라 SMS를 먼저 발송함으로 SMS답변수신 값과 동일하게 처리*/
		     -- , EMAIL_SEND_YN = EMAIL_REQ_YN /*이메일답변수신 값에 따라 이메일을 먼저 발송함으로 이메일답변수신 값과 동일하게 처리*/
		     , ANS_TITLE = #{ansTitle}
		     , ANS_CONTENT = #{ansContent}
		     , ANS_STAT = 'G060_20' /*답변완료*/
		     , ANS_NO = #{ansNo}
		     , ANS_DT = NOW()
		     , UPD_NO = #{updNo}
		     , UPD_DT = NOW()
		WHERE  COUNSEL_SQ = #{counselSq}
		AND    ANS_COMP_CD = #{ansCompCd}
	</update>

</mapper>