<?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="cokr.xit.fims.crdn.dao.CrdnStngMapper">

<select id="selectDuplicateCrdns" parameterType="list" resultType="dataobject">/* 단속 중복 조회(crdnStngMapper.selectDuplicateCrdns) */
SELECT T.VHRNO, T.CRDN_YMD, SUM(T.CNT) CRDN_CNT
 FROM (
SELECT A.VHRNO, A.CRDN_YMD, CASE WHEN B.CRDN_ID IS NOT NULL THEN 1 ELSE 0 END CNT
 FROM (<foreach collection="crdns" item="crdn" separator="UNION">
       SELECT #{crdn.taskSeCd} TASK_SE_CD, <if test="crdn.crdnId != null">#{crdn.crdnId}</if><if test="crdn.crdnId == null">'new-crdn'</if> CRDN_ID, #{crdn.vhrno} VHRNO, #{crdn.crdnYmd} CRDN_YMD FROM DUAL</foreach>
      ) A
LEFT OUTER JOIN TB_CRDN B ON (A.TASK_SE_CD = B.TASK_SE_CD AND A.VHRNO = B.VHRNO AND A.CRDN_YMD = B.CRDN_YMD AND A.CRDN_ID != B.CRDN_ID AND B.DEL_YN = 'N')
      ) T
GROUP BY T.VHRNO, T.CRDN_YMD</select>

<select id="selectBasicAmt" parameterType="cokr.xit.fims.crdn.CrdnQuery" resultType="string">/* 기본금액 조회(crdnStngMapper.selectBasicAmt) */
SELECT AMT
  FROM TB_AMT
 WHERE USE_YN = 'Y'
   AND TASK_SE_CD = #{taskSeCd}
   AND BGNG_YMD <![CDATA[ <= ]]> #{aplcnYmd}
   AND END_YMD <![CDATA[ >= ]]> #{aplcnYmd}
   <if test="amtDcsnCdGroupId != null">
   AND AMT_DCSN_CD_GROUP_ID = #{amtDcsnCdGroupId}
   AND AMT_DCSN_CD = #{amtDcsnCd}
   </if>
   <if test="dtlAmtDcsnGroupId != null">
   AND DTL_AMT_DCSN_GROUP_ID = #{dtlAmtDcsnGroupId}
   AND DTL_AMT_DCSN_CD = #{dtlAmtDcsnCd}
   </if>
</select>

<select id="selectStdgNm" parameterType="string" resultType="string">/* 법정동코드 조회(crdnStngMapper.selectStdgNm) */
SELECT CONCAT(A.EMD_NM,
       CASE WHEN A.LI_NM = ''
       THEN ''
       ELSE CONCAT(' ', A.LI_NM)
       END) AS STDG_NM
  FROM TB_STDG A
 WHERE STDG_CD = #{stdgCd} 
</select>

<select id="selectStdgNmList" parameterType="string" resultType="string">/* 법정동코드 목록 조회(crdnStngMapper.selectStdgNmList) */
SELECT CONCAT(A.EMD_NM,
       CASE WHEN A.LI_NM = ''
       THEN ''
       ELSE CONCAT(' ', A.LI_NM)
       END) AS STDG_NM
  FROM TB_STDG A
 WHERE A.STDG_CD LIKE CONCAT(#{sggCd},'%')
   AND A.STDG_CD != CONCAT(#{sggCd},'00000')
 ORDER BY A.EMD_NM, A.LI_NM
</select>

<select id="selectStdgCdListByStdgNm" parameterType="map" resultType="string">/* 법정동명으로 법정동코드 조회(crdnStngMapper.selectStdgCdListByStdgNm) */
SELECT STDG_CD
  FROM TB_STDG
 WHERE EMD_NM IS NOT NULL 
   AND EMD_NM != ''
   AND USE_YN = 'Y'
   AND EMD_NM = #{emdNm}
   AND SUBSTR(STDG_CD,1,5) = #{sggCd}
</select>

</mapper>