u
This commit is contained in:
1 parent
78c7a812be
commit
1934225c53
24 files changed
+711
-654
No files matched your search
@@ -164,3 +164,39 @@ func CreateMoneyCategory(ctx context.Context, c *app.RequestContext) {
|
||||
|
||||
response.Success(c).Data(resp).Send()
|
||||
}
|
||||
|
||||
// UpdateMoneyCategory .
|
||||
// @router /money/category/update [POST]
|
||||
func UpdateMoneyCategory(ctx context.Context, c *app.RequestContext) {
|
||||
var err error
|
||||
var req money.UpdateMoneyCategoryReq
|
||||
err = c.BindAndValidate(&req)
|
||||
if err != nil {
|
||||
c.String(consts.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
resp := new(money.UpdateMoneyCategoryResp)
|
||||
|
||||
params := repo.UpdateMoneyCategoryParams{
|
||||
ID: req.Id,
|
||||
Name: req.Name,
|
||||
Icon: req.Icon,
|
||||
}
|
||||
|
||||
// 插入
|
||||
err = db.WithTx(ctx, func(q *repo.Queries) error {
|
||||
err = q.UpdateMoneyCategory(ctx, params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
errorx.AddError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c).Data(resp).Send()
|
||||
}
|
||||
@@ -12,11 +12,9 @@ import (
|
||||
"allapp/utils/response"
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/shopspring/decimal"
|
||||
|
||||
money "allapp/biz/model/money"
|
||||
|
||||
@@ -46,19 +44,19 @@ func QueryMoneyRecord(ctx context.Context, c *app.RequestContext) {
|
||||
spaceID, ok := middleware.GetSpaceID(ctx)
|
||||
|
||||
// 定义统一的 records 切片
|
||||
var dbRecordsUser []repo.FindUserMoneyRecordsRow
|
||||
var dbRecordsUser []repo.MoneyRecord
|
||||
var dbRecordsSpace []repo.FindMoneyRecordsBySpaceRow
|
||||
var errQuery error
|
||||
|
||||
// 最终返回统一类型
|
||||
var records []*money.MoneyRecordItem
|
||||
var records []*money.MoneyRecord
|
||||
|
||||
// 有空间ID,查询空间内所有用户
|
||||
if ok {
|
||||
params := repo.FindMoneyRecordsBySpaceParams{
|
||||
SpaceID: spaceID,
|
||||
RecordTime: pgtype.Timestamp{Time: startTime, Valid: true},
|
||||
RecordTime_2: pgtype.Timestamp{Time: endTime, Valid: true},
|
||||
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 {
|
||||
@@ -67,27 +65,24 @@ func QueryMoneyRecord(ctx context.Context, c *app.RequestContext) {
|
||||
}
|
||||
|
||||
for _, r := range dbRecordsSpace {
|
||||
records = append(records, &money.MoneyRecordItem{
|
||||
Id: r.ID,
|
||||
CategoryId: r.CategoryID,
|
||||
CategoryType: r.CategoryType.Int32,
|
||||
CategoryIcon: r.CategoryIcon.String,
|
||||
CategoryName: r.CategoryName.String,
|
||||
UserId: r.UserID,
|
||||
Username: r.Username.String,
|
||||
Amount: pgtypex.NumericToString(r.Amount),
|
||||
Remark: r.Remark.String,
|
||||
RecordTime: pgtypex.TimestampToDateString(r.RecordTime),
|
||||
CreateTime: pgtypex.TimestampToDateTimeString(r.CreateTime),
|
||||
UpdateTime: pgtypex.TimestampToDateTimeString(r.UpdateTime),
|
||||
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.Timestamp{Time: startTime, Valid: true},
|
||||
RecordTime_2: pgtype.Timestamp{Time: endTime, Valid: true},
|
||||
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 {
|
||||
@@ -96,76 +91,22 @@ func QueryMoneyRecord(ctx context.Context, c *app.RequestContext) {
|
||||
}
|
||||
|
||||
for _, r := range dbRecordsUser {
|
||||
records = append(records, &money.MoneyRecordItem{
|
||||
Id: r.ID,
|
||||
CategoryId: r.CategoryID,
|
||||
CategoryType: r.CategoryType.Int32,
|
||||
CategoryIcon: r.CategoryIcon.String,
|
||||
CategoryName: r.CategoryName.String,
|
||||
UserId: r.UserID,
|
||||
Username: "",
|
||||
Amount: pgtypex.NumericToString(r.Amount),
|
||||
Remark: r.Remark.String,
|
||||
RecordTime: pgtypex.TimestampToDateString(r.RecordTime),
|
||||
CreateTime: pgtypex.TimestampToDateTimeString(r.CreateTime),
|
||||
UpdateTime: pgtypex.TimestampToDateTimeString(r.UpdateTime),
|
||||
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),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 分组统计
|
||||
groupMap := make(map[string]*money.MoneyRecordGroup)
|
||||
totalIncome := decimal.NewFromInt(0)
|
||||
totalExpense := decimal.NewFromInt(0)
|
||||
|
||||
for _, r := range records {
|
||||
day := r.RecordTime[:10] // "YYYY-MM-DD"
|
||||
|
||||
group, ok := groupMap[day]
|
||||
if !ok {
|
||||
group = &money.MoneyRecordGroup{
|
||||
RecordTime: day,
|
||||
Income: "0.00",
|
||||
Expense: "0.00",
|
||||
Records: make([]*money.MoneyRecordItem, 0),
|
||||
}
|
||||
groupMap[day] = group
|
||||
}
|
||||
|
||||
group.Records = append(group.Records, r)
|
||||
|
||||
// 收支累加
|
||||
amount := decimal.RequireFromString(r.Amount)
|
||||
if r.CategoryType == 1 {
|
||||
incomeDec, _ := decimal.NewFromString(group.Income)
|
||||
incomeDec = incomeDec.Add(amount)
|
||||
group.Income = incomeDec.Round(2).StringFixed(2)
|
||||
totalIncome = totalIncome.Add(amount)
|
||||
} else if r.CategoryType == 0 {
|
||||
expenseDec, _ := decimal.NewFromString(group.Expense)
|
||||
expenseDec = expenseDec.Add(amount)
|
||||
group.Expense = expenseDec.Round(2).StringFixed(2)
|
||||
totalExpense = totalExpense.Add(amount)
|
||||
}
|
||||
}
|
||||
|
||||
// 对日期排序
|
||||
dates := make([]string, 0, len(groupMap))
|
||||
for k := range groupMap {
|
||||
dates = append(dates, k)
|
||||
}
|
||||
sort.Slice(dates, func(i, j int) bool { return dates[i] > dates[j] })
|
||||
|
||||
resultGroups := make([]*money.MoneyRecordGroup, 0, len(dates))
|
||||
for _, day := range dates {
|
||||
resultGroups = append(resultGroups, groupMap[day])
|
||||
}
|
||||
|
||||
result := &money.QueryMoneyRecordsResult{
|
||||
Records: resultGroups,
|
||||
TotalIncome: totalIncome.Round(2).StringFixed(2),
|
||||
TotalExpense: totalExpense.Round(2).StringFixed(2),
|
||||
TotalBalance: totalIncome.Sub(totalExpense).Round(2).StringFixed(2),
|
||||
Records: records,
|
||||
}
|
||||
|
||||
response.Success(c).Data(result).Send()
|
||||
@@ -192,17 +133,22 @@ func InsertMoneyRecord(ctx context.Context, c *app.RequestContext) {
|
||||
UserID: middleware.GetUserID(ctx),
|
||||
Amount: pgtypex.StringToNumeric(req.Amount),
|
||||
Remark: pgtypex.StringToText(req.Remark),
|
||||
RecordTime: pgtypex.StringToTimestamp(req.RecordTime),
|
||||
RecordTime: pgtypex.StringToDate(req.RecordTime),
|
||||
}
|
||||
|
||||
record, 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.TimestampToDateString(record.RecordTime)
|
||||
resp.RecordTime = pgtypex.DateToString(record.RecordTime)
|
||||
resp.CreateTime = pgtypex.TimestampToDateTimeString(record.CreateTime)
|
||||
resp.UpdateTime = pgtypex.TimestampToDateTimeString(record.UpdateTime)
|
||||
|
||||
@@ -234,7 +180,7 @@ func UpdateMoneyRecord(ctx context.Context, c *app.RequestContext) {
|
||||
CategoryID: req.CategoryId,
|
||||
Amount: pgtypex.StringToNumeric(req.Amount),
|
||||
Remark: pgtypex.StringToText(req.Remark),
|
||||
RecordTime: pgtypex.StringToTimestamp(req.RecordTime),
|
||||
RecordTime: pgtypex.StringToDate(req.RecordTime),
|
||||
}
|
||||
|
||||
if err := db.Queries.UpdateMoneyRecord(ctx, params); err != nil {
|
||||
|
||||
Reference in new issue
Block a user