2026-05-31 15:43:09 +09:00
|
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|
|
|
|
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
|
|
|
|
<mapper namespace="com.sb.web.account.mapper.AccountEntryMapper">
|
|
|
|
|
|
|
|
|
|
<resultMap id="entryResultMap" type="com.sb.web.account.domain.AccountEntry">
|
|
|
|
|
<id property="id" column="id"/>
|
|
|
|
|
<result property="memberId" column="member_id"/>
|
|
|
|
|
<result property="entryDate" column="entry_date"/>
|
|
|
|
|
<result property="type" column="type"/>
|
|
|
|
|
<result property="category" column="category"/>
|
|
|
|
|
<result property="amount" column="amount"/>
|
|
|
|
|
<result property="memo" column="memo"/>
|
|
|
|
|
<result property="walletId" column="wallet_id"/>
|
|
|
|
|
<result property="walletName" column="wallet_name"/>
|
|
|
|
|
<result property="toWalletId" column="to_wallet_id"/>
|
|
|
|
|
<result property="toWalletName" column="to_wallet_name"/>
|
2026-06-03 16:41:58 +09:00
|
|
|
<result property="installmentMonths" column="installment_months"/>
|
2026-06-03 18:51:30 +09:00
|
|
|
<result property="pending" column="pending"/>
|
|
|
|
|
<result property="notifKey" column="notif_key"/>
|
2026-05-31 15:43:09 +09:00
|
|
|
<result property="createdAt" column="created_at"/>
|
|
|
|
|
<result property="updatedAt" column="updated_at"/>
|
|
|
|
|
</resultMap>
|
|
|
|
|
|
|
|
|
|
<sql id="periodFilter">
|
|
|
|
|
<if test="year != null">AND YEAR(entry_date) = #{year}</if>
|
|
|
|
|
<if test="month != null">AND MONTH(entry_date) = #{month}</if>
|
|
|
|
|
</sql>
|
|
|
|
|
|
|
|
|
|
<sql id="entryCols">
|
|
|
|
|
e.id, e.member_id, e.entry_date, e.type, e.category, e.amount, e.memo,
|
|
|
|
|
e.wallet_id, w.name AS wallet_name,
|
|
|
|
|
e.to_wallet_id, tw.name AS to_wallet_name,
|
2026-06-03 18:51:30 +09:00
|
|
|
e.installment_months, e.pending, e.notif_key,
|
2026-05-31 15:43:09 +09:00
|
|
|
e.created_at, e.updated_at
|
|
|
|
|
</sql>
|
|
|
|
|
<sql id="entryJoins">
|
|
|
|
|
FROM account_entry e
|
|
|
|
|
LEFT JOIN wallet w ON w.id = e.wallet_id
|
|
|
|
|
LEFT JOIN wallet tw ON tw.id = e.to_wallet_id
|
|
|
|
|
</sql>
|
|
|
|
|
|
|
|
|
|
<select id="findByMember" resultMap="entryResultMap">
|
|
|
|
|
SELECT <include refid="entryCols"/>
|
|
|
|
|
<include refid="entryJoins"/>
|
|
|
|
|
WHERE e.member_id = #{memberId}
|
|
|
|
|
<if test="year != null">AND YEAR(e.entry_date) = #{year}</if>
|
|
|
|
|
<if test="month != null">AND MONTH(e.entry_date) = #{month}</if>
|
|
|
|
|
<if test="type != null and type != ''">AND e.type = #{type}</if>
|
|
|
|
|
<if test="category != null and category != ''">AND e.category = #{category}</if>
|
|
|
|
|
<if test="walletId != null">AND (e.wallet_id = #{walletId} OR e.to_wallet_id = #{walletId})</if>
|
|
|
|
|
<if test="keyword != null and keyword != ''">
|
|
|
|
|
AND (e.memo LIKE CONCAT('%', #{keyword}, '%') OR e.category LIKE CONCAT('%', #{keyword}, '%'))
|
|
|
|
|
</if>
|
|
|
|
|
<if test="tagId != null">
|
|
|
|
|
AND EXISTS (SELECT 1 FROM account_entry_tag aet WHERE aet.entry_id = e.id AND aet.tag_id = #{tagId})
|
|
|
|
|
</if>
|
|
|
|
|
ORDER BY e.entry_date DESC, e.id DESC
|
|
|
|
|
</select>
|
|
|
|
|
|
|
|
|
|
<select id="findByWalletAndMember" resultMap="entryResultMap">
|
|
|
|
|
SELECT <include refid="entryCols"/>
|
|
|
|
|
<include refid="entryJoins"/>
|
|
|
|
|
WHERE e.member_id = #{memberId}
|
|
|
|
|
AND (e.wallet_id = #{walletId} OR e.to_wallet_id = #{walletId})
|
|
|
|
|
ORDER BY e.entry_date DESC, e.id DESC
|
|
|
|
|
</select>
|
|
|
|
|
|
|
|
|
|
<select id="findByIdAndMember" resultMap="entryResultMap">
|
|
|
|
|
SELECT <include refid="entryCols"/>
|
|
|
|
|
<include refid="entryJoins"/>
|
|
|
|
|
WHERE e.id = #{id} AND e.member_id = #{memberId}
|
|
|
|
|
</select>
|
|
|
|
|
|
|
|
|
|
<select id="sumByType" resultType="map">
|
|
|
|
|
SELECT type AS type, COALESCE(SUM(amount), 0) AS total
|
|
|
|
|
FROM account_entry
|
|
|
|
|
WHERE member_id = #{memberId}
|
|
|
|
|
<include refid="periodFilter"/>
|
|
|
|
|
GROUP BY type
|
|
|
|
|
</select>
|
|
|
|
|
|
|
|
|
|
<select id="sumExpenseByCategory" resultType="map">
|
|
|
|
|
SELECT COALESCE(category, '') AS category, COALESCE(SUM(amount), 0) AS total
|
|
|
|
|
FROM account_entry
|
|
|
|
|
WHERE member_id = #{memberId} AND type = 'EXPENSE'
|
|
|
|
|
<include refid="periodFilter"/>
|
|
|
|
|
GROUP BY category
|
|
|
|
|
</select>
|
|
|
|
|
|
|
|
|
|
<select id="sumByCategory" resultType="map">
|
|
|
|
|
SELECT COALESCE(NULLIF(category, ''), '미분류') AS category, COALESCE(SUM(amount), 0) AS total
|
|
|
|
|
FROM account_entry
|
|
|
|
|
WHERE member_id = #{memberId} AND type = #{type}
|
|
|
|
|
<include refid="periodFilter"/>
|
|
|
|
|
GROUP BY COALESCE(NULLIF(category, ''), '미분류')
|
|
|
|
|
ORDER BY total DESC
|
|
|
|
|
</select>
|
|
|
|
|
|
|
|
|
|
<select id="monthlyWalletFlow" resultType="map">
|
|
|
|
|
SELECT DATE_FORMAT(entry_date, '%Y-%m') AS ym,
|
|
|
|
|
COALESCE(SUM(CASE WHEN type = 'INCOME' THEN amount
|
|
|
|
|
WHEN type = 'EXPENSE' THEN -amount
|
|
|
|
|
ELSE 0 END), 0) AS net
|
|
|
|
|
FROM account_entry
|
|
|
|
|
WHERE member_id = #{memberId} AND wallet_id IS NOT NULL AND type IN ('INCOME', 'EXPENSE')
|
|
|
|
|
GROUP BY ym
|
|
|
|
|
ORDER BY ym
|
|
|
|
|
</select>
|
|
|
|
|
|
|
|
|
|
<select id="statsByPeriod" resultType="map">
|
|
|
|
|
SELECT
|
|
|
|
|
<choose>
|
|
|
|
|
<when test="unit == 'DAY'">DAY(entry_date)</when>
|
|
|
|
|
<when test="unit == 'WEEK'">FLOOR((DAY(entry_date) - 1) / 7) + 1</when>
|
|
|
|
|
<when test="unit == 'YEAR'">YEAR(entry_date)</when>
|
|
|
|
|
<otherwise>MONTH(entry_date)</otherwise>
|
|
|
|
|
</choose> AS bucket,
|
|
|
|
|
COALESCE(SUM(CASE WHEN type = 'INCOME' THEN amount ELSE 0 END), 0) AS income,
|
|
|
|
|
COALESCE(SUM(CASE WHEN type = 'EXPENSE' THEN amount ELSE 0 END), 0) AS expense
|
|
|
|
|
FROM account_entry
|
|
|
|
|
WHERE member_id = #{memberId}
|
|
|
|
|
<if test="unit == 'DAY' or unit == 'WEEK' or unit == 'MONTH'">AND YEAR(entry_date) = #{year}</if>
|
|
|
|
|
<if test="unit == 'DAY' or unit == 'WEEK'">AND MONTH(entry_date) = #{month}</if>
|
|
|
|
|
GROUP BY bucket
|
|
|
|
|
ORDER BY bucket
|
|
|
|
|
</select>
|
|
|
|
|
|
|
|
|
|
<insert id="insert" parameterType="com.sb.web.account.domain.AccountEntry"
|
|
|
|
|
useGeneratedKeys="true" keyProperty="id">
|
|
|
|
|
INSERT INTO account_entry (member_id, entry_date, type, category, amount, memo,
|
2026-06-03 18:51:30 +09:00
|
|
|
wallet_id, to_wallet_id, installment_months, pending, notif_key,
|
|
|
|
|
created_at, updated_at)
|
2026-05-31 15:43:09 +09:00
|
|
|
VALUES (#{memberId}, #{entryDate}, #{type}, #{category}, #{amount}, #{memo},
|
2026-06-03 18:51:30 +09:00
|
|
|
#{walletId}, #{toWalletId}, #{installmentMonths},
|
|
|
|
|
COALESCE(#{pending}, 0), #{notifKey}, NOW(), NOW())
|
2026-05-31 15:43:09 +09:00
|
|
|
</insert>
|
|
|
|
|
|
|
|
|
|
<update id="update" parameterType="com.sb.web.account.domain.AccountEntry">
|
|
|
|
|
UPDATE account_entry
|
|
|
|
|
SET entry_date = #{entryDate}, type = #{type}, category = #{category},
|
2026-06-03 16:41:58 +09:00
|
|
|
amount = #{amount}, memo = #{memo}, wallet_id = #{walletId}, to_wallet_id = #{toWalletId},
|
|
|
|
|
installment_months = #{installmentMonths}
|
2026-05-31 15:43:09 +09:00
|
|
|
WHERE id = #{id} AND member_id = #{memberId}
|
|
|
|
|
</update>
|
|
|
|
|
|
|
|
|
|
<delete id="delete">
|
|
|
|
|
DELETE FROM account_entry WHERE id = #{id} AND member_id = #{memberId}
|
|
|
|
|
</delete>
|
|
|
|
|
|
2026-06-03 18:51:30 +09:00
|
|
|
<!-- ===== 알림 자동인식(미확인) ===== -->
|
|
|
|
|
<select id="findByMemberAndNotifKey" resultMap="entryResultMap">
|
|
|
|
|
SELECT <include refid="entryCols"/>
|
|
|
|
|
<include refid="entryJoins"/>
|
|
|
|
|
WHERE e.member_id = #{memberId} AND e.notif_key = #{notifKey}
|
|
|
|
|
LIMIT 1
|
|
|
|
|
</select>
|
|
|
|
|
|
|
|
|
|
<select id="countPending" resultType="int">
|
|
|
|
|
SELECT COUNT(*) FROM account_entry WHERE member_id = #{memberId} AND pending = 1
|
|
|
|
|
</select>
|
|
|
|
|
|
|
|
|
|
<update id="confirm">
|
|
|
|
|
UPDATE account_entry
|
|
|
|
|
SET pending = 0,
|
|
|
|
|
<if test="category != null">category = #{category},</if>
|
|
|
|
|
<if test="walletId != null">wallet_id = #{walletId},</if>
|
|
|
|
|
updated_at = NOW()
|
|
|
|
|
WHERE id = #{id} AND member_id = #{memberId} AND pending = 1
|
|
|
|
|
</update>
|
|
|
|
|
|
2026-05-31 15:43:09 +09:00
|
|
|
<select id="findDistinctCategories" resultType="string">
|
|
|
|
|
SELECT DISTINCT category FROM account_entry
|
|
|
|
|
WHERE member_id = #{memberId} AND type = #{type}
|
|
|
|
|
AND category IS NOT NULL AND category != ''
|
|
|
|
|
ORDER BY category
|
|
|
|
|
</select>
|
|
|
|
|
|
|
|
|
|
<update id="renameCategory">
|
|
|
|
|
UPDATE account_entry SET category = #{newName}
|
|
|
|
|
WHERE member_id = #{memberId} AND type = #{type} AND category = #{oldName}
|
|
|
|
|
</update>
|
|
|
|
|
|
|
|
|
|
<!-- ===== 항목-태그 ===== -->
|
|
|
|
|
<insert id="insertEntryTag">
|
|
|
|
|
INSERT INTO account_entry_tag (entry_id, tag_id) VALUES (#{entryId}, #{tagId})
|
|
|
|
|
</insert>
|
|
|
|
|
|
|
|
|
|
<delete id="deleteEntryTagsByEntryId" parameterType="long">
|
|
|
|
|
DELETE FROM account_entry_tag WHERE entry_id = #{entryId}
|
|
|
|
|
</delete>
|
|
|
|
|
|
|
|
|
|
<select id="findTagNamesByEntryId" parameterType="long" resultType="string">
|
|
|
|
|
SELECT t.name
|
|
|
|
|
FROM account_tag t
|
|
|
|
|
JOIN account_entry_tag aet ON aet.tag_id = t.id
|
|
|
|
|
WHERE aet.entry_id = #{entryId}
|
|
|
|
|
ORDER BY t.name
|
|
|
|
|
</select>
|
|
|
|
|
|
|
|
|
|
</mapper>
|