<?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="MobileMembership">

    <select id="selectTotalMembershipBalance" parameterType="hashmap" resultType="hashmap">
        SELECT FORMAT(IFNULL(MM.MEMBERSHIP_BALANCE,0),0)           AS membershipBalance
          FROM MU_MEMBERSHIP MM
         WHERE MM.USE_YN = 'Y'
           AND MM.MU_USER_ID=#{muUserId}
    </select>

    <select id="selectListMembershipChargeCount" parameterType="hashmap" resultType="Integer">
        SELECT COUNT(1)         AS "totalCount"
          FROM MU_MEMBERSHIP_HISTORY MMH
         WHERE MMH.USE_YN='Y'
           AND MMH.MU_USER_ID=#{muUserId}
           AND (MMH.USAGE_TYPE='C' OR MMH.USAGE_TYPE = 'P' OR MMH.USAGE_TYPE = 'A' OR MMH.USAGE_TYPE = 'D')
        <if test="membershipSearchDate != null and membershipSearchDate != ''">
           AND DATE_FORMAT(MMH.WRITE_DATE, '%Y-%m') = #{membershipSearchDate}
        </if>
    </select>

    <select id="selectListMembershipCharge" parameterType="hashmap" resultType="hashmap">
        SELECT DATE_FORMAT(MMH.WRITE_DATE, '%Y-%m-%d')            AS "writeDate"
             , FORMAT(MMH.RECHARGE_AMOUNT,0)                      AS "rechargeAmount"
             , CASE
                   WHEN MMH.USAGE_TYPE = 'C' THEN '카드 충전'
                   WHEN MMH.USAGE_TYPE = 'A' THEN '멤버십 적립'
                   WHEN MMH.USAGE_TYPE = 'D' THEN '환불'
                   WHEN MMH.USAGE_TYPE = 'P' THEN CONCAT('멤버십 양수', ' (', MU_FROM.USER_NAME, ')')
                   ELSE ''
               END                                                AS "chargePaymentItemName"
          FROM MU_MEMBERSHIP_HISTORY MMH
     LEFT JOIN MU_USER MU_FROM
            ON MMH.TRANSFER_FROM_USER_ID = MU_FROM.MU_USER_ID
         WHERE MMH.USE_YN='Y'
           AND MMH.MU_USER_ID=#{muUserId}
           AND (MMH.USAGE_TYPE='C' OR MMH.USAGE_TYPE = 'P' OR MMH.USAGE_TYPE = 'A' OR MMH.USAGE_TYPE = 'D')
        <if test="membershipSearchDate != null and membershipSearchDate != ''">
           AND DATE_FORMAT(MMH.WRITE_DATE, '%Y-%m') = #{membershipSearchDate}
        </if>
        <choose>
            <when test="sortBy eq 'amount'.toString()">
                ORDER BY MMH.RECHARGE_AMOUNT DESC
            </when>
            <otherwise>
                ORDER BY MMH.REG_DATE DESC
            </otherwise>
        </choose>
    </select>

    <select id="selectListMembershipUseCount" parameterType="hashmap" resultType="Integer">
        SELECT COUNT(1)     AS "totalCount"
          FROM MU_MEMBERSHIP_HISTORY MMH
         WHERE MMH.USE_YN='Y'
           AND MMH.MU_USER_ID = #{muUserId}
           AND (MMH.USAGE_TYPE ='U' OR MMH.USAGE_TYPE ='T')
        <if test="membershipSearchDate != null and membershipSearchDate != ''">
            AND DATE_FORMAT(MMH.WRITE_DATE, '%Y-%m') = #{membershipSearchDate}
        </if>
    </select>

    <select id="selectListMembershipUse" parameterType="hashmap" resultType="hashmap">
        SELECT DATE_FORMAT(MMH.WRITE_DATE, '%Y-%m-%d')                             AS writeDate
             , IFNULL(MT.TREATMENT_NAME,'기타')               AS treatmentName
             , CASE
                   WHEN MMH.USAGE_TYPE = 'U' THEN '사용'
                   WHEN MMH.USAGE_TYPE = 'T' THEN CONCAT('멤버십 양도', ' (', MU_TO.USER_NAME, ')')
                   ELSE MTP.TREATMENT_PROCEDURE_NAME
               END                                                                 AS procedureName
             , FORMAT(MMH.USED_AMOUNT,0)                                           AS usedAmount
          FROM MU_MEMBERSHIP_HISTORY MMH
     LEFT JOIN MU_TREATMENT MT
            ON MMH.MU_TREATMENT_ID = MT.MU_TREATMENT_ID
     LEFT JOIN MU_TREATMENT_PROCEDURE MTP
            ON MTP.MU_TREATMENT_PROCEDURE_ID = MMH.MU_TREATMENT_PROCEDURE_ID
     LEFT JOIN MU_USER MU_TO
            ON MMH.TRANSFER_TO_USER_ID = MU_TO.MU_USER_ID
         WHERE MMH.USE_YN='Y'
           AND MMH.MU_USER_ID = #{muUserId}
           AND (MMH.USAGE_TYPE ='U' OR MMH.USAGE_TYPE ='T')
        <if test="membershipSearchDate != null and membershipSearchDate != ''">
            AND DATE_FORMAT(MMH.WRITE_DATE, '%Y-%m') = #{membershipSearchDate}
        </if>
        <choose>
            <when test="sortBy eq 'amount'.toString()">
            ORDER BY MMH.USED_AMOUNT DESC
            </when>
            <otherwise>
                ORDER BY MMH.REG_DATE DESC
            </otherwise>
        </choose>
    </select>

    <insert id="insertMembership" parameterType="hashmap">
        <selectKey resultType="string" keyProperty="id" order="BEFORE">
            SELECT CONCAT(#{muMembershipId},LPAD((SELECT NEXTVAL(MU_MEMBERSHIP_SEQ)), 11, 0))
        </selectKey>
        INSERT INTO MU_MEMBERSHIP(
        MU_MEMBERSHIP_ID
        ,MU_USER_ID
        ,MEMBERSHIP_BALANCE
        ,LAST_RECHARGE_DATE
        ,CUD_FLAG
        ,USE_YN
        ,REG_ID
        ,REG_DATE
        ,MOD_ID
        ,MOD_DATE
        ,T_ID
        ,T_DATE
        )VALUES(
        #{id}
        ,#{muUserId}
        ,0
        ,NOW()
        ,'C'
        ,'Y'
        ,#{regId}
        ,NOW()
        ,#{modId}
        ,NOW()
        ,#{tId}
        ,NOW()
        )
    </insert>

    <insert id="insertDevice" parameterType="hashmap">
        <selectKey resultType="string" keyProperty="id" order="BEFORE">
            SELECT CONCAT(#{muDeviceId},LPAD((SELECT NEXTVAL(MU_DEVICE_SEQ)), 11, 0))
        </selectKey>
        INSERT INTO MU_DEVICE(
          MU_DEVICE_ID
        , MU_USER_ID
        , PHONE_TYPE
        , DEVICE_UUID
        , PUSH_KEY
        , MODEL_NAME
        , OS_VERSION
        , USE_YN
        , REG_ID
        , REG_DATE
        , MOD_ID
        , MOD_DATE
        )VALUES(
        #{id}
        ,#{muUserId}
        ,#{phoneType}
        ,#{deviceUuid}
        ,#{pushKey}
        ,#{modelName}
        ,#{osVersion}
        ,'Y'
        ,#{regId}
        ,NOW()
        ,#{modId}
        ,NOW()
        )
    </insert>

    <update id="updateMembershipYn" parameterType="hashmap">
        UPDATE MU_USER
           SET MOD_ID              = #{modId}
             ,MOD_DATE            = NOW()
             ,CUD_FLAG            = 'U'
             ,MEMBERSHIP_YN       = 'Y'
         WHERE USE_YN        = 'Y'
           AND MU_USER_ID  = #{muUserId}
    </update>

    <update id="updateMembership" parameterType="hashmap">
        UPDATE MU_MEMBERSHIP
        SET
        MEMBERSHIP_BALANCE =  #{membershipBalance}
        <if test="lastRechargeDate != null and lastRechargeDate != ''">
            ,LAST_RECHARGE_DATE = #{lastRechargeDate}
        </if>
        <if test="lastUseDate != null and lastUseDate != ''">
            ,LAST_USE_DATE =  #{lastUseDate}
        </if>
        ,CUD_FLAG = 'U'
        ,USE_YN = 'Y'
        ,MOD_ID = #{modId}
        ,MOD_DATE = NOW()
        ,T_ID = #{tId}
        ,T_DATE = NOW()
        WHERE MU_USER_ID = #{muUserId}
        AND  USE_YN        = 'Y'
    </update>
</mapper>