This commit is contained in:
oneao committed 2026-01-28 22:32:28 +08:00
1 parent d9c49b34db
commit d33cef825a
36 files changed
+753 -1077

No files matched your search

@@ -69,7 +69,24 @@ func DeleteUserMoneyCategory(ctx context.Context, c *app.RequestContext) {
return
}
err = db.Queries.DeleteMoneyCategoryById(ctx, req.Id)
userID := middleware.GetUserID(ctx)
categoryId := req.Id
err = db.WithTx(ctx, func(q *repo.Queries) error {
err2 := q.DeleteMoneyCategoryById(ctx, categoryId)
if err2 != nil {
return err2
}
err2 = q.DeleteMoneyRecordByCategoryId(ctx, repo.DeleteMoneyRecordByCategoryIdParams{
UserID: userID,
CategoryID: categoryId,
})
if err2 != nil {
return err2
}
return nil
})
if err != nil {
errorx.AddError(c, err)
@@ -12,101 +12,66 @@ import (
"allapp/utils/response"
"context"
"fmt"
"time"
money "allapp/biz/model/money"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/protocol/consts"
"github.com/jackc/pgx/v5/pgtype"
)
// QueryMoneyRecord .
// @router /money/record/query [POST]
func QueryMoneyRecord(ctx context.Context, c *app.RequestContext) {
var req money.QueryMoneyRecordsParams
// ListMoneyRecords 账目列表
// @router /money/record/list [POST]
func ListMoneyRecords(ctx context.Context, c *app.RequestContext) {
var req money.ListMoneyRecordsParams
if err := c.BindAndValidate(&req); err != nil {
c.String(consts.StatusBadRequest, err.Error())
return
}
//// 解析年月
//startTime, err := time.Parse("2006-01", req.RecordTime)
//if err != nil {
// errorx.AddError(c, fmt.Errorf("invalid record_time format: %v", err))
// return
//}
//endTime := startTime.AddDate(0, 1, 0).Add(-time.Nanosecond)
//
//// 获取 userID 和 spaceID
//userID := middleware.GetUserID(ctx)
//spaceIspaceID := middleware.GetSpaceID(ctx)
//
//// 定义统一的 records 切片
//var dbRecordsUser []repo.MoneyRecord
//var dbRecordsSpace []repo.FindMoneyRecordsBySpaceRow
//var errQuery error
//
//// 最终返回统一类型
//var records []*money.MoneyRecord
//
//// 有空间ID,查询空间内所有用户
//if ok {
// params := repo.FindMoneyRecordsBySpaceParams{
// SpaceID: spaceID,
// RecordTime: pgtype.Date{Time: startTime, Valid: true},
// RecordTime_2: pgtype.Date{Time: endTime, Valid: true},
// }
// dbRecordsSpace, errQuery = db.Queries.FindMoneyRecordsBySpace(ctx, params)
// if errQuery != nil {
// errorx.AddError(c, errQuery)
// return
// }
//
// for _, r := range dbRecordsSpace {
// records = append(records, &money.MoneyRecord{
// Id: r.ID,
// CategoryId: r.CategoryID,
// UserId: r.UserID,
// Username: r.Username.String,
// Amount: pgtypex.NumericToString(r.Amount),
// Remark: r.Remark.String,
// RecordTime: pgtypex.DateToString(r.RecordTime),
// CreateTime: pgtypex.TimestampToMillis(r.CreateTime),
// UpdateTime: pgtypex.TimestampToMillis(r.UpdateTime),
// })
// }
//} else {
// // 没有空间ID,只查询单用户
// params := repo.FindUserMoneyRecordsParams{
// UserID: userID,
// RecordTime: pgtype.Date{Time: startTime, Valid: true},
// RecordTime_2: pgtype.Date{Time: endTime, Valid: true},
// }
// dbRecordsUser, errQuery = db.Queries.FindUserMoneyRecords(ctx, params)
// if errQuery != nil {
// errorx.AddError(c, errQuery)
// return
// }
//
// for _, r := range dbRecordsUser {
// records = append(records, &money.MoneyRecord{
// Id: r.ID,
// CategoryId: r.CategoryID,
// UserId: r.UserID,
// Username: "",
// Amount: pgtypex.NumericToString(r.Amount),
// Remark: r.Remark.String,
// RecordTime: pgtypex.DateToString(r.RecordTime),
// CreateTime: pgtypex.TimestampToMillis(r.CreateTime),
// UpdateTime: pgtypex.TimestampToMillis(r.UpdateTime),
// })
// }
//}
//
//result := &money.QueryMoneyRecordsResult{
// Records: records,
//}
// 解析年月(yyyy-MM)
startTime, err := time.Parse("2006-01", req.RecordTime)
if err != nil {
errorx.AddError(c, fmt.Errorf("invalid record_time format: %v", err))
return
}
endTime := startTime.AddDate(0, 1, 0).Add(-time.Nanosecond)
response.Success(c).Send()
userID := middleware.GetUserID(ctx)
// 查询账目列表(统一 SQL)
dbRecords, err := db.Queries.ListMoneyRecords(
ctx,
repo.ListMoneyRecordsParams{
UserID: userID,
RecordTime: pgtype.Date{Time: startTime, Valid: true},
RecordTime_2: pgtype.Date{Time: endTime, Valid: true},
},
)
if err != nil {
errorx.AddError(c, err)
return
}
records := make([]*money.MoneyRecord, 0, len(dbRecords))
for _, r := range dbRecords {
records = append(records, &money.MoneyRecord{
Id: r.ID,
CategoryId: r.CategoryID,
UserId: r.UserID,
Username: r.Username.String,
Amount: pgtypex.NumericToString(r.Amount),
Remark: r.Remark.String,
RecordTime: pgtypex.DateToString(r.RecordTime),
CreateTime: pgtypex.TimestampToMillis(r.CreateTime),
UpdateTime: pgtypex.TimestampToMillis(r.UpdateTime),
})
}
response.Success(c).Data(&money.ListMoneyRecordsResult{
Records: records,
}).Send()
}
// InsertMoneyRecord .
@@ -122,8 +87,6 @@ func InsertMoneyRecord(ctx context.Context, c *app.RequestContext) {
return
}
resp := new(money.InsertMoneyRecordResult)
params := repo.InsertMoneyRecordParams{
ID: idgen.NextId(),
CategoryID: req.CategoryId,
@@ -133,22 +96,14 @@ func InsertMoneyRecord(ctx context.Context, c *app.RequestContext) {
RecordTime: pgtypex.StringToDate(req.RecordTime),
}
record, err := db.Queries.InsertMoneyRecord(ctx, params)
err = db.Queries.InsertMoneyRecord(ctx, params)
if err != nil {
errorx.AddError(c, err)
return
}
resp.Id = record.ID
resp.CategoryId = record.CategoryID
resp.UserId = record.UserID
resp.Amount = pgtypex.NumericToString(record.Amount)
resp.Remark = record.Remark.String
resp.RecordTime = pgtypex.DateToString(record.RecordTime)
resp.CreateTime = pgtypex.TimestampToDateTimeString(record.CreateTime)
resp.UpdateTime = pgtypex.TimestampToDateTimeString(record.UpdateTime)
response.Success(c).Data(resp).Message("新增成功").Send()
response.Success(c).Message("新增成功").Send()
}
// UpdateMoneyRecord .
@@ -34,7 +34,7 @@ func GetUserSpace(ctx context.Context, c *app.RequestContext) {
if err != nil {
if errorx.IsNotFound(err) {
response.Success(c).Data(resp).Send()
response.Success(c).Data(nil).Send()
return
}