<?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="WebPayment">
    <select id="selectTotalPaymentCount" parameterType="hashmap" resultType="hashmap">
        SELECT COUNT(1)  AS "totalCount"
          FROM (
                SELECT *
                  FROM (
                        SELECT MP.PAYMENT_NUMBER                                                  AS "paymentNumber"
                              ,MP.MAX_RESERVE_DATE                                                AS "reserveDate"
                              ,MP.MAX_PAYMENT_DATE                                                AS "paymentDate"
                              ,IF(MP.PAYMENT_CATEGORY_ITEM_NAME = ''
                                 ,IF(DISCOUNT_PRICE = TOTAL_AMOUNT
                                    ,'-'
                                    ,'미납')
                                 ,MP.PAYMENT_CATEGORY_ITEM_NAME)                                  AS "paymentCategoryItemName"
                              ,MP.TREATMENT_NAME                                                  AS "treatmentName"
                              ,MP.TREATMENT_PROCEDURE_NAME                                        AS "treatmentProcedureNm"
                              ,MP.RESERVE_MEMBER_NAME                                             AS "reserveMemberName"
                              ,MP.MU_USER_ID                                                      AS "muUserId"
                              ,MP.USER_NAME                                                       AS "userName"
                              ,MP.MEMBER_NAME                                                     AS "muMemberName"
                              ,MP.ETC                                                             AS "etc"
                              ,MP.TOTAL_AMOUNT                                                    AS "totalAmount"
                              ,MP.DISCOUNT_PRICE                                                  AS "discountAmount"
                              ,MP.MEMBERSHIP_PRICE + MP.REFUND_MEMBERSHIP_PRICE                   AS "membershipPrice"
                              ,IF(MP.STATUS IN ('NP', 'P')
                                 ,MP.TOTAL_AMOUNT - MP.PAY_AMOUNT + MP.DISCOUNT_PRICE - MP.MEMBERSHIP_PRICE
                                 ,0)                                                              AS "nonpaymentAmount"
                              ,MP.PAY_AMOUNT                                                      AS "paymentAmount"
                              ,MP.AMOUNT                                                          AS "amount"
                              ,MP.VAT                                                             AS "vat"
                              ,MP.REFUND_TOTAL_AMOUNT                                             AS "refundAmount"
                              ,CASE
                               WHEN MP.STATUS = 'C'  THEN '결제취소'
                               WHEN MP.STATUS = 'R'  THEN '환불'
                               WHEN MP.STATUS = 'PY' THEN '부분결제(완료)'
                               WHEN MP.STATUS = 'NY' THEN '미수결제'
                               WHEN MP.STATUS = 'CN' THEN '재결제(취소)'
                               WHEN MP.STATUS = 'CY' THEN '재결제(완료)'
                               WHEN MP.STATUS = 'Y'  THEN '결제완료'
                               WHEN MP.STATUS = 'P'  THEN '부분결제'
                               WHEN MP.STATUS = 'NP' THEN '미수'
                               ELSE ''
                                END                                                               AS "status1"
                              ,MP.MAX_REG_DATE                                                    AS "regDate"
                          FROM (
                                SELECT MP.PAYMENT_NUMBER                                                  AS PAYMENT_NUMBER
                                      ,GROUP_CONCAT(DISTINCT MP.MAX_RESERVE_DATE)                                                AS MAX_RESERVE_DATE
                                      ,GROUP_CONCAT(DISTINCT MP.MAX_PAYMENT_DATE)                                                AS MAX_PAYMENT_DATE
                                      ,GROUP_CONCAT(DISTINCT MP.PAYMENT_CATEGORY_ITEM_NAME)                                      AS PAYMENT_CATEGORY_ITEM_NAME
                                      ,GROUP_CONCAT(DISTINCT MP.MU_TREATMENT_ID)                                                 AS MU_TREATMENT_ID
                                      ,GROUP_CONCAT(DISTINCT MP.TREATMENT_NAME)                                                  AS TREATMENT_NAME
                                      ,GROUP_CONCAT(MP.MU_TREATMENT_PROCEDURE_ID)                                       AS MU_TREATMENT_PROCEDURE_ID
                                      ,GROUP_CONCAT(MP.TREATMENT_PROCEDURE_NAME)                                        AS TREATMENT_PROCEDURE_NAME
                                      ,GROUP_CONCAT(DISTINCT MP.RESERVE_MEMBER_NAME)                                             AS RESERVE_MEMBER_NAME
                                      ,GROUP_CONCAT(DISTINCT MP.MU_USER_ID)                                                      AS MU_USER_ID
                                      ,GROUP_CONCAT(DISTINCT MP.USER_NAME)                                                       AS USER_NAME
                                      ,GROUP_CONCAT(DISTINCT MP.MEMBER_NAME)                                                     AS MEMBER_NAME
                                      ,GROUP_CONCAT(DISTINCT MP.ETC)                                                             AS ETC
                                      ,GROUP_CONCAT(DISTINCT MP.PAY_AMOUNT)                                                      AS PAY_AMOUNT
                                      ,GROUP_CONCAT(DISTINCT MP.DISCOUNT_PRICE)                                                  AS DISCOUNT_PRICE
                                      ,GROUP_CONCAT(DISTINCT MP.MEMBERSHIP_PRICE)                                                AS MEMBERSHIP_PRICE
                                      ,GROUP_CONCAT(DISTINCT MP.AMOUNT)                                                          AS AMOUNT
                                      ,GROUP_CONCAT(DISTINCT MP.VAT)                                                             AS VAT
                                      ,GROUP_CONCAT(DISTINCT MP.REFUND_TOTAL_AMOUNT)                                             AS REFUND_TOTAL_AMOUNT
                                      ,GROUP_CONCAT(DISTINCT MP.REFUND_MEMBERSHIP_PRICE)                                         AS REFUND_MEMBERSHIP_PRICE
                                      ,IFNULL((SELECT MP2.STATUS
                                                 FROM MU_PAYMENT AS MP2
                                                WHERE MP2.USE_YN = 'Y'
                                                  AND MP2.PAYMENT_NUMBER = MP.PAYMENT_NUMBER
                                               <if test="paymentSearchStartDate != null and paymentSearchStartDate != '' and paymentSearchEndDate != null and paymentSearchEndDate != ''">
                                                  AND MP2.WRITE_DATE &gt;= #{paymentSearchStartDate}
                                                  AND MP2.WRITE_DATE &lt;= #{paymentSearchEndDate}
                                               </if>
                                               <if test="muUserId != null and muUserId != ''">
                                                  AND MP.MU_USER_ID = #{muUserId}
                                               </if>
                                             ORDER BY MP2.REG_DATE DESC
                                                LIMIT 0, 1), '')                                          AS STATUS
                                      ,MP.ORG_TREATMENT_PROCEDURE_PRICE + MP.ORG_TREATMENT_PROCEDURE_VAT  AS TOTAL_AMOUNT
                                      ,GROUP_CONCAT(DISTINCT MP.MAX_REG_DATE)                                                    AS MAX_REG_DATE
                                  FROM (
                                        SELECT MP.PAYMENT_NUMBER                                                 AS PAYMENT_NUMBER
                                              ,MP.MU_USER_ID                                                     AS MU_USER_ID
                                              ,MP.USER_NAME                                                      AS USER_NAME
                                              ,MAX(MP.RESERVE_DATE)                                              AS MAX_RESERVE_DATE
                                              ,MAX(MP.PAYMENT_DATE)                                              AS MAX_PAYMENT_DATE
                                              ,GROUP_CONCAT(DISTINCT MP.PAYMENT_CATEGORY_ITEM_NAME)              AS PAYMENT_CATEGORY_ITEM_NAME
                                              ,GROUP_CONCAT(DISTINCT MP.MU_TREATMENT_ID)                         AS MU_TREATMENT_ID
                                              ,GROUP_CONCAT(DISTINCT MP.TREATMENT_NAME)                          AS TREATMENT_NAME
                                              ,GROUP_CONCAT(DISTINCT MP.MU_TREATMENT_PROCEDURE_ID)               AS MU_TREATMENT_PROCEDURE_ID
                                              ,GROUP_CONCAT(DISTINCT MP.TREATMENT_PROCEDURE_NAME SEPARATOR ', ') AS TREATMENT_PROCEDURE_NAME
                                              ,GROUP_CONCAT(DISTINCT MP.RESERVE_MEMBER_NAME SEPARATOR ', ')      AS RESERVE_MEMBER_NAME
                                              ,GROUP_CONCAT(DISTINCT MP.MEMBER_NAME SEPARATOR ', ')              AS MEMBER_NAME
                                              ,GROUP_CONCAT(DISTINCT MP.ETC)                                     AS ETC
                                              ,SUM(MP.PAY_AMOUNT)                                                AS PAY_AMOUNT
                                              ,SUM(MP.DISCOUNT_PRICE)                                            AS DISCOUNT_PRICE
                                              ,SUM(MP.MEMBERSHIP_PRICE)                                          AS MEMBERSHIP_PRICE
                                              ,SUM(MP.AMOUNT)                                                    AS AMOUNT
                                              ,SUM(MP.VAT)                                                       AS VAT
                                              ,SUM(MP.REFUND_TOTAL_AMOUNT)                                       AS REFUND_TOTAL_AMOUNT
                                              ,SUM(MP.REFUND_MEMBERSHIP_PRICE)                                   AS REFUND_MEMBERSHIP_PRICE
                                              ,SUM(MP.ORG_TREATMENT_PROCEDURE_PRICE)           AS ORG_TREATMENT_PROCEDURE_PRICE
                                              ,SUM(MP.ORG_TREATMENT_PROCEDURE_VAT)             AS ORG_TREATMENT_PROCEDURE_VAT
                                              ,MAX(MP.REG_DATE)                                                  AS MAX_REG_DATE
                                          FROM (
                                                SELECT MP.RESERVE_DATE                                                       AS RESERVE_DATE
                                                      ,MP.PAYMENT_DATE                                                       AS PAYMENT_DATE
                                                      ,IF(MP.STATUS IN ('NP')
                                                         ,NULL
                                                         ,MP.PAYMENT_CATEGORY_ITEM_NAME)                                     AS PAYMENT_CATEGORY_ITEM_NAME
                                                      ,MP.MU_USER_ID                                                         AS MU_USER_ID
                                                      ,MP.USER_NAME                                                          AS USER_NAME
                                                      ,MP.MU_TREATMENT_ID                                                    AS MU_TREATMENT_ID
                                                      ,MP.TREATMENT_NAME                                                     AS TREATMENT_NAME
                                                      ,MP.MU_TREATMENT_PROCEDURE_ID                                          AS MU_TREATMENT_PROCEDURE_ID
                                                      ,MP.TREATMENT_PROCEDURE_NAME                                           AS TREATMENT_PROCEDURE_NAME
                                                      ,MP.RESERVE_MEMBER_ID                                                  AS RESERVE_MEMBER_ID
                                                      ,MP.RESERVE_MEMBER_NAME                                                AS RESERVE_MEMBER_NAME
                                                      ,MP.MU_MEMBER_ID                                                       AS MU_MEMBER_ID
                                                      ,MP.MEMBER_NAME                                                        AS MEMBER_NAME
                                                      ,NULLIF(MP.ETC, '')                                                    AS ETC
                                                      ,IF(MP.STATUS IN ('R', 'C', 'CN'), 0, MP.PAY_AMOUNT)                   AS PAY_AMOUNT
                                                      ,MP.DISCOUNT_PRICE                                                     AS DISCOUNT_PRICE
                                                      ,IF(MP.STATUS IN ('R', 'C', 'CN'), 0, IFNULL(MP.MEMBERSHIP_PRICE, 0))  AS MEMBERSHIP_PRICE
                                                      ,IF(MP.STATUS IN ('R', 'C'), 0, MP.AMOUNT)                             AS AMOUNT
                                                      ,IF(MP.STATUS IN ('R', 'C'), 0, MP.VAT)                                AS VAT
                                                      ,IF(MP.STATUS IN ('R', 'C', 'CN'), MP.PAY_AMOUNT, 0)                   AS REFUND_TOTAL_AMOUNT
                                                      ,IF(MP.STATUS IN ('R', 'C', 'CN'), IFNULL(MP.MEMBERSHIP_PRICE, 0), 0)  AS REFUND_MEMBERSHIP_PRICE
                                                      ,MP.PAYMENT_NUMBER                                                     AS PAYMENT_NUMBER
                                                      ,MP.ORG_TREATMENT_PROCEDURE_PRICE                                      AS ORG_TREATMENT_PROCEDURE_PRICE
                                                      ,MP.ORG_TREATMENT_PROCEDURE_VAT                                        AS ORG_TREATMENT_PROCEDURE_VAT
                                                      ,MP.STATUS                                                             AS STATUS
                                                      ,MP.REG_DATE                                                           AS REG_DATE
                                                      ,MP.MU_RESERVE_ID                                                      AS MU_RESERVE_ID
                                                  FROM (
                                                        SELECT IFNULL(DATE_FORMAT(MR.RESERVE_DATE, '%Y-%m-%d'), '-')   AS RESERVE_DATE
                                                              ,DATE_FORMAT(MP.WRITE_DATE, '%Y-%m-%d')                  AS PAYMENT_DATE
                                                              ,MP.PAYMENT_CATEGORY_ITEM_NAME                           AS PAYMENT_CATEGORY_ITEM_NAME
                                                              ,MP.MU_USER_ID                                           AS MU_USER_ID
                                                              ,(SELECT MU.USER_NAME
                                                                  FROM MU_USER AS MU
                                                                 WHERE MU.USE_YN = 'Y'
                                                                   AND MU.MU_USER_ID = MP.MU_USER_ID
                                                                 LIMIT 0, 1)                                           AS USER_NAME
                                                              ,MPD.MU_TREATMENT_ID                                     AS MU_TREATMENT_ID
                                                              ,MPD.TREATMENT_NAME                                      AS TREATMENT_NAME
                                                              ,MPD.MU_TREATMENT_PROCEDURE_ID                           AS MU_TREATMENT_PROCEDURE_ID
                                                              ,MPD.TREATMENT_PROCEDURE_NAME                            AS TREATMENT_PROCEDURE_NAME
                                                              ,MPD.RESERVE_MEMBER_ID                                   AS RESERVE_MEMBER_ID
                                                              ,(SELECT MM.NAME
                                                                  FROM MU_MEMBER AS MM
                                                                 WHERE MM.USE_YN = 'Y'
                                                                   AND MM.MU_MEMBER_ID = MPD.RESERVE_MEMBER_ID
                                                                 LIMIT 0, 1)                                           AS RESERVE_MEMBER_NAME
                                                              ,MPD.MU_MEMBER_ID                                        AS MU_MEMBER_ID
                                                              ,(SELECT MM.NAME
                                                                  FROM MU_MEMBER AS MM
                                                                 WHERE MM.USE_YN = 'Y'
                                                                   AND MM.MU_MEMBER_ID = MPD.MU_MEMBER_ID
                                                                 LIMIT 0, 1)                                           AS MEMBER_NAME
                                                              ,MP.ETC                                    AS ETC
                                                              ,MPD.TOTAL_AMOUNT                          AS PAY_AMOUNT
                                                              ,MPD.DISCOUNT_PRICE                        AS DISCOUNT_PRICE
                                                              ,MPD.MEMBERSHIP_PRICE                      AS MEMBERSHIP_PRICE
                                                              ,MPD.AMOUNT                                AS AMOUNT
                                                              ,MPD.VAT                                   AS VAT
                                                              ,MP.PAYMENT_NUMBER                         AS PAYMENT_NUMBER
                                                              ,(SELECT MTPP.PRICE
                                                                  FROM MU_TREATMENT_PROCEDURE_PRICE AS MTPP
                                                                 WHERE MTPP.USE_YN = 'Y'
                                                                   AND MTPP.MU_TREATMENT_PROCEDURE_ID = MPD.MU_TREATMENT_PROCEDURE_ID
                                                                 LIMIT 0, 1)                             AS ORG_TREATMENT_PROCEDURE_PRICE
                                                              ,(SELECT MTPP.VAT
                                                                  FROM MU_TREATMENT_PROCEDURE_PRICE AS MTPP
                                                                 WHERE MTPP.USE_YN = 'Y'
                                                                   AND MTPP.MU_TREATMENT_PROCEDURE_ID = MPD.MU_TREATMENT_PROCEDURE_ID
                                                                 LIMIT 0, 1)                             AS ORG_TREATMENT_PROCEDURE_VAT
                                                              ,MP.STATUS                                 AS STATUS
                                                              ,MP.REG_DATE                               AS REG_DATE
                                                              ,MPD.MU_RESERVE_ID                         AS MU_RESERVE_ID
                                                          FROM MU_PAYMENT AS MP
                                                    INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                                            ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                                           AND MP.USE_YN = 'Y'
                                                           AND MPD.USE_YN = 'Y'
                                                       <if test="paymentSearchStartDate != null and paymentSearchStartDate != '' and paymentSearchEndDate != null and paymentSearchEndDate != ''">
                                                           AND MP.WRITE_DATE &gt;= #{paymentSearchStartDate}
                                                           AND MP.WRITE_DATE &lt;= #{paymentSearchEndDate}
                                                        </if>
                                                    INNER JOIN (
                                                                SELECT MP.PAYMENT_NUMBER
                                                                  FROM MU_PAYMENT AS MP
                                                            INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                                                    ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                                                   AND MP.USE_YN = 'Y'
                                                                   AND MPD.USE_YN = 'Y'
                                                                   AND MP.CRM_MEMBER_PAY_PID IS NULL
                                                                   AND MPD.CRM_MEMBER_PAY_PID IS NULL
                                                                <if test="paymentSearchStartDate != null and paymentSearchStartDate != '' and paymentSearchEndDate != null and paymentSearchEndDate != ''">
                                                                   AND MP.WRITE_DATE &gt;= #{paymentSearchStartDate}
                                                                   AND MP.WRITE_DATE &lt;= #{paymentSearchEndDate}
                                                                </if>
                                                                <if test="paymentSearchKeywordParam != null and paymentSearchKeywordParam != ''">
                                                                   AND ((MU.USER_NAME LIKE CONCAT('%',TRIM(#{paymentSearchKeywordParam}),'%'))
                                                                    OR (MPD.TREATMENT_PROCEDURE_NAME LIKE CONCAT('%',TRIM(#{paymentSearchKeywordParam}),'%')))
                                                                </if>
                                                                <if test="muUserId != null and muUserId != ''">
                                                                   AND MP.MU_USER_ID = #{muUserId}
                                                                </if>
                                                              GROUP BY MP.PAYMENT_NUMBER
                                                          ) AS MP2
                                                            ON MP.PAYMENT_NUMBER = MP2.PAYMENT_NUMBER
                                                     LEFT JOIN MU_RESERVE AS MR
                                                            ON MPD.MU_RESERVE_ID = MR.MU_RESERVE_ID
                                                  ) AS MP
                                                 WHERE 1 = 1
                                          ) AS MP
                                         WHERE 1 = 1
                                      GROUP BY MP.PAYMENT_NUMBER
                                              ,MP.MU_USER_ID
                                              ,MP.USER_NAME
                                  ) AS MP
                                 WHERE 1 = 1
                                <if test="paymentCategoryItemNm != null and paymentCategoryItemNm != ''">
                                   AND MP.PAYMENT_CATEGORY_ITEM_NAME LIKE CONCAT('%', #{paymentCategoryItemNm}, '%')
                                </if>
                                <if test="muTreatmentId != null and muTreatmentId != ''">
                                   AND MP.TREATMENT_NAME LIKE CONCAT('%', #{treatmentName}, '%')
                                </if>
                              GROUP BY MP.PAYMENT_NUMBER
                          ) AS MP
                         WHERE 1 = 1
                  ) AS MP
                 WHERE 1 = 1
                <if test="paymentStatusKeyword != null and paymentStatusKeyword != ''">
                   AND MP.status1 LIKE CONCAT('%',  #{paymentStatusKeyword}, '%')
                </if>

                <if test="muUserId != null and muUserId != ''">
             UNION ALL

                SELECT *
                  FROM (
                        SELECT MP.PAYMENT_NUMBER                                 AS paymentNumber
                              ,DATE_FORMAT(MP.WRITE_DATE, '%Y-%m-%d')            AS reserveDate
                              ,DATE_FORMAT(MP.WRITE_DATE, '%Y-%m-%d')            AS paymentDate
                              ,MP.PAYMENT_CATEGORY_ITEM_NAME                     AS paymentCategoryItemName
                              ,MPD.TREATMENT_NAME                                AS treatmentName
                              ,CASE
                               WHEN MPD.CRM_MEMBER_PAY_M_DIVI4_NAME != ''
                               THEN CONCAT(MPD.CRM_MEMBER_PAY_M_DIVI2_NAME, '/', MPD.CRM_MEMBER_PAY_M_DIVI3_NAME, '/', MPD.CRM_MEMBER_PAY_M_DIVI4_NAME)
                               WHEN MPD.CRM_MEMBER_PAY_M_DIVI3_NAME != ''
                               THEN CONCAT(MPD.CRM_MEMBER_PAY_M_DIVI2_NAME, '/', MPD.CRM_MEMBER_PAY_M_DIVI3_NAME)
                               WHEN MPD.CRM_MEMBER_PAY_M_DIVI2_NAME != ''
                               THEN CONCAT(MPD.CRM_MEMBER_PAY_M_DIVI2_NAME)
                               ELSE ''
                                END                                              AS treatmentProcedureNm
                              ,MPD.CRM_MEMBER_PAY_DOCTOR_NAME                    AS reserveMemberName
                              ,MP.MU_USER_ID                                     AS muUserId
                              ,(SELECT MU.USER_NAME
                                  FROM MU_USER AS MU
                                 WHERE MU.MU_USER_ID = MP.MU_USER_ID
                                 LIMIT 0, 1)                                     AS userName
                              ,MPD.CRM_MEMBER_PAY_STAFF_NAME                     AS muMemberName
                              ,MP.ETC                                            AS etc
                              ,IFNULL(MPD.TOTAL_AMOUNT, 0)                       AS totalAmount
                              ,IFNULL(MPD.DISCOUNT_PRICE, 0)                     AS discountAmount
                              ,IFNULL(MPD.MEMBERSHIP_PRICE, 0)                   AS membershipPrice
                              ,0                                                 AS nonpaymentAmount
                              ,IFNULL(MPD.TOTAL_AMOUNT, 0)                       AS paymentAmount
                              ,IFNULL(MP.AMOUNT, 0)                              AS amount
                              ,IFNULL(MP.VAT, 0)                                 AS vat
                              ,0                                                 AS refundAmount
                              ,CASE
                               WHEN MP.STATUS = 'C'  THEN '결제취소'
                               WHEN MP.STATUS = 'R'  THEN '환불'
                               WHEN MP.STATUS = 'PY' THEN '부분결제(완료)'
                               WHEN MP.STATUS = 'NY' THEN '미수결제'
                               WHEN MP.STATUS = 'Y'  THEN '결제완료'
                               WHEN MP.STATUS = 'P'  THEN '부분결제'
                               WHEN MP.STATUS = 'NP' THEN '미수'
                               WHEN MP.STATUS = ''   THEN MP.CRM_MEMBER_PAY_PAY_DIVI1
                               ELSE MP.STATUS
                               END                                               AS status1
                              ,MP.REG_DATE                                       AS regDate
                          FROM MU_PAYMENT AS MP
                    INNER JOIN MU_PAYMENT_DETAIL AS MPD
                            ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                           AND MP.USE_YN = 'Y'
                           AND MPD.USE_YN = 'Y'
                           AND MP.CRM_MEMBER_PAY_PID IS NOT NULL
                           AND MPD.CRM_MEMBER_PAY_PID IS NOT NULL
                        <if test="paymentSearchStartDate != null and paymentSearchStartDate != '' and paymentSearchEndDate != null and paymentSearchEndDate != ''">
                           AND MP.WRITE_DATE &gt;= #{paymentSearchStartDate}
                           AND MP.WRITE_DATE &lt;= #{paymentSearchEndDate}
                        </if>
                        <if test="muUserId != null and muUserId != ''">
                           AND MP.MU_USER_ID = #{muUserId}
                        </if>
                  ) AS MP
                 WHERE 1 = 1
                <if test="paymentStatusKeyword != null and paymentStatusKeyword != ''">
                   AND status1 = #{paymentStatusKeyword}
                </if>
                </if>
          ) AS MP
    </select>

    <select id="selectListPayment" parameterType="hashmap" resultType="hashmap">
        SELECT *
          FROM (
                SELECT MP.PAYMENT_NUMBER                                                  AS "paymentNumber"
                      ,MP.MAX_RESERVE_DATE                                                AS "reserveDate"
                      ,MP.MAX_PAYMENT_DATE                                                AS "paymentDate"
                      ,IF(MP.PAYMENT_CATEGORY_ITEM_NAME = ''
                         ,IF(DISCOUNT_PRICE = MP.ORG_TREATMENT_PROCEDURE_PRICE+MP.ORG_TREATMENT_PROCEDURE_VAT
                            ,'-'
                            ,'미납')
                         ,REGEXP_REPLACE(REGEXP_REPLACE(MP.PAYMENT_CATEGORY_ITEM_NAME
                                                        ,'([^,]+)(,\\1)+'
                                                        ,'\\1')
                                        ,',+'
                                        ,','))                                            AS "paymentCategoryItemName"
                      ,MP.TREATMENT_NAME                                                  AS "treatmentName"
                      ,MP.TREATMENT_PROCEDURE_NAME                                        AS "treatmentProcedureNm"
                      ,MP.RESERVE_MEMBER_NAME                                             AS "reserveMemberName"
                      ,MP.MU_USER_ID                                                      AS "muUserId"
                      ,MP.USER_NAME                                                       AS "userName"
                      ,MP.MEMBER_NAME                                                     AS "muMemberName"
                      ,MP.ETC                                                             AS "etc"
                      ,MP.ORG_TREATMENT_PROCEDURE_PRICE+MP.ORG_TREATMENT_PROCEDURE_VAT                                                    AS "totalAmount"
                      ,MP.DISCOUNT_PRICE                                                  AS "discountAmount"
                      ,MP.MEMBERSHIP_PRICE + MP.REFUND_MEMBERSHIP_PRICE                   AS "membershipPrice"
                      ,IF(MP.STATUS IN ('NP', 'P', 'NY', 'PY')
                         ,MP.ORG_TREATMENT_PROCEDURE_PRICE+MP.ORG_TREATMENT_PROCEDURE_VAT - MP.PAY_AMOUNT + MP.DISCOUNT_PRICE - MP.MEMBERSHIP_PRICE
                         ,0)                                                              AS "nonpaymentAmount"
                      ,MP.PAY_AMOUNT                                                      AS "paymentAmount"
                      ,MP.AMOUNT                                                          AS "amount"
                      ,MP.VAT                                                             AS "vat"
                      ,MP.REFUND_TOTAL_AMOUNT                                             AS "refundAmount"
                      ,CASE
                       WHEN MP.STATUS = 'C'  THEN '결제취소'
                       WHEN MP.STATUS = 'R'  THEN '환불'
                       WHEN MP.STATUS = 'PY' THEN '부분결제(완료)'
                       WHEN MP.STATUS = 'NY' THEN '미수결제'
                       WHEN MP.STATUS = 'CN' THEN '재결제(취소)'
                       WHEN MP.STATUS = 'CY' THEN '재결제(완료)'
                       WHEN MP.STATUS = 'Y'  THEN '결제완료'
                       WHEN MP.STATUS = 'P'  THEN '부분결제'
                       WHEN MP.STATUS = 'NP' THEN '미수'
                       ELSE ''
                        END                                                               AS "status1"
                      ,MP.MAX_REG_DATE                                                    AS "regDate"
                  FROM (
                        SELECT MP.PAYMENT_NUMBER                                                       AS PAYMENT_NUMBER
                              ,GROUP_CONCAT(DISTINCT MP.MAX_RESERVE_DATE)                              AS MAX_RESERVE_DATE
                              ,GROUP_CONCAT(DISTINCT MP.MAX_PAYMENT_DATE)                              AS MAX_PAYMENT_DATE
                              ,GROUP_CONCAT(DISTINCT MP.PAYMENT_CATEGORY_ITEM_NAME)                    AS PAYMENT_CATEGORY_ITEM_NAME
                              ,GROUP_CONCAT(DISTINCT MP.MU_TREATMENT_ID)                               AS MU_TREATMENT_ID
                              ,GROUP_CONCAT(DISTINCT MP.TREATMENT_NAME)                                AS TREATMENT_NAME
                              ,GROUP_CONCAT(MP.MU_TREATMENT_PROCEDURE_ID)                              AS MU_TREATMENT_PROCEDURE_ID
                              ,GROUP_CONCAT(MP.TREATMENT_PROCEDURE_NAME)                               AS TREATMENT_PROCEDURE_NAME
                              ,GROUP_CONCAT(DISTINCT MP.RESERVE_MEMBER_NAME)                           AS RESERVE_MEMBER_NAME
                              ,GROUP_CONCAT(DISTINCT MP.MU_USER_ID)                                    AS MU_USER_ID
                              ,GROUP_CONCAT(DISTINCT MP.USER_NAME)                                     AS USER_NAME
                              ,GROUP_CONCAT(DISTINCT MP.MEMBER_NAME)                                   AS MEMBER_NAME
                              ,GROUP_CONCAT(DISTINCT MP.ETC)                                           AS ETC
                              ,SUM(MP.PAY_AMOUNT)                                                      AS PAY_AMOUNT
                              ,SUM(MP.DISCOUNT_PRICE)                                                  AS DISCOUNT_PRICE
                              ,SUM(MP.MEMBERSHIP_PRICE)                                                AS MEMBERSHIP_PRICE
                              ,SUM(MP.AMOUNT)                                                          AS AMOUNT
                              ,SUM(MP.VAT)                                                             AS VAT
                              ,SUM(MP.REFUND_TOTAL_AMOUNT)                                             AS REFUND_TOTAL_AMOUNT
                              ,SUM(MP.REFUND_MEMBERSHIP_PRICE)                                         AS REFUND_MEMBERSHIP_PRICE
                              ,IFNULL((SELECT MP2.STATUS
                                         FROM MU_PAYMENT AS MP2
                                        WHERE MP2.USE_YN = 'Y'
                                          AND MP2.PAYMENT_NUMBER = MP.PAYMENT_NUMBER
                                       <if test="paymentSearchStartDate != null and paymentSearchStartDate != '' and paymentSearchEndDate != null and paymentSearchEndDate != ''">
                                          AND MP2.WRITE_DATE &gt;= #{paymentSearchStartDate}
                                          AND MP2.WRITE_DATE &lt;= #{paymentSearchEndDate}
                                       </if>
                                       <if test="muUserId != null and muUserId != ''">
                                          AND MP.MU_USER_ID = #{muUserId}
                                       </if>
                                     ORDER BY MP2.REG_DATE DESC
                                        LIMIT 0, 1), '')                                               AS STATUS
                              ,GROUP_CONCAT(DISTINCT MP.MAX_REG_DATE)                                  AS MAX_REG_DATE
                              ,SUM((SELECT MTPP.PRICE
                                      FROM MU_TREATMENT_PROCEDURE_PRICE AS MTPP
                                     WHERE MTPP.USE_YN = 'Y'
                                       AND MTPP.MU_TREATMENT_PROCEDURE_ID = MP.MU_TREATMENT_PROCEDURE_ID
                                     LIMIT 0, 1))                                                      AS ORG_TREATMENT_PROCEDURE_PRICE
                              ,SUM((SELECT MTPP.VAT
                                      FROM MU_TREATMENT_PROCEDURE_PRICE AS MTPP
                                     WHERE MTPP.USE_YN = 'Y'
                                       AND MTPP.MU_TREATMENT_PROCEDURE_ID = MP.MU_TREATMENT_PROCEDURE_ID
                                     LIMIT 0, 1))                                                      AS ORG_TREATMENT_PROCEDURE_VAT
                          FROM (
                                SELECT MP.PAYMENT_NUMBER                                                 AS PAYMENT_NUMBER
                                      ,MP.MU_USER_ID                                                     AS MU_USER_ID
                                      ,MP.USER_NAME                                                      AS USER_NAME
                                      ,MAX(MP.RESERVE_DATE)                                              AS MAX_RESERVE_DATE
                                      ,MAX(MP.PAYMENT_DATE)                                              AS MAX_PAYMENT_DATE
                                      ,GROUP_CONCAT(DISTINCT MP.PAYMENT_CATEGORY_ITEM_NAME)              AS PAYMENT_CATEGORY_ITEM_NAME
                                      ,GROUP_CONCAT(DISTINCT MP.MU_TREATMENT_ID)                         AS MU_TREATMENT_ID
                                      ,GROUP_CONCAT(DISTINCT MP.TREATMENT_NAME)                          AS TREATMENT_NAME
                                      ,MP.MU_TREATMENT_PROCEDURE_ID                                      AS MU_TREATMENT_PROCEDURE_ID
                                      ,GROUP_CONCAT(DISTINCT MP.TREATMENT_PROCEDURE_NAME SEPARATOR ', ') AS TREATMENT_PROCEDURE_NAME
                                      ,GROUP_CONCAT(DISTINCT MP.RESERVE_MEMBER_NAME SEPARATOR ', ')      AS RESERVE_MEMBER_NAME
                                      ,GROUP_CONCAT(DISTINCT MP.MEMBER_NAME SEPARATOR ', ')              AS MEMBER_NAME
                                      ,GROUP_CONCAT(DISTINCT MP.ETC)                                     AS ETC
                                      ,SUM(MP.PAY_AMOUNT)                                                AS PAY_AMOUNT
                                      ,SUM(MP.DISCOUNT_PRICE)                                            AS DISCOUNT_PRICE
                                      ,SUM(MP.MEMBERSHIP_PRICE)                                          AS MEMBERSHIP_PRICE
                                      ,SUM(MP.AMOUNT)                                                    AS AMOUNT
                                      ,SUM(MP.VAT)                                                       AS VAT
                                      ,SUM(MP.REFUND_TOTAL_AMOUNT)                                       AS REFUND_TOTAL_AMOUNT
                                      ,SUM(MP.REFUND_MEMBERSHIP_PRICE)                                   AS REFUND_MEMBERSHIP_PRICE
                                      ,MAX(MP.REG_DATE)                                                  AS MAX_REG_DATE
                                  FROM (
                                        SELECT MP.RESERVE_DATE                                                       AS RESERVE_DATE
                                              ,MP.PAYMENT_DATE                                                       AS PAYMENT_DATE
                                              ,IF(MP.STATUS IN ('NP')
                                                 ,NULL
                                                 ,MP.PAYMENT_CATEGORY_ITEM_NAME)                                     AS PAYMENT_CATEGORY_ITEM_NAME
                                              ,MP.MU_USER_ID                                                         AS MU_USER_ID
                                              ,MP.USER_NAME                                                          AS USER_NAME
                                              ,MP.MU_TREATMENT_ID                                                    AS MU_TREATMENT_ID
                                              ,MP.TREATMENT_NAME                                                     AS TREATMENT_NAME
                                              ,MP.MU_TREATMENT_PROCEDURE_ID                                          AS MU_TREATMENT_PROCEDURE_ID
                                              ,MP.TREATMENT_PROCEDURE_NAME                                           AS TREATMENT_PROCEDURE_NAME
                                              ,MP.RESERVE_MEMBER_ID                                                  AS RESERVE_MEMBER_ID
                                              ,MP.RESERVE_MEMBER_NAME                                                AS RESERVE_MEMBER_NAME
                                              ,MP.MU_MEMBER_ID                                                       AS MU_MEMBER_ID
                                              ,MP.MEMBER_NAME                                                        AS MEMBER_NAME
                                              ,NULLIF(MP.ETC, '')                                                    AS ETC
                                              ,IF(MP.STATUS IN ('R', 'C', 'CN'), 0, MP.PAY_AMOUNT)                   AS PAY_AMOUNT
                                              ,MP.DISCOUNT_PRICE                                                     AS DISCOUNT_PRICE
                                              ,IF(MP.STATUS IN ('R', 'C', 'CN'), 0, IFNULL(MP.MEMBERSHIP_PRICE, 0))  AS MEMBERSHIP_PRICE
                                              ,IF(MP.STATUS IN ('R', 'C'), 0, MP.AMOUNT)                             AS AMOUNT
                                              ,IF(MP.STATUS IN ('R', 'C'), 0, MP.VAT)                                AS VAT
                                              ,IF(MP.STATUS IN ('R', 'C', 'CN'), MP.PAY_AMOUNT, 0)                   AS REFUND_TOTAL_AMOUNT
                                              ,IF(MP.STATUS IN ('R', 'C', 'CN'), IFNULL(MP.MEMBERSHIP_PRICE, 0), 0)  AS REFUND_MEMBERSHIP_PRICE
                                              ,MP.PAYMENT_NUMBER                                                     AS PAYMENT_NUMBER
                                              ,MP.STATUS                                                             AS STATUS
                                              ,MP.REG_DATE                                                           AS REG_DATE
                                              ,MP.MU_RESERVE_ID                                                      AS MU_RESERVE_ID
                                          FROM (
                                                SELECT IFNULL(DATE_FORMAT(MR.RESERVE_DATE, '%Y-%m-%d'), '-')   AS RESERVE_DATE
                                                      ,DATE_FORMAT(MP.WRITE_DATE, '%Y-%m-%d')                  AS PAYMENT_DATE
                                                      ,MP.PAYMENT_CATEGORY_ITEM_NAME                           AS PAYMENT_CATEGORY_ITEM_NAME
                                                      ,MP.MU_USER_ID                                           AS MU_USER_ID
                                                      ,(SELECT MU.USER_NAME
                                                          FROM MU_USER AS MU
                                                         WHERE MU.USE_YN = 'Y'
                                                           AND MU.MU_USER_ID = MP.MU_USER_ID
                                                         LIMIT 0, 1)                                           AS USER_NAME
                                                      ,MPD.MU_TREATMENT_ID                                     AS MU_TREATMENT_ID
                                                      ,MPD.TREATMENT_NAME                                      AS TREATMENT_NAME
                                                      ,MPD.MU_TREATMENT_PROCEDURE_ID                           AS MU_TREATMENT_PROCEDURE_ID
                                                      ,MPD.TREATMENT_PROCEDURE_NAME                            AS TREATMENT_PROCEDURE_NAME
                                                      ,MPD.RESERVE_MEMBER_ID                                   AS RESERVE_MEMBER_ID
                                                      ,(SELECT MM.NAME
                                                          FROM MU_MEMBER AS MM
                                                         WHERE MM.USE_YN = 'Y'
                                                           AND MM.MU_MEMBER_ID = MPD.RESERVE_MEMBER_ID
                                                         LIMIT 0, 1)                                           AS RESERVE_MEMBER_NAME
                                                      ,MPD.MU_MEMBER_ID                                        AS MU_MEMBER_ID
                                                      ,(SELECT MM.NAME
                                                          FROM MU_MEMBER AS MM
                                                         WHERE MM.USE_YN = 'Y'
                                                           AND MM.MU_MEMBER_ID = MPD.MU_MEMBER_ID
                                                         LIMIT 0, 1)                                           AS MEMBER_NAME
                                                      ,MP.ETC                                    AS ETC
                                                      ,MPD.TOTAL_AMOUNT                          AS PAY_AMOUNT
                                                      ,MPD.DISCOUNT_PRICE                        AS DISCOUNT_PRICE
                                                      ,MPD.MEMBERSHIP_PRICE                      AS MEMBERSHIP_PRICE
                                                      ,MPD.AMOUNT                                AS AMOUNT
                                                      ,MPD.VAT                                   AS VAT
                                                      ,MP.PAYMENT_NUMBER                         AS PAYMENT_NUMBER
                                                      ,MP.STATUS                                 AS STATUS
                                                      ,MP.REG_DATE                               AS REG_DATE
                                                      ,MPD.MU_RESERVE_ID                         AS MU_RESERVE_ID
                                                  FROM MU_PAYMENT AS MP
                                            INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                                    ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                                   AND MP.USE_YN = 'Y'
                                                   AND MPD.USE_YN = 'Y'
                                                <if test="paymentSearchStartDate != null and paymentSearchStartDate != '' and paymentSearchEndDate != null and paymentSearchEndDate != ''">
                                                   AND MP.WRITE_DATE &gt;= #{paymentSearchStartDate}
                                                   AND MP.WRITE_DATE &lt;= #{paymentSearchEndDate}
                                                </if>
                                            INNER JOIN (
                                                        SELECT MP.PAYMENT_NUMBER
                                                          FROM MU_PAYMENT AS MP
                                                    INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                                            ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                                           AND MP.USE_YN = 'Y'
                                                           AND MPD.USE_YN = 'Y'
                                                           AND MP.CRM_MEMBER_PAY_PID IS NULL
                                                           AND MPD.CRM_MEMBER_PAY_PID IS NULL
                                                        <if test="paymentSearchStartDate != null and paymentSearchStartDate != '' and paymentSearchEndDate != null and paymentSearchEndDate != ''">
                                                           AND MP.WRITE_DATE &gt;= #{paymentSearchStartDate}
                                                           AND MP.WRITE_DATE &lt;= #{paymentSearchEndDate}
                                                        </if>
                                                        <if test="paymentSearchKeywordParam != null and paymentSearchKeywordParam != ''">
                                                           AND ((MU.USER_NAME LIKE CONCAT('%',TRIM(#{paymentSearchKeywordParam}),'%'))
                                                            OR (MPD.TREATMENT_PROCEDURE_NAME LIKE CONCAT('%',TRIM(#{paymentSearchKeywordParam}),'%')))
                                                        </if>
                                                        <if test="muUserId != null and muUserId != ''">
                                                           AND MP.MU_USER_ID = #{muUserId}
                                                        </if>
                                                      GROUP BY MP.PAYMENT_NUMBER
                                                  ) AS MP2
                                                    ON MP.PAYMENT_NUMBER = MP2.PAYMENT_NUMBER
                                             LEFT JOIN MU_RESERVE AS MR
                                                    ON MPD.MU_RESERVE_ID = MR.MU_RESERVE_ID
                                          ) AS MP
                                         WHERE 1 = 1
                                  ) AS MP
                                 WHERE 1 = 1
                              GROUP BY MP.PAYMENT_NUMBER
                                      ,MP.MU_USER_ID
                                      ,MP.USER_NAME
                                      ,MP.MU_RESERVE_ID
                                      ,MP.MU_TREATMENT_PROCEDURE_ID
                          ) AS MP
                         WHERE 1 = 1
                        <if test="paymentCategoryItemNm != null and paymentCategoryItemNm != ''">
                           AND MP.PAYMENT_CATEGORY_ITEM_NAME LIKE CONCAT('%', #{paymentCategoryItemNm}, '%')
                        </if>
                        <if test="muTreatmentId != null and muTreatmentId != ''">
                           AND MP.TREATMENT_NAME LIKE CONCAT('%', #{treatmentName}, '%')
                        </if>
                      GROUP BY MP.PAYMENT_NUMBER
                  ) AS MP
                 WHERE 1 = 1
          ) AS MP
         WHERE 1 = 1
        <if test="paymentStatusKeyword != null and paymentStatusKeyword != ''">
           AND MP.status1 LIKE CONCAT('%',  #{paymentStatusKeyword}, '%')
        </if>
        <if test="muUserId != null and muUserId != ''">
     UNION ALL

        SELECT *
          FROM (
                SELECT MP.PAYMENT_NUMBER                                 AS paymentNumber
                      ,DATE_FORMAT(MP.WRITE_DATE, '%Y-%m-%d')            AS reserveDate
                      ,DATE_FORMAT(MP.WRITE_DATE, '%Y-%m-%d')            AS paymentDate
                      ,MP.PAYMENT_CATEGORY_ITEM_NAME                     AS paymentCategoryItemName
                      ,MPD.TREATMENT_NAME                                AS treatmentName
                      ,CASE
                       WHEN MPD.CRM_MEMBER_PAY_M_DIVI4_NAME != ''
                       THEN CONCAT(MPD.CRM_MEMBER_PAY_M_DIVI2_NAME, '/', MPD.CRM_MEMBER_PAY_M_DIVI3_NAME, '/', MPD.CRM_MEMBER_PAY_M_DIVI4_NAME)
                       WHEN MPD.CRM_MEMBER_PAY_M_DIVI3_NAME != ''
                       THEN CONCAT(MPD.CRM_MEMBER_PAY_M_DIVI2_NAME, '/', MPD.CRM_MEMBER_PAY_M_DIVI3_NAME)
                       WHEN MPD.CRM_MEMBER_PAY_M_DIVI2_NAME != ''
                       THEN CONCAT(MPD.CRM_MEMBER_PAY_M_DIVI2_NAME)
                       ELSE ''
                        END                                              AS treatmentProcedureNm
                      ,MPD.CRM_MEMBER_PAY_DOCTOR_NAME                    AS reserveMemberName
                      ,MP.MU_USER_ID                                     AS muUserId
                      ,(SELECT MU.USER_NAME
                          FROM MU_USER AS MU
                         WHERE MU.MU_USER_ID = MP.MU_USER_ID
                         LIMIT 0, 1)                                     AS userName
                      ,MPD.CRM_MEMBER_PAY_STAFF_NAME                     AS muMemberName
                      ,MP.ETC                                            AS etc
                      ,IFNULL(MPD.TOTAL_AMOUNT, 0)                       AS totalAmount
                      ,IFNULL(MPD.DISCOUNT_PRICE, 0)                     AS discountAmount
                      ,IFNULL(MPD.MEMBERSHIP_PRICE, 0)                   AS membershipPrice
                      ,0                                                 AS nonpaymentAmount
                      ,IFNULL(MPD.TOTAL_AMOUNT, 0)                       AS paymentAmount
                      ,IFNULL(MP.AMOUNT, 0)                              AS amount
                      ,IFNULL(MP.VAT, 0)                                 AS vat
                      ,0                                                 AS refundAmount
                      ,CASE
                       WHEN MP.STATUS = 'C'  THEN '결제취소'
                       WHEN MP.STATUS = 'R'  THEN '환불'
                       WHEN MP.STATUS = 'PY' THEN '부분결제(완료)'
                       WHEN MP.STATUS = 'NY' THEN '미수결제'
                       WHEN MP.STATUS = 'Y'  THEN '결제완료'
                       WHEN MP.STATUS = 'P'  THEN '부분결제'
                       WHEN MP.STATUS = 'NP' THEN '미수'
                       WHEN MP.STATUS = ''   THEN MP.CRM_MEMBER_PAY_PAY_DIVI1
                       ELSE MP.STATUS
                       END                                               AS status1
                      ,MP.REG_DATE                                       AS regDate
                  FROM MU_PAYMENT AS MP
            INNER JOIN MU_PAYMENT_DETAIL AS MPD
                    ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                   AND MP.USE_YN = 'Y'
                   AND MPD.USE_YN = 'Y'
                   AND MP.CRM_MEMBER_PAY_PID IS NOT NULL
                   AND MPD.CRM_MEMBER_PAY_PID IS NOT NULL
                <if test="paymentSearchStartDate != null and paymentSearchStartDate != '' and paymentSearchEndDate != null and paymentSearchEndDate != ''">
                   AND MP.WRITE_DATE &gt;= #{paymentSearchStartDate}
                   AND MP.WRITE_DATE &lt;= #{paymentSearchEndDate}
                </if>
                <if test="muUserId != null and muUserId != ''">
                   AND MP.MU_USER_ID = #{muUserId}
                </if>
          ) AS MP
         WHERE 1 = 1
        <if test="paymentStatusKeyword != null and paymentStatusKeyword != ''">
           AND status1 = #{paymentStatusKeyword}
        </if>
        </if>
        <choose>
        <when test="sort != null and sort != ''">
      ORDER BY ${sort}
       </when>
       <otherwise>
      ORDER BY paymentDate DESC
              ,regDate DESC
       </otherwise>
       </choose>
         LIMIT ${paymentStart}, ${paymentLimit}
<!--        SELECT *
          FROM (
                SELECT PAYMENT_DATE                                                         AS paymentDate
                      ,IFNULL(RESERVE_DATE, '-')                                            AS reserveDate
                      ,GROUP_CONCAT(MP.TREATMENT_PROCEDURE_NAME SEPARATOR ', ')             AS treatmentProcedureNm
                      ,GROUP_CONCAT(DISTINCT MP.TREATMENT_NAME)                             AS treatmentName
                      ,MP.ETC                                                               AS etc
                      ,IFNULL(GROUP_CONCAT(DISTINCT MP.PAYMENT_CATEGORY_ITEM_NAME),
                               IF(DISCOUNT_PRICE2=TOTAL_AMOUNT,'-','미납'))                  AS paymentCategoryItemName
                      ,GROUP_CONCAT(DISTINCT RESERVE_MEMBER_NAME SEPARATOR ', ')            AS reserveMemberName
                      ,MEMBER_NAME                                                          AS muMemberName
                      ,SUM(AMOUNT)                                                          AS amount
                      ,SUM(VAT)                                                             AS vat
                      ,SUM(TOTAL_AMOUNT)                                                    AS totalAmount
                      ,MEMBERSHIP_PRICE+REFUND_MEMBERSHIP_AMOUNT                            AS membershipPrice
                      ,DISCOUNT_PRICE2                                                      AS discountAmount
                      ,SUM(REFUND_AMOUNT)                                                   AS refundAmount
                      ,SUM(PAYMENT_AMOUNT)                                                  AS paymentAmount
                      ,PAYMENT_NUMBER                                                       AS paymentNumber
                      ,MU_USER_ID                                                           AS muUserId
                      ,USER_NAME                                                            AS userName
                      ,IFNULL((SELECT CASE
                                      WHEN MP2.STATUS = 'C'
                                      THEN '결제취소'
                                      WHEN MP2.STATUS = 'R'
                                      THEN '환불'
                                      WHEN MP2.STATUS = 'PY'
                                      THEN '부분결제(완료)'
                                      WHEN MP2.STATUS = 'NY'
                                      THEN '미수결제'
                                      WHEN MP2.STATUS = 'CN'
                                      THEN '재결제(취소)'
                                      WHEN MP2.STATUS = 'CY'
                                      THEN '재결제(완료)'
                                      WHEN MP2.STATUS = 'Y'
                                      THEN '결제완료'
                                      WHEN MP2.STATUS = 'P'
                                      THEN '부분결제'
                                      WHEN MP2.STATUS = 'NP'
                                      THEN '미수'
                                      ELSE ''
                                       END
                                 FROM MU_PAYMENT AS MP2
                                WHERE MP2.USE_YN = 'Y'
                                  AND MP2.PAYMENT_NUMBER = MP.PAYMENT_NUMBER
                             ORDER BY MP2.REG_DATE DESC
                                LIMIT 0, 1), '')                     AS status1
                      ,IFNULL(
                              IF((FIND_IN_SET('NY', STATUS_DESC1) = 0 AND FIND_IN_SET('NP', STATUS_DESC1) > 0) OR
                                 (FIND_IN_SET('PY', STATUS_DESC1) = 0 AND FIND_IN_SET('P', STATUS_DESC1) > 0)
                                 ,PAYMENT_AMOUNT-SUM(TOTAL_AMOUNT)+ DISCOUNT_PRICE2-MEMBERSHIP_PRICE
                                 ,0)
                              ,0)                                                           AS nonpaymentAmount
                      ,MAX(REG_DATE)                                                        AS regDate
                  FROM (
                        SELECT MAX(PAYMENT_DATE)                                                AS PAYMENT_DATE
                              ,RESERVE_DATE                                                      AS RESERVE_DATE
                              ,MP.ETC                                                            AS 'ETC'
                              ,GROUP_CONCAT(DISTINCT MP.TREATMENT_PROCEDURE_NAME SEPARATOR ', ') AS 'TREATMENT_PROCEDURE_NAME'
                              ,GROUP_CONCAT(DISTINCT MP.TREATMENT_NAME)                          AS 'TREATMENT_NAME'
                              ,STATUS_DESC1                                                      AS 'STATUS_DESC1'
                              ,GROUP_CONCAT(DISTINCT RESERVE_MEMBER_NAME SEPARATOR ', ')         AS 'RESERVE_MEMBER_NAME'
                              ,GROUP_CONCAT(DISTINCT IF(PAYMENT_CATEGORY_ITEM_NAME = ''
                                                     ,NULL
                                                     ,PAYMENT_CATEGORY_ITEM_NAME)
                                          ORDER BY MP.PAYMENT_CATEGORY_ITEM_NAME)              AS 'PAYMENT_CATEGORY_ITEM_NAME'
                              ,SUM(PRICE + VAT)                                                  AS TOTAL_AMOUNT
                              ,SUM(PAYMENT_VAT)                                                  AS 'VAT'
                              ,SUM(AMOUNT)                                                       AS 'AMOUNT'
                              ,DISCOUNT_PRICE2
                              ,IFNULL(SUM(MEMBERSHIP_PRICE),0)                                   AS 'MEMBERSHIP_PRICE'
                              ,SUM(REFUND_AMOUNT)                                                AS 'REFUND_AMOUNT'
                              ,SUM(REFUND_MEMBERSHIP_AMOUNT)                                     AS 'REFUND_MEMBERSHIP_AMOUNT'
                              ,IFNULL(SUM(PAYMENT_AMOUNT), 0)                                    AS 'PAYMENT_AMOUNT'
                              ,PAYMENT_NUMBER                                                    AS 'PAYMENT_NUMBER'
                              ,MU_USER_ID                                                        AS 'MU_USER_ID'
                              ,USER_NAME
                              ,REG_DATE
                              ,MEMBER_NAME
                         FROM (
                               SELECT MAX(PAYMENT_DATE)                                                     AS PAYMENT_DATE
                                     ,RESERVE_DATE
                                     ,TREATMENT_PROCEDURE_NAME                                              AS 'TREATMENT_PROCEDURE_NAME'
                                     ,IF(TREATMENT_NAME='멤버십','기타',TREATMENT_NAME)                       AS 'TREATMENT_NAME'
                                     ,GROUP_CONCAT(DISTINCT STATUS_DESC1 ORDER BY REG_DATE SEPARATOR ',')   AS 'STATUS_DESC1'
                                     ,RESERVE_MEMBER_NAME                                                   AS 'RESERVE_MEMBER_NAME'
                                     ,GROUP_CONCAT(DISTINCT IF(PAYMENT_CATEGORY_ITEM_NAME = ''
                                                            ,NULL
                                                            ,PAYMENT_CATEGORY_ITEM_NAME)
                                                ORDER BY MP.PAYMENT_CATEGORY_ITEM_NAME)                   AS 'PAYMENT_CATEGORY_ITEM_NAME'
                                     ,MEMBER_NAME
                                     ,GROUP_CONCAT(IF(MP.ETC = '', NULL, MP.ETC))                            AS ETC
                                     ,SUM(MEMBERSHIP_PRICE)                                                 AS 'MEMBERSHIP_PRICE'
                                     ,SUM(paymentAmount)                                                    AS 'PAYMENT_AMOUNT'
                                     ,SUM(AMOUNT)                                                           AS 'AMOUNT'
                                     ,SUM(VAT)                                                              AS 'PAYMENT_VAT'
                                     ,SUM(REFUND_AMOUNT)                                                    AS 'REFUND_AMOUNT'
                                     ,SUM(REFUND_MEMBERSHIP_AMOUNT)                                         AS 'REFUND_MEMBERSHIP_AMOUNT'
                                     ,DISCOUNT_PRICE2
                                     ,PAYMENT_NUMBER
                                     ,MU_TREATMENT_PROCEDURE_ID
                                     ,MU_USER_ID
                                     ,USER_NAME
                                     ,REG_DATE
                                     ,MU_RESERVE_ID
                                 FROM (
                                       SELECT MP.MU_USER_ID
                                             ,MU.USER_NAME
                                             ,IF(MP.STATUS='C' OR MP.STATUS='R'
                                                ,NULL
                                                ,DATE_FORMAT(MP.WRITE_DATE, '%Y-%m-%d'))        AS 'PAYMENT_DATE'
                                             ,DATE_FORMAT(MR.RESERVE_DATE, '%Y-%m-%d')            AS 'RESERVE_DATE'
                                             ,MPD.TREATMENT_NAME                                  AS 'TREATMENT_NAME'
                                             ,MPD.TREATMENT_PROCEDURE_NAME                        AS 'TREATMENT_PROCEDURE_NAME'
                                             ,MP.ETC                                              AS 'etc'
                                             ,IF(MP.STATUS = 'R' OR MP.STATUS = 'C'
                                                ,''
                                                ,PAYMENT_CATEGORY_ITEM_NAME)                    AS 'PAYMENT_CATEGORY_ITEM_NAME'
                                             ,MM.NAME                                             AS 'MEMBER_NAME'
                                             ,RM.NAME                                             AS 'RESERVE_MEMBER_NAME'
                                             ,MAX((SELECT SUM(MPD2.DISCOUNT_PRICE)
                                                     FROM MU_PAYMENT_DETAIL MPD2
                                                    WHERE MP.PAYMENT_NUMBER = MPD2.PAYMENT_NUMBER)) AS 'DISCOUNT_PRICE2'
                                             ,SUM(IF(MP.STATUS = 'R' OR MP.STATUS = 'C' OR MP.STATUS = 'CN'
                                                  ,MPD.TOTAL_AMOUNT
                                                  ,0))                                         AS 'REFUND_AMOUNT'
                                             ,SUM(IF(MP.STATUS = 'R' OR MP.STATUS = 'C' OR MP.STATUS = 'CN'
                                                  ,MPD.MEMBERSHIP_PRICE
                                                  ,0))                                         AS 'REFUND_MEMBERSHIP_AMOUNT'
                                             ,SUM(IF(MP.STATUS = 'R' OR MP.STATUS = 'C' OR MP.STATUS = 'CN', 0, MPD.TOTAL_AMOUNT))            AS 'paymentAmount'
                                             ,SUM(IF(MP.STATUS = 'R' OR MP.STATUS = 'C', 0, MPD.AMOUNT))                  AS 'AMOUNT'
                                             ,SUM(IF(MP.STATUS = 'R' OR MP.STATUS = 'C', 0, MPD.VAT))                     AS 'VAT'
                                             ,SUM(IF(MP.STATUS = 'R' OR MP.STATUS = 'C' OR MP.STATUS = 'CN', 0, MPD.MEMBERSHIP_PRICE))        AS 'MEMBERSHIP_PRICE'
                                             ,MP.STATUS                                                              AS 'STATUS_DESC1'
                                             ,MP.PAYMENT_NUMBER
                                             ,MPD.MU_TREATMENT_PROCEDURE_ID
                                             ,MP.REG_DATE
                                             ,MP.MU_PAYMENT_ID
                                             ,MPD.MU_RESERVE_ID
                                         FROM MU_PAYMENT MP
                                         JOIN MU_PAYMENT_DETAIL MPD
                                           ON MPD.MU_PAYMENT_ID = MP.MU_PAYMENT_ID
                                    LEFT JOIN MU_RESERVE MR
                                           ON MPD.MU_RESERVE_ID = MR.MU_RESERVE_ID
                                         JOIN MU_MEMBER MM
                                           ON MPD.MU_MEMBER_ID = MM.MU_MEMBER_ID
                                         JOIN MU_MEMBER RM
                                           ON MPD.RESERVE_MEMBER_ID = RM.MU_MEMBER_ID
                                         JOIN MU_USER MU
                                           ON MP.MU_USER_ID = MU.MU_USER_ID
                                         <if test="paymentSearchKeywordParam != null and paymentSearchKeywordParam != ''">
                                          AND ((MU.USER_NAME LIKE CONCAT('%',TRIM(#{paymentSearchKeywordParam}),'%'))
                                           OR (MPD.TREATMENT_PROCEDURE_NAME LIKE CONCAT('%',TRIM(#{paymentSearchKeywordParam}),'%')))
                                         </if>
                                         <if test="muUserId != null and muUserId != ''">
                                          AND MP.MU_USER_ID = #{muUserId}
                                         </if>
                                          AND MP.CRM_MEMBER_PAY_PID IS NULL
                                     GROUP BY MP.MU_USER_ID
                                             ,MU.USER_NAME
                                             ,MP.WRITE_DATE
                                             ,MR.WRITE_DATE
                                             ,MP.ETC
                                             ,PAYMENT_CATEGORY_ITEM_NAME
                                             ,MM.NAME
                                             ,RM.NAME
                                             ,REFUND_AMOUNT
                                             ,PAYMENT_AMOUNT
                                             ,MP.STATUS
                                             ,MP.PAYMENT_NUMBER
                                             ,MPD.MU_TREATMENT_PROCEDURE_ID
                                             ,MP.REG_DATE
                                             ,MU_PAYMENT_ID
                                             ,MU_RESERVE_ID
                                 ) AS MP
                             GROUP BY MEMBER_NAME
                                     ,DISCOUNT_PRICE2
                                     ,PAYMENT_NUMBER
                                     ,MU_TREATMENT_PROCEDURE_ID
                                     ,MU_USER_ID
                                     ,USER_NAME
                                     ,MU_RESERVE_ID
                               HAVING 1 = 1
                                <if test="paymentCategoryItemNm != null and paymentCategoryItemNm != ''">
                                  AND FIND_IN_SET( #{paymentCategoryItemNm},PAYMENT_CATEGORY_ITEM_NAME)>0
                                </if>
                         ) MP
                         JOIN (
                               SELECT PRICE
                                     ,VAT
                                     ,MU_TREATMENT_PROCEDURE_ID
                                 FROM (
                                       SELECT PRICE
                                             ,MTPP.VAT
                                             ,MTPP.MU_TREATMENT_PROCEDURE_ID
                                         FROM MU_TREATMENT_PROCEDURE_PRICE MTPP
                                         JOIN MU_PAYMENT_DETAIL MPD
                                           ON MTPP.MU_TREATMENT_PROCEDURE_ID = MPD.MU_TREATMENT_PROCEDURE_ID
                                    LEFT JOIN MU_RESERVE MR
                                           ON MPD.MU_RESERVE_ID = MR.MU_RESERVE_ID
                                         JOIN MU_PAYMENT MP
                                           ON MPD.MU_PAYMENT_ID = MP.MU_PAYMENT_ID
                                        WHERE MTPP.USE_YN='Y'
                                       <if test="muUserId != null and muUserId != ''">
                                          AND MP.MU_USER_ID = #{muUserId}
                                       </if>
                                    GROUP BY MTPP.MU_TREATMENT_PROCEDURE_ID
                                 ) AS MTPP
                         ) AS MTPP
                           ON MP.MU_TREATMENT_PROCEDURE_ID = MTPP.MU_TREATMENT_PROCEDURE_ID
                        <if test="paymentSearchStartDate != null and paymentSearchStartDate != '' and paymentSearchEndDate != null and paymentSearchEndDate != ''">
                          AND (DATE_FORMAT(PAYMENT_DATE, '%Y-%m-%d') BETWEEN #{paymentSearchStartDate} AND #{paymentSearchEndDate})
                        </if>
                     GROUP BY MP.ETC
                             ,RESERVE_DATE
                             ,MEMBER_NAME
                             ,DISCOUNT_PRICE2
                             ,PAYMENT_NUMBER
                             ,MU_USER_ID
                             ,USER_NAME
                             ,STATUS_DESC1
                             ,REG_DATE
                             ,MU_RESERVE_ID
                       HAVING 1 = 1
                        <if test="muTreatmentId != null and muTreatmentId != ''and treatmentName == '기타'">
                          AND FIND_IN_SET( '비만',TREATMENT_NAME)=0
                          AND FIND_IN_SET( '피부',TREATMENT_NAME)=0
                          AND FIND_IN_SET( '쁘띠',TREATMENT_NAME)=0
                        </if>
                        <if test="muTreatmentId != null and muTreatmentId != '' and treatmentName != '기타'">
                          AND FIND_IN_SET( #{treatmentName},TREATMENT_NAME)>0
                        </if>
                  ) AS MP
              GROUP BY ETC
                       ,MEMBER_NAME
                       ,PAYMENT_NUMBER
                       ,MU_USER_ID
                       ,USER_NAME
          ) AS MP
         WHERE 1 = 1
        <if test="paymentStatusKeyword != null and paymentStatusKeyword != ''">
           AND status1 = #{paymentStatusKeyword}
        </if>

        <if test="muUserId != null and muUserId != ''">
     UNION ALL

        SELECT *
          FROM (
                SELECT DATE_FORMAT(MP.WRITE_DATE, '%Y-%m-%d')            AS paymentDate
                      ,DATE_FORMAT(MP.WRITE_DATE, '%Y-%m-%d')            AS reserveDate
                      ,CASE
                       WHEN MPD.CRM_MEMBER_PAY_M_DIVI4_NAME != ''
                       THEN CONCAT(MPD.CRM_MEMBER_PAY_M_DIVI2_NAME, '/', MPD.CRM_MEMBER_PAY_M_DIVI3_NAME, '/', MPD.CRM_MEMBER_PAY_M_DIVI4_NAME)
                       WHEN MPD.CRM_MEMBER_PAY_M_DIVI3_NAME != ''
                       THEN CONCAT(MPD.CRM_MEMBER_PAY_M_DIVI2_NAME, '/', MPD.CRM_MEMBER_PAY_M_DIVI3_NAME)
                       WHEN MPD.CRM_MEMBER_PAY_M_DIVI2_NAME != ''
                       THEN CONCAT(MPD.CRM_MEMBER_PAY_M_DIVI2_NAME)
                       ELSE ''
                        END                                              AS treatmentProcedureNm
                      ,MPD.TREATMENT_NAME                                AS treatmentName
                      ,MP.ETC                                            AS etc
                      ,MP.PAYMENT_CATEGORY_ITEM_NAME                     AS paymentCategoryItemName
                      ,MPD.CRM_MEMBER_PAY_DOCTOR_NAME                    AS reserveMemberName
                      ,MPD.CRM_MEMBER_PAY_STAFF_NAME                     AS muMemberName
                      ,IFNULL(MP.AMOUNT, 0)                              AS amount
                      ,IFNULL(MP.VAT, 0)                                 AS vat
                      ,IFNULL(MPD.TOTAL_AMOUNT, 0)                       AS totalAmount
                      ,IFNULL(MPD.MEMBERSHIP_PRICE, 0)                   AS membershipPrice
                      ,IFNULL(MPD.DISCOUNT_PRICE, 0)                     AS discountAmount
                      ,0                                                 AS refundAmount
                      ,IFNULL(MPD.TOTAL_AMOUNT, 0)                       AS paymentAmount
                      ,MP.PAYMENT_NUMBER                                 AS paymentNumber
                      ,MP.MU_USER_ID                                     AS muUserId
                      ,(SELECT MU.USER_NAME
                          FROM MU_USER AS MU
                         WHERE MU.MU_USER_ID = MP.MU_USER_ID
                         LIMIT 0, 1)                                     AS userName
                      ,CASE
                       WHEN MP.STATUS = 'C'
                       THEN '결제취소'
                       WHEN MP.STATUS = 'R'
                       THEN '환불'
                       WHEN MP.STATUS = 'PY'
                       THEN '부분결제(완료)'
                       WHEN MP.STATUS = 'NY'
                       THEN '미수결제'
                       WHEN MP.STATUS = 'Y'
                       THEN '결제완료'
                       WHEN MP.STATUS = 'P'
                       THEN '부분결제'
                       WHEN MP.STATUS = 'NP'
                       THEN '미수'
                       WHEN MP.STATUS = ''
                       THEN MP.CRM_MEMBER_PAY_PAY_DIVI1
                       ELSE MP.STATUS
                       END                                               AS status1
                      ,0                                                 AS nonpaymentAmount
                      ,MP.REG_DATE                                       AS regDate
                  FROM MU_PAYMENT AS MP
            INNER JOIN MU_PAYMENT_DETAIL AS MPD
                    ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                   AND MP.USE_YN = 'Y'
                   AND MPD.USE_YN = 'Y'
                   <if test="muUserId != null and muUserId != ''">
                   AND MP.MU_USER_ID = #{muUserId}
                   </if>
                   AND MP.CRM_MEMBER_PAY_PID IS NOT NULL
          ) AS MP
         WHERE 1 = 1
        <if test="paymentStatusKeyword != null and paymentStatusKeyword != ''">
           AND status1=#{paymentStatusKeyword}
        </if>
        </if>
        <if test="paymentSearchStartDate != null and paymentSearchStartDate != '' and paymentSearchEndDate != null and paymentSearchEndDate != ''">
           AND (DATE_FORMAT(MP.paymentDate, '%Y-%m-%d') BETWEEN #{paymentSearchStartDate} AND #{paymentSearchEndDate})
        </if>
        <choose>
        <when test="sort != null and sort != ''">
      ORDER BY ${sort}
       </when>
       <otherwise>
      ORDER BY regDate DESC
       </otherwise>
       </choose>
         LIMIT ${paymentStart}, ${paymentLimit} -->
    </select>

    <select id="selectGroupByPayment" parameterType="hashmap" resultType="hashmap">
        SELECT MP.PAYMENT_CATEGORY_ITEM_ID
              ,MP.PAYMENT_CATEGORY_ITEM_NAME                                   AS paymentCategoryItemName
              ,SUM(MP.TOTAL_AMOUNT)                                            AS paymentAmount
              ,COUNT(DISTINCT MP.PAYMENT_NUMBER) AS count
          FROM (
                SELECT GROUP_CONCAT(DISTINCT IF(MPD.TREATMENT_NAME ='비만'
                                                OR MPD.TREATMENT_NAME = '쁘띠'
                                                OR MPD.TREATMENT_NAME = '제모'
                                                OR MPD.TREATMENT_NAME = '피부'
                                               ,MPD.TREATMENT_NAME
                                               ,'기타'))               AS TREATMENT_NAME
                      ,MP.PAYMENT_CATEGORY_ITEM_ID                    AS PAYMENT_CATEGORY_ITEM_ID
                      ,MP.PAYMENT_CATEGORY_ITEM_NAME                  AS PAYMENT_CATEGORY_ITEM_NAME
                      ,SUM(IF(MPD.TOTAL_AMOUNT>0,MPD.TOTAL_AMOUNT,0)) AS TOTAL_AMOUNT
                      ,MPD.PAYMENT_NUMBER                             AS PAYMENT_NUMBER
                      ,IFNULL((SELECT MP2.STATUS
                                 FROM MU_PAYMENT AS MP2
                                WHERE MP2.USE_YN = 'Y'
                                  AND MP2.PAYMENT_NUMBER = MP.PAYMENT_NUMBER
                               <if test="paymentSearchStartDate != null and paymentSearchStartDate != '' and paymentSearchEndDate != null and paymentSearchEndDate != ''">
                                  AND MP2.WRITE_DATE &gt;= #{paymentSearchStartDate}
                                  AND MP2.WRITE_DATE &lt;= #{paymentSearchEndDate}
                               </if>
                             ORDER BY MP2.REG_DATE DESC
                                LIMIT 0, 1), '')                        AS STATUS
                  FROM MU_PAYMENT AS MP
            INNER JOIN MU_PAYMENT_DETAIL AS MPD
                    ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                   AND MP.USE_YN = 'Y'
                   AND MPD.USE_YN = 'Y'
                   AND MP.PAYMENT_CATEGORY_ITEM_ID != ''
                   AND MP.PAYMENT_CATEGORY_ITEM_ID != 'MM'
                <if test="paymentSearchStartDate != null and paymentSearchStartDate != '' and paymentSearchEndDate != null and paymentSearchEndDate != ''">
                   AND MP.WRITE_DATE &gt;= #{paymentSearchStartDate}
                   AND MP.WRITE_DATE &lt;= #{paymentSearchEndDate}
                </if>
            INNER JOIN (
                        SELECT MP.PAYMENT_NUMBER
                          FROM MU_PAYMENT AS MP
                    INNER JOIN MU_PAYMENT_DETAIL AS MPD
                            ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                           AND MP.USE_YN = 'Y'
                           AND MPD.USE_YN = 'Y'
                           AND MP.CRM_MEMBER_PAY_PID IS NULL
                           AND MPD.CRM_MEMBER_PAY_PID IS NULL
                        <if test="paymentSearchStartDate != null and paymentSearchStartDate != '' and paymentSearchEndDate != null and paymentSearchEndDate != ''">
                           AND MP.WRITE_DATE &gt;= #{paymentSearchStartDate}
                           AND MP.WRITE_DATE &lt;= #{paymentSearchEndDate}
                        </if>
                        <if test="paymentSearchKeywordParam != null and paymentSearchKeywordParam != ''">
                           AND ((MU.USER_NAME LIKE CONCAT( '%', TRIM( #{paymentSearchKeywordParam} ), '%' ))
                            OR (MPD.TREATMENT_PROCEDURE_NAME LIKE CONCAT('%',TRIM(#{paymentSearchKeywordParam}),'%')))
                        </if>
                      GROUP BY MP.PAYMENT_NUMBER
                  ) AS MP2
                    ON MP.PAYMENT_NUMBER = MP2.PAYMENT_NUMBER
              GROUP BY MP.PAYMENT_CATEGORY_ITEM_ID
                      ,MP.PAYMENT_CATEGORY_ITEM_NAME
                      ,MPD.PAYMENT_NUMBER
          ) AS MP
         WHERE 1 = 1
        <if test="paymentStatus != null and paymentStatus != ''">
           AND MP.STATUS LIKE CONCAT('%',  #{paymentStatusKeyword}, '%')
        </if>
        <if test="paymentCategoryItemNm != null and paymentCategoryItemNm != ''">
           AND MP.PAYMENT_CATEGORY_ITEM_NAME LIKE CONCAT('%', #{paymentCategoryItemNm}, '%')
        </if>
        <if test="muTreatmentId != null and muTreatmentId != ''">
           AND MP.TREATMENT_NAME LIKE CONCAT('%', #{treatmentName}, '%')
        </if>
      GROUP BY MP.PAYMENT_CATEGORY_ITEM_ID
              ,MP.PAYMENT_CATEGORY_ITEM_NAME
<!--        SELECT PAYMENT_CATEGORY_ITEM_ID
              ,PAYMENT_CATEGORY_ITEM_NAME                                  AS paymentCategoryItemName
              ,SUM(TOTAL_AMOUNT)                                         AS paymentAmount
              ,COUNT(DISTINCT PAYMENT_NUMBER, MU_TREATMENT_PROCEDURE_ID) AS count
              ,MAX(WRITE_DATE)                                             AS writeDate
          FROM (
                SELECT MP.PAYMENT_CATEGORY_ITEM_ID
                      ,MP.PAYMENT_CATEGORY_ITEM_NAME
                      ,SUM(IF(MPD.TOTAL_AMOUNT>0,MPD.TOTAL_AMOUNT,0)) AS TOTAL_AMOUNT
                      ,MPD.PAYMENT_NUMBER
                      ,MP.MU_PAYMENT_ID
                      ,MU_TREATMENT_PROCEDURE_ID
                      ,IF(MP.STATUS='C' OR MP.STATUS='R' OR MP.STATUS='CN', NULL, DATE_FORMAT(MP.WRITE_DATE, '%Y-%m-%d')) AS WRITE_DATE
                      ,GROUP_CONCAT( STATUS ) AS status
                  FROM MU_PAYMENT_DETAIL MPD
                  JOIN MU_PAYMENT        MP
                    ON MPD.MU_PAYMENT_ID = MP.MU_PAYMENT_ID
                  JOIN MU_USER MU
                    ON MP.MU_USER_ID = MU.MU_USER_ID
                 WHERE MPD.USE_YN = 'Y'
                   AND MP.PAYMENT_CATEGORY_ITEM_ID != ''
                   AND MP.PAYMENT_CATEGORY_ITEM_ID != 'MM'
                  <if test="paymentStatus != null and paymentStatus != ''">
                   AND MPD.PAYMENT_NUMBER IN (
                                              SELECT PAYMENT_NUMBER
                                                FROM MU_PAYMENT
                                               WHERE STATUS = #{paymentStatus}
                                             )
                  </if>
                  <if test="paymentCategoryItemNm != null and paymentCategoryItemNm != ''">
                   AND PAYMENT_CATEGORY_ITEM_NAME=#{paymentCategoryItemNm}
                  </if>
                  <if test="muTreatmentId != null and muTreatmentId != '' and treatmentName != '기타'">
                   AND TREATMENT_NAME=#{treatmentName}
                  </if>
                  <if test="muTreatmentId != null and muTreatmentId != ''and treatmentName == '기타'">
                   AND FIND_IN_SET('비만', TREATMENT_NAME) = 0
                   AND FIND_IN_SET('피부', TREATMENT_NAME) = 0
                   AND FIND_IN_SET('쁘띠', TREATMENT_NAME) = 0
                  </if>
                  <if test="paymentSearchKeywordParam != null and paymentSearchKeywordParam != ''">
                   AND ((MU.USER_NAME LIKE CONCAT('%', TRIM(#{paymentSearchKeywordParam}), '%'))
                    OR (MPD.TREATMENT_PROCEDURE_NAME LIKE CONCAT('%',TRIM(#{paymentSearchKeywordParam}),'%')))
                  </if>
                   AND MP.CRM_MEMBER_PAY_PID IS NULL
              GROUP BY MPD.MU_TREATMENT_ID
                      ,MPD.TREATMENT_NAME
                      ,MPD.PAYMENT_NUMBER
                      ,MU_TREATMENT_PROCEDURE_ID
                      ,MU_PAYMENT_ID
                      ,WRITE_DATE
                HAVING 1 = 1
                <if test="paymentSearchStartDate != null and paymentSearchStartDate != '' and paymentSearchEndDate != null and paymentSearchEndDate != ''">
                   AND (DATE_FORMAT(WRITE_DATE, '%Y-%m-%d') BETWEEN #{paymentSearchStartDate} AND #{paymentSearchEndDate})
                </if>
          ) AS MTD
         WHERE 1=1
        <if test="paymentStatus != null and paymentStatus != '' and !paymentStatus.equals('C')">
           AND FIND_IN_SET('C', status) = 0
        </if>
        <if test="paymentStatus != null and paymentStatus != '' and !paymentStatus.equals('R')">
           AND FIND_IN_SET('R', status) = 0
        </if>
        <if test="paymentStatus != null and paymentStatus != '' and !paymentStatus.equals('NP')">
           AND FIND_IN_SET('NP', status) = 0
        </if>
      GROUP BY PAYMENT_CATEGORY_ITEM_ID
              ,PAYMENT_CATEGORY_ITEM_NAME -->
    </select>

    <select id="selectGroupByMembership" parameterType="hashmap" resultType="hashmap">
        SELECT MP.PAYMENT_CATEGORY_ITEM_NAME                                   AS paymentCategoryItemName
              ,SUM(MP.MEMBERSHIP_PRICE)                                        AS paymentAmount
              ,COUNT(DISTINCT MP.PAYMENT_NUMBER, MP.MU_TREATMENT_PROCEDURE_ID) AS count
          FROM (
                SELECT MP.TREATMENT_NAME                 AS TREATMENT_NAME
                      ,MP.PAYMENT_CATEGORY_ITEM_NAME     AS PAYMENT_CATEGORY_ITEM_NAME
                      ,SUM(MP.MEMBERSHIP_PRICE)          AS MEMBERSHIP_PRICE
                      ,MP.PAYMENT_NUMBER                 AS PAYMENT_NUMBER
                      ,MP.MU_TREATMENT_PROCEDURE_ID      AS MU_TREATMENT_PROCEDURE_ID
                      ,GROUP_CONCAT(DISTINCT MP.STATUS)  AS STATUS
                  FROM (
                        SELECT IF(MPD.TREATMENT_NAME ='비만'
                                  OR MPD.TREATMENT_NAME = '쁘띠'
                                  OR MPD.TREATMENT_NAME = '제모'
                                  OR MPD.TREATMENT_NAME = '피부'
                                 ,MPD.TREATMENT_NAME
                                 ,'기타')                                      AS TREATMENT_NAME
                              ,IF(MP.STATUS IN ('C', 'CN'), '취소적립', '사용차감')  AS PAYMENT_CATEGORY_ITEM_NAME
                              ,IFNULL(MPD.MEMBERSHIP_PRICE, 0)               AS MEMBERSHIP_PRICE
                              ,MPD.PAYMENT_NUMBER                             AS PAYMENT_NUMBER
                              ,MPD.MU_TREATMENT_PROCEDURE_ID                  AS MU_TREATMENT_PROCEDURE_ID
                              ,IFNULL((SELECT MP2.STATUS
                                         FROM MU_PAYMENT AS MP2
                                        WHERE MP2.USE_YN = 'Y'
                                          AND MP2.PAYMENT_NUMBER = MP.PAYMENT_NUMBER
                                       <if test="paymentSearchStartDate != null and paymentSearchStartDate != '' and paymentSearchEndDate != null and paymentSearchEndDate != ''">
                                          AND MP2.WRITE_DATE &gt;= #{paymentSearchStartDate}
                                          AND MP2.WRITE_DATE &lt;= #{paymentSearchEndDate}
                                       </if>
                                     ORDER BY MP2.REG_DATE DESC
                                        LIMIT 0, 1), '')                        AS STATUS
                          FROM MU_PAYMENT AS MP
                    INNER JOIN MU_PAYMENT_DETAIL AS MPD
                            ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                           AND MP.USE_YN = 'Y'
                           AND MPD.USE_YN = 'Y'
                           AND MP.PAYMENT_CATEGORY_ITEM_ID = 'MM'
                        <if test="paymentSearchStartDate != null and paymentSearchStartDate != '' and paymentSearchEndDate != null and paymentSearchEndDate != ''">
                           AND MP.WRITE_DATE &gt;= #{paymentSearchStartDate}
                           AND MP.WRITE_DATE &lt;= #{paymentSearchEndDate}
                        </if>
                    INNER JOIN (
                                SELECT MP.PAYMENT_NUMBER
                                  FROM MU_PAYMENT AS MP
                            INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                    ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                   AND MP.USE_YN = 'Y'
                                   AND MPD.USE_YN = 'Y'
                                   AND MP.CRM_MEMBER_PAY_PID IS NULL
                                   AND MPD.CRM_MEMBER_PAY_PID IS NULL
                                <if test="paymentSearchStartDate != null and paymentSearchStartDate != '' and paymentSearchEndDate != null and paymentSearchEndDate != ''">
                                   AND MP.WRITE_DATE &gt;= #{paymentSearchStartDate}
                                   AND MP.WRITE_DATE &lt;= #{paymentSearchEndDate}
                                </if>
                                <if test="paymentSearchKeywordParam != null and paymentSearchKeywordParam != ''">
                                   AND ((MU.USER_NAME LIKE CONCAT( '%', TRIM( #{paymentSearchKeywordParam} ), '%' ))
                                    OR (MPD.TREATMENT_PROCEDURE_NAME LIKE CONCAT('%',TRIM(#{paymentSearchKeywordParam}),'%')))
                                </if>
                              GROUP BY MP.PAYMENT_NUMBER
                          ) AS MP2
                            ON MP.PAYMENT_NUMBER = MP2.PAYMENT_NUMBER
                  ) AS MP
                 WHERE 1 = 1
              GROUP BY MP.PAYMENT_CATEGORY_ITEM_NAME
                      ,MP.PAYMENT_NUMBER
                      ,MP.MU_TREATMENT_PROCEDURE_ID
          ) AS MP
         WHERE 1 = 1
        <if test="paymentStatus != null and paymentStatus != ''">
           AND MP.STATUS LIKE CONCAT('%',  #{paymentStatusKeyword}, '%')
        </if>
        <if test="paymentCategoryItemNm != null and paymentCategoryItemNm != ''">
           AND MP.PAYMENT_CATEGORY_ITEM_NAME LIKE CONCAT('%', #{paymentCategoryItemNm}, '%')
        </if>
        <if test="muTreatmentId != null and muTreatmentId != ''">
           AND MP.TREATMENT_NAME LIKE CONCAT('%', #{treatmentName}, '%')
        </if>
      GROUP BY MP.PAYMENT_CATEGORY_ITEM_NAME
<!--        SELECT PAYMENT_CATEGORY_ITEM_ID
              ,PAYMENT_CATEGORY_ITEM_NAME                                  AS paymentCategoryItemName
              ,SUM( MEMBERSHIP_PRICE )                                         AS paymentAmount
              ,COUNT( DISTINCT PAYMENT_NUMBER, MU_TREATMENT_PROCEDURE_ID ) AS count
              ,MAX(WRITE_DATE)                                             AS writeDate
          FROM (
                SELECT MP.PAYMENT_CATEGORY_ITEM_ID
                      ,IF(STATUS='C' OR STATUS='CN','취소적립','사용차감')  AS PAYMENT_CATEGORY_ITEM_NAME
                      ,IFNULL(MPD.MEMBERSHIP_PRICE,0)               AS 'MEMBERSHIP_PRICE'
                      ,SUM(IF(MPD.TOTAL_AMOUNT>0,MPD.TOTAL_AMOUNT,0)) AS TOTAL_AMOUNT
                      ,MPD.PAYMENT_NUMBER
                      ,MP.MU_PAYMENT_ID
                      ,MU_TREATMENT_PROCEDURE_ID
                      ,DATE_FORMAT(MP.WRITE_DATE, '%Y-%m-%d') AS WRITE_DATE
                      ,GROUP_CONCAT( STATUS ) AS status
                  FROM MU_PAYMENT_DETAIL MPD
                  JOIN MU_PAYMENT        MP
                    ON MPD.MU_PAYMENT_ID = MP.MU_PAYMENT_ID
                  JOIN MU_USER MU
                    ON MP.MU_USER_ID = MU.MU_USER_ID
                 WHERE MPD.USE_YN = 'Y'
                   AND MP.PAYMENT_CATEGORY_ITEM_ID ='MM'
                  <if test="paymentStatus != null and paymentStatus != ''">
                   AND MPD.PAYMENT_NUMBER IN (
                                              SELECT PAYMENT_NUMBER
                                                FROM MU_PAYMENT
                                               WHERE STATUS = #{paymentStatus}
                                             )
                  </if>
                  <if test="paymentCategoryItemNm != null and paymentCategoryItemNm != ''">
                   AND PAYMENT_CATEGORY_ITEM_NAME=#{paymentCategoryItemNm}
                  </if>

                  <if test="paymentSearchKeywordParam != null and paymentSearchKeywordParam != ''">
                   AND ((MU.USER_NAME LIKE CONCAT( '%', TRIM( #{paymentSearchKeywordParam} ), '%' ))
                    OR (MPD.TREATMENT_PROCEDURE_NAME LIKE CONCAT('%',TRIM(#{paymentSearchKeywordParam}),'%')))
                  </if>
                  <if test="muTreatmentId != null and muTreatmentId != '' and treatmentName != '기타'">
                   AND TREATMENT_NAME=#{treatmentName}
                  </if>
                  <if test="muTreatmentId != null and muTreatmentId != ''and treatmentName == '기타'">
                   AND FIND_IN_SET( '비만',TREATMENT_NAME)=0
                   AND FIND_IN_SET( '피부',TREATMENT_NAME)=0
                   AND FIND_IN_SET( '쁘띠',TREATMENT_NAME)=0
                 </if>
                   AND MP.CRM_MEMBER_PAY_PID IS NULL
              GROUP BY MPD.MU_TREATMENT_ID
                      ,MPD.TREATMENT_NAME
                      ,MPD.PAYMENT_NUMBER
                      ,MU_TREATMENT_PROCEDURE_ID
                      ,MU_PAYMENT_ID
                      ,WRITE_DATE
                HAVING 1 = 1
                <if test="paymentSearchStartDate != null and paymentSearchStartDate != '' and paymentSearchEndDate != null and paymentSearchEndDate != ''">
                   AND (DATE_FORMAT(WRITE_DATE, '%Y-%m-%d') BETWEEN #{paymentSearchStartDate} AND #{paymentSearchEndDate})
                </if>
          ) AS MTD
         WHERE 1=1
        <if test="paymentStatus != null and paymentStatus != '' and !paymentStatus.equals('C')">
           AND FIND_IN_SET('C', status) = 0
        </if>
        <if test="paymentStatus != null and paymentStatus != '' and !paymentStatus.equals('R')">
           AND FIND_IN_SET('R', status) = 0
        </if>
        <if test="paymentStatus != null and paymentStatus != '' and !paymentStatus.equals('NP')">
           AND FIND_IN_SET('NP', status) = 0
        </if>
      GROUP BY PAYMENT_CATEGORY_ITEM_ID
              ,PAYMENT_CATEGORY_ITEM_NAME -->
    </select>

    <select id="selectGroupByTreatment" parameterType="hashmap" resultType="hashmap">
        SELECT MP.TREATMENT_NAME                                                                 AS paymentCategoryItemName
              ,SUM(MP.TOTAL_AMOUNT)                                                              AS paymentAmount
              ,COUNT(DISTINCT MP.PAYMENT_NUMBER, MP.MU_TREATMENT_PROCEDURE_ID, MP.MU_RESERVE_ID) AS count
          FROM (
                SELECT IF(MPD.TREATMENT_NAME ='비만'
                          OR MPD.TREATMENT_NAME = '쁘띠'
                          OR MPD.TREATMENT_NAME = '제모'
                          OR MPD.TREATMENT_NAME = '피부'
                         ,MPD.TREATMENT_NAME
                         ,'기타')                                            AS TREATMENT_NAME
                      ,GROUP_CONCAT(DISTINCT MP.PAYMENT_CATEGORY_ITEM_NAME) AS PAYMENT_CATEGORY_ITEM_NAME
                      ,SUM(IF(MPD.TOTAL_AMOUNT>0, MPD.TOTAL_AMOUNT, 0))     AS TOTAL_AMOUNT
                      ,MPD.PAYMENT_NUMBER                                   AS PAYMENT_NUMBER
                      ,MPD.MU_TREATMENT_PROCEDURE_ID                        AS MU_TREATMENT_PROCEDURE_ID
                      ,IFNULL((SELECT MP2.STATUS
                                 FROM MU_PAYMENT AS MP2
                                WHERE MP2.USE_YN = 'Y'
                                  AND MP2.PAYMENT_NUMBER = MP.PAYMENT_NUMBER
                               <if test="paymentSearchStartDate != null and paymentSearchStartDate != '' and paymentSearchEndDate != null and paymentSearchEndDate != ''">
                                  AND MP2.WRITE_DATE &gt;= #{paymentSearchStartDate}
                                  AND MP2.WRITE_DATE &lt;= #{paymentSearchEndDate}
                               </if>
                             ORDER BY MP2.REG_DATE DESC
                                LIMIT 0, 1), '')                            AS STATUS
                      ,IFNULL(MPD.MU_RESERVE_ID, '')                        AS MU_RESERVE_ID
                  FROM MU_PAYMENT AS MP
            INNER JOIN MU_PAYMENT_DETAIL AS MPD
                    ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                   AND MP.USE_YN = 'Y'
                   AND MPD.USE_YN = 'Y'
                <if test="paymentSearchStartDate != null and paymentSearchStartDate != '' and paymentSearchEndDate != null and paymentSearchEndDate != ''">
                   AND MP.WRITE_DATE &gt;= #{paymentSearchStartDate}
                   AND MP.WRITE_DATE &lt;= #{paymentSearchEndDate}
                </if>
            INNER JOIN (
                        SELECT MP.PAYMENT_NUMBER
                          FROM MU_PAYMENT AS MP
                    INNER JOIN MU_PAYMENT_DETAIL AS MPD
                            ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                           AND MP.USE_YN = 'Y'
                           AND MPD.USE_YN = 'Y'
                           AND MP.CRM_MEMBER_PAY_PID IS NULL
                           AND MPD.CRM_MEMBER_PAY_PID IS NULL
                        <if test="paymentSearchStartDate != null and paymentSearchStartDate != '' and paymentSearchEndDate != null and paymentSearchEndDate != ''">
                           AND MP.WRITE_DATE &gt;= #{paymentSearchStartDate}
                           AND MP.WRITE_DATE &lt;= #{paymentSearchEndDate}
                        </if>
                        <if test="paymentSearchKeywordParam != null and paymentSearchKeywordParam != ''">
                           AND ((MU.USER_NAME LIKE CONCAT( '%', TRIM( #{paymentSearchKeywordParam} ), '%' ))
                            OR (MPD.TREATMENT_PROCEDURE_NAME LIKE CONCAT('%',TRIM(#{paymentSearchKeywordParam}),'%')))
                        </if>
                      GROUP BY MP.PAYMENT_NUMBER
                  ) AS MP2
                    ON MP.PAYMENT_NUMBER = MP2.PAYMENT_NUMBER
              GROUP BY MPD.PAYMENT_NUMBER
                      ,MPD.TREATMENT_NAME
                      ,MPD.MU_TREATMENT_PROCEDURE_ID
                      ,MPD.MU_RESERVE_ID
          ) AS MP
         WHERE 1 = 1
        <if test="paymentStatus != null and paymentStatus != ''">
           AND MP.STATUS LIKE CONCAT('%',  #{paymentStatusKeyword}, '%')
        </if>
        <if test="paymentCategoryItemNm != null and paymentCategoryItemNm != ''">
           AND MP.PAYMENT_CATEGORY_ITEM_NAME LIKE CONCAT('%', #{paymentCategoryItemNm}, '%')
        </if>
        <if test="muTreatmentId != null and muTreatmentId != ''">
           AND MP.TREATMENT_NAME LIKE CONCAT('%', #{treatmentName}, '%')
        </if>
      GROUP BY MP.TREATMENT_NAME
<!--        SELECT IF(TREATMENT_NAME ='비만'
                  OR TREATMENT_NAME = '쁘띠'
                  OR TREATMENT_NAME = '제모'
                  OR TREATMENT_NAME = '피부'
                 ,TREATMENT_NAME
                 ,'기타')                                                 AS paymentCategoryItemName
              ,SUM( TOTAL_AMOUNT )                                         AS paymentAmount
              ,COUNT( DISTINCT PAYMENT_NUMBER, MU_TREATMENT_PROCEDURE_ID, MU_RESERVE_ID ) AS count
              ,MAX(WRITE_DATE)                                             AS writeDate
          FROM (
                SELECT MPD.MU_TREATMENT_ID
                      ,MPD.TREATMENT_NAME
                      ,SUM(IF(MPD.TOTAL_AMOUNT>0, MPD.TOTAL_AMOUNT, 0)) AS TOTAL_AMOUNT
                      ,MPD.PAYMENT_NUMBER
                      ,MP.MU_PAYMENT_ID
                      ,MU_TREATMENT_PROCEDURE_ID
                      ,IF(MP.STATUS='C' OR MP.STATUS='R' OR MP.STATUS='CN', NULL, DATE_FORMAT(MP.WRITE_DATE, '%Y-%m-%d')) AS WRITE_DATE
                      ,GROUP_CONCAT( STATUS ) AS status
                      ,IFNULL(MPD.MU_RESERVE_ID, '')    AS MU_RESERVE_ID
                  FROM MU_PAYMENT_DETAIL AS MPD
                  JOIN MU_PAYMENT AS MP
                    ON MPD.MU_PAYMENT_ID = MP.MU_PAYMENT_ID
                  JOIN MU_USER AS MU
                    ON MP.MU_USER_ID = MU.MU_USER_ID
                 WHERE MPD.USE_YN = 'Y'
                  <if test="paymentStatus != null and paymentStatus != ''">
                   AND MPD.PAYMENT_NUMBER IN (
                                              SELECT PAYMENT_NUMBER
                                                FROM MU_PAYMENT
                                               WHERE STATUS = #{paymentStatus}
                                              )
                  </if>
                  <if test="paymentCategoryItemNm != null and paymentCategoryItemNm != ''">
                   AND PAYMENT_CATEGORY_ITEM_NAME=#{paymentCategoryItemNm}
                  </if>
                  <if test="paymentSearchKeywordParam != null and paymentSearchKeywordParam != ''">
                   AND ((MU.USER_NAME LIKE CONCAT( '%', TRIM( #{paymentSearchKeywordParam} ), '%' ))
                    OR (MPD.TREATMENT_PROCEDURE_NAME LIKE CONCAT('%',TRIM(#{paymentSearchKeywordParam}),'%')))
                  </if>
                   AND MP.CRM_MEMBER_PAY_PID IS NULL
              GROUP BY MPD.MU_TREATMENT_ID
                      ,MPD.TREATMENT_NAME
                      ,MPD.PAYMENT_NUMBER
                      ,MU_TREATMENT_PROCEDURE_ID
                      ,MU_PAYMENT_ID
                      ,WRITE_DATE
                      ,MU_RESERVE_ID
                HAVING 1=1
                <if test="paymentSearchStartDate != null and paymentSearchStartDate != '' and paymentSearchEndDate != null and paymentSearchEndDate != ''">
                   AND (DATE_FORMAT(WRITE_DATE, '%Y-%m-%d') BETWEEN #{paymentSearchStartDate} AND #{paymentSearchEndDate})
                </if>
          ) AS MTD
         WHERE 1=1
        <if test="paymentStatus != null and paymentStatus != '' and !paymentStatus.equals('C')">
           AND FIND_IN_SET('C', status) = 0
        </if>
        <if test="paymentStatus != null and paymentStatus != '' and !paymentStatus.equals('R')">
           AND FIND_IN_SET('R', status) = 0
        </if>
        <if test="paymentStatus != null and paymentStatus != '' and !paymentStatus.equals('NP')">
           AND FIND_IN_SET('NP', status) = 0
        </if>
      GROUP BY paymentCategoryItemName
        HAVING 1 = 1
           <if test="treatmentName != null and treatmentName != ''">
           AND paymentCategoryItemName=#{treatmentName}
           </if>-->
    </select>

    <select id="selectTotalAmount" parameterType="hashmap" resultType="hashmap">
        SELECT SUM(MP.TOTAL_AMOUNT)   AS totalAmount
              ,SUM(MP.AMOUNT)         AS amount
              ,SUM(MP.VAT)            AS vat
              ,GROUP_CONCAT(MP.PAYMENT_CATEGORY_ITEM_NAME)  AS PAYMENT_CATEGORY_ITEM_NAME
          FROM (
                SELECT IF(MPD.TREATMENT_NAME ='비만'
                          OR MPD.TREATMENT_NAME = '쁘띠'
                          OR MPD.TREATMENT_NAME = '제모'
                          OR MPD.TREATMENT_NAME = '피부'
                         ,MPD.TREATMENT_NAME
                         ,'기타')                                        AS TREATMENT_NAME
                      ,IFNULL(MPD.TOTAL_AMOUNT, 0)  AS TOTAL_AMOUNT
                      ,IFNULL(MPD.AMOUNT, 0)  AS AMOUNT
                      ,IFNULL(MPD.VAT, 0)  AS VAT
                      ,MPD.PAYMENT_NUMBER                               AS PAYMENT_NUMBER
                      ,MPD.MU_TREATMENT_PROCEDURE_ID                    AS MU_TREATMENT_PROCEDURE_ID
                      ,IFNULL((SELECT MP2.STATUS
                                 FROM MU_PAYMENT AS MP2
                                WHERE MP2.USE_YN = 'Y'
                                  AND MP2.PAYMENT_NUMBER = MP.PAYMENT_NUMBER
                               <if test="paymentSearchStartDate != null and paymentSearchStartDate != '' and paymentSearchEndDate != null and paymentSearchEndDate != ''">
                                  AND MP2.WRITE_DATE &gt;= #{paymentSearchStartDate}
                                  AND MP2.WRITE_DATE &lt;= #{paymentSearchEndDate}
                               </if>
                             ORDER BY MP2.REG_DATE DESC
                                LIMIT 0, 1), '')                        AS STATUS
                      ,MP.PAYMENT_CATEGORY_ITEM_NAME                    AS PAYMENT_CATEGORY_ITEM_NAME
                  FROM MU_PAYMENT AS MP
            INNER JOIN MU_PAYMENT_DETAIL AS MPD
                    ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                   AND MP.USE_YN = 'Y'
                   AND MPD.USE_YN = 'Y'
                <if test="paymentSearchStartDate != null and paymentSearchStartDate != '' and paymentSearchEndDate != null and paymentSearchEndDate != ''">
                   AND MP.WRITE_DATE &gt;= #{paymentSearchStartDate}
                   AND MP.WRITE_DATE &lt;= #{paymentSearchEndDate}
                </if>
            INNER JOIN (
                        SELECT MP.PAYMENT_NUMBER
                          FROM MU_PAYMENT AS MP
                    INNER JOIN MU_PAYMENT_DETAIL AS MPD
                            ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                           AND MP.USE_YN = 'Y'
                           AND MPD.USE_YN = 'Y'
                           AND MP.CRM_MEMBER_PAY_PID IS NULL
                           AND MPD.CRM_MEMBER_PAY_PID IS NULL
                        <if test="paymentSearchStartDate != null and paymentSearchStartDate != '' and paymentSearchEndDate != null and paymentSearchEndDate != ''">
                           AND MP.WRITE_DATE &gt;= #{paymentSearchStartDate}
                           AND MP.WRITE_DATE &lt;= #{paymentSearchEndDate}
                        </if>
                        <if test="paymentSearchKeywordParam != null and paymentSearchKeywordParam != ''">
                           AND ((MU.USER_NAME LIKE CONCAT( '%', TRIM( #{paymentSearchKeywordParam} ), '%' ))
                            OR (MPD.TREATMENT_PROCEDURE_NAME LIKE CONCAT('%',TRIM(#{paymentSearchKeywordParam}),'%')))
                        </if>
                      GROUP BY MP.PAYMENT_NUMBER
                  ) AS MP2
                    ON MP.PAYMENT_NUMBER = MP2.PAYMENT_NUMBER
          ) AS MP
         WHERE 1 = 1
        <if test="paymentStatus != null and paymentStatus != ''">
           AND MP.STATUS LIKE CONCAT('%',  #{paymentStatusKeyword}, '%')
        </if>
        <if test="paymentCategoryItemNm != null and paymentCategoryItemNm != ''">
           AND MP.PAYMENT_CATEGORY_ITEM_NAME LIKE CONCAT('%', #{paymentCategoryItemNm}, '%')
        </if>
        <if test="muTreatmentId != null and muTreatmentId != ''">
           AND MP.TREATMENT_NAME LIKE CONCAT('%', #{treatmentName}, '%')
        </if>
<!--        SELECT SUM(DETAIL_AMOUNT)  AS 'amount'
              ,SUM(DETAIL_VAT)     AS 'vat'
              ,SUM(TOTAL_AMOUNT)   AS 'totalAmount'
              ,MAX(WRITE_DATE)     AS writeDate
          FROM (
                SELECT SUM(IFNULL(MPD.TOTAL_AMOUNT,0))                                                AS 'TOTAL_AMOUNT'
                      ,SUM(IFNULL(MPD.AMOUNT,0))                                                      AS 'DETAIL_AMOUNT'
                      ,SUM(IFNULL(MPD.VAT,0))                                                         AS 'DETAIL_VAT'
                      ,MPD.PAYMENT_NUMBER
                      ,MP.MU_PAYMENT_ID
                      ,MP.WRITE_DATE
                      ,GROUP_CONCAT( STATUS ) AS status
                  FROM MU_PAYMENT_DETAIL MPD
                  JOIN MU_PAYMENT        MP
                    ON MPD.MU_PAYMENT_ID = MP.MU_PAYMENT_ID
                  JOIN MU_USER MU
                    ON MP.MU_USER_ID = MU.MU_USER_ID
                 WHERE MPD.USE_YN = 'Y'
                  <if test="paymentStatus != null and paymentStatus != ''">
                   AND MPD.PAYMENT_NUMBER IN (
                                              SELECT PAYMENT_NUMBER
                                                FROM MU_PAYMENT
                                               WHERE STATUS = #{paymentStatus}
                                             )
                  </if>
                  <if test="paymentCategoryItemNm != null and paymentCategoryItemNm != ''">
                   AND PAYMENT_CATEGORY_ITEM_NAME=#{paymentCategoryItemNm}
                  </if>
                  <if test="muTreatmentId != null and muTreatmentId != '' and treatmentName != '기타'">
                   AND TREATMENT_NAME=#{treatmentName}
                  </if>
                  <if test="muTreatmentId != null and muTreatmentId != ''and treatmentName == '기타'">
                   AND FIND_IN_SET( '비만',TREATMENT_NAME)=0
                   AND FIND_IN_SET( '피부',TREATMENT_NAME)=0
                   AND FIND_IN_SET( '쁘띠',TREATMENT_NAME)=0
                  </if>
                  <if test="paymentSearchKeywordParam != null and paymentSearchKeywordParam != ''">
                   AND ((MU.USER_NAME LIKE CONCAT( '%', TRIM( #{paymentSearchKeywordParam} ), '%' ))
                    OR (MPD.TREATMENT_PROCEDURE_NAME LIKE CONCAT('%',TRIM(#{paymentSearchKeywordParam}),'%')))
                  </if>
                   AND MP.CRM_MEMBER_PAY_PID IS NULL
              GROUP BY MPD.MU_TREATMENT_ID
                      ,MPD.TREATMENT_NAME
                      ,MPD.TOTAL_AMOUNT
                      ,MPD.PAYMENT_NUMBER
                      ,MU_PAYMENT_ID
                      ,WRITE_DATE
                HAVING 1 = 1
                <if test="paymentSearchStartDate != null and paymentSearchStartDate != '' and paymentSearchEndDate != null and paymentSearchEndDate != ''">
                   AND (DATE_FORMAT(WRITE_DATE, '%Y-%m-%d') BETWEEN #{paymentSearchStartDate} AND #{paymentSearchEndDate})
                </if>
          ) AS MTD -->
    </select>

    <select id="selectPaymentListExcel" parameterType="hashmap" resultType="hashmap">
         SELECT *
         FROM
         (
          SELECT PAYMENT_DATE                                                               AS paymentDate
                ,IFNULL(RESERVE_DATE, '-')                                                  AS reserveDate
                ,GROUP_CONCAT(DISTINCT MP.TREATMENT_PROCEDURE_NAME SEPARATOR ', ')          AS treatmentProcedureNm
                ,GROUP_CONCAT(DISTINCT MP.TREATMENT_NAME)                                   AS treatmentName
                ,MP.ETC                                                                     AS etc
                ,IFNULL(GROUP_CONCAT(DISTINCT MP.PAYMENT_CATEGORY_ITEM_NAME SEPARATOR ', '),
                        '미납')                                                              AS paymentCategoryItemName
                ,GROUP_CONCAT(DISTINCT RESERVE_MEMBER_NAME SEPARATOR ', ')                  AS reserveMemberName
                ,MEMBER_NAME                                                                AS muMemberName
                ,AMOUNT                                                                     AS amount
                ,VAT                                                                        AS vat
                ,SUM(TOTAL_AMOUNT)                                                          AS totalAmount
                ,MEMBERSHIP_PRICE                                                           AS membershipPrice
                ,DISCOUNT_PRICE2                                                            AS discountAmount
                ,REFUND_AMOUNT                                                              AS refundAmount
                ,PAYMENT_AMOUNT                                                             AS paymentAmount
                ,PAYMENT_NUMBER                                                             AS paymentNumber
                ,MU_USER_ID                                                                 AS muUserId
                ,USER_NAME                                                                  AS userName
                ,CASE
                     WHEN FIND_IN_SET('C', STATUS_DESC1) > 0 THEN '결제취소'
                     WHEN FIND_IN_SET('R', STATUS_DESC1) > 0 THEN '환불'
                     WHEN FIND_IN_SET('PY', STATUS_DESC1) > 0 THEN '부분결제(완료)'
                     WHEN FIND_IN_SET('NY', STATUS_DESC1) > 0 THEN '미수결제'
                     WHEN FIND_IN_SET('Y', STATUS_DESC1) > 0 THEN '결제완료'
                     WHEN FIND_IN_SET('P', STATUS_DESC1) > 0 THEN '부분결제'
                     WHEN FIND_IN_SET('NP', STATUS_DESC1) > 0 THEN '미수'
                END                                                                              AS status1
                ,IFNULL(IF(FIND_IN_SET('NY', STATUS_DESC1) = 0 AND FIND_IN_SET('NP', STATUS_DESC1) > 0
                  OR FIND_IN_SET('PY', STATUS_DESC1) = 0 AND FIND_IN_SET('P', STATUS_DESC1) > 0,
                        PAYMENT_AMOUNT-SUM(TOTAL_AMOUNT)+ DISCOUNT_PRICE2-MEMBERSHIP_PRICE,0),0) AS nonpaymentAmount
               ,REG_DATE
            FROM (
                SELECT MAX(PAYMENT_DATE)                                                AS PAYMENT_DATE
                     ,RESERVE_DATE                                                      AS RESERVE_DATE
                     ,MP.ETC                                                            AS 'ETC'
                     ,GROUP_CONCAT(DISTINCT MP.TREATMENT_PROCEDURE_NAME SEPARATOR ', ') AS 'TREATMENT_PROCEDURE_NAME'
                     ,GROUP_CONCAT(DISTINCT MP.TREATMENT_NAME)                          AS 'TREATMENT_NAME'
                     ,STATUS_DESC1                                                      AS 'STATUS_DESC1'
                     ,GROUP_CONCAT(DISTINCT RESERVE_MEMBER_NAME SEPARATOR ', ')         AS 'RESERVE_MEMBER_NAME'
                     ,GROUP_CONCAT(DISTINCT IF(PAYMENT_CATEGORY_ITEM_NAME = '', NULL, PAYMENT_CATEGORY_ITEM_NAME) ORDER BY
                                   MP.PAYMENT_CATEGORY_ITEM_NAME)                       AS 'PAYMENT_CATEGORY_ITEM_NAME'
                     ,SUM(PRICE + VAT)                                                  AS TOTAL_AMOUNT
                     ,SUM(PAYMENT_VAT)                                                  AS 'VAT'
                     ,SUM(AMOUNT)                                                       AS 'AMOUNT'
                     ,DISCOUNT_PRICE2
                     ,IFNULL(SUM(MEMBERSHIP_PRICE),0)                                   AS 'MEMBERSHIP_PRICE'
                     ,SUM(REFUND_AMOUNT)                                                AS 'REFUND_AMOUNT'
                     ,IFNULL(SUM(PAYMENT_AMOUNT), 0)                                    AS 'PAYMENT_AMOUNT'
                     ,PAYMENT_NUMBER                                                    AS 'PAYMENT_NUMBER'
                     ,MU_USER_ID                                                        AS 'MU_USER_ID'
                     ,USER_NAME
                     ,REG_DATE
                     ,MEMBER_NAME
                FROM (
                        SELECT MAX(PAYMENT_DATE)                                                     AS PAYMENT_DATE
                              ,RESERVE_DATE
                              ,TREATMENT_PROCEDURE_NAME                                              AS 'TREATMENT_PROCEDURE_NAME'
                              ,IF(TREATMENT_NAME='멤버십','기타',TREATMENT_NAME)                       AS 'TREATMENT_NAME'
                              ,GROUP_CONCAT(DISTINCT STATUS_DESC1 ORDER BY REG_DATE SEPARATOR ',')   AS 'STATUS_DESC1'
                              ,RESERVE_MEMBER_NAME                                                   AS 'RESERVE_MEMBER_NAME'
                             ,GROUP_CONCAT(DISTINCT IF(PAYMENT_CATEGORY_ITEM_NAME = '', NULL, PAYMENT_CATEGORY_ITEM_NAME) ORDER BY
                                           MP.PAYMENT_CATEGORY_ITEM_NAME)                            AS 'PAYMENT_CATEGORY_ITEM_NAME'
                             ,MEMBER_NAME
                             ,GROUP_CONCAT(IF(MP.ETC = '', NULL,MP.ETC))                             AS ETC
                             ,SUM(MEMBERSHIP_PRICE)                                                  AS 'MEMBERSHIP_PRICE'
                             ,SUM(paymentAmount)                                                     AS 'PAYMENT_AMOUNT'
                             ,SUM(AMOUNT)                                                            AS 'AMOUNT'
                             ,SUM(VAT)                                                               AS 'PAYMENT_VAT'
                             ,SUM(REFUND_AMOUNT)                                                     AS 'REFUND_AMOUNT'
                             ,DISCOUNT_PRICE2
                             ,PAYMENT_NUMBER
                             ,MU_TREATMENT_PROCEDURE_ID
                             ,MU_USER_ID
                             ,USER_NAME
                             ,REG_DATE
                         FROM (
                                SELECT MP.MU_USER_ID
                                      ,MU.USER_NAME
                                      ,IF(MP.STATUS='C' OR MP.STATUS='R'
                                         , NULL
                                         , DATE_FORMAT(MP.WRITE_DATE, '%Y-%m-%d'))                            AS 'PAYMENT_DATE'
                                      ,DATE_FORMAT(MR.WRITE_DATE, '%Y-%m-%d')                                 AS 'RESERVE_DATE'
                                      ,MPD.TREATMENT_NAME                                                     AS 'TREATMENT_NAME'
                                      ,MPD.TREATMENT_PROCEDURE_NAME                                           AS 'TREATMENT_PROCEDURE_NAME'
                                      ,MP.ETC                                                                 AS 'etc'
                                      ,IF(MP.STATUS = 'R' OR MP.STATUS = 'C', '', PAYMENT_CATEGORY_ITEM_NAME) AS 'PAYMENT_CATEGORY_ITEM_NAME'
                                      ,MM.NAME                                                                AS MEMBER_NAME
                                      ,RM.NAME                                                                AS RESERVE_MEMBER_NAME
                                      ,MAX((SELECT SUM(MPD2.DISCOUNT_PRICE)
                                              FROM MU_PAYMENT_DETAIL MPD2
                                              WHERE MP.PAYMENT_NUMBER = MPD2.PAYMENT_NUMBER))                 AS DISCOUNT_PRICE2
                                      ,SUM( IF(MP.STATUS = 'R' OR MP.STATUS = 'C'
                                            , MPD.TOTAL_AMOUNT+IFNULL(MEMBERSHIP_PRICE,0), 0)
                                          )                                                                   AS 'REFUND_AMOUNT'
                                     ,IF(MP.STATUS = 'R' OR MP.STATUS = 'C', 0, MPD.TOTAL_AMOUNT)             AS 'paymentAmount'
                                     ,IF(MP.STATUS = 'R' OR MP.STATUS = 'C', 0, MPD.AMOUNT)                   AS 'AMOUNT'
                                     ,IF(MP.STATUS = 'R' OR MP.STATUS = 'C', 0, MPD.VAT)                      AS 'VAT'
                                     ,IF(MP.STATUS = 'R' OR MP.STATUS = 'C', 0, MPD.MEMBERSHIP_PRICE)         AS 'MEMBERSHIP_PRICE'
                                     ,MP.STATUS                                                               AS 'STATUS_DESC1'
                                     ,MP.PAYMENT_NUMBER
                                     ,MPD.MU_TREATMENT_PROCEDURE_ID
                                     ,MP.REG_DATE
                                     ,MP.MU_PAYMENT_ID
                                 FROM MU_PAYMENT MP
                                 JOIN MU_PAYMENT_DETAIL MPD
                                    ON MPD.MU_PAYMENT_ID = MP.MU_PAYMENT_ID
                                 LEFT JOIN MU_RESERVE MR
                                    ON MPD.MU_RESERVE_ID = MR.MU_RESERVE_ID
                                 JOIN MU_MEMBER MM
                                    ON MPD.MU_MEMBER_ID = MM.MU_MEMBER_ID
                                 JOIN MU_MEMBER RM
                                    ON MPD.RESERVE_MEMBER_ID = RM.MU_MEMBER_ID
                                 JOIN MU_USER MU
                                    ON MP.MU_USER_ID = MU.MU_USER_ID
                                <if test="paymentSearchKeywordParam != null and paymentSearchKeywordParam != ''">
                                    AND ((MU.USER_NAME LIKE CONCAT('%',TRIM(#{paymentSearchKeywordParam}),'%'))
                                    OR (MPD.TREATMENT_PROCEDURE_NAME LIKE CONCAT('%',TRIM(#{paymentSearchKeywordParam}),'%')))
                                </if>
                                 <if test="muUserId != null and muUserId != ''">
                                  AND MP.MU_USER_ID = #{muUserId}
                                 </if>
                                  GROUP BY MP.MU_USER_ID
                                         , MU.USER_NAME
                                         , MP.WRITE_DATE
                                         , MR.WRITE_DATE
                                         , MP.ETC
                                         , PAYMENT_CATEGORY_ITEM_NAME
                                         , MM.NAME
                                         , RM.NAME
                                         , REFUND_AMOUNT
                                         , PAYMENT_AMOUNT
                                         , MP.STATUS
                                         , MP.PAYMENT_NUMBER
                                         , MPD.MU_TREATMENT_PROCEDURE_ID
                                         , MP.REG_DATE
                                         , MU_PAYMENT_ID
                          ) AS MP
                        GROUP BY MEMBER_NAME
                               , DISCOUNT_PRICE2
                               , PAYMENT_NUMBER
                               , MU_TREATMENT_PROCEDURE_ID
                               , MU_USER_ID
                               , USER_NAME
                       HAVING 1=1
                       <if test="paymentCategoryItemNm != null and paymentCategoryItemNm != ''">
                        AND FIND_IN_SET( #{paymentCategoryItemNm},PAYMENT_CATEGORY_ITEM_NAME)>0
                       </if>
                   ) MP
                       JOIN (
                            SELECT PRICE
                                  , VAT
                                  , MU_TREATMENT_PROCEDURE_ID
                            FROM (SELECT PRICE
                                        , MTPP.VAT
                                        , MTPP.MU_TREATMENT_PROCEDURE_ID
                                  FROM MU_TREATMENT_PROCEDURE_PRICE MTPP
                                            JOIN MU_PAYMENT_DETAIL MPD
                                                 ON MTPP.MU_TREATMENT_PROCEDURE_ID = MPD.MU_TREATMENT_PROCEDURE_ID
                                            LEFT JOIN MU_RESERVE MR
                                                      ON MPD.MU_RESERVE_ID = MR.MU_RESERVE_ID
                                            JOIN MU_PAYMENT MP
                                                 ON MPD.MU_PAYMENT_ID = MP.MU_PAYMENT_ID
                                   WHERE MTPP.USE_YN='Y'
                                   <if test="muUserId != null and muUserId != ''">
                                   AND MP.MU_USER_ID = #{muUserId}
                                   </if>
                                   GROUP BY MTPP.MU_TREATMENT_PROCEDURE_ID
                                  ) AS MTPP
                             ) AS MTPP
                       ON MP.MU_TREATMENT_PROCEDURE_ID = MTPP.MU_TREATMENT_PROCEDURE_ID
                       <if test="paymentSearchStartDate != null and paymentSearchStartDate != '' and paymentSearchEndDate != null and paymentSearchEndDate != ''">
                       AND (DATE_FORMAT(PAYMENT_DATE, '%Y-%m-%d') BETWEEN #{paymentSearchStartDate} AND #{paymentSearchEndDate})
                       </if>
                       GROUP BY  MP.ETC
                               , RESERVE_DATE
                               , MEMBER_NAME
                               , DISCOUNT_PRICE2
                               , PAYMENT_NUMBER
                               , MU_USER_ID
                               , USER_NAME
                               , REG_DATE
                       HAVING 1=1
                       <if test="muTreatmentId != null and muTreatmentId != ''and treatmentName == '기타'">
                        AND FIND_IN_SET( '비만',TREATMENT_NAME)=0
                        AND FIND_IN_SET( '피부',TREATMENT_NAME)=0
                        AND FIND_IN_SET( '쁘띠',TREATMENT_NAME)=0
                       </if>
                       <if test="muTreatmentId != null and muTreatmentId != '' and treatmentName != '기타'">
                        AND FIND_IN_SET( #{treatmentName},TREATMENT_NAME)>0
                       </if>
             ) MP
            GROUP BY  ETC
                   , MEMBER_NAME
                   , PAYMENT_NUMBER
                   , MU_USER_ID
                   , USER_NAME
                   , status1
                   ,REG_DATE
        )MP
        <if test="paymentStatusKeyword != null and paymentStatusKeyword != ''">
            WHERE status1=#{paymentStatusKeyword}
        </if>
        <choose>
            <when test="sort != null and sort != ''">
                ORDER BY ${sort}
           </when>
           <otherwise>
           ORDER BY MP.REG_DATE DESC
           </otherwise>
       </choose>
    </select>

    <select id="selectCancelByPayment" parameterType="hashmap" resultType="hashmap">
        SELECT MP.STATUS               AS paymentCategoryItemName
              ,SUM(MP.COUNT)           AS count
              ,SUM(MP.PAYMENT_AMOUNT)  AS paymentAmount
          FROM (
                SELECT CASE
                       WHEN MP.STATUS IN ('P', 'NP') THEN '미수'
                       WHEN MP.STATUS IN ('C', 'CN', 'CY') THEN '취소'
                       WHEN MP.STATUS IN ('R')       THEN '환불'
                        END                                      AS STATUS
                      ,MP.COUNT                                  AS COUNT
                      ,IF(MP.STATUS IN ('P', 'NP'), MP.NON_PAYMENT_AMOUNT, MP.REFUND_TOTAL_AMOUNT)                                  AS PAYMENT_AMOUNT
                  FROM (
                        SELECT MP.STATUS                   AS STATUS
                              ,SUM(MP.REFUND_TOTAL_AMOUNT) AS REFUND_TOTAL_AMOUNT
                              ,SUM(IF(MP.STATUS IN ('NP', 'P')
                                     ,MP.TOTAL_AMOUNT - MP.PAY_AMOUNT + MP.DISCOUNT_PRICE - MP.MEMBERSHIP_PRICE
                                     ,0))                  AS NON_PAYMENT_AMOUNT
                              ,COUNT(1)                    AS COUNT
                          FROM (
                                SELECT MP.PAYMENT_NUMBER                                                 AS PAYMENT_NUMBER
                                      ,MP.MU_USER_ID                                                     AS MU_USER_ID
                                      ,MP.USER_NAME                                                      AS USER_NAME
                                      ,SUM(MP.PAY_AMOUNT)                                                AS PAY_AMOUNT
                                      ,SUM(MP.DISCOUNT_PRICE)                                            AS DISCOUNT_PRICE
                                      ,SUM(MP.MEMBERSHIP_PRICE)                                          AS MEMBERSHIP_PRICE
                                      ,SUM(MP.AMOUNT)                                                    AS AMOUNT
                                      ,SUM(MP.VAT)                                                       AS VAT
                                      ,SUM(MP.REFUND_TOTAL_AMOUNT)                                       AS REFUND_TOTAL_AMOUNT
                                      ,IFNULL((SELECT MP2.STATUS
                                                 FROM MU_PAYMENT AS MP2
                                                WHERE MP2.USE_YN = 'Y'
                                                  AND MP2.PAYMENT_NUMBER = MP.PAYMENT_NUMBER
                                               <if test="paymentSearchStartDate != null and paymentSearchStartDate != '' and paymentSearchEndDate != null and paymentSearchEndDate != ''">
                                                  AND MP2.WRITE_DATE &gt;= #{paymentSearchStartDate}
                                                  AND MP2.WRITE_DATE &lt;= #{paymentSearchEndDate}
                                               </if>
                                             ORDER BY MP2.REG_DATE DESC
                                                LIMIT 0, 1), '')                                          AS STATUS
                                      ,SUM(MP.ORG_TREATMENT_PROCEDURE_PRICE + MP.ORG_TREATMENT_PROCEDURE_VAT)  AS TOTAL_AMOUNT
                                      ,GROUP_CONCAT(MP.PAYMENT_CATEGORY_ITEM_NAME)                        AS PAYMENT_CATEGORY_ITEM_NAME
                                      ,GROUP_CONCAT(MP.TREATMENT_NAME)                                    AS TREATMENT_NAME
                                      ,GROUP_CONCAT(MP.TREATMENT_PROCEDURE_NAME)                          AS TREATMENT_PROCEDURE_NAME
                                  FROM (
                                        SELECT MP.PAYMENT_CATEGORY_ITEM_NAME                                         AS PAYMENT_CATEGORY_ITEM_NAME
                                              ,MP.TREATMENT_NAME                                                     AS TREATMENT_NAME
                                              ,MP.MU_TREATMENT_PROCEDURE_ID                                          AS MU_TREATMENT_PROCEDURE_ID
                                              ,MP.TREATMENT_PROCEDURE_NAME                                           AS TREATMENT_PROCEDURE_NAME
                                              ,MP.MU_USER_ID                                                         AS MU_USER_ID
                                              ,MP.USER_NAME                                                          AS USER_NAME
                                              ,IF(MP.STATUS IN ('R', 'C', 'CN'), 0, MP.PAY_AMOUNT)                   AS PAY_AMOUNT
                                              ,MP.DISCOUNT_PRICE                                                     AS DISCOUNT_PRICE
                                              ,IF(MP.STATUS IN ('R', 'C', 'CN'), 0, IFNULL(MP.MEMBERSHIP_PRICE, 0))  AS MEMBERSHIP_PRICE
                                              ,IF(MP.STATUS IN ('R', 'C'), 0, MP.AMOUNT)                             AS AMOUNT
                                              ,IF(MP.STATUS IN ('R', 'C'), 0, MP.VAT)                                AS VAT
                                              ,IF(MP.STATUS IN ('R', 'C', 'CN'), MP.PAY_AMOUNT, 0)                   AS REFUND_TOTAL_AMOUNT
                                              ,MP.PAYMENT_NUMBER                                                     AS PAYMENT_NUMBER
                                              ,MP.ORG_TREATMENT_PROCEDURE_PRICE                                      AS ORG_TREATMENT_PROCEDURE_PRICE
                                              ,MP.ORG_TREATMENT_PROCEDURE_VAT                                        AS ORG_TREATMENT_PROCEDURE_VAT
                                              ,MP.MU_RESERVE_ID                                                      AS MU_RESERVE_ID
                                              ,MP.STATUS                                                             AS STATUS
                                          FROM (
                                                SELECT MP.PAYMENT_CATEGORY_ITEM_NAME                           AS PAYMENT_CATEGORY_ITEM_NAME
                                                      ,MPD.TREATMENT_NAME                                      AS TREATMENT_NAME
                                                      ,MPD.MU_TREATMENT_PROCEDURE_ID                           AS MU_TREATMENT_PROCEDURE_ID
                                                      ,MPD.TREATMENT_PROCEDURE_NAME                            AS TREATMENT_PROCEDURE_NAME
                                                      ,MP.MU_USER_ID                                           AS MU_USER_ID
                                                      ,(SELECT MU.USER_NAME
                                                          FROM MU_USER AS MU
                                                         WHERE MU.USE_YN = 'Y'
                                                           AND MU.MU_USER_ID = MP.MU_USER_ID
                                                         LIMIT 0, 1)                                           AS USER_NAME
                                                      ,MPD.TOTAL_AMOUNT                          AS PAY_AMOUNT
                                                      ,MPD.DISCOUNT_PRICE                        AS DISCOUNT_PRICE
                                                      ,MPD.MEMBERSHIP_PRICE                      AS MEMBERSHIP_PRICE
                                                      ,MPD.AMOUNT                                AS AMOUNT
                                                      ,MPD.VAT                                   AS VAT
                                                      ,MP.PAYMENT_NUMBER                         AS PAYMENT_NUMBER
                                                      ,(SELECT MTPP.PRICE
                                                          FROM MU_TREATMENT_PROCEDURE_PRICE AS MTPP
                                                         WHERE MTPP.USE_YN = 'Y'
                                                           AND MTPP.MU_TREATMENT_PROCEDURE_ID = MPD.MU_TREATMENT_PROCEDURE_ID
                                                         LIMIT 0, 1)                             AS ORG_TREATMENT_PROCEDURE_PRICE
                                                      ,(SELECT MTPP.VAT
                                                          FROM MU_TREATMENT_PROCEDURE_PRICE AS MTPP
                                                         WHERE MTPP.USE_YN = 'Y'
                                                           AND MTPP.MU_TREATMENT_PROCEDURE_ID = MPD.MU_TREATMENT_PROCEDURE_ID
                                                         LIMIT 0, 1)                             AS ORG_TREATMENT_PROCEDURE_VAT
                                                      ,MP.STATUS                                 AS STATUS
                                                      ,MPD.MU_RESERVE_ID                         AS MU_RESERVE_ID
                                                  FROM MU_PAYMENT AS MP
                                            INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                                    ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                                   AND MP.USE_YN = 'Y'
                                                   AND MPD.USE_YN = 'Y'
                                                   AND MP.PAYMENT_CATEGORY_ITEM_ID != 'MM'
                                                <if test="paymentSearchStartDate != null and paymentSearchStartDate != '' and paymentSearchEndDate != null and paymentSearchEndDate != ''">
                                                   AND MP.WRITE_DATE &gt;= #{paymentSearchStartDate}
                                                   AND MP.WRITE_DATE &lt;= #{paymentSearchEndDate}
                                                </if>
                                            INNER JOIN (
                                                        SELECT MP.PAYMENT_NUMBER
                                                          FROM MU_PAYMENT AS MP
                                                    INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                                            ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                                           AND MP.USE_YN = 'Y'
                                                           AND MPD.USE_YN = 'Y'
                                                           AND MP.CRM_MEMBER_PAY_PID IS NULL
                                                           AND MPD.CRM_MEMBER_PAY_PID IS NULL
                                                           AND MP.STATUS IN ('C', 'R', 'CN', 'P', 'NP')
                                                        <if test="paymentSearchStartDate != null and paymentSearchStartDate != '' and paymentSearchEndDate != null and paymentSearchEndDate != ''">
                                                           AND MP.WRITE_DATE &gt;= #{paymentSearchStartDate}
                                                           AND MP.WRITE_DATE &lt;= #{paymentSearchEndDate}
                                                        </if>
                                                        <if test="paymentSearchKeywordParam != null and paymentSearchKeywordParam != ''">
                                                           AND ((MU.USER_NAME LIKE CONCAT('%',TRIM(#{paymentSearchKeywordParam}),'%'))
                                                            OR (MPD.TREATMENT_PROCEDURE_NAME LIKE CONCAT('%',TRIM(#{paymentSearchKeywordParam}),'%')))
                                                        </if>
                                                      GROUP BY MP.PAYMENT_NUMBER
                                                  ) AS MP2
                                                    ON MP.PAYMENT_NUMBER = MP2.PAYMENT_NUMBER
                                          ) AS MP
                                         WHERE 1 = 1
                                  ) AS MP
                                 WHERE 1 = 1
                              GROUP BY MP.PAYMENT_NUMBER
                                      ,MP.MU_USER_ID
                                      ,MP.USER_NAME
                          ) AS MP
                        <if test="paymentCategoryItemNm != null and paymentCategoryItemNm != ''">
                           AND MP.PAYMENT_CATEGORY_ITEM_NAME LIKE CONCAT('%', #{paymentCategoryItemNm}, '%')
                        </if>
                        <if test="muTreatmentId != null and muTreatmentId != ''">
                           AND MP.TREATMENT_NAME LIKE CONCAT('%', #{treatmentName}, '%')
                        </if>
                      GROUP BY MP.STATUS
                  ) AS MP
                 WHERE 1 = 1
                <if test="paymentStatusKeyword != null and paymentStatusKeyword != ''">
                   AND MP.STATUS LIKE CONCAT('%',  #{paymentStatusKeyword}, '%')
                </if>
          ) AS MP
      GROUP BY MP.STATUS
<!--        SELECT STATUS              AS "paymentCategoryItemName"
              ,SUM(COUNT)          AS "count"
              ,SUM(PAYMENT_AMOUNT) AS "paymentAmount"
          FROM (
                SELECT CASE
                       WHEN STATUS = 'P'
                         OR STATUS = 'NP' THEN '미수'
                       WHEN STATUS = 'C'
                         OR STATUS = 'CN' THEN '취소'
                       WHEN STATUS = 'R'  THEN '환불'
                        END                                                         AS STATUS
                      ,IFNULL(IF(STATUS='P' OR STATUS='NP',IFNULL(NONPAY_AMOUNT,0),SUM(REFUND_AMOUNT)),0)
                                                                                    AS PAYMENT_AMOUNT
                      ,COUNT(1)  AS COUNT
                  FROM (
                        SELECT PAYMENT_DATE
                              ,CASE
                               WHEN FIND_IN_SET('C',STATUS)>0     THEN 'C'
                               WHEN FIND_IN_SET('R',STATUS)>0     THEN 'R'
                               WHEN FIND_IN_SET('NP',STATUS)>0    THEN 'P'
                               WHEN FIND_IN_SET('P',STATUS)>0     THEN 'P'
                               WHEN FIND_IN_SET('CN',STATUS)>0    THEN 'CN'
                                END                                               AS STATUS
                              ,REFUND_AMOUNT
                          FROM (
                                SELECT MAX(PAYMENT_DATE) AS PAYMENT_DATE
                                      ,GROUP_CONCAT(DISTINCT MP.STATUS SEPARATOR ',') AS STATUS
                                      ,STATUS2
                                      ,SUM(REFUND_AMOUNT)                             AS REFUND_AMOUNT
                                  FROM (
                                        SELECT MP.WRITE_DATE              AS 'PAYMENT_DATE'
                                              ,MP.STATUS
                                              ,STATUS2
                                              ,SUM(MPD.TOTAL_AMOUNT)  AS REFUND_AMOUNT
                                              ,MP.PAYMENT_NUMBER
                                          FROM MU_PAYMENT MP
                                          JOIN MU_PAYMENT_DETAIL MPD
                                            ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                          JOIN MU_RESERVE MR
                                            ON MPD.MU_RESERVE_ID = MR.MU_RESERVE_ID
                                          JOIN MU_USER MU
                                            ON MP.MU_USER_ID = MU.MU_USER_ID
                                           AND PAYMENT_CATEGORY_ITEM_ID!='MM'
                                           AND MP.CRM_MEMBER_PAY_PID IS NULL
                                           AND MP.STATUS IN ('C', 'R', 'CN', 'P', 'NP')
                                        <if test="paymentSearchKeywordParam != null and paymentSearchKeywordParam != ''">
                                           AND ((MU.USER_NAME LIKE CONCAT('%',TRIM(#{paymentSearchKeywordParam}),'%'))
                                            OR (MPD.TREATMENT_PROCEDURE_NAME LIKE CONCAT('%',TRIM(#{paymentSearchKeywordParam}),'%')))
                                        </if>
                                        <if test="muTreatmentId != null and muTreatmentId != ''and treatmentName == '기타'">
                                           AND FIND_IN_SET( '비만',MPD.TREATMENT_NAME)=0
                                           AND FIND_IN_SET( '피부',MPD.TREATMENT_NAME)=0
                                           AND FIND_IN_SET( '쁘띠',MPD.TREATMENT_NAME)=0
                                        </if>
                                        <if test="muTreatmentId != null and muTreatmentId != '' and treatmentName != '기타'">
                                           AND FIND_IN_SET( #{treatmentName},MPD.TREATMENT_NAME)>0
                                        </if>
                                        <if test="paymentCategoryItemNm != null and paymentCategoryItemNm != ''">
                                           AND PAYMENT_CATEGORY_ITEM_NAME = #{paymentCategoryItemNm}
                                        </if>
                                        <if test="paymentStatus != null and paymentStatus != ''">
                                         WHERE STATUS2 = #{paymentStatus}
                                           <if test="paymentStatus2 != null and paymentStatus2 != ''">
                                            OR FIND_IN_SET(#{paymentStatus2}, STATUS_DESC1) > 0
                                           </if>
                                        </if>
                                      GROUP BY MP.STATUS
                                              ,MP.PAYMENT_NUMBER
                                              ,REFUND_AMOUNT
                                  ) AS MP
                              GROUP BY PAYMENT_NUMBER
                          ) AS MP
                         WHERE (FIND_IN_SET('C',STATUS)>0 OR FIND_IN_SET('R',STATUS)>0 OR STATUS2='NP'OR STATUS2='P' OR FIND_IN_SET('CN',STATUS)>0)
                        <if test="paymentSearchStartDate != null and paymentSearchStartDate != '' and paymentSearchEndDate != null and paymentSearchEndDate != ''">
                           AND (DATE_FORMAT(PAYMENT_DATE, '%Y-%m-%d') BETWEEN #{paymentSearchStartDate} AND #{paymentSearchEndDate})
                        </if>
                  ) AS MP,
                       (
                        SELECT SUM(-MEMBERSHIP_PRICE)-SUM(PRICE+VAT) AS NONPAY_AMOUNT
                          FROM (
                                SELECT IFNULL(MTPP.PRICE,0)            AS PRICE
                                      ,IFNULL(MTPP.VAT,0)              AS VAT
                                      ,IFNULL(MPD.MEMBERSHIP_PRICE,0)  AS MEMBERSHIP_PRICE
                                  FROM MU_RESERVE MR
                                  JOIN MU_TREATMENT_PROCEDURE_PRICE MTPP
                                    ON MR.MU_TREATMENT_PROCEDURE_ID = MTPP.MU_TREATMENT_PROCEDURE_ID
                                   AND MR.USE_YN='Y'
                                   AND MTPP.USE_YN = 'Y'
                                  JOIN MU_PAYMENT_DETAIL MPD
                                    ON MPD.MU_RESERVE_ID=MR.MU_RESERVE_ID
                                  JOIN MU_PAYMENT MP
                                    ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                   AND (MR.STATUS2='P' OR MR.STATUS2='NP')
                                  JOIN MU_USER MU
                                    ON MP.MU_USER_ID = MU.MU_USER_ID
                                   AND MP.CRM_MEMBER_PAY_PID IS NULL
                                <if test="paymentSearchStartDate != null and paymentSearchStartDate != '' and paymentSearchEndDate != null and paymentSearchEndDate != ''">
                                   AND DATE_FORMAT(MP.WRITE_DATE, '%Y-%m-%d') BETWEEN #{paymentSearchStartDate} AND #{paymentSearchEndDate}
                                </if>
                                <if test="paymentSearchKeywordParam != null and paymentSearchKeywordParam != ''">
                                   AND ((MU.USER_NAME LIKE CONCAT('%',TRIM(#{paymentSearchKeywordParam}),'%'))
                                    OR (MPD.TREATMENT_PROCEDURE_NAME LIKE CONCAT('%',TRIM(#{paymentSearchKeywordParam}),'%')))
                                </if>
                                <if test="muTreatmentId != null and muTreatmentId != ''and treatmentName == '기타'">
                                   AND FIND_IN_SET( '비만',MPD.TREATMENT_NAME)=0
                                   AND FIND_IN_SET( '피부',MPD.TREATMENT_NAME)=0
                                   AND FIND_IN_SET( '쁘띠',MPD.TREATMENT_NAME)=0
                                </if>
                                <if test="muTreatmentId != null and muTreatmentId != '' and treatmentName != '기타'">
                                   AND FIND_IN_SET( #{treatmentName},MPD.TREATMENT_NAME)>0
                                </if>
                                <if test="paymentCategoryItemNm != null and paymentCategoryItemNm != ''">
                                   AND PAYMENT_CATEGORY_ITEM_NAME = #{paymentCategoryItemNm}
                                </if>
                                <if test="paymentStatus != null and paymentStatus != ''">
                                 WHERE MP.STATUS = #{paymentStatus}
                                    <if test="paymentStatus2 != null and paymentStatus3 != ''">
                                    OR FIND_IN_SET(#{paymentStatus2}, STATUS_DESC1) > 0
                                    </if>
                                </if>
                              GROUP BY MTPP.PRICE
                                      ,MTPP.VAT
                                      ,MR.MU_RESERVE_ID
                                      ,MTPP.MU_TREATMENT_PROCEDURE_ID
                          ) AS NA
                       ) AS NONPAY_AMOUNT
              GROUP BY STATUS
                      ,NONPAY_AMOUNT
          ) AS MP
      GROUP BY STATUS -->
    </select>

	<select id="selectListPaymentUser" parameterType="hashmap" resultType="hashmap">
        SELECT WRITE_TIME                        AS writeTime
              ,TREATMENT_PROCEDURE_NAME          AS treatmentProcedureNm
              ,ETC                               AS etc
              ,PAYMENT_CATEGORY_ITEM_NAME        AS paymentCategoryItemNm
              ,AMOUNT                            AS amount
              ,VAT                               AS vat
              ,TOTAL_AMOUNT                      AS totalAmount
              ,DISCOUNT_PRICE                    AS discountPrice
              ,MEMBERSHIP_PRICE                  AS membershipPrice
              ,REFUND_AMOUNT                     AS refundAmount
              ,TOTAL_AMOUNT
               -DISCOUNT_PRICE
               +MEMBERSHIP_PRICE
               -PAYMENT_AMOUNT                  AS nonpayAmount
              ,PAYMENT_AMOUNT                    AS paymentAmount
              ,PAYMENT_NUMBER                    AS paymentNumber
              ,MU_USER_ID                        AS muUserId
              ,IFNULL((SELECT CASE
                              WHEN MP2.STATUS = 'C'
                              THEN '결제취소'
                              WHEN MP2.STATUS = 'R'
                              THEN '환불'
                              WHEN MP2.STATUS = 'PY'
                              THEN '부분결제(완료)'
                              WHEN MP2.STATUS = 'NY'
                              THEN '미수결제'
                              WHEN MP2.STATUS = 'CN'
                              THEN '재결제(취소)'
                              WHEN MP2.STATUS = 'CY'
                              THEN '재결제(완료)'
                              WHEN MP2.STATUS = 'Y'
                              THEN '결제완료'
                              WHEN MP2.STATUS = 'P'
                              THEN '부분결제'
                              WHEN MP2.STATUS = 'NP'
                              THEN '미수'
                              ELSE ''
                               END
                         FROM MU_PAYMENT AS MP2
                        WHERE MP2.USE_YN = 'Y'
                          AND MP2.PAYMENT_NUMBER = MP.PAYMENT_NUMBER
                     ORDER BY MP2.REG_DATE DESC
                        LIMIT 0, 1), '')                     AS status1
              <!--,CASE
               WHEN FIND_IN_SET('C', STATUS_DESC1)  > 0  THEN '결제취소'
               WHEN FIND_IN_SET('R', STATUS_DESC1)  > 0  THEN '환불'
               WHEN FIND_IN_SET('PY', STATUS_DESC1) > 0  THEN '부분결제(완료)'
               WHEN FIND_IN_SET('NY', STATUS_DESC1) > 0  THEN '미수결제'
               WHEN FIND_IN_SET('CN', STATUS_DESC1) > 0  THEN '재결제(취소)'
               WHEN FIND_IN_SET('Y', STATUS_DESC1)  > 0  THEN '결제완료'
               WHEN FIND_IN_SET('P', STATUS_DESC1)  > 0  THEN '부분결제'
               WHEN FIND_IN_SET('NP', STATUS_DESC1) > 0  THEN '미수'
               END                               AS 'status1'-->
          FROM (
                SELECT MP.WRITE_TIME                                                       AS 'WRITE_TIME'
                      ,GROUP_CONCAT(MP.TREATMENT_PROCEDURE_NAME SEPARATOR ', ')            AS 'TREATMENT_PROCEDURE_NAME'
                      ,MP.ETC                                                              AS 'ETC'
                      ,GROUP_CONCAT(DISTINCT MP.PAYMENT_CATEGORY_ITEM_NAME)                AS 'PAYMENT_CATEGORY_ITEM_NAME'
                      ,GROUP_CONCAT(DISTINCT MP.STATUS_DESC1 SEPARATOR ',')                AS 'STATUS_DESC1'
                      ,SUM(AMOUNT)                                                         AS 'AMOUNT'
                      ,SUM(PAYMENT_VAT)                                                    AS 'VAT'
                      ,SUM(PRICE + VAT)                                                    AS 'TOTAL_AMOUNT'
                      ,SUM(DISCOUNT_PRICE)                                                 AS 'DISCOUNT_PRICE'
                      ,IFNULL(SUM(MEMBERSHIP_PRICE),0)                                     AS 'MEMBERSHIP_PRICE'
                      ,SUM(REFUND_AMOUNT)                                                  AS 'REFUND_AMOUNT'
                      ,IFNULL(SUM(PAYMENT_AMOUNT), 0)                                      AS 'PAYMENT_AMOUNT'
                      ,PAYMENT_NUMBER                                                      AS 'PAYMENT_NUMBER'
                      ,MU_USER_ID                                                          AS 'MU_USER_ID'
                  FROM (
                        SELECT MAX(MP.WRITE_TIME)                                                  AS WRITE_TIME
                              ,GROUP_CONCAT(DISTINCT MP.TREATMENT_PROCEDURE_NAME SEPARATOR ', ')   AS 'TREATMENT_PROCEDURE_NAME'
                              ,GROUP_CONCAT(DISTINCT STATUS_DESC1 SEPARATOR ',')                   AS 'STATUS_DESC1'
                              ,GROUP_CONCAT(DISTINCT IF(MP.ETC = '', NULL, MP.ETC) SEPARATOR ',')  AS ETC
                              ,GROUP_CONCAT(DISTINCT IF(PAYMENT_CATEGORY_ITEM_NAME = ''
                                                        ,NULL
                                                        ,PAYMENT_CATEGORY_ITEM_NAME)
                                            ORDER BY MP.PAYMENT_CATEGORY_ITEM_NAME)                AS 'PAYMENT_CATEGORY_ITEM_NAME'
                              ,SUM(DISCOUNT_PRICE)                                                 AS DISCOUNT_PRICE
                              ,SUM(MEMBERSHIP_PRICE)                                               AS MEMBERSHIP_PRICE
                              ,SUM(paymentAmount)                                                  AS 'PAYMENT_AMOUNT'
                              ,SUM(AMOUNT)                                                         AS 'AMOUNT'
                              ,SUM(VAT)                                                            AS 'PAYMENT_VAT'
                              ,SUM(REFUND_AMOUNT)                                                  AS 'REFUND_AMOUNT'
                              ,PAYMENT_NUMBER
                              ,MU_TREATMENT_PROCEDURE_ID
                              ,MU_USER_ID
                          FROM (
                                SELECT MP.MU_USER_ID
                                      ,IF(MP.STATUS='C' OR MP.STATUS='R'
                                          ,NULL
                                          ,CONCAT(DATE_FORMAT(MP.WRITE_DATE, '%Y-%m-%d'), ' ', LEFT(MP.WRITE_TIME, 5))
                                         )                                                                         AS 'WRITE_TIME'
                                      ,GROUP_CONCAT(DISTINCT CONCAT(MPD.TREATMENT_NAME
                                                                    ,'-'
                                                                    ,MPD.TREATMENT_PROCEDURE_NAME)
                                                   SEPARATOR ', ')                                                 AS 'TREATMENT_PROCEDURE_NAME'
                                      ,MP.ETC                                                                      AS 'etc'
                                      ,IF(MP.STATUS = 'R' || MP.STATUS = 'C', '', PAYMENT_CATEGORY_ITEM_NAME)      AS 'PAYMENT_CATEGORY_ITEM_NAME'
                                      ,SUM(MPD.DISCOUNT_PRICE)                                                     AS 'DISCOUNT_PRICE'
                                      ,IF(MP.STATUS = 'R' || MP.STATUS = 'C', 0, MPD.MEMBERSHIP_PRICE)             AS 'MEMBERSHIP_PRICE'
                                      ,IF(MP.STATUS = 'R' || MP.STATUS = 'C', MPD.TOTAL_AMOUNT, 0)                 AS 'REFUND_AMOUNT'
                                      ,IF(MP.STATUS = 'R' || MP.STATUS = 'C', 0, MPD.TOTAL_AMOUNT)                 AS 'paymentAmount'
                                      ,IF(MP.STATUS = 'R' || MP.STATUS = 'C', 0, MPD.AMOUNT)                       AS 'AMOUNT'
                                      ,IF(MP.STATUS = 'R' || MP.STATUS = 'C', 0, MPD.VAT)                          AS 'VAT'
                                      ,MP.STATUS                                                                   AS 'STATUS_DESC1'
                                      ,MP.PAYMENT_NUMBER
                                      ,MPD.MU_TREATMENT_PROCEDURE_ID
                                      ,MPD.MU_RESERVE_ID
                                  FROM MU_PAYMENT MP
                                  JOIN MU_PAYMENT_DETAIL MPD
                                    ON MPD.MU_PAYMENT_ID = MP.MU_PAYMENT_ID
                                   AND MP.USE_YN = 'Y'
                                   AND MPD.USE_YN = 'Y'
                             LEFT JOIN MU_RESERVE MR
                                    ON MPD.MU_RESERVE_ID = MR.MU_RESERVE_ID
                                 WHERE MP.MU_USER_ID = #{muUserId}
                                   AND MP.CRM_MEMBER_PAY_PID IS NULL
                              <if test="paymentNumber != null and paymentNumber != ''">
                                   AND MPD.PAYMENT_NUMBER = #{paymentNumber}
                              </if>
                              GROUP BY MP.MU_USER_ID
                                      ,MP.WRITE_DATE
                                      ,MP.WRITE_TIME
                                      ,MP.ETC
                                      ,PAYMENT_CATEGORY_ITEM_NAME
                                      ,MPD.MEMBERSHIP_PRICE
                                      ,REFUND_AMOUNT
                                      ,MP.STATUS
                                      ,MP.PAYMENT_NUMBER
                                      ,MPD.MU_TREATMENT_PROCEDURE_ID
                                      ,MPD.MU_RESERVE_ID
                          ) AS MP
                      GROUP BY MU_TREATMENT_PROCEDURE_ID
                              ,PAYMENT_NUMBER
                              ,MU_RESERVE_ID
                              ,MU_USER_ID
                  ) AS MP
                  JOIN (
                        SELECT PRICE
                              ,VAT
                              ,MU_TREATMENT_PROCEDURE_ID
                          FROM (
                                SELECT PRICE
                                      ,MTPP.VAT
                                      ,MTPP.MU_TREATMENT_PROCEDURE_ID
                                  FROM MU_TREATMENT_PROCEDURE_PRICE MTPP
                                  JOIN MU_PAYMENT_DETAIL MPD
                                    ON MTPP.MU_TREATMENT_PROCEDURE_ID = MPD.MU_TREATMENT_PROCEDURE_ID
                             LEFT JOIN MU_RESERVE MR
                                    ON MPD.MU_RESERVE_ID = MR.MU_RESERVE_ID
                                  JOIN MU_PAYMENT MP
                                    ON MPD.MU_PAYMENT_ID = MP.MU_PAYMENT_ID
                                 WHERE MP.MU_USER_ID = #{muUserId}
                                   AND MTPP.USE_YN='Y'
                              <if test="paymentNumber != null and paymentNumber != ''">
                                   AND MPD.PAYMENT_NUMBER = #{paymentNumber}
                              </if>
                             GROUP BY MTPP.MU_TREATMENT_PROCEDURE_ID
                          ) AS MTPP
                  ) AS MTPP
                    ON MP.MU_TREATMENT_PROCEDURE_ID = MTPP.MU_TREATMENT_PROCEDURE_ID
              GROUP BY PAYMENT_NUMBER
                      ,MP.ETC
                      ,MU_USER_ID
          ) AS MP

     UNION ALL

        SELECT CONCAT(DATE_FORMAT(MP.WRITE_DATE, '%Y-%m-%d'), ' ', LEFT(MP.WRITE_TIME, 5))   AS writeTime
              ,CASE
               WHEN MPD.CRM_MEMBER_PAY_M_DIVI4_NAME != ''
               THEN CONCAT(MPD.CRM_MEMBER_PAY_M_DIVI2_NAME, '/', MPD.CRM_MEMBER_PAY_M_DIVI3_NAME, '/', MPD.CRM_MEMBER_PAY_M_DIVI4_NAME)
               WHEN MPD.CRM_MEMBER_PAY_M_DIVI3_NAME != ''
               THEN CONCAT(MPD.CRM_MEMBER_PAY_M_DIVI2_NAME, '/', MPD.CRM_MEMBER_PAY_M_DIVI3_NAME)
               WHEN MPD.CRM_MEMBER_PAY_M_DIVI2_NAME != ''
               THEN CONCAT(MPD.CRM_MEMBER_PAY_M_DIVI2_NAME)
               ELSE ''
                END                                              AS treatmentProcedureNm
              ,MP.ETC                                            AS etc
              ,MP.PAYMENT_CATEGORY_ITEM_NAME                     AS paymentCategoryItemNm
              ,MPD.AMOUNT                                        AS amount
              ,MPD.VAT                                           AS vat
              ,MPD.TOTAL_AMOUNT                                  AS totalAmount
              ,MPD.DISCOUNT_PRICE                                AS discountPrice
              ,MPD.MEMBERSHIP_PRICE                              AS membershipPrice
              ,0                                                 AS refundAmount
              ,0                                                 AS nonpayAmount
              ,MPD.TOTAL_AMOUNT                                  AS paymentAmount
              ,MP.PAYMENT_NUMBER                                 AS paymentNumber
              ,MP.MU_USER_ID                                     AS muUserId
              ,CASE
               WHEN MP.STATUS = 'C'
               THEN '결제취소'
               WHEN MP.STATUS = 'R'
               THEN '환불'
               WHEN MP.STATUS = 'PY'
               THEN '부분결제(완료)'
               WHEN MP.STATUS = 'NY'
               THEN '미수결제'
               WHEN MP.STATUS = 'Y'
               THEN '결제완료'
               WHEN MP.STATUS = 'P'
               THEN '부분결제'
               WHEN MP.STATUS = 'NP'
               THEN '미수'
               WHEN MP.STATUS = ''
               THEN MP.CRM_MEMBER_PAY_PAY_DIVI1
               ELSE MP.STATUS
               END                                               AS status1
          FROM MU_PAYMENT AS MP
    INNER JOIN MU_PAYMENT_DETAIL AS MPD
            ON MPD.MU_PAYMENT_ID = MP.MU_PAYMENT_ID
           AND MP.USE_YN = 'Y'
           AND MPD.USE_YN = 'Y'
           AND MP.MU_USER_ID = #{muUserId}
           AND MP.CRM_MEMBER_PAY_PID IS NOT NULL
          <if test="paymentNumber != null and paymentNumber != ''">
           AND MPD.PAYMENT_NUMBER = #{paymentNumber}
          </if>
      ORDER BY writeTime DESC
	</select>
    <select id="selectListPaymentProcedureDetail" parameterType="hashmap" resultType="hashmap">
        SELECT
              WRITE_DATE                       AS 'writeDate'
            , WRITE_TIME                       AS 'writeTime'
            , PAYMENT_NUMBER                   AS 'paymentNumber'
            , TREATMENT_NAME                   AS 'treatmentName'
            , TREATMENT_PROCEDURE_NAME         AS 'treatmentProcedureName'
            , TOTAL_AMOUNT                     AS 'totalAmount'
            , DETAIL_VAT                       AS 'vat'
            , DETAIL_AMOUNT                    AS 'amount'
            , MU_RESERVE_ID                    AS 'muReserveId'
            , MU_TREATMENT_PROCEDURE_ID        AS 'muTreatmentProcedureId'
            , MU_TREATMENT_ID                  AS 'muTreatmentId'
            , RESERVE_MEMBER_ID                AS 'reserveMemberId'
            , STATUS                           AS 'status'
        FROM (
               SELECT WRITE_DATE
                    , WRITE_TIME
                    , PAYMENT_NUMBER
                    , TREATMENT_NAME
                    , TREATMENT_PROCEDURE_NAME
                    , IFNULL(SUM(TOTAL_AMOUNT),0)                             AS TOTAL_AMOUNT
                    , IFNULL(SUM(DETAIL_AMOUNT),0)                            AS DETAIL_AMOUNT
                    , IFNULL(SUM(DETAIL_VAT) ,0)                              AS DETAIL_VAT
                    , MU_RESERVE_ID
                    , MU_TREATMENT_PROCEDURE_ID
                    , MU_TREATMENT_ID
                    , RESERVE_MEMBER_ID
                    , STATUS
               FROM (
                     SELECT DATE_FORMAT(IFNULL(MR.RESERVE_DATE, MP.WRITE_DATE), '%Y-%m-%d') AS 'WRITE_DATE'
                          , LEFT(IFNULL(MR.RESERVE_TIME, MP.WRITE_TIME), 5)
                                                        AS 'WRITE_TIME'
                          , CASE
                               WHEN MR.STATUS2 = 'NP' THEN '미수'
                               WHEN MR.STATUS2 = 'CN' THEN '재결제(취소)'
                               WHEN MR.STATUS2 = 'CY' THEN '재결제(완료)'
                               WHEN MR.STATUS2 = 'Y' THEN '결제완료'
                               WHEN MR.STATUS2 = 'C' THEN '결제취소'
                               WHEN MR.STATUS2 = 'P' THEN '부분결제'
                               WHEN MR.STATUS2 = 'PY' THEN '부분결제(완납)'
                               WHEN MR.STATUS2 = 'R' THEN '환불'
                               WHEN MR.STATUS2 = 'NY' THEN '미수결제'
                               WHEN MR.STATUS2 = 'R' THEN '환불'
                           END                                              AS 'STATUS'
                         , MP.PAYMENT_NUMBER            AS 'PAYMENT_NUMBER'
                         , MPD.TREATMENT_NAME           AS 'TREATMENT_NAME'
                         , MPD.TREATMENT_PROCEDURE_NAME AS 'TREATMENT_PROCEDURE_NAME'
                         , MPD.MU_TREATMENT_PROCEDURE_ID
                         , MPD.MU_TREATMENT_ID
                         , MPD.RESERVE_MEMBER_ID
                         , MPD.MU_RESERVE_ID
                         ,MPD.TOTAL_AMOUNT              AS TOTAL_AMOUNT
                         ,MPD.AMOUNT                    AS DETAIL_AMOUNT
                         ,MPD.VAT                       AS DETAIL_VAT
                    FROM MU_PAYMENT_DETAIL MPD
               LEFT JOIN MU_RESERVE MR
                      ON MR.MU_RESERVE_ID = MPD.MU_RESERVE_ID
                    JOIN MU_PAYMENT MP
                      ON MPD.MU_PAYMENT_ID = MP.MU_PAYMENT_ID
                     AND MP.USE_YN = 'Y'
                     AND MPD.USE_YN = 'Y'
                    JOIN MU_TREATMENT_PROCEDURE_PRICE MTPP
                      ON MPD.MU_TREATMENT_PROCEDURE_ID = MTPP.MU_TREATMENT_PROCEDURE_ID
                   WHERE MP.MU_USER_ID = #{muUserId}
                    <if test="paymentNumber != null and paymentNumber != ''">
                        AND MPD.PAYMENT_NUMBER = #{paymentNumber}
                    </if>
                        AND MP.STATUS !='R'
                        AND MP.STATUS !='C'
                        GROUP BY
                              PAYMENT_CATEGORY_ITEM_ID
                              ,MPD.MU_PAYMENT_ID
                              ,MPD.MU_RESERVE_ID
                        ) PD
                  GROUP BY
                      MU_RESERVE_ID
                  ) PD
            GROUP BY MU_RESERVE_ID

     UNION ALL

        SELECT DATE_FORMAT(MP.WRITE_DATE, '%Y-%m-%d')   AS writeDate
              ,LEFT(MP.WRITE_TIME, 5)                   AS writeTime
              ,MP.PAYMENT_NUMBER                        AS paymentNumber
              ,MPD.TREATMENT_NAME                       AS treatmentName
              ,CASE
               WHEN MPD.CRM_MEMBER_PAY_M_DIVI4_NAME != ''
               THEN CONCAT(MPD.CRM_MEMBER_PAY_M_DIVI2_NAME, '/', MPD.CRM_MEMBER_PAY_M_DIVI3_NAME, '/', MPD.CRM_MEMBER_PAY_M_DIVI4_NAME)
               WHEN MPD.CRM_MEMBER_PAY_M_DIVI3_NAME != ''
               THEN CONCAT(MPD.CRM_MEMBER_PAY_M_DIVI2_NAME, '/', MPD.CRM_MEMBER_PAY_M_DIVI3_NAME)
               WHEN MPD.CRM_MEMBER_PAY_M_DIVI2_NAME != ''
               THEN CONCAT(MPD.CRM_MEMBER_PAY_M_DIVI2_NAME)
               ELSE ''
                END                                     AS treatmentProcedureNm
              ,MPD.TOTAL_AMOUNT                         AS totalAmount
              ,0                                        AS vat
              ,MPD.AMOUNT                               AS amount
              ,NULL                                     AS muReserveId
              ,NULL                                     AS muTreatmentProcedureId
              ,NULL                                     AS muTreatmentId
              ,NULL                                     AS reserveMemberId
              ,CASE
               WHEN MP.STATUS = 'C'
               THEN '결제취소'
               WHEN MP.STATUS = 'R'
               THEN '환불'
               WHEN MP.STATUS = 'PY'
               THEN '부분결제(완료)'
               WHEN MP.STATUS = 'NY'
               THEN '미수결제'
               WHEN MP.STATUS = 'Y'
               THEN '결제완료'
               WHEN MP.STATUS = 'P'
               THEN '부분결제'
               WHEN MP.STATUS = 'NP'
               THEN '미수'
               WHEN MP.STATUS = ''
               THEN MP.CRM_MEMBER_PAY_PAY_DIVI1
               ELSE MP.STATUS
               END                                      AS status
          FROM MU_PAYMENT AS MP
    INNER JOIN MU_PAYMENT_DETAIL AS MPD
            ON MPD.MU_PAYMENT_ID = MP.MU_PAYMENT_ID
           AND MP.USE_YN = 'Y'
           AND MPD.USE_YN = 'Y'
           AND MP.MU_USER_ID = #{muUserId}
           AND MP.CRM_MEMBER_PAY_PID IS NOT NULL
          <if test="paymentNumber != null and paymentNumber != ''">
           AND MPD.PAYMENT_NUMBER = #{paymentNumber}
          </if>
    </select>

    <select id="selectListPaymentDetail" parameterType="hashmap" resultType="hashmap">
        SELECT WRITE_TIME                                      AS writeTime
              ,PAYMENT_CATEGORY_ITEM_ID                        AS paymentCategoryItemId
              ,PAYMENT_CATEGORY_ITEM_NAME                      AS paymentCategoryItemNm
              ,MP.CARD_INSTALLMENTS                            AS cardInstallments
              ,MP.CARD_INSTALLMENTS_CODE                       AS cardInstallmentsCode
              ,MP.PAYMENT_CARD_INFO                            AS paymentCardInfo
              ,SUM(DETAIL_AMOUNT)                              AS amount
              ,SUM(DETAIL_VAT)                                 AS vat
              ,IFNULL(SUM(PAYMENT_AMOUNT),0)                   AS totalAmount
              ,SUM(DISCOUNT_PRICE)                             AS discountPrice
              ,SUM(MEMBERSHIP_PRICE)                           AS membershipPrice
              ,STATUS                                          AS status
              ,REFUND_ACCOUNT                                  AS refundAccount
              ,REFUND_ETC                                      AS refundEtc
              ,ETC                                             AS etc
              ,PAYMENT_NUMBER                                  AS paymentNumber
              ,MU_PAYMENT_ID                                   AS muPaymentId
              ,PAYMENT_APPROVAL_ID                             AS paymentApprovalId
              ,PAYMENT_APPROVAL_DATE                           AS paymentApprovalDate
              ,IFNULL((SELECT CASE
                              WHEN MPRH.ACQUIRER_NAME IS NOT NULL
                               AND MPRH.ACQUIRER_NAME != ''
                              THEN CONCAT(MPRH.ISSUER_NAME, '(', RIGHT(SUBSTRING_INDEX(MPRH.CARD_NUMBER, '=', 1), 4), ')')
                              WHEN MPRH.ACQUIRER_NAME IS NOT NULL
                               AND MPRH.ACQUIRER_NAME = ''
                              THEN CONCAT(MPRH.ISSUER_NAME)
                              ELSE '현금'
                               END
                         FROM MU_PAYMENT_RESPONSE_HISTORY AS MPRH
                        WHERE MPRH.USE_YN = 'Y'
                          AND MPRH.MU_PAYMENT_RESPONSE_HISTORY_ID = MP.MU_PAYMENT_RESPONSE_HISTORY_ID), MP.PAYMENT_CATEGORY_ITEM_NAME)  AS cardInfo
          FROM (
                SELECT CONCAT(DATE_FORMAT(MPD.WRITE_DATE, '%Y-%m-%d'), ' '
                               , LEFT(MPD.WRITE_TIME, 5))                      AS WRITE_TIME
                      ,MP.CARD_INSTALLMENTS
                      ,MP.CARD_INSTALLMENTS_CODE
                      ,MP.PAYMENT_CARD_INFO
                      ,PAYMENT_CATEGORY_ITEM_NAME
                      ,PAYMENT_CATEGORY_ITEM_ID
                      ,MP.AMOUNT
                      ,MP.VAT
                      ,SUM(MPD.AMOUNT)                                      AS DETAIL_AMOUNT
                      ,SUM(MPD.VAT)                                         AS DETAIL_VAT
                      ,MPD.TOTAL_AMOUNT                                     AS PAYMENT_AMOUNT
                      ,SUM(MPD.DISCOUNT_PRICE)                              AS DISCOUNT_PRICE
                      ,SUM(MPD.MEMBERSHIP_PRICE)                            AS MEMBERSHIP_PRICE
                      ,MP.REFUND_ACCOUNT
                      ,MP.REFUND_ETC
                      ,MP.ETC
                      ,MP.PAYMENT_NUMBER
                      ,MPD.MU_RESERVE_ID
                      ,PAYMENT_APPROVAL_ID
                      ,DATE_FORMAT(MP.WRITE_DATE,'%Y%m%d')                 AS PAYMENT_APPROVAL_DATE
                      ,CASE
                       WHEN MP.STATUS = 'NP' THEN '미수'
                       WHEN MP.STATUS = 'Y'  THEN '결제완료'
                       WHEN MP.STATUS = 'C'  THEN '결제취소'
                       WHEN MP.STATUS = 'P'  THEN IF(FIND_IN_SET('Y',(SELECT GROUP_CONCAT(STATUS)
                                                                        FROM MU_PAYMENT MP2
                                                                       WHERE MP2.PAYMENT_NUMBER=MP.PAYMENT_NUMBER))>0
                                                     ,'결제완료','부분결제')
                       WHEN MP.STATUS = 'NY' THEN '미수결제'
                       WHEN MP.STATUS = 'PY' THEN '부분결제(완납)'
                       WHEN MP.STATUS = 'R'  THEN  '환불'
                       WHEN MP.STATUS = 'PP' THEN '선결제'
                       WHEN MP.STATUS = 'CN' THEN '재결제(취소)'
                       WHEN MP.STATUS = 'CY' THEN '재결제(완료)'
                        END                                                 AS STATUS
                      ,MP.STATUS                                            AS STATUS_CODE
                      ,MP.MU_PAYMENT_ID
                      ,MP.REG_DATE
                      ,MP.MU_PAYMENT_RESPONSE_HISTORY_ID
                  FROM MU_PAYMENT_DETAIL MPD
             LEFT JOIN MU_RESERVE MR
                    ON MR.MU_RESERVE_ID = MPD.MU_RESERVE_ID
                  JOIN MU_PAYMENT MP
                    ON MPD.MU_PAYMENT_ID = MP.MU_PAYMENT_ID
                   AND MP.USE_YN = 'Y'
                   AND MPD.USE_YN = 'Y'
                 WHERE MP.MU_USER_ID = #{muUserId}
                   <if test="paymentNumber != null and paymentNumber != ''">
                   AND MPD.PAYMENT_NUMBER = #{paymentNumber}
                   </if>
              GROUP BY MPD.WRITE_DATE
                      ,MPD.WRITE_TIME
                      ,CARD_INSTALLMENTS
                      ,PAYMENT_CARD_INFO
                      ,PAYMENT_CATEGORY_ITEM_NAME
                      ,PAYMENT_CATEGORY_ITEM_ID
                      ,MP.AMOUNT
                      ,MP.VAT
                      ,REFUND_AMOUNT
                      ,PAYMENT_AMOUNT
                      ,REFUND_AMOUNT
                      ,REFUND_ETC
                      ,PAYMENT_NUMBER
                      ,MP.MU_PAYMENT_ID
                      ,MPD.MU_RESERVE_ID
          ) AS MP
      GROUP BY WRITE_TIME
              ,PAYMENT_CATEGORY_ITEM_ID
              ,PAYMENT_CATEGORY_ITEM_NAME
              ,MP.CARD_INSTALLMENTS
              ,MP.PAYMENT_CARD_INFO
              ,STATUS
              ,REFUND_ACCOUNT
              ,REFUND_ETC
              ,PAYMENT_NUMBER
              ,MU_PAYMENT_ID
              ,PAYMENT_APPROVAL_ID
              ,REG_DATE
      ORDER BY REG_DATE DESC
    </select>
    <select id="selectTotalPaymentUser2Count" parameterType="hashmap" resultType="hashmap">
       SELECT COUNT(DISTINCT MP.PAYMENT_NUMBER) AS totalCount
        FROM MU_PAYMENT MP
                 JOIN MU_PAYMENT_DETAIL MPD
                     ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                 LEFT JOIN MU_RESERVE MR
                     ON MPD.MU_RESERVE_ID = MR.MU_RESERVE_ID
                 JOIN MU_MEMBER MM
                     ON MPD.MU_MEMBER_ID = MM.MU_MEMBER_ID
                 JOIN MU_MEMBER RM
                     ON MPD.RESERVE_MEMBER_ID = RM.MU_MEMBER_ID
                 JOIN MU_USER MU
                     ON MP.MU_USER_ID = MU.MU_USER_ID
        WHERE MP.MU_USER_ID = #{muUserId}
        <if test="searchStartDate != null and searchStartDate != '' and searchEndDate != null and searchEndDate != ''">
            AND (DATE_FORMAT(MP.WRITE_DATE, '%Y-%m-%d') BETWEEN #{searchStartDate} AND #{searchEndDate})
        </if>
        <if test="searchKeywordParam != null and searchKeywordParam != ''">
            AND ((MP.USER_NAME LIKE CONCAT('%', TRIM(#{searchKeywordParam}), '%'))
            OR (MPD.TREATMENT_PROCEDURE_NAME LIKE CONCAT('%', TRIM(#{searchKeywordParam}), '%')))
        </if>
        <if test="muTreatmentId != null and muTreatmentId != ''and treatmentName == '기타'">
            AND FIND_IN_SET( '비만',MPD.TREATMENT_NAME)=0
            AND FIND_IN_SET( '피부',MPD.TREATMENT_NAME)=0
            AND FIND_IN_SET( '쁘띠',MPD.TREATMENT_NAME)=0
        </if>
        <if test="muTreatmentId != null and muTreatmentId != '' and treatmentName != '기타'">
            AND FIND_IN_SET( #{treatmentName},MPD.TREATMENT_NAME)>0
        </if>
        <if test="paymentCategoryItemNm != null and paymentCategoryItemNm != ''">
            AND MP.PAYMENT_CATEGORY_ITEM_NAME = #{paymentCategoryItemNm}
        </if>
    </select>

    <select id="selectListPaymentUser2" parameterType="hashmap" resultType="hashmap">
        SELECT PAYMENT_DATE                                                                                                AS paymentDate
               ,IFNULL(RESERVE_DATE, '-')                                                                                  AS reserveDate
               ,GROUP_CONCAT(DISTINCT MP.TREATMENT_PROCEDURE_NAME SEPARATOR ', ')                                          AS treatmentProcedureNm
               ,GROUP_CONCAT(DISTINCT MP.TREATMENT_NAME)                                                                   AS treatmentName
               ,MP.ETC                                                                                                     AS etc
               ,IFNULL(GROUP_CONCAT(DISTINCT MP.PAYMENT_CATEGORY_ITEM_NAME SEPARATOR ', '),
                      '미납')                                                                                               AS paymentCategoryItemName
               ,GROUP_CONCAT(DISTINCT RESERVE_MEMBER_NAME SEPARATOR ', ')                                                  AS reserveMemberName
               ,MEMBER_NAME                                                                                                AS muMemberName
               ,AMOUNT                                                                                                     AS amount
               ,VAT                                                                                                        AS vat
               ,SUM(TOTAL_AMOUNT) - DISCOUNT_PRICE2                                                                        AS totalAmount
               ,MEMBERSHIP_PRICE                                                                                           AS membershipPrice
               ,REFUND_AMOUNT                                                                                              AS refundAmount
               ,PAYMENT_AMOUNT+MEMBERSHIP_PRICE                                                                            AS paymentAmount
               ,PAYMENT_NUMBER                                                                                             AS paymentNumber
               ,MU_USER_ID                                                                                                 AS muUserId
               ,USER_NAME                                                                                                  AS userName
               ,IFNULL((SELECT CASE
                              WHEN MP2.STATUS = 'C'
                              THEN '결제취소'
                              WHEN MP2.STATUS = 'R'
                              THEN '환불'
                              WHEN MP2.STATUS = 'PY'
                              THEN '부분결제(완료)'
                              WHEN MP2.STATUS = 'NY'
                              THEN '미수결제'
                              WHEN MP2.STATUS = 'CN'
                              THEN '재결제(취소)'
                              WHEN MP2.STATUS = 'CY'
                              THEN '재결제(완료)'
                              WHEN MP2.STATUS = 'Y'
                              THEN '결제완료'
                              WHEN MP2.STATUS = 'P'
                              THEN '부분결제'
                              WHEN MP2.STATUS = 'NP'
                              THEN '미수'
                              ELSE ''
                               END
                         FROM MU_PAYMENT AS MP2
                        WHERE MP2.USE_YN = 'Y'
                          AND MP2.PAYMENT_NUMBER = MP.PAYMENT_NUMBER
                     ORDER BY MP2.REG_DATE DESC
                        LIMIT 0, 1), '')                     AS status1
               <!--,CASE
                    WHEN FIND_IN_SET('C', STATUS_DESC1) > 0 THEN '결제취소'
                    WHEN FIND_IN_SET('R', STATUS_DESC1) > 0 THEN '환불'
                    WHEN FIND_IN_SET('PY', STATUS_DESC1) > 0 THEN '부분결제(완료)'
                    WHEN FIND_IN_SET('NY', STATUS_DESC1) > 0 THEN '미수결제'
                    WHEN FIND_IN_SET('Y', STATUS_DESC1) > 0 THEN '결제완료'
                    WHEN FIND_IN_SET('P', STATUS_DESC1) > 0 THEN '부분결제'
                    WHEN FIND_IN_SET('NP', STATUS_DESC1) > 0 THEN '미수'
               END                                                                                                    AS status1 -->
               , IFNULL(IF(FIND_IN_SET('NY', STATUS_DESC1) = 0 AND FIND_IN_SET('NP', STATUS_DESC1) > 0
                            OR FIND_IN_SET('PY', STATUS_DESC1) = 0 AND FIND_IN_SET('P', STATUS_DESC1) > 0,
                            PAYMENT_AMOUNT-SUM(TOTAL_AMOUNT)+ DISCOUNT_PRICE2-MEMBERSHIP_PRICE,'-'),0)                    AS nonpaymentAmount
        FROM (SELECT MAX(PAYMENT_DATE)                                                   AS PAYMENT_DATE
                     ,MAX(RESERVE_DATE)                                                 AS RESERVE_DATE
                     ,MP.ETC                                                            AS 'ETC'
                     ,GROUP_CONCAT(DISTINCT MP.TREATMENT_PROCEDURE_NAME SEPARATOR ', ') AS 'TREATMENT_PROCEDURE_NAME'
                     ,GROUP_CONCAT(DISTINCT MP.TREATMENT_NAME)                          AS 'TREATMENT_NAME'
                     ,STATUS_DESC1                                                      AS 'STATUS_DESC1'
                     ,GROUP_CONCAT(DISTINCT RESERVE_MEMBER_NAME SEPARATOR ', ')         AS 'RESERVE_MEMBER_NAME'
                     ,GROUP_CONCAT(DISTINCT IF(PAYMENT_CATEGORY_ITEM_NAME = '', NULL, PAYMENT_CATEGORY_ITEM_NAME) ORDER BY
                                  MP.PAYMENT_CATEGORY_ITEM_NAME SEPARATOR ', ')         AS 'PAYMENT_CATEGORY_ITEM_NAME'
                     ,SUM(PRICE + VAT)                                                  AS TOTAL_AMOUNT
                     ,SUM(VAT)                                                          AS 'VAT'
                     ,SUM(PRICE)                                                        AS 'AMOUNT'
                     ,DISCOUNT_PRICE2
                     ,IFNULL(SUM(MEMBERSHIP_PRICE),0)                                   AS 'MEMBERSHIP_PRICE'
                     ,SUM(REFUND_AMOUNT)                                                AS 'REFUND_AMOUNT'
                     ,IFNULL(SUM(PAYMENT_AMOUNT), 0)                                    AS 'PAYMENT_AMOUNT'
                     ,PAYMENT_NUMBER                                                    AS 'PAYMENT_NUMBER'
                     ,MU_USER_ID                                                        AS 'MU_USER_ID'
                     ,USER_NAME
                     ,REG_DATE
                     ,MEMBER_NAME
              FROM (SELECT PAYMENT_DATE
                           ,RESERVE_DATE
                           ,TREATMENT_PROCEDURE_NAME   AS 'TREATMENT_PROCEDURE_NAME'
                           ,TREATMENT_NAME             AS 'TREATMENT_NAME'
                           ,GROUP_CONCAT(DISTINCT STATUS_DESC1 SEPARATOR ',')
                                                      AS 'STATUS_DESC1'
                           ,RESERVE_MEMBER_NAME        AS 'RESERVE_MEMBER_NAME'
                           ,GROUP_CONCAT(DISTINCT IF(PAYMENT_CATEGORY_ITEM_NAME = '', NULL, PAYMENT_CATEGORY_ITEM_NAME) ORDER BY
                                        MP.PAYMENT_CATEGORY_ITEM_NAME SEPARATOR ', ')
                                                      AS 'PAYMENT_CATEGORY_ITEM_NAME'
                           ,MEMBER_NAME
                           ,MP.ETC
                           ,SUM(MEMBERSHIP_PRICE)      AS 'MEMBERSHIP_PRICE'
                           ,SUM(paymentAmount)         AS 'PAYMENT_AMOUNT'
                           ,SUM(REFUND_AMOUNT)         AS 'REFUND_AMOUNT'
                           ,DISCOUNT_PRICE2
                           ,PAYMENT_NUMBER
                           ,MU_TREATMENT_PROCEDURE_ID
                           ,MU_USER_ID
                           ,USER_NAME
                           ,REG_DATE
                    FROM (SELECT MP.MU_USER_ID
                                 ,MU.USER_NAME
                                 ,DATE_FORMAT(MP.WRITE_DATE, '%Y-%m-%d')                                 AS 'PAYMENT_DATE'
                                 ,DATE_FORMAT(MR.WRITE_DATE, '%Y-%m-%d')                                 AS 'RESERVE_DATE'
                                 ,MPD.TREATMENT_NAME                                                     AS 'TREATMENT_NAME'
                                 ,MPD.TREATMENT_PROCEDURE_NAME                                           AS 'TREATMENT_PROCEDURE_NAME'
                                 ,MP.ETC                                                                 AS 'etc'
                                 ,IF(MP.STATUS = 'R' OR MP.STATUS = 'C', '', PAYMENT_CATEGORY_ITEM_NAME) AS 'PAYMENT_CATEGORY_ITEM_NAME'
                                 ,MM.NAME                                                                AS MEMBER_NAME
                                 ,RM.NAME                                                                AS RESERVE_MEMBER_NAME
                                 ,MAX((SELECT SUM(MPD2.DISCOUNT_PRICE)
                                      FROM MU_PAYMENT_DETAIL MPD2
                                      WHERE MP.PAYMENT_NUMBER = MPD2.PAYMENT_NUMBER))                   AS DISCOUNT_PRICE2
                                 ,SUM(
                                    IF(MP.STATUS = 'R' OR MP.STATUS = 'C'
                                    , MPD.TOTAL_AMOUNT+IFNULL(MEMBERSHIP_PRICE,0), 0)
                                    )                                                                    AS 'REFUND_AMOUNT'
                                 ,IF(MP.STATUS = 'R' OR MP.STATUS = 'C', 0, MPD.TOTAL_AMOUNT)            AS 'paymentAmount'
                                 ,IF(MP.STATUS = 'R' OR MP.STATUS = 'C', 0, MPD.MEMBERSHIP_PRICE)        AS 'MEMBERSHIP_PRICE'
                                 ,MP.STATUS                                                              AS 'STATUS_DESC1'
                                 ,MP.PAYMENT_NUMBER
                                 ,MPD.MU_TREATMENT_PROCEDURE_ID
                                 ,MP.REG_DATE
                          FROM MU_PAYMENT MP
                                   JOIN MU_PAYMENT_DETAIL MPD
                                       ON MPD.MU_PAYMENT_ID = MP.MU_PAYMENT_ID
                                   LEFT JOIN MU_RESERVE MR
                                       ON MPD.MU_RESERVE_ID = MR.MU_RESERVE_ID
                                   JOIN MU_MEMBER MM
                                       ON MPD.MU_MEMBER_ID = MM.MU_MEMBER_ID
                                   JOIN MU_MEMBER RM
                                       ON MPD.RESERVE_MEMBER_ID = RM.MU_MEMBER_ID
                                   JOIN MU_USER MU
                                       ON MP.MU_USER_ID = MU.MU_USER_ID
                          WHERE MP.MU_USER_ID = #{muUserId}
                            <if test="searchStartDate != null and searchStartDate != '' and searchEndDate != null and searchEndDate != ''">
                                AND (DATE_FORMAT(MP.WRITE_DATE, '%Y-%m-%d') BETWEEN #{searchStartDate} AND #{searchEndDate})
                            </if>
                            <if test="searchKeywordParam != null and searchKeywordParam != ''">
                            AND ((MP.USER_NAME LIKE CONCAT('%',TRIM(#{searchKeywordParam}),'%'))
                            OR (MP.TREATMENT_PROCEDURE_NAME LIKE CONCAT('%',TRIM(#{searchKeywordParam}),'%')))
                            </if>
                            <if test="muTreatmentId != null and muTreatmentId != ''and treatmentName == '기타'">
                                AND MPD.TREATMENT_NAME != '비만'
                                AND MPD.TREATMENT_NAME != '피부'
                                AND MPD.TREATMENT_NAME != '쁘띠'
                            </if>
                            <if test="muTreatmentId != null and muTreatmentId != '' and treatmentName != '기타'">
                                AND MPD.TREATMENT_NAME = #{treatmentName}
                            </if>
                            <if test="paymentCategoryItemNm != null and paymentCategoryItemNm != ''">
                            AND PAYMENT_CATEGORY_ITEM_NAME = #{paymentCategoryItemNm}
                            </if>
                          GROUP BY MP.MU_USER_ID
                                 , MU.USER_NAME
                                 , MP.WRITE_DATE
                                 , MR.WRITE_DATE
                                 , MP.ETC
                                 , PAYMENT_CATEGORY_ITEM_NAME
                                 , MM.NAME
                                 , RM.NAME
                                 , REFUND_AMOUNT
                                 , PAYMENT_AMOUNT
                                 , MP.STATUS
                                 , MP.PAYMENT_NUMBER
                                 ,MPD.MU_TREATMENT_PROCEDURE_ID
                          ) AS MP
                    GROUP BY  MEMBER_NAME
                             , DISCOUNT_PRICE2
                             , PAYMENT_NUMBER
                             , MU_TREATMENT_PROCEDURE_ID
                             , MU_USER_ID
                             , USER_NAME
                    ) MP
                       JOIN (SELECT PRICE
                                  , VAT
                                  , MU_TREATMENT_PROCEDURE_ID
                             FROM (SELECT PRICE
                                        , MTPP.VAT
                                        , MTPP.MU_TREATMENT_PROCEDURE_ID
                                   FROM MU_TREATMENT_PROCEDURE_PRICE MTPP
                                            JOIN MU_PAYMENT_DETAIL MPD
                                                 ON MTPP.MU_TREATMENT_PROCEDURE_ID = MPD.MU_TREATMENT_PROCEDURE_ID
                                            LEFT JOIN MU_RESERVE MR
                                                ON MPD.MU_RESERVE_ID = MR.MU_RESERVE_ID
                                            JOIN MU_PAYMENT MP
                                                ON MPD.MU_PAYMENT_ID = MP.MU_PAYMENT_ID
                                   WHERE MP.MU_USER_ID = #{muUserId}
                                   GROUP BY MTPP.MU_TREATMENT_PROCEDURE_ID
                                            ,MR.MU_RESERVE_ID
                                   ) AS MTPP
                             ) AS MTPP
                            ON MP.MU_TREATMENT_PROCEDURE_ID = MTPP.MU_TREATMENT_PROCEDURE_ID
              GROUP BY  MP.ETC
                       , MEMBER_NAME
                       , DISCOUNT_PRICE2
                       , PAYMENT_NUMBER
                       , MU_USER_ID
                       , USER_NAME
              ) MP
        GROUP BY  ETC
               , MEMBER_NAME
               , PAYMENT_NUMBER
               , MU_USER_ID
               , USER_NAME
               , status1
               ,REG_DATE
        <choose>
            <when test="sort != null and sort != ''">
                ORDER BY ${sort}
            </when>
            <otherwise>
                ORDER BY MP.REG_DATE DESC
            </otherwise>
        </choose>
                LIMIT ${start}, ${limit}
    </select>

    <select id="selectListPaymentUser2Excel" parameterType="hashmap" resultType="hashmap">
       SELECT  PAYMENT_DATE                                                                                               AS paymentDate
               ,IFNULL(RESERVE_DATE, '-')                                                                                  AS reserveDate
               ,GROUP_CONCAT(DISTINCT MP.TREATMENT_PROCEDURE_NAME SEPARATOR ', ')                                          AS treatmentProcedureNm
               ,GROUP_CONCAT(DISTINCT MP.TREATMENT_NAME)                                                                   AS treatmentName
               ,MP.ETC                                                                                                     AS etc
               ,IFNULL(GROUP_CONCAT(DISTINCT MP.PAYMENT_CATEGORY_ITEM_NAME SEPARATOR ', '),
                      '미납')                                                                                               AS paymentCategoryItemName
               ,GROUP_CONCAT(DISTINCT RESERVE_MEMBER_NAME SEPARATOR ', ')                                                  AS reserveMemberName
               ,MEMBER_NAME                                                                                                AS muMemberName
               ,AMOUNT                                                                                                     AS amount
               ,VAT                                                                                                        AS vat
               ,SUM(TOTAL_AMOUNT) - DISCOUNT_PRICE2                                                                        AS totalAmount
               ,MEMBERSHIP_PRICE                                                                                           AS membershipPrice
               ,REFUND_AMOUNT                                                                                              AS refundAmount
               ,PAYMENT_AMOUNT+MEMBERSHIP_PRICE                                                                            AS paymentAmount
               ,PAYMENT_NUMBER                                                                                             AS paymentNumber
               ,MU_USER_ID                                                                                                 AS muUserId
               ,USER_NAME                                                                                                  AS userName
               ,CASE
                    WHEN FIND_IN_SET('C', STATUS_DESC1) > 0 THEN '결제취소'
                    WHEN FIND_IN_SET('R', STATUS_DESC1) > 0 THEN '환불'
                    WHEN FIND_IN_SET('PY', STATUS_DESC1) > 0 THEN '부분결제(완료)'
                    WHEN FIND_IN_SET('NY', STATUS_DESC1) > 0 THEN '미수결제'
                    WHEN FIND_IN_SET('Y', STATUS_DESC1) > 0 THEN '결제완료'
                    WHEN FIND_IN_SET('P', STATUS_DESC1) > 0 THEN '부분결제'
                    WHEN FIND_IN_SET('NP', STATUS_DESC1) > 0 THEN '미수'
                   END                                                                                                    AS status1
               ,IFNULL(IF(FIND_IN_SET('NY', STATUS_DESC1) = 0 AND FIND_IN_SET('NP', STATUS_DESC1) > 0
                        OR FIND_IN_SET('PY', STATUS_DESC1) = 0 AND FIND_IN_SET('P', STATUS_DESC1) > 0,
                        PAYMENT_AMOUNT-SUM(TOTAL_AMOUNT)+ DISCOUNT_PRICE2-MEMBERSHIP_PRICE,'-'),0)                        AS nonpaymentAmount
        FROM (SELECT MAX(PAYMENT_DATE)                                                   AS PAYMENT_DATE
                     ,MAX(RESERVE_DATE)                                                 AS RESERVE_DATE
                     ,MP.ETC                                                            AS 'ETC'
                     ,GROUP_CONCAT(DISTINCT MP.TREATMENT_PROCEDURE_NAME SEPARATOR ', ') AS 'TREATMENT_PROCEDURE_NAME'
                     ,GROUP_CONCAT(DISTINCT MP.TREATMENT_NAME)                          AS 'TREATMENT_NAME'
                     ,STATUS_DESC1                                                      AS 'STATUS_DESC1'
                     ,GROUP_CONCAT(DISTINCT RESERVE_MEMBER_NAME SEPARATOR ', ')         AS 'RESERVE_MEMBER_NAME'
                     ,GROUP_CONCAT(DISTINCT IF(PAYMENT_CATEGORY_ITEM_NAME = '', NULL, PAYMENT_CATEGORY_ITEM_NAME) ORDER BY
                                  MP.PAYMENT_CATEGORY_ITEM_NAME SEPARATOR ', ')         AS 'PAYMENT_CATEGORY_ITEM_NAME'
                     ,SUM(PRICE + VAT)                                                  AS TOTAL_AMOUNT
                     ,SUM(VAT)                                                          AS 'VAT'
                     ,SUM(PRICE)                                                        AS 'AMOUNT'
                     ,DISCOUNT_PRICE2
                     ,IFNULL(SUM(MEMBERSHIP_PRICE),0)                                   AS 'MEMBERSHIP_PRICE'
                     ,SUM(REFUND_AMOUNT)                                                AS 'REFUND_AMOUNT'
                     ,IFNULL(SUM(PAYMENT_AMOUNT), 0)                                    AS 'PAYMENT_AMOUNT'
                     ,PAYMENT_NUMBER                                                    AS 'PAYMENT_NUMBER'
                     ,MU_USER_ID                                                        AS 'MU_USER_ID'
                     ,USER_NAME
                     ,REG_DATE
                     ,MEMBER_NAME
              FROM (SELECT PAYMENT_DATE
                           ,RESERVE_DATE
                           ,TREATMENT_PROCEDURE_NAME   AS 'TREATMENT_PROCEDURE_NAME'
                           ,TREATMENT_NAME             AS 'TREATMENT_NAME'
                           ,GROUP_CONCAT(DISTINCT STATUS_DESC1 SEPARATOR ',')
                                                      AS 'STATUS_DESC1'
                           ,RESERVE_MEMBER_NAME        AS 'RESERVE_MEMBER_NAME'
                           ,GROUP_CONCAT(DISTINCT IF(PAYMENT_CATEGORY_ITEM_NAME = '', NULL, PAYMENT_CATEGORY_ITEM_NAME) ORDER BY
                                        MP.PAYMENT_CATEGORY_ITEM_NAME SEPARATOR ', ')
                                                      AS 'PAYMENT_CATEGORY_ITEM_NAME'
                           ,MEMBER_NAME
                           ,MP.ETC
                           ,SUM(MEMBERSHIP_PRICE)      AS 'MEMBERSHIP_PRICE'
                           ,SUM(paymentAmount)         AS 'PAYMENT_AMOUNT'
                           ,SUM(REFUND_AMOUNT)         AS 'REFUND_AMOUNT'
                           ,DISCOUNT_PRICE2
                           ,PAYMENT_NUMBER
                           ,MU_TREATMENT_PROCEDURE_ID
                           ,MU_USER_ID
                           ,USER_NAME
                           ,REG_DATE
                    FROM (SELECT MP.MU_USER_ID
                                 ,MU.USER_NAME
                                 ,DATE_FORMAT(MP.WRITE_DATE, '%Y-%m-%d')                                 AS 'PAYMENT_DATE'
                                 ,DATE_FORMAT(MR.WRITE_DATE, '%Y-%m-%d')                                 AS 'RESERVE_DATE'
                                 ,MPD.TREATMENT_NAME                                                     AS 'TREATMENT_NAME'
                                 ,MPD.TREATMENT_PROCEDURE_NAME                                           AS 'TREATMENT_PROCEDURE_NAME'
                                 ,MP.ETC                                                                 AS 'etc'
                                 ,IF(MP.STATUS = 'R' OR MP.STATUS = 'C', '', PAYMENT_CATEGORY_ITEM_NAME) AS 'PAYMENT_CATEGORY_ITEM_NAME'
                                 ,MM.NAME                                                                AS MEMBER_NAME
                                 ,RM.NAME                                                                AS RESERVE_MEMBER_NAME
                                 ,MAX((SELECT SUM(MPD2.DISCOUNT_PRICE)
                                      FROM MU_PAYMENT_DETAIL MPD2
                                      WHERE MP.PAYMENT_NUMBER = MPD2.PAYMENT_NUMBER))                   AS DISCOUNT_PRICE2
                                 ,SUM(
                                    IF(MP.STATUS = 'R' OR MP.STATUS = 'C'
                                    , MPD.TOTAL_AMOUNT+IFNULL(MEMBERSHIP_PRICE,0), 0)
                                    )                                                                    AS 'REFUND_AMOUNT'
                                 ,IF(MP.STATUS = 'R' OR MP.STATUS = 'C', 0, MPD.TOTAL_AMOUNT)            AS 'paymentAmount'
                                 ,IF(MP.STATUS = 'R' OR MP.STATUS = 'C', 0, MPD.MEMBERSHIP_PRICE)        AS 'MEMBERSHIP_PRICE'
                                 ,MP.STATUS                                                              AS 'STATUS_DESC1'
                                 ,MP.PAYMENT_NUMBER
                                 ,MPD.MU_TREATMENT_PROCEDURE_ID
                                 ,MP.REG_DATE
                          FROM MU_PAYMENT MP
                                   JOIN MU_PAYMENT_DETAIL MPD
                                       ON MPD.MU_PAYMENT_ID = MP.MU_PAYMENT_ID
                                   LEFT JOIN MU_RESERVE MR
                                       ON MPD.MU_RESERVE_ID = MR.MU_RESERVE_ID
                                   JOIN MU_MEMBER MM
                                       ON MPD.MU_MEMBER_ID = MM.MU_MEMBER_ID
                                   JOIN MU_MEMBER RM
                                       ON MPD.RESERVE_MEMBER_ID = RM.MU_MEMBER_ID
                                   JOIN MU_USER MU
                                       ON MP.MU_USER_ID = MU.MU_USER_ID
                          WHERE MP.MU_USER_ID = #{muUserId}
                            <if test="searchStartDate != null and searchStartDate != '' and searchEndDate != null and searchEndDate != ''">
                                AND (DATE_FORMAT(MP.WRITE_DATE, '%Y-%m-%d') BETWEEN #{searchStartDate} AND #{searchEndDate})
                            </if>
                            <if test="searchKeywordParam != null and searchKeywordParam != ''">
                            AND ((MP.USER_NAME LIKE CONCAT('%',TRIM(#{searchKeywordParam}),'%'))
                            OR (MP.TREATMENT_PROCEDURE_NAME LIKE CONCAT('%',TRIM(#{searchKeywordParam}),'%')))
                            </if>
                            <if test="muTreatmentId != null and muTreatmentId != ''and treatmentName == '기타'">
                                AND MPD.TREATMENT_NAME != '비만'
                                AND MPD.TREATMENT_NAME != '피부'
                                AND MPD.TREATMENT_NAME != '쁘띠'
                            </if>
                            <if test="muTreatmentId != null and muTreatmentId != '' and treatmentName != '기타'">
                                AND MPD.TREATMENT_NAME = #{treatmentName}
                            </if>
                            <if test="paymentCategoryItemNm != null and paymentCategoryItemNm != ''">
                            AND PAYMENT_CATEGORY_ITEM_NAME = #{paymentCategoryItemNm}
                            </if>
                          GROUP BY MP.MU_USER_ID
                                 , MU.USER_NAME
                                 , MP.WRITE_DATE
                                 , MR.WRITE_DATE
                                 , MP.ETC
                                 , PAYMENT_CATEGORY_ITEM_NAME
                                 , MM.NAME
                                 , RM.NAME
                                 , REFUND_AMOUNT
                                 , PAYMENT_AMOUNT
                                 , MP.STATUS
                                 , MP.PAYMENT_NUMBER
                                 ,MPD.MU_TREATMENT_PROCEDURE_ID
                          ) AS MP
                    GROUP BY  MEMBER_NAME
                             , DISCOUNT_PRICE2
                             , PAYMENT_NUMBER
                             , MU_TREATMENT_PROCEDURE_ID
                             , MU_USER_ID
                             , USER_NAME
                    ) MP
                       JOIN (SELECT PRICE
                                  , VAT
                                  , MU_TREATMENT_PROCEDURE_ID
                             FROM (SELECT PRICE
                                        , MTPP.VAT
                                        , MTPP.MU_TREATMENT_PROCEDURE_ID
                                   FROM MU_TREATMENT_PROCEDURE_PRICE MTPP
                                            JOIN MU_PAYMENT_DETAIL MPD
                                                 ON MTPP.MU_TREATMENT_PROCEDURE_ID = MPD.MU_TREATMENT_PROCEDURE_ID
                                            LEFT JOIN MU_RESERVE MR
                                                ON MPD.MU_RESERVE_ID = MR.MU_RESERVE_ID
                                            JOIN MU_PAYMENT MP
                                                ON MPD.MU_PAYMENT_ID = MP.MU_PAYMENT_ID
                                   WHERE MP.MU_USER_ID = #{muUserId}
                                   GROUP BY MTPP.MU_TREATMENT_PROCEDURE_ID
                                           ,MR.MU_RESERVE_ID
                                   ) AS MTPP
                             ) AS MTPP
                            ON MP.MU_TREATMENT_PROCEDURE_ID = MTPP.MU_TREATMENT_PROCEDURE_ID
              GROUP BY  MP.ETC
                       , MEMBER_NAME
                       , DISCOUNT_PRICE2
                       , PAYMENT_NUMBER
                       , MU_USER_ID
                       , USER_NAME
              ) MP
        GROUP BY  ETC
               , MEMBER_NAME
               , PAYMENT_NUMBER
               , MU_USER_ID
               , USER_NAME
               , status1
               ,REG_DATE
        <choose>
            <when test="sort != null and sort != ''">
                ORDER BY ${sort}
            </when>
            <otherwise>
                ORDER BY MP.REG_DATE DESC
            </otherwise>
        </choose>
    </select>

    <select id="selectPayment" parameterType="hashmap" resultType="hashmap">
        SELECT
              MU_PAYMENT_ID                 AS muPaymentId
             ,PAYMENT_NUMBER                AS paymentNumber
             ,MU_USER_ID                    AS muUserId
             ,PAYMENT_CATEGORY_ITEM_ID      AS paymentCategoryItemId
             ,PAYMENT_CATEGORY_ITEM_NAME    AS paymentCategoryItemName
             ,PAYMENT_CARD_INFO             AS paymentCardInfo
             ,CARD_INSTALLMENTS             AS cardInstallments
             ,AMOUNT                        AS amount
             ,VAT                           AS vat
             ,PAYMENT_AMOUNT                AS paymentAmount
        FROM MU_PAYMENT
        WHERE MU_USER_ID = #{muUserId}
          AND MU_PAYMENT_ID = #{muPaymentId}
          AND PAYMENT_AMOUNT !=''
          AND( STATUS = 'Y'|| STATUS = 'P'|| STATUS = 'PY'|| STATUS = 'NY')
        ORDER BY PAYMENT_CATEGORY_ITEM_ID
    </select>

    <select id="selectPaymentPaymentNumber" parameterType="hashmap" resultType="hashmap">
        SELECT CASE
               WHEN Y_COUNT = 0
                AND X_COUNT = 0
               THEN 'Y'
               ELSE IF(MP.Y_COUNT = X_COUNT, 'N', 'Y')
                END       AS "cancelYn"
          FROM (
                SELECT COUNT(CASE WHEN MP.STATUS IN ('NY', 'Y', 'PY', 'CY') THEN 1 END) AS Y_COUNT
                      ,COUNT(CASE WHEN MP.STATUS IN ('C', 'R', 'CN') THEN 1 END) AS X_COUNT
                  FROM MU_PAYMENT AS MP
                 WHERE MP.USE_YN = 'Y'
                   AND MP.MU_USER_ID = #{muUserId}
                   AND MP.PAYMENT_NUMBER = #{paymentNumber}
          ) AS MP
        <!-- SELECT MP.MU_PAYMENT_ID     AS "muPaymentId"
              ,MP.PAYMENT_NUMBER    AS "paymentNumber"
              ,MP.STATUS            AS "status"
          FROM MU_PAYMENT AS MP
         WHERE MP.USE_YN = 'Y'
           AND MP.MU_USER_ID = #{muUserId}
           AND MP.PAYMENT_NUMBER = #{paymentNumber}
      ORDER BY MP.REG_DATE DESC
        LIMIT 0, 1 -->
    </select>

    <select id="selectPaymentDetail" parameterType="hashmap" resultType="hashmap">
        SELECT MU_PAYMENT_ID                   AS muPaymentId
              ,PAYMENT_NUMBER                AS paymentNumber
              ,MU_RESERVE_ID                 AS muReserveId
              ,MU_TREATMENT_ID               AS muTreatmentId
              ,TREATMENT_NAME                AS treatmentName
              ,MU_TREATMENT_PROCEDURE_ID     AS muTreatmentProcedureId
              ,TREATMENT_PROCEDURE_NAME      AS treatmentProcedureName
              ,RESERVE_MEMBER_ID             AS reserveMemberId
              ,AMOUNT                        AS amount
              ,VAT                           AS vat
              ,TOTAL_AMOUNT                  AS totalAmount
              ,MEMBERSHIP_PRICE              AS membershipPrice
          FROM MU_PAYMENT_DETAIL
         WHERE MU_PAYMENT_ID = #{muPaymentId}
           AND (TOTAL_AMOUNT>0
                OR NOT ISNULL(MEMBERSHIP_PRICE))
    </select>
    <select id="selectPaymentStatusCheck" parameterType="hashmap" resultType="hashmap">
    SELECT SUBSTR(MP.STATUS_DESC, 1, 1) AS "status"
      FROM (
            SELECT IFNULL(GROUP_CONCAT(DISTINCT MP.STATUS ORDER BY MP.REG_DATE DESC SEPARATOR ','), '')   AS STATUS_DESC
              FROM MU_PAYMENT AS MP
        INNER JOIN MU_PAYMENT_DETAIL AS MPD
                ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
               AND MP.USE_YN = 'Y'
               AND MPD.USE_YN = 'Y'
               AND MP.PAYMENT_NUMBER IN (
                                         SELECT MPD.PAYMENT_NUMBER
                                           FROM MU_PAYMENT_DETAIL AS MPD
                                          WHERE MPD.USE_YN = 'Y'
                                            AND MPD.MU_RESERVE_ID = #{muReserveId}
                                       GROUP BY MPD.PAYMENT_NUMBER
                        )
             LIMIT 0, 1
      ) AS MP
     WHERE MP.STATUS_DESC != ''
    </select>
    <select id="selectPaymentNumber" parameterType="hashmap" resultType="hashmap">
        SELECT PAYMENT_NUMBER                                            AS paymentNumber
              ,IF(SUM(MTPP.PRICE)+SUM(MTPP.VAT)-SUM(PREVIOUS_AMOUNT)-SUM(DISCOUNT_PRICE)=ABS(#{paymentAmount})
                  ,IF(STATUS='NP','NY',IF(STATUS = 'CN', 'CY', 'PY'))
                  ,IF(#{paymentCategoryItemId}='MM','P',IF(STATUS='NP','NY',IF(STATUS = 'CN', 'CY', 'Y'))))
                                                                         AS status
         FROM (SELECT MP.PAYMENT_NUMBER
                     ,SUM(IF(PAYMENT_CATEGORY_ITEM_ID='MM'
                             ,-MEMBERSHIP_PRICE
                             ,TOTAL_AMOUNT))            AS PREVIOUS_AMOUNT
                     ,(SELECT MP2.STATUS
                         FROM MU_PAYMENT AS MP2
                        WHERE MP2.USE_YN = 'Y'
                          AND MP2.MU_PAYMENT_ID = #{id}
                     ORDER BY MP2.REG_DATE DESC
                        LIMIT 0, 1)                     AS STATUS
                     ,MU_TREATMENT_PROCEDURE_ID
                     ,SUM(MPD.DISCOUNT_PRICE)           AS DISCOUNT_PRICE
                 FROM MU_PAYMENT MP
                 JOIN MU_PAYMENT_DETAIL MPD
                   ON MPD.MU_PAYMENT_ID=MP.MU_PAYMENT_ID
                WHERE MP.PAYMENT_NUMBER = (SELECT PAYMENT_NUMBER
                                             FROM MU_PAYMENT
                                            WHERE MU_PAYMENT_ID = #{id})
             GROUP BY MP.PAYMENT_NUMBER
                     ,MU_TREATMENT_PROCEDURE_ID
         ) AS MP
         JOIN MU_TREATMENT_PROCEDURE_PRICE MTPP
           ON MTPP.MU_TREATMENT_PROCEDURE_ID = MP.MU_TREATMENT_PROCEDURE_ID
          AND USE_YN='Y'
    </select>
    <select id="selectReserveId" parameterType="String" resultType="hashmap">
        SELECT MU_RESERVE_ID    AS muReserveId
        FROM MU_PAYMENT_DETAIL
        WHERE MU_PAYMENT_ID = #{muPaymentId}
        GROUP BY MU_RESERVE_ID
    </select>

    <select id="selectListPaymentCheck" parameterType="hashmap" resultType="hashmap">
		SELECT MR.MU_PAYMENT_ID        AS "muPaymentId"
		  FROM MU_PAYMENT AS MR
		 WHERE MR.CRM_MEMBER_PAY_PID = #{crmMemberPayPid}
    </select>

    <insert id="insertPayment" parameterType="hashmap">
        <selectKey resultType="string" keyProperty="id" order="BEFORE">
            SELECT CONCAT(#{muPaymentId},LPAD((SELECT NEXTVAL(MU_PAYMENT_SEQ)), 9, 0))
        </selectKey>
        INSERT INTO MU_PAYMENT(
             MU_PAYMENT_ID
            ,PAYMENT_NUMBER
            <if test="muPaymentRequestHistoryId != null and muPaymentRequestHistoryId != ''">
            ,MU_PAYMENT_REQUEST_HISTORY_ID
            </if>
            <if test="muPaymentResponseHistoryId != null and muPaymentResponseHistoryId != ''">
            ,MU_PAYMENT_RESPONSE_HISTORY_ID
            </if>
            <if test="paymentApprovalId != null and paymentApprovalId != ''">
            ,PAYMENT_APPROVAL_ID
            </if>
            ,MU_USER_ID
            ,PAYMENT_CATEGORY_ITEM_ID
            ,PAYMENT_CATEGORY_ITEM_NAME
            <if test="paymentCardInfo != null and paymentCardInfo != ''">
            ,PAYMENT_CARD_INFO
            </if>
            <if test="cardInstallments != null and cardInstallments != ''">
            ,CARD_INSTALLMENTS
            </if>
            <if test="cardInstallmentsCode != null and cardInstallmentsCode != ''">
            ,CARD_INSTALLMENTS_CODE
            </if>
            ,MU_MEMBER_ID
            <if test="etc != null and etc != ''">
            ,ETC
            </if>
            <if test="refundEtc != null and refundEtc != ''">
            ,REFUND_ETC
            </if>
            <if test="refundAccount != null and refundAccount != ''">
            ,REFUND_ACCOUNT
            </if>
            ,AMOUNT
            ,VAT
            <if test="paymentAmount != null and paymentAmount != ''">
            ,PAYMENT_AMOUNT
            </if>
            <if test="refundAmount != null and refundAmount != ''">
            ,REFUND_AMOUNT
            </if>
            ,STATUS
            <if test="firstVisitYn != null and firstVisitYn != ''">
            ,FIRST_VISIT_YN
            </if>
            ,WRITE_DATE
            ,WRITE_TIME
            ,CUD_FLAG
            ,USE_YN
            ,REG_ID
            ,REG_DATE
            ,MOD_ID
            ,MOD_DATE
            ,T_ID
            ,T_DATE
        )VALUES(
            #{id}
            ,#{paymentNumber}
            <if test="muPaymentRequestHistoryId != null and muPaymentRequestHistoryId != ''">
            ,#{muPaymentRequestHistoryId}
            </if>
            <if test="muPaymentResponseHistoryId != null and muPaymentResponseHistoryId != ''">
            ,#{muPaymentResponseHistoryId}
            </if>
            <if test="paymentApprovalId != null and paymentApprovalId != ''">
            ,#{paymentApprovalId}
            </if>
            ,#{muUserId}
            ,#{paymentCategoryItemId}
            ,#{paymentCategoryItemName}
            <if test="paymentCardInfo != null and paymentCardInfo != ''">
            ,#{paymentCardInfo}
            </if>
            <if test="cardInstallments != null and cardInstallments != ''">
            ,#{cardInstallments}
            </if>
            <if test="cardInstallmentsCode != null and cardInstallmentsCode != ''">
            ,#{cardInstallmentsCode}
            </if>
            ,#{muMemberId}
            <if test="etc != null and etc != ''">
            ,#{etc}
            </if>
            <if test="refundEtc != null and refundEtc != ''">
            ,#{refundEtc}
            </if>
            <if test="refundAccount != null and refundAccount != ''">
            ,#{refundAccount}
            </if>
            ,#{amount}
            ,#{vat}
            <if test="paymentAmount != null and paymentAmount != ''">
            ,#{paymentAmount}
            </if>
            <if test="refundAmount != null and refundAmount != ''">
            ,#{refundAmount}
            </if>
            ,#{status}
            <if test="firstVisitYn != null and firstVisitYn != ''">
            ,#{firstVisitYn}
            </if>
            ,CURDATE()
            ,CURTIME()
            ,'C'
            ,'Y'
            ,#{regId}
            ,NOW()
            ,#{modId}
            ,NOW()
            ,#{tId}
            ,#{tDate}
        )
    </insert>

    <insert id="insertPaymentBatch" parameterType="hashmap">
        <selectKey resultType="string" keyProperty="id" order="BEFORE">
            SELECT CONCAT(#{muPaymentId},LPAD((SELECT NEXTVAL(MU_PAYMENT_SEQ)), 9, 0))
        </selectKey>
        INSERT INTO MU_PAYMENT(
                     MU_PAYMENT_ID
                    ,PAYMENT_NUMBER
                    ,MU_USER_ID
                    ,PAYMENT_CATEGORY_ITEM_ID
                    ,PAYMENT_CATEGORY_ITEM_NAME
                    ,PAYMENT_CARD_INFO
                    ,MU_MEMBER_ID
                    ,ETC
                    ,AMOUNT
                    ,VAT
                    ,PAYMENT_AMOUNT
                    ,REFUND_AMOUNT
                    ,STATUS
                    ,FIRST_VISIT_YN
                    ,WRITE_DATE
                    ,WRITE_TIME
                    ,REMARK
                    ,CUD_FLAG
                    ,USE_YN
                    ,REG_ID
                    ,REG_DATE
                    ,MOD_ID
                    ,MOD_DATE
                    ,T_ID
                    ,T_DATE
                    ,CRM_MEMBER_PAY_PID
                    ,CRM_MEMBER_PAY_MB_PID
                    ,CRM_MEMBER_PAY_PAY_MEMO
                    ,CRM_MEMBER_PAY_PAY_MEMO2
                    ,CRM_MEMBER_PAY_PAY_DIVI1
                    ,CRM_MEMBER_PAY_PAY_DIVI2
                    ,CRM_MEMBER_PAY_PAY_STAFF
                    ,CRM_MEMBER_PAY_STAFF_NAME
                    ,CRM_MEMBER_PAY_M_CNT
                    ,CRM_MEMBER_PAY_M_CNT_DIVI
                    ,CRM_MEMBER_PAY_PAY_MEMBERSHIP
                    ,CRM_MEMBER_PAY_PAY_SUM
                    ,CRM_MEMBER_PAY_PAY_CARD
                    ,CRM_MEMBER_PAY_PAY_CASH_RECEIPT_TYPE
                    ,CRM_MEMBER_PAY_PAY_CASH_RECEIPT
                    ,CRM_MEMBER_PAY_PAY_CASH
                    ,CRM_MEMBER_PAY_PAY_CASH_BANK
                    ,CRM_MEMBER_PAY_PAY_VAT
                    ,CRM_MEMBER_PAY_PAY_DEBT
                    ,CRM_MEMBER_PAY_PAY_BACK
                    ,CRM_MEMBER_PAY_TAX_FREE
                    ,CRM_MEMBER_PAY_PAY_RECEIPT
                    ,CRM_MEMBER_PAY_PAY_CARD_NAME
                    ,CRM_MEMBER_PAY_PAY_BACK_CARD_NAME
                    ,CRM_MEMBER_PAY_PAY_BACK_CARD_COMMISSION
                    ,CRM_MEMBER_PAY_PAY_BACK_CARD_COMMISSION_PAY
                    ,CRM_MEMBER_PAY_PAY_BACK_CARD_IN
                    ,CRM_MEMBER_PAY_PAY_SUM_CARD_NAME
                    ,CRM_MEMBER_PAY_PAY_SUM_CARD_COMMISSION
                    ,CRM_MEMBER_PAY_PAY_SUM_CARD_COMMISSION_PAY
                    ,CRM_MEMBER_PAY_PAY_SUM_CARD_IN
                    ,CRM_MEMBER_PAY_PAY_SUM_CARD
                    ,CRM_MEMBER_PAY_PAY_TYPE
                    ,CRM_MEMBER_PAY_PAY_COUNTRY
                    ,CRM_MEMBER_PAY_MBSHIP_NOTE
                    ,CRM_MEMBER_PAY_MBSHIP_TYPE
        )     SELECT #{id}                                       AS MU_PAYMENT_ID <!-- 식별자 -->
                    ,CMP.PID                                     AS PAYMENT_NUMBER <!-- 결제번호(부분결제식별) (기존 DB 식별자로 활용) -->
                    ,#{muUserId}                                 AS MU_USER_ID <!-- 고객 식별자 -->
                    ,CASE
                     WHEN CMP.PAY_MEMBERSHIP &lt; 0
                      AND CMP.M_DIVI2_NAME != '양도' <!-- 순수 멤버십 사용 -->
                     THEN 'MM'
                     WHEN CMP.PAY_TYPE = '카드'
                     THEN 'C'
                     WHEN CMP.PAY_TYPE = '통장입금'
                     THEN 'A'
                     WHEN CMP.PAY_TYPE = '현금'
                     THEN 'M'
                     WHEN CMP.PAY_TYPE = '현금영수증'
                     THEN 'MB'
                     ELSE ''
                      END                                        AS PAYMENT_CATEGORY_ITEM_ID <!-- 결제구분 식별자(M:현금, MM:멤버십, C:카드, A:계좌이체,PP:선결제,Q:간편결제) -->
                    ,CASE
                     WHEN CMP.PAY_MEMBERSHIP &lt; 0
                      AND CMP.M_DIVI2_NAME != '양도' <!-- 순수 멤버십 사용 -->
                     THEN '멤버십'
                     WHEN CMP.PAY_TYPE = '카드'
                     THEN '카드'
                     WHEN CMP.PAY_TYPE = '통장입금'
                     THEN '계좌이체'
                     WHEN CMP.PAY_TYPE = '현금'
                     THEN '현금'
                     ELSE CMP.PAY_TYPE
                      END                                        AS PAYMENT_CATEGORY_ITEM_NAME <!-- 결제구분 이름 -->
                    ,CMP.PAY_CARD_NAME                           AS PAYMENT_CARD_INFO <!-- 결제 카드 정보 -->
                    ,NULL                                        AS MU_MEMBER_ID <!-- 담당자 식별자 -->
                    ,CONCAT(CMP.PAY_MEMO, '\n', CMP.PAY_MEMO2)   AS ETC <!-- 결제 메모 -->
                    ,NULL                                        AS AMOUNT <!-- 공급가(결제 금액) -->
                    ,CMP.PAY_VAT                                 AS VAT <!-- 부가세(결제 금액) -->
                    ,CMP.PAY_SUM                                 AS PAYMENT_AMOUNT <!-- 결제 총액 -->
                    ,CMP.PAY_BACK                                AS REFUND_AMOUNT	<!-- 결제 취소 금액(환불금액) -->
                    ,CASE
                     WHEN CMP.PAY_DIVI1 = '결제'
                     THEN 'Y'
                     WHEN CMP.PAY_DIVI1 = '결제-미수금'
                     THEN 'NY'
                     WHEN CMP.PAY_DIVI1 LIKE '%환불%'
                     THEN 'R'
                     ELSE ''
                      END                                        AS STATUS <!-- 결제상태(NP:미수,NY:미수결제, Y:결제완료, C:결제취소, R:환불,P:부분결제,PY:부분결제(완료)) -->
                    ,CASE
                     WHEN CMP.M_CNT_DIVI = '초진'
                     THEN 'Y'
                     WHEN CMP.M_CNT_DIVI = '재진'
                     THEN 'N'
                     ELSE ''
                      END                                        AS FIRST_VISIT_YN <!-- 초진 여부('Y':초진,'N':재진) -->
                    ,CMP.PAY_DATE                                AS WRITE_DATE <!-- 작성일자 -->
                    ,CMP.REG_DATE                                AS WRITE_TIME <!-- 작성시간 -->
                    ,NULL                                        AS REMARK <!-- 비고 -->
                    ,'C'                                         AS CUD_FLAG <!-- 작업타입 (C: 등록, U: 수정, D : 삭제) -->
                    ,CASE
                     WHEN UPPER(CMP.LIST_DEL) = 'N'
                     THEN 'Y'
                     WHEN UPPER(CMP.LIST_DEL) = 'Y'
                     THEN 'N'
                     ELSE ''
                      END                                        AS USE_YN <!-- 사용여부 -->
                    ,'BatchSystem'                               AS REG_ID <!-- 등록자 -->
                    ,CMP.REG_DATE                                AS REG_DATE <!-- 등록일자 -->
                    ,'BatchSystem'                               AS MOD_ID <!-- 수정자 -->
                    ,CMP.UP_DATE                                 AS MOD_DATE <!-- 수정일자 -->
                    ,#{tId}                                      AS T_ID
                    ,#{tDate}                                    AS T_DATE
                    ,CMP.PID                                     AS CRM_MEMBER_PAY_PID <!-- 기존 DB 식별자 -->
                    ,CMP.MB_PID                                  AS CRM_MEMBER_PAY_MB_PID <!-- 기존 DB 고객 식별자 -->
                    ,CMP.PAY_MEMO                                AS CRM_MEMBER_PAY_PAY_MEMO <!-- 기존 DB 메모 -->
                    ,CMP.PAY_MEMO2                               AS CRM_MEMBER_PAY_PAY_MEMO2 <!-- 기존 DB 메모2 -->
                    ,CMP.PAY_DIVI1                               AS CRM_MEMBER_PAY_PAY_DIVI1 <!-- 기존 DB 결제상태 -->
                    ,CMP.PAY_DIVI2                               AS CRM_MEMBER_PAY_PAY_DIVI2 <!-- 기존 DB 결제상태2 -->
                    ,CMP.PAY_STAFF                               AS CRM_MEMBER_PAY_PAY_STAFF <!-- 기존 DB 담당자 식별자 -->
                    ,CMP.STAFF_NAME                              AS CRM_MEMBER_PAY_STAFF_NAME <!-- 기존 DB 담당자 이름 -->
                    ,CMP.M_CNT                                   AS CRM_MEMBER_PAY_M_CNT <!-- 기존 DB 초진/재진 카운트 -->
                    ,CMP.M_CNT_DIVI                              AS CRM_MEMBER_PAY_M_CNT_DIVI <!-- 기존 DB 초진/재진 구분 -->
                    ,CMP.PAY_MEMBERSHIP                          AS CRM_MEMBER_PAY_PAY_MEMBERSHIP <!-- 기존 DB 멤버쉽 -->
                    ,CMP.PAY_SUM                                 AS CRM_MEMBER_PAY_PAY_SUM <!-- 기존 DB 계 -->
                    ,CMP.PAY_CARD                                AS CRM_MEMBER_PAY_PAY_CARD <!-- 기존 DB 카드 -->
                    ,CMP.PAY_CASH_RECEIPT_TYPE                   AS CRM_MEMBER_PAY_PAY_CASH_RECEIPT_TYPE <!-- 기존 DB 현금영수증(현금, 통장) 구분 -->
                    ,CMP.PAY_CASH_RECEIPT                        AS CRM_MEMBER_PAY_PAY_CASH_RECEIPT <!-- 기존 DB 현금영수증 금액 -->
                    ,CMP.PAY_CASH                                AS CRM_MEMBER_PAY_PAY_CASH <!-- 기존 DB 현금 -->
                    ,CMP.PAY_CASH_BANK                           AS CRM_MEMBER_PAY_PAY_CASH_BANK <!-- 기존 DB 통장 -->
                    ,CMP.PAY_VAT                                 AS CRM_MEMBER_PAY_PAY_VAT <!-- 기존 DB vat -->
                    ,CMP.PAY_DEBT                                AS CRM_MEMBER_PAY_PAY_DEBT <!-- 기존 DB 미수 -->
                    ,CMP.PAY_BACK                                AS CRM_MEMBER_PAY_PAY_BACK <!-- 기존 DB 환불 -->
                    ,CMP.TAX_FREE                                AS CRM_MEMBER_PAY_TAX_FREE <!-- 기존 DB TAX_FREE -->
                    ,CMP.PAY_RECEIPT                             AS CRM_MEMBER_PAY_PAY_RECEIPT <!-- 기존 DB 영수증 -->
                    ,CMP.PAY_CARD_NAME                           AS CRM_MEMBER_PAY_PAY_CARD_NAME <!-- 기존 DB 결제카드명 -->
                    ,CMP.PAY_BACK_CARD_NAME                      AS CRM_MEMBER_PAY_PAY_BACK_CARD_NAME <!-- 기존 DB 결제취소 카드명 -->
                    ,CMP.PAY_BACK_CARD_COMMISSION                AS CRM_MEMBER_PAY_PAY_BACK_CARD_COMMISSION <!-- 기존 DB 결제취소 카드 수수료 -->
                    ,CMP.PAY_BACK_CARD_COMMISSION_PAY            AS CRM_MEMBER_PAY_PAY_BACK_CARD_COMMISSION_PAY <!-- 기존 DB 결제취소 수수료 금액 -->
                    ,CMP.PAY_BACK_CARD_IN                        AS CRM_MEMBER_PAY_PAY_BACK_CARD_IN <!-- 기존 DB 결제취소 금액 -->
                    ,CMP.PAY_SUM_CARD_NAME                       AS CRM_MEMBER_PAY_PAY_SUM_CARD_NAME <!-- 기존 DB 결제 카드명 -->
                    ,CMP.PAY_SUM_CARD_COMMISSION                 AS CRM_MEMBER_PAY_PAY_SUM_CARD_COMMISSION <!-- 기존 DB 카드 수수료% -->
                    ,CMP.PAY_SUM_CARD_COMMISSION_PAY             AS CRM_MEMBER_PAY_PAY_SUM_CARD_COMMISSION_PAY <!-- 기존 DB 카드 수수료 금액 -->
                    ,CMP.PAY_SUM_CARD_IN                         AS CRM_MEMBER_PAY_PAY_SUM_CARD_IN <!-- 기존 DB 정산금액 -->
                    ,CMP.PAY_SUM_CARD                            AS CRM_MEMBER_PAY_PAY_SUM_CARD <!-- 기존 DB 카드사 식별번호 -->
                    ,CMP.PAY_TYPE                                AS CRM_MEMBER_PAY_PAY_TYPE <!-- 기존 DB 결제타입 -->
                    ,CMP.PAY_COUNTRY                             AS CRM_MEMBER_PAY_PAY_COUNTRY <!-- 기존 DB 결제국가 -->
                    ,CMP.MBSHIP_NOTE                             AS CRM_MEMBER_PAY_MBSHIP_NOTE <!-- 기존 DB 멤버쉽 비고 -->
                    ,CMP.MBSHIP_TYPE                             AS CRM_MEMBER_PAY_MBSHIP_TYPE <!-- 기존 DB 멤버십 타입(멤버십 상품) -->
                FROM CRM_MEMBER_PAY AS CMP
               WHERE CMP.PID = #{crmMemberPayPid}
    </insert>

    <insert id="insertPaymentRequestHistory" parameterType="hashmap">
        <selectKey resultType="string" keyProperty="id" order="BEFORE">
            SELECT CONCAT(#{muPaymentRequestHistoryId},LPAD((SELECT NEXTVAL(MU_PAYMENT_REQUEST_HISTORY_SEQ)), 9, 0))
        </selectKey>
        INSERT INTO MU_PAYMENT_REQUEST_HISTORY(
             MU_PAYMENT_REQUEST_HISTORY_ID
            ,MU_USER_ID
            ,USER_NAME
            ,TRANSACTION_TYPE
            <if test="installmentMonths != null and installmentMonths != ''">
            ,INSTALLMENT_MONTHS
            </if>
            <if test="cashReceiptInfo != null and cashReceiptInfo != ''">
            ,CASH_RECEIPT_INFO
            </if>
            <if test="cashReceiptPurpose != null and cashReceiptPurpose != ''">
            ,CASH_RECEIPT_PURPOSE
            </if>
            <if test="earnPoint != null and earnPoint != ''">
            ,EARN_POINT
            </if>
            <if test="cancelOriginalApprovalDate != null and cancelOriginalApprovalDate != ''">
            ,CANCEL_ORIGINAL_APPROVAL_DATE
            </if>
            <if test="cancelOriginalApprovalNo != null and cancelOriginalApprovalNo != ''">
            ,CANCEL_ORIGINAL_APPROVAL_NO
            </if>
            ,TOTAL_AMOUNT
            <if test="vatAmount != null and vatAmount != ''">
            ,VAT_AMOUNT
            </if>
            <if test="posTransactionNumber != null and posTransactionNumber != ''">
            ,POS_TRANSACTION_NUMBER
            </if>
            ,WRITE_DATE
            ,WRITE_TIME
            ,REMARK
            ,CUD_FLAG
            ,USE_YN
            ,REG_ID
            ,REG_DATE
            ,MOD_ID
            ,MOD_DATE
            ,T_ID
            ,T_DATE
        )VALUES(
            #{id}
            ,#{muUserId}
            ,#{userName}
            ,#{transactionType}
            <if test="installmentMonths != null and installmentMonths != ''">
            ,#{installmentMonths}
            </if>
            <if test="cashReceiptInfo != null and cashReceiptInfo != ''">
            ,#{cashReceiptInfo}
            </if>
            <if test="cashReceiptPurpose != null and cashReceiptPurpose != ''">
            ,#{cashReceiptPurpose}
            </if>
            <if test="earnPoint != null and earnPoint != ''">
            ,#{earnPoint}
            </if>
            <if test="cancelOriginalApprovalDate != null and cancelOriginalApprovalDate != ''">
            ,#{cancelOriginalApprovalDate}
            </if>
            <if test="cancelOriginalApprovalNo != null and cancelOriginalApprovalNo != ''">
            ,#{cancelOriginalApprovalNo}
            </if>
            ,#{totalAmount}
            <if test="vatAmount != null and vatAmount != ''">
            ,#{vatAmount}
            </if>
            <if test="posTransactionNumber != null and posTransactionNumber != ''">
            ,#{posTransactionNumber}
            </if>
            ,CURDATE()
            ,CURTIME()
            ,#{remark}
            ,'C'
            ,'Y'
            ,#{regId}
            ,NOW()
            ,#{modId}
            ,NOW()
            ,#{tId}
            ,#{tDate}
        )
    </insert>

    <insert id="insertPaymentResponseHistory" parameterType="hashmap">
        <selectKey resultType="string" keyProperty="id" order="BEFORE">
            SELECT CONCAT(#{muPaymentResponseHistoryId},LPAD((SELECT NEXTVAL(MU_PAYMENT_RESPONSE_HISTORY_SEQ)), 9, 0))
        </selectKey>
        INSERT INTO MU_PAYMENT_RESPONSE_HISTORY(
         MU_PAYMENT_RESPONSE_HISTORY_ID
        ,MU_PAYMENT_REQUEST_HISTORY_ID
        ,MU_USER_ID
        ,USER_NAME
        ,TRANSACTION_TYPE
        ,RESPONSE_CODE
        ,TERMINAL_ID
        <if test="WCC != null and WCC != ''">
        ,WCC
        </if>
        <if test="cardNumber != null and cardNumber != ''">
        ,CARD_NUMBER
        </if>
        <if test="installmentMonths != null and installmentMonths != ''">
        ,INSTALLMENT_MONTHS
        </if>
        <if test="cashReceiptInfo != null and cashReceiptInfo != ''">
        ,CASH_RECEIPT_INFO
        </if>
        <if test="cashReceiptPurpose != null and cashReceiptPurpose != ''">
        ,CASH_RECEIPT_PURPOSE
        </if>
        <if test="earnPoint != null and earnPoint != ''">
        ,EARN_POINT
        </if>
        ,TOTAL_AMOUNT
        <if test="serviceFee != null and serviceFee != ''">
        ,SERVICE_FEE
        </if>
        <if test="vatAmount != null and vatAmount != ''">
        ,VAT_AMOUNT
        </if>
        ,APPROVAL_NO
        ,APPROVAL_DATE
        <if test="issuerCode != null and issuerCode != ''">
        ,ISSUER_CODE
        </if>
        <if test="issuerName != null and issuerName != ''">
        ,ISSUER_NAME
        </if>
        <if test="merchantCode != null and merchantCode != ''">
        ,MERCHANT_CODE
        </if>
        <if test="acquirerCode != null and acquirerCode != ''">
        ,ACQUIRER_CODE
        </if>
        <if test="acquirerName != null and acquirerName != ''">
        ,ACQUIRER_NAME
        </if>
        <if test="posTransactionNumber != null and posTransactionNumber != ''">
        ,POS_TRANSACTION_NUMBER
        </if>
        <if test="dscType != null and dscType != ''">
        ,DSC_TYPE
        </if>
        <if test="digitalSignature != null and digitalSignature != ''">
        ,DIGITAL_SIGNATURE
        </if>
        ,WRITE_DATE
        ,WRITE_TIME
        ,REMARK
        ,CUD_FLAG
        ,USE_YN
        ,REG_ID
        ,REG_DATE
        ,MOD_ID
        ,MOD_DATE
        ,T_ID
        ,T_DATE
        )VALUES(
        #{id}
        ,#{muPaymentRequestHistoryId}
        ,#{muUserId}
        ,#{userName}
        ,#{transactionType}
        ,#{responseCode}
        ,#{terminalId}
        <if test="WCC != null and WCC != ''">
        ,#{WCC}
        </if>
        <if test="cardNumber != null and cardNumber != ''">
        ,#{cardNumber}
        </if>
        <if test="installmentMonths != null and installmentMonths != ''">
        ,#{installmentMonths}
        </if>
        <if test="cashReceiptInfo != null and cashReceiptInfo != ''">
        ,#{cashReceiptInfo}
        </if>
        <if test="cashReceiptPurpose != null and cashReceiptPurpose != ''">
        ,#{cashReceiptPurpose}
        </if>
        <if test="earnPoint != null and earnPoint != ''">
        ,#{earnPoint}
        </if>
        ,#{totalAmount}
        <if test="serviceFee != null and serviceFee != ''">
        ,#{serviceFee}
        </if>
        <if test="vatAmount != null and vatAmount != ''">
        ,#{vatAmount}
        </if>
        ,#{approvalNo}
        ,#{approvalDate}
        <if test="issuerCode != null and issuerCode != ''">
        ,#{issuerCode}
        </if>
        <if test="issuerName != null and issuerName != ''">
        ,#{issuerName}
        </if>
        <if test="merchantCode != null and merchantCode != ''">
        ,#{merchantCode}
        </if>
        <if test="acquirerCode != null and acquirerCode != ''">
        ,#{acquirerCode}
        </if>
        <if test="acquirerName != null and acquirerName != ''">
        ,#{acquirerName}
        </if>
        <if test="posTransactionNumber != null and posTransactionNumber != ''">
        ,#{posTransactionNumber}
        </if>
        <if test="dscType != null and dscType != ''">
        ,#{dscType}
        </if>
        <if test="digitalSignature != null and digitalSignature != ''">
        ,#{digitalSignature}
        </if>
        ,CURDATE()
        ,CURTIME()
        ,#{remark}
        ,'C'
        ,'Y'
        ,#{regId}
        ,NOW()
        ,#{modId}
        ,NOW()
        ,#{tId}
        ,#{tDate}
        )
    </insert>

    <insert id="insertPaymentDetail" parameterType="hashmap">
        <selectKey resultType="string" keyProperty="id" order="BEFORE">
            SELECT CONCAT(#{muPaymentDetailId},LPAD((SELECT NEXTVAL(MU_PAYMENT_DETAIL_SEQ)), 9, 0))
        </selectKey>
        INSERT INTO MU_PAYMENT_DETAIL(
            MU_PAYMENT_DETAIL_ID
            ,MU_PAYMENT_ID
            ,PAYMENT_NUMBER
            ,MU_RESERVE_ID
            ,MU_TREATMENT_ID
            ,TREATMENT_NAME
            ,MU_TREATMENT_PROCEDURE_ID
            ,TREATMENT_PROCEDURE_NAME
            <if test="reserveMemberId != null and reserveMemberId != ''">
            ,RESERVE_MEMBER_ID
            </if>
            ,MU_MEMBER_ID
            ,AMOUNT
            ,VAT
            <if test="discountPrice != null and discountPrice != ''">
            ,DISCOUNT_PRICE
            </if>
            <if test="membershipPrice != null and membershipPrice != ''">
            ,MEMBERSHIP_PRICE
            </if>
            ,TOTAL_AMOUNT
            ,WRITE_DATE
            ,WRITE_TIME
            ,CUD_FLAG
            ,USE_YN
            ,REG_ID
            ,REG_DATE
            ,MOD_ID
            ,MOD_DATE
            ,T_ID
            ,T_DATE
        )VALUES(
             #{id}
            ,#{muPaymentId}
            ,#{paymentNumber}
            ,#{muReserveId}
            ,#{muTreatmentId}
            ,#{treatmentName}
            ,#{muTreatmentProcedureId}
            ,#{treatmentProcedureName}
            <if test="reserveMemberId != null and reserveMemberId != ''">
            ,#{reserveMemberId}
            </if>
            ,#{muMemberId}
            ,#{amount}
            ,#{vat}
            <if test="discountPrice != null and discountPrice != ''">
            ,#{discountPrice}
            </if>
            <if test="membershipPrice != null and membershipPrice != ''">
            ,#{membershipPrice}
            </if>
            ,#{totalAmount}
            ,CURDATE()
            ,CURTIME()
            ,'C'
            ,'Y'
            ,#{regId}
            ,NOW()
            ,#{modId}
            ,NOW()
            ,#{tId}
            ,#{tDate}
        )
    </insert>

    <insert id="insertPaymentDetailBatch" parameterType="hashmap">
        <selectKey resultType="string" keyProperty="id" order="BEFORE">
            SELECT CONCAT(#{muPaymentDetailId},LPAD((SELECT NEXTVAL(MU_PAYMENT_DETAIL_SEQ)), 9, 0))
        </selectKey>
        INSERT INTO MU_PAYMENT_DETAIL(
                    MU_PAYMENT_DETAIL_ID
                    ,MU_PAYMENT_ID
                    ,PAYMENT_NUMBER
                    ,MU_RESERVE_ID
                    ,MU_TREATMENT_ID
                    ,TREATMENT_NAME
                    ,MU_TREATMENT_PROCEDURE_ID
                    ,TREATMENT_PROCEDURE_NAME
                    ,RESERVE_MEMBER_ID
                    ,MU_MEMBER_ID
                    ,AMOUNT
                    ,VAT
                    ,DISCOUNT_PRICE
                    ,MEMBERSHIP_PRICE
                    ,TOTAL_AMOUNT
                    ,WRITE_DATE
                    ,WRITE_TIME
                    ,CUD_FLAG
                    ,USE_YN
                    ,REG_ID
                    ,REG_DATE
                    ,MOD_ID
                    ,MOD_DATE
                    ,T_ID
                    ,T_DATE
                    ,CRM_MEMBER_PAY_PID
                    ,CRM_MEMBER_PAY_MB_PID
                    ,CRM_MEMBER_PAY_PAY_MEMO
                    ,CRM_MEMBER_PAY_PAY_MEMO2
                    ,CRM_MEMBER_PAY_M_DIVI1
                    ,CRM_MEMBER_PAY_M_DIVI2
                    ,CRM_MEMBER_PAY_M_DIVI3
                    ,CRM_MEMBER_PAY_M_DIVI4
                    ,CRM_MEMBER_PAY_M_DIVI1_NAME
                    ,CRM_MEMBER_PAY_M_DIVI2_NAME
                    ,CRM_MEMBER_PAY_M_DIVI3_NAME
                    ,CRM_MEMBER_PAY_M_DIVI4_NAME
                    ,CRM_MEMBER_PAY_PAY_DOCTOR
                    ,CRM_MEMBER_PAY_DOCTOR_NAME
                    ,CRM_MEMBER_PAY_PAY_STAFF
                    ,CRM_MEMBER_PAY_STAFF_NAME
                    ,CRM_MEMBER_PAY_PAY_STAFF2
                    ,CRM_MEMBER_PAY_STAFF_NAME2
                    ,CRM_MEMBER_PAY_PAY_DIVI1
                    ,CRM_MEMBER_PAY_PAY_DIVI2
                    ,CRM_MEMBER_PAY_M_CNT
                    ,CRM_MEMBER_PAY_M_CNT_DIVI
                    ,CRM_MEMBER_PAY_PAY_MEMBERSHIP
                    ,CRM_MEMBER_PAY_PAY_SUM
                    ,CRM_MEMBER_PAY_PAY_CARD
                    ,CRM_MEMBER_PAY_PAY_CASH_RECEIPT_TYPE
                    ,CRM_MEMBER_PAY_PAY_CASH_RECEIPT
                    ,CRM_MEMBER_PAY_PAY_CASH
                    ,CRM_MEMBER_PAY_PAY_CASH_BANK
                    ,CRM_MEMBER_PAY_PAY_VAT
                    ,CRM_MEMBER_PAY_PAY_DEBT
                    ,CRM_MEMBER_PAY_PAY_BACK
                    ,CRM_MEMBER_PAY_TAX_FREE
                    ,CRM_MEMBER_PAY_PAY_RECEIPT
                    ,CRM_MEMBER_PAY_PAY_CARD_NAME
                    ,CRM_MEMBER_PAY_PAY_BACK_CARD_NAME
                    ,CRM_MEMBER_PAY_PAY_BACK_CARD_COMMISSION
                    ,CRM_MEMBER_PAY_PAY_BACK_CARD_COMMISSION_PAY
                    ,CRM_MEMBER_PAY_PAY_BACK_CARD_IN
                    ,CRM_MEMBER_PAY_PAY_SUM_CARD_NAME
                    ,CRM_MEMBER_PAY_PAY_SUM_CARD_COMMISSION
                    ,CRM_MEMBER_PAY_PAY_SUM_CARD_COMMISSION_PAY
                    ,CRM_MEMBER_PAY_PAY_SUM_CARD_IN
                    ,CRM_MEMBER_PAY_PAY_SUM_CARD
                    ,CRM_MEMBER_PAY_PAY_TYPE
                    ,CRM_MEMBER_PAY_PAY_COUNTRY
                    ,CRM_MEMBER_PAY_MBSHIP_NOTE
                    ,CRM_MEMBER_PAY_MBSHIP_TYPE
        )     SELECT #{id}                                       AS MU_PAYMENT_DETAIL_ID <!-- 식별자 -->
                    ,#{muPaymentId}                              AS MU_PAYMENT_ID <!-- 결제 식별자 -->
                    ,CMP.PID                                     AS PAYMENT_NUMBER <!-- 결제번호(부분결제식별) -->
                    ,NULL                                        AS MU_RESERVE_ID <!-- 예약식별자  (*추가) -->
                    ,(SELECT MCI.MU_CATEGORY_ITEM_ID
                        FROM MU_CATEGORY_ITEM AS MCI
                       WHERE MCI.MU_CATEGORY_ID = 'C17128016604480000000005'
                         AND MCI.CATEGORY_ITEM_NAME = (CASE
                                                       WHEN CMP.M_DIVI1_NAME LIKE '%비만%'
                                                       THEN '비만'
                                                       WHEN CMP.M_DIVI1_NAME = '쁘띠'
                                                       THEN '쁘띠'
                                                       WHEN CMP.M_DIVI1_NAME = '제모'
                                                       THEN '제모'
                                                       WHEN CMP.M_DIVI1_NAME = '피부'
                                                       THEN '피부'
                                                       ELSE '기타'
                                                       END))     AS MU_TREATMENT_ID <!-- 진료 식별자 -->
                    ,CASE
                     WHEN CMP.M_DIVI1_NAME LIKE '%비만%'
                     THEN '비만'
                     WHEN CMP.M_DIVI1_NAME = '쁘띠'
                     THEN '쁘띠'
                     WHEN CMP.M_DIVI1_NAME = '제모'
                     THEN '제모'
                     WHEN CMP.M_DIVI1_NAME = '피부'
                     THEN '피부'
                     ELSE '기타'
                     END                                         AS TREATMENT_NAME <!-- 진료명 -->
                    ,NULL                                        AS MU_TREATMENT_PROCEDURE_ID <!-- 진료 시술 식별자 -->
                    ,NULL                                        AS TREATMENT_PROCEDURE_NAME <!-- 진료 시술명 -->
                    ,NULL                                        AS RESERVE_MEMBER_ID <!-- 의사, 상담자 식별자 -->
                    ,NULL                                        AS MU_MEMBER_ID <!-- 담당자 식별자 -->
                    ,NULL                                        AS AMOUNT <!-- 공급가(결제 금액) -->
                    ,CMP.PAY_VAT                                 AS VAT <!-- 부가세(결제 금액) -->
                    ,0                                           AS DISCOUNT_PRICE <!-- 수납 할인금액 -->
                    ,CASE
                     WHEN CMP.M_DIVI1_NAME = '멤버쉽'
                      AND CMP.PAY_MEMBERSHIP &gt;= 0
                     THEN NULL
                     ELSE CMP.PAY_MEMBERSHIP
                      END                                        AS MEMBERSHIP_PRICE <!-- 멤버십 차감액 -->
                    ,CMP.PAY_SUM                                 AS TOTAL_AMOUNT <!-- 총 금액(시술 원가) -->
                    ,CMP.PAY_DATE                                AS WRITE_DATE <!-- 작성일자 -->
                    ,CMP.REG_DATE                                AS WRITE_TIME <!-- 작성시간 -->
                    ,'C'                                         AS CUD_FLAG <!-- 작업타입 (C: 등록, U: 수정, D : 삭제) -->
                    ,CASE
                     WHEN UPPER(CMP.LIST_DEL) = 'N'
                     THEN 'Y'
                     WHEN UPPER(CMP.LIST_DEL) = 'Y'
                     THEN 'N'
                     ELSE ''
                      END                                        AS USE_YN <!-- 사용여부 -->
                    ,'BatchSystem'                               AS REG_ID <!-- 등록자 -->
                    ,CMP.REG_DATE                                AS REG_DATE <!-- 등록일자 -->
                    ,'BatchSystem'                               AS MOD_ID <!-- 수정자 -->
                    ,CMP.UP_DATE                                 AS MOD_DATE <!-- 수정일자 -->
                    ,#{tId}                                      AS T_ID
                    ,#{tDate}                                    AS T_DATE
                    ,CMP.PID                                     AS CRM_MEMBER_PAY_PID <!-- 기존 DB 식별자 -->
                    ,CMP.MB_PID                                  AS CRM_MEMBER_PAY_MB_PID <!-- 기존 DB 고객 식별자 -->
                    ,CMP.PAY_MEMO                                AS CRM_MEMBER_PAY_PAY_MEMO <!-- 기존 DB 메모 -->
                    ,CMP.PAY_MEMO2                               AS CRM_MEMBER_PAY_PAY_MEMO2 <!-- 기존 DB 메모2 -->
                    ,CMP.M_DIVI1                                 AS CRM_MEMBER_PAY_M_DIVI1 <!-- 기존 DB 시술분류 (진료 식별자) -->
                    ,CMP.M_DIVI2                                 AS CRM_MEMBER_PAY_M_DIVI2 <!-- 기존 DB 시술분류 (진료 시술 식별자) -->
                    ,CMP.M_DIVI3                                 AS CRM_MEMBER_PAY_M_DIVI3 <!-- 기존 DB 시술분류 (진료 시술 식별자) -->
                    ,CMP.M_DIVI4                                 AS CRM_MEMBER_PAY_M_DIVI4 <!-- 기존 DB 시술분류 (진료 시술 식별자) -->
                    ,CMP.M_DIVI1_NAME                            AS CRM_MEMBER_PAY_M_DIVI1_NAME <!-- 기존 DB 시술분류 (진료명) -->
                    ,CMP.M_DIVI2_NAME                            AS CRM_MEMBER_PAY_M_DIVI2_NAME <!-- 기존 DB 시술분류 (진료 시술명) -->
                    ,CMP.M_DIVI3_NAME                            AS CRM_MEMBER_PAY_M_DIVI3_NAME <!-- 기존 DB 시술분류 (진료 시술명) -->
                    ,CMP.M_DIVI4_NAME                            AS CRM_MEMBER_PAY_M_DIVI4_NAME <!-- 기존 DB 시술분류 (진료 시술명) -->
                    ,CMP.PAY_DOCTOR                              AS CRM_MEMBER_PAY_PAY_DOCTOR <!-- 기존 DB 담당의사 식별자 -->
                    ,CMP.DOCTOR_NAME                             AS CRM_MEMBER_PAY_DOCTOR_NAME <!-- 기존 DB 담당의사 이름 -->
                    ,CMP.PAY_STAFF                               AS CRM_MEMBER_PAY_PAY_STAFF <!-- 기존 DB 담당자 식별자 -->
                    ,CMP.STAFF_NAME                              AS CRM_MEMBER_PAY_STAFF_NAME <!-- 기존 DB 담당자 이름 -->
                    ,CMP.PAY_STAFF2                              AS CRM_MEMBER_PAY_PAY_STAFF2 <!-- 기존 DB 담당자 식별자2 -->
                    ,CMP.STAFF_NAME2                             AS CRM_MEMBER_PAY_STAFF_NAME2 <!-- 기존 DB 담당자 이름2 -->
                    ,CMP.PAY_DIVI1                               AS CRM_MEMBER_PAY_PAY_DIVI1 <!-- 기존 DB 결제상태 -->
                    ,CMP.PAY_DIVI2                               AS CRM_MEMBER_PAY_PAY_DIVI2 <!-- 기존 DB 결제상태2 -->
                    ,CMP.M_CNT                                   AS CRM_MEMBER_PAY_M_CNT <!-- 기존 DB 초진/재진 카운트 -->
                    ,CMP.M_CNT_DIVI                              AS CRM_MEMBER_PAY_M_CNT_DIVI <!-- 기존 DB 초진/재진 구분 -->
                    ,CMP.PAY_MEMBERSHIP                          AS CRM_MEMBER_PAY_PAY_MEMBERSHIP <!-- 기존 DB 멤버쉽 -->
                    ,CMP.PAY_SUM                                 AS CRM_MEMBER_PAY_PAY_SUM <!-- 기존 DB 계 -->
                    ,CMP.PAY_CARD                                AS CRM_MEMBER_PAY_PAY_CARD <!-- 기존 DB 카드 -->
                    ,CMP.PAY_CASH_RECEIPT_TYPE                   AS CRM_MEMBER_PAY_PAY_CASH_RECEIPT_TYPE <!-- 기존 DB 현금영수증(현금, 통장) 구분 -->
                    ,CMP.PAY_CASH_RECEIPT                        AS CRM_MEMBER_PAY_PAY_CASH_RECEIPT <!-- 기존 DB 현금영수증 금액 -->
                    ,CMP.PAY_CASH                                AS CRM_MEMBER_PAY_PAY_CASH <!-- 기존 DB 현금 -->
                    ,CMP.PAY_CASH_BANK                           AS CRM_MEMBER_PAY_PAY_CASH_BANK <!-- 기존 DB 통장 -->
                    ,CMP.PAY_VAT                                 AS CRM_MEMBER_PAY_PAY_VAT <!-- 기존 DB vat -->
                    ,CMP.PAY_DEBT                                AS CRM_MEMBER_PAY_PAY_DEBT <!-- 기존 DB 미수 -->
                    ,CMP.PAY_BACK                                AS CRM_MEMBER_PAY_PAY_BACK <!-- 기존 DB 환불 -->
                    ,CMP.TAX_FREE                                AS CRM_MEMBER_PAY_TAX_FREE <!-- 기존 DB TAX_FREE -->
                    ,CMP.PAY_RECEIPT                             AS CRM_MEMBER_PAY_PAY_RECEIPT <!-- 기존 DB 영수증 -->
                    ,CMP.PAY_CARD_NAME                           AS CRM_MEMBER_PAY_PAY_CARD_NAME <!-- 기존 DB 결제카드명 -->
                    ,CMP.PAY_BACK_CARD_NAME                      AS CRM_MEMBER_PAY_PAY_BACK_CARD_NAME <!-- 기존 DB 결제취소 카드명 -->
                    ,CMP.PAY_BACK_CARD_COMMISSION                AS CRM_MEMBER_PAY_PAY_BACK_CARD_COMMISSION <!-- 기존 DB 결제취소 카드 수수료 -->
                    ,CMP.PAY_BACK_CARD_COMMISSION_PAY            AS CRM_MEMBER_PAY_PAY_BACK_CARD_COMMISSION_PAY <!-- 기존 DB 결제취소 수수료 금액 -->
                    ,CMP.PAY_BACK_CARD_IN                        AS CRM_MEMBER_PAY_PAY_BACK_CARD_IN <!-- 기존 DB 결제취소 금액 -->
                    ,CMP.PAY_SUM_CARD_NAME                       AS CRM_MEMBER_PAY_PAY_SUM_CARD_NAME <!-- 기존 DB 결제 카드명 -->
                    ,CMP.PAY_SUM_CARD_COMMISSION                 AS CRM_MEMBER_PAY_PAY_SUM_CARD_COMMISSION <!-- 기존 DB 카드 수수료% -->
                    ,CMP.PAY_SUM_CARD_COMMISSION_PAY             AS CRM_MEMBER_PAY_PAY_SUM_CARD_COMMISSION_PAY <!-- 기존 DB 카드 수수료 금액 -->
                    ,CMP.PAY_SUM_CARD_IN                         AS CRM_MEMBER_PAY_PAY_SUM_CARD_IN <!-- 기존 DB 정산금액 -->
                    ,CMP.PAY_SUM_CARD                            AS CRM_MEMBER_PAY_PAY_SUM_CARD <!-- 기존 DB 카드사 식별번호 -->
                    ,CMP.PAY_TYPE                                AS CRM_MEMBER_PAY_PAY_TYPE <!-- 기존 DB 결제타입 -->
                    ,CMP.PAY_COUNTRY                             AS CRM_MEMBER_PAY_PAY_COUNTRY <!-- 기존 DB 결제국가 -->
                    ,CMP.MBSHIP_NOTE                             AS CRM_MEMBER_PAY_MBSHIP_NOTE <!-- 기존 DB 멤버쉽 비고 -->
                    ,CMP.MBSHIP_TYPE                             AS CRM_MEMBER_PAY_MBSHIP_TYPE <!-- 기존 DB 멤버십 타입(멤버십 상품) -->
                FROM CRM_MEMBER_PAY AS CMP
               WHERE CMP.PID = #{crmMemberPayPid}
    </insert>
</mapper>