<?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.cmmn.dao.CrdnPayerHstryMapper">

<!-- 단속 납부자 이력 정보 매퍼
 ========== 변경 이력 ==========
 2023-08-16	leebj 최초 작성
 ============================ -->

    <resultMap id="crdnPayerHstryRow" type="cokr.xit.fims.cmmn.CrdnPayerHstry"> <!-- 단속 납부자 이력 -->
        <result property="chgHstryId"			column="CHG_HSTRY_ID" />				<!-- 변경 이력 ID -->
        <result property="crdnId"				column="CRDN_ID" />						<!-- 단속 ID -->
        <result property="rtpyrId"				column="RTPYR_ID" />					<!-- 납부자 ID -->
        <result property="createdAt"			column="REG_DT" />						<!-- 등록 일시 -->
        <result property="createdBy"			column="RGTR" />						<!-- 등록자 -->
    </resultMap>

    <sql id="select">
        SELECT CPH.CHG_HSTRY_ID                                                                                 /* 변경 이력 ID */
             , CPH.CRDN_ID                                                                                      /* 단속 ID */
             , CPH.RTPYR_ID                                                                                     /* 납부자 ID */
             , CPH.REG_DT                                                                                       /* 등록 일시 */
             , CPH.RGTR                                                                                         /* 등록자 */
             , (SELECT USER_NM FROM TB_USER X WHERE X.USER_ID = CPH.RGTR)               AS RGTR_NM              /* 등록자 명 */
             , PH.RTPYR_NO                                                                                      /* 납부자 번호 */
             , PH.RTPYR_NM                                                                                      /* 납부자 명 */
             , PH.ZIP                                                                                           /* 우편번호 */
             , PH.WHOL_ADDR                                                                                     /* 전체 주소 */
          FROM TB_CRDN_PAYER_HSTRY  CPH
         INNER JOIN TB_PAYER_HSTRY  PH  ON (CPH.RTPYR_ID = PH.RTPYR_ID)
    </sql>

    <select id="selectCrdnPayerHstryList" parameterType="map" resultType="dataobject">/* 단속 납부자 이력 목록 조회(crdnPayerHstryMapper.selectCrdnPayerHstryList) */
        <include refid="utility.paging-prefix" />
        <include refid="select" />
        <where>
        </where>
        <include refid="utility.orderBy" />
        <include refid="utility.paging-suffix" />
    </select>

    <select id="selectCrdnPayerHstrys" parameterType="map" resultType="dataobject">/* 단속 납부자 이력 객체 가져오기(crdnPayerHstryMapper.selectCrdnPayerHstrys) */
        <include refid="select" />
        <where>
            <if test="chgHstryId != null">
           AND CPH.CHG_HSTRY_ID         =  #{chgHstryId}                                                        /* 변경 이력 ID */
            </if>
            <if test="crdnId != null">
           AND CPH.CRDN_ID              =  #{crdnId}                                                            /* 단속 ID */
            </if>
            <if test="rtpyrId != null">
           AND CPH.RTPYR_ID             =  #{rtpyrId}                                                           /* 납부자 ID */
            </if>
        </where>
        <include refid="utility.orderBy" />
    </select>

    <insert id="insertCrdnPayerHstry" parameterType="map">/* 단속 납부자 이력 등록(crdnPayerHstryMapper.insertCrdnPayerHstry) */
        <selectKey resultType="string" keyProperty="crdnPayerHstry.chgHstryId" keyColumn="NEW_ID" order="BEFORE">
            SELECT CONCAT(DATE_FORMAT(CURRENT_DATE, '%Y'), LPAD(CAST(IFNULL(MAX(SUBSTRING(CHG_HSTRY_ID, 5)) + 1, 1) AS INT), 16, '0')) AS NEW_ID
              FROM TB_CRDN_PAYER_HSTRY
             WHERE CHG_HSTRY_ID LIKE CONCAT(DATE_FORMAT(CURRENT_DATE, '%Y'), '%')
        </selectKey>
        INSERT
          INTO TB_CRDN_PAYER_HSTRY (
               CHG_HSTRY_ID                                                                                     /* 변경 이력 ID */
             , CRDN_ID                                                                                          /* 단속 ID */
             , RTPYR_ID                                                                                         /* 납부자 ID */
             , REG_DT                                                                                           /* 등록 일시 */
             , RGTR                                                                                             /* 등록자 */
        )
        VALUES (
               #{crdnPayerHstry.chgHstryId}                                                                     /* 변경 이력 ID */
             , #{crdnPayerHstry.crdnId}                                                                         /* 단속 ID */
             , #{crdnPayerHstry.rtpyrId}                                                                        /* 납부자 ID */
             , #{crdnPayerHstry.createdAt}                                                                      /* 등록 일시 */
             , #{crdnPayerHstry.createdBy}                                                                      /* 등록자 */
        )
    </insert>

    <update id="updateCrdnPayer" parameterType="map">/* 단속 납부자 수정(crdnPayerHstryMapper.updateCrdnPayer) */
        UPDATE TB_CRDN_PAYER_HSTRY
           SET RTPYR_ID                 =  #{crdnSttsHstry.rtpyrId}                                             /* 납부자 ID */
             , MDFCN_DT                 =  #{crdnSttsHstry.lastModified}                                        /* 수정 일시 */
             , MDFR                     =  #{crdnSttsHstry.modifiedBy}                                          /* 수정자 */
         WHERE CRDN_ID                  =  #{crdnSttsHstry.crdnId}                                              /* 단속 ID */
    </update>

</mapper>