feat: 添加分类名称更新功能,支持在交易记录中同步更新分类名称
All checks were successful
Docker Build & Deploy / Build Docker Image (push) Successful in 23s
Docker Build & Deploy / Deploy to Production (push) Successful in 7s

This commit is contained in:
2026-01-01 15:20:59 +08:00
parent c58404491f
commit 8c72102e87
6 changed files with 173 additions and 34 deletions

29
.sql/init_categories.sql Normal file
View File

@@ -0,0 +1,29 @@
-- 支出分类 (Type = 0)
INSERT INTO TransactionCategory (Id, CreateTime, Name, Type) VALUES (10001, '2026-01-01 00:00:00', 'C餐饮美食', 0);
INSERT INTO TransactionCategory (Id, CreateTime, Name, Type) VALUES (10002, '2026-01-01 00:00:00', 'F服饰美容', 0);
INSERT INTO TransactionCategory (Id, CreateTime, Name, Type) VALUES (10003, '2026-01-01 00:00:00', 'S生活日用', 0);
INSERT INTO TransactionCategory (Id, CreateTime, Name, Type) VALUES (10004, '2026-01-01 00:00:00', 'G交通出行', 0);
INSERT INTO TransactionCategory (Id, CreateTime, Name, Type) VALUES (10005, '2026-01-01 00:00:00', 'X休闲娱乐', 0);
INSERT INTO TransactionCategory (Id, CreateTime, Name, Type) VALUES (10006, '2026-01-01 00:00:00', 'Y医疗保健', 0);
INSERT INTO TransactionCategory (Id, CreateTime, Name, Type) VALUES (10007, '2026-01-01 00:00:00', 'Z住房物业', 0);
INSERT INTO TransactionCategory (Id, CreateTime, Name, Type) VALUES (10008, '2026-01-01 00:00:00', 'S水电煤气', 0);
INSERT INTO TransactionCategory (Id, CreateTime, Name, Type) VALUES (10009, '2026-01-01 00:00:00', 'T通讯物流', 0);
INSERT INTO TransactionCategory (Id, CreateTime, Name, Type) VALUES (10010, '2026-01-01 00:00:00', 'S学习教育', 0);
INSERT INTO TransactionCategory (Id, CreateTime, Name, Type) VALUES (10011, '2026-01-01 00:00:00', 'R人情往来', 0);
INSERT INTO TransactionCategory (Id, CreateTime, Name, Type) VALUES (10012, '2026-01-01 00:00:00', 'Q其他支出', 0);
INSERT INTO TransactionCategory (Id, CreateTime, Name, Type) VALUES (10013, '2026-01-01 00:00:00', 'N奶茶咖啡', 0);
INSERT INTO TransactionCategory (Id, CreateTime, Name, Type) VALUES (10014, '2026-01-01 00:00:00', 'D钻石福袋', 0);
-- 收入分类 (Type = 1)
INSERT INTO TransactionCategory (Id, CreateTime, Name, Type) VALUES (11001, '2026-01-01 00:00:00', 'G工资薪金', 1);
INSERT INTO TransactionCategory (Id, CreateTime, Name, Type) VALUES (11002, '2026-01-01 00:00:00', 'J奖金补贴', 1);
INSERT INTO TransactionCategory (Id, CreateTime, Name, Type) VALUES (11003, '2026-01-01 00:00:00', 'L理财收益', 1);
INSERT INTO TransactionCategory (Id, CreateTime, Name, Type) VALUES (11004, '2026-01-01 00:00:00', 'J兼职收入', 1);
INSERT INTO TransactionCategory (Id, CreateTime, Name, Type) VALUES (11005, '2026-01-01 00:00:00', 'L礼金收入', 1);
INSERT INTO TransactionCategory (Id, CreateTime, Name, Type) VALUES (11006, '2026-01-01 00:00:00', 'Q其他收入', 1);
INSERT INTO TransactionCategory (Id, CreateTime, Name, Type) VALUES (11007, '2026-01-01 00:00:00', 'X闲鱼收入', 1);
-- 不记收支分类 (Type = 2)
INSERT INTO TransactionCategory (Id, CreateTime, Name, Type) VALUES (12001, '2026-01-01 00:00:00', 'Z转账给自己', 2);
INSERT INTO TransactionCategory (Id, CreateTime, Name, Type) VALUES (12002, '2026-01-01 00:00:00', 'Z转账到公共', 2);

View File

@@ -178,6 +178,15 @@ public interface ITransactionRecordRepository : IBaseRepository<TransactionRecor
/// <param name="currentType">当前交易类型</param>
/// <returns>候选交易列表</returns>
Task<List<TransactionRecord>> GetCandidatesForOffsetAsync(long currentId, decimal amount, TransactionType currentType);
/// <summary>
/// 更新分类名称
/// </summary>
/// <param name="oldName">旧分类名称</param>
/// <param name="newName">新分类名称</param>
/// <param name="type">交易类型</param>
/// <returns>影响行数</returns>
Task<int> UpdateCategoryNameAsync(string oldName, string newName, TransactionType type);
}
public class TransactionRecordRepository(IFreeSql freeSql) : BaseRepository<TransactionRecord>(freeSql), ITransactionRecordRepository
@@ -661,6 +670,14 @@ public class TransactionRecordRepository(IFreeSql freeSql) : BaseRepository<Tran
return list.OrderBy(t => Math.Abs(Math.Abs(t.Amount) - absAmount)).ToList();
}
public async Task<int> UpdateCategoryNameAsync(string oldName, string newName, TransactionType type)
{
return await FreeSql.Update<TransactionRecord>()
.Set(a => a.Classify, newName)
.Where(a => a.Classify == oldName && a.Type == type)
.ExecuteAffrowsAsync();
}
}
/// <summary>

View File

@@ -13,10 +13,10 @@
>
<van-tabs v-model:active="activeTab" shrink>
<van-tab title="一句话录账" name="one">
<OneLineBillAdd @success="handleSuccess" />
<OneLineBillAdd :key="componentKey" @success="handleSuccess" />
</van-tab>
<van-tab title="手动录账" name="manual">
<ManualBillAdd @success="handleSuccess" />
<ManualBillAdd :key="componentKey" @success="handleSuccess" />
</van-tab>
</van-tabs>
</PopupContainer>
@@ -33,11 +33,15 @@ const emit = defineEmits(['success'])
const showAddBill = ref(false)
const activeTab = ref('one')
const componentKey = ref(0)
const openAddBill = () => {
showAddBill.value = true
// Reset to default tab if needed, or keep last used
// activeTab.value = 'one'
// 清理状态,默认选中一句话录账
activeTab.value = 'one'
// 清理子组件状态通过 key 强制重渲染
componentKey.value++
}
const handleSuccess = () => {
@@ -50,7 +54,7 @@ const handleSuccess = () => {
<style scoped>
.floating-add {
position: fixed;
bottom: 80px; /* Above tabbar */
bottom: 95px; /* Above tabbar */
right: 20px;
width: 50px;
height: 50px;

View File

@@ -41,7 +41,11 @@
<van-cell-group v-else inset>
<van-swipe-cell v-for="category in categories" :key="category.id">
<van-cell :title="category.name" />
<van-cell
:title="category.name"
is-link
@click="handleEdit(category)"
/>
<template #right>
<van-button
square
@@ -85,6 +89,24 @@
</van-form>
</van-dialog>
<!-- 编辑分类对话框 -->
<van-dialog
v-model:show="showEditDialog"
title="编辑分类"
show-cancel-button
@confirm="handleConfirmEdit"
>
<van-form ref="editFormRef">
<van-field
v-model="editForm.name"
name="name"
label="分类名称"
placeholder="请输入分类名称"
:rules="[{ required: true, message: '请输入分类名称' }]"
/>
</van-form>
</van-dialog>
<!-- 删除确认对话框 -->
<van-dialog
v-model:show="showDeleteConfirm"
@@ -108,7 +130,8 @@ import {
import {
getCategoryList,
createCategory,
deleteCategory
deleteCategory,
updateCategory
} from '@/api/transactionCategory'
const router = useRouter()
@@ -142,6 +165,14 @@ const addForm = ref({
const showDeleteConfirm = ref(false)
const deleteTarget = ref(null)
// 编辑对话框
const showEditDialog = ref(false)
const editFormRef = ref(null)
const editForm = ref({
id: 0,
name: ''
})
// 计算导航栏标题
const navTitle = computed(() => {
if (currentLevel.value === 0) {
@@ -251,6 +282,50 @@ const handleConfirmAdd = async () => {
}
}
/**
* 编辑分类
*/
const handleEdit = (category) => {
editForm.value = {
id: category.id,
name: category.name
}
showEditDialog.value = true
}
/**
* 确认编辑
*/
const handleConfirmEdit = async () => {
try {
await editFormRef.value?.validate()
showLoadingToast({
message: '保存中...',
forbidClick: true,
duration: 0
})
const { success, message } = await updateCategory({
id: editForm.value.id,
name: editForm.value.name
})
if (success) {
showSuccessToast('保存成功')
showEditDialog.value = false
await loadCategories()
} else {
showToast(message || '保存失败')
}
} catch (error) {
console.error('保存失败:', error)
showToast('保存失败: ' + (error.message || '未知错误'))
} finally {
closeToast()
}
}
/**
* 删除分类
*/

View File

@@ -57,18 +57,23 @@
</van-pull-refresh>
<!-- 详情弹出层 -->
<van-popup
v-model:show="detailVisible"
position="bottom"
:style="{ height: '80%' }"
round
closeable
<PopupContainer
v-model="detailVisible"
:title="currentEmail ? (currentEmail.Subject || currentEmail.subject || '(无主题)') : ''"
height="80%"
>
<div class="popup-container" v-if="currentEmail">
<div class="popup-header-fixed">
<h3>{{ currentEmail.Subject || currentEmail.subject || '(无主题)' }}</h3>
</div>
<div class="popup-scroll-content">
<template #header-actions>
<van-button
size="small"
type="primary"
:loading="refreshingAnalysis"
@click="handleRefreshAnalysis"
>
重新分析
</van-button>
</template>
<div v-if="currentEmail">
<van-cell-group inset style="margin-top: 12px;">
<van-cell title="发件人" :value="currentEmail.From || currentEmail.from || '未知'" />
<van-cell title="接收时间" :value="formatDate(currentEmail.ReceivedDate || currentEmail.receivedDate)" />
@@ -101,21 +106,8 @@
</div>
</div>
</div>
<div style="margin: 16px;">
<van-button
round
block
type="primary"
:loading="refreshingAnalysis"
@click="handleRefreshAnalysis"
>
重新分析
</van-button>
</div>
</div>
</div>
</van-popup>
</PopupContainer>
<!-- 账单列表弹出层 -->
<PopupContainer
@@ -343,6 +335,7 @@ const viewTransactions = async () => {
// 监听全局删除事件,保持弹窗内交易列表一致
const onGlobalTransactionDeleted = (e) => {
console.log('收到全局交易删除事件:', e)
// 如果交易列表弹窗打开,尝试重新加载邮箱的交易列表
if (transactionListVisible.value && currentEmail.value) {
const emailId = currentEmail.value.id || currentEmail.value.Id
@@ -362,6 +355,7 @@ onBeforeUnmount(() => {
// 监听新增/修改/批量更新事件,刷新弹窗内交易或邮件列表
const onGlobalTransactionsChanged = (e) => {
console.log('收到全局交易变更事件:', e)
if (transactionListVisible.value && currentEmail.value) {
const emailId = currentEmail.value.id || currentEmail.value.Id
getEmailTransactions(emailId).then(response => {
@@ -413,7 +407,12 @@ const handleTransactionDelete = (transactionId) => {
}
})
}
try { window.dispatchEvent(new CustomEvent('transaction-deleted', { detail: transactionId })) } catch(e) {}
try {
window.dispatchEvent(
new CustomEvent('transaction-deleted', { detail: transactionId }))
} catch(e) {
console.error(e)
}
}
// 账单保存后刷新列表
@@ -426,7 +425,18 @@ const handleTransactionSave = async () => {
transactionList.value = response.data || []
}
}
try { window.dispatchEvent(new CustomEvent('transactions-changed', { detail: { emailId: currentEmail.value?.id } })) } catch(e) {}
try {
window.dispatchEvent(
new CustomEvent(
'transactions-changed',
{
detail: {
emailId: currentEmail.value?.id
}
}))
} catch(e) {
console.error(e)
}
}
// 格式化日期

View File

@@ -4,6 +4,7 @@
[Route("api/[controller]/[action]")]
public class TransactionCategoryController(
ITransactionCategoryRepository categoryRepository,
ITransactionRecordRepository transactionRecordRepository,
ILogger<TransactionCategoryController> logger
) : ControllerBase
{
@@ -129,6 +130,9 @@ public class TransactionCategoryController(
{
return BaseResponse.Fail("已存在相同名称的分类");
}
// 同步更新交易记录中的分类名称
await transactionRecordRepository.UpdateCategoryNameAsync(category.Name, dto.Name, category.Type);
}
category.Name = dto.Name;