u
This commit is contained in:
1 parent
33ab76f73e
commit
3af982d669
31 files changed
+632
-316
No files matched your search
@@ -39,20 +39,75 @@ func QueryMoneyRecord(ctx context.Context, c *app.RequestContext) {
|
||||
}
|
||||
endTime := startTime.AddDate(0, 1, 0).Add(-time.Nanosecond)
|
||||
|
||||
// 获取 userID
|
||||
// 获取 userID 和 spaceID
|
||||
userID := middleware.GetUserID(ctx)
|
||||
spaceID, ok := middleware.GetSpaceID(ctx)
|
||||
|
||||
// 查询记录
|
||||
params := repo.FindUserMoneyRecordsParams{
|
||||
UserID: userID,
|
||||
RecordTime: pgtype.Timestamp{Time: startTime, Valid: true},
|
||||
RecordTime_2: pgtype.Timestamp{Time: endTime, Valid: true},
|
||||
}
|
||||
// 定义统一的 records 切片
|
||||
var dbRecordsUser []repo.FindUserMoneyRecordsRow
|
||||
var dbRecordsSpace []repo.FindMoneyRecordsBySpaceRow
|
||||
var errQuery error
|
||||
|
||||
records, err := db.Queries.FindUserMoneyRecords(ctx, params)
|
||||
if err != nil {
|
||||
errorx.AddError(c, err)
|
||||
return
|
||||
// 最终返回统一类型
|
||||
var records []*money.MoneyRecordItem
|
||||
|
||||
if ok { // 有空间ID,查询空间内所有用户
|
||||
params := repo.FindMoneyRecordsBySpaceParams{
|
||||
SpaceID: spaceID,
|
||||
RecordTime: pgtype.Timestamp{Time: startTime, Valid: true},
|
||||
RecordTime_2: pgtype.Timestamp{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.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),
|
||||
})
|
||||
}
|
||||
|
||||
} else { // 没有空间ID,只查询单用户
|
||||
params := repo.FindUserMoneyRecordsParams{
|
||||
UserID: userID,
|
||||
RecordTime: pgtype.Timestamp{Time: startTime, Valid: true},
|
||||
RecordTime_2: pgtype.Timestamp{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.MoneyRecordItem{
|
||||
Id: r.ID,
|
||||
CategoryId: r.CategoryID,
|
||||
CategoryType: r.CategoryType.Int32,
|
||||
CategoryIcon: r.CategoryIcon.String,
|
||||
CategoryName: r.CategoryName.String,
|
||||
UserId: r.UserID,
|
||||
Username: "", // 单用户查询没有 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),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 分组统计
|
||||
@@ -61,7 +116,7 @@ func QueryMoneyRecord(ctx context.Context, c *app.RequestContext) {
|
||||
totalExpense := decimal.NewFromInt(0)
|
||||
|
||||
for _, r := range records {
|
||||
day := r.RecordTime.Time.Format("2006-01-02")
|
||||
day := r.RecordTime[:10] // "YYYY-MM-DD"
|
||||
|
||||
group, ok := groupMap[day]
|
||||
if !ok {
|
||||
@@ -74,28 +129,16 @@ func QueryMoneyRecord(ctx context.Context, c *app.RequestContext) {
|
||||
groupMap[day] = group
|
||||
}
|
||||
|
||||
// 构造单条记录
|
||||
item := &money.MoneyRecordItem{
|
||||
Id: r.ID,
|
||||
CategoryId: r.CategoryID,
|
||||
UserId: r.UserID,
|
||||
Amount: pgtypex.NumericToString(r.Amount),
|
||||
Remark: r.Remark.String,
|
||||
RecordTime: pgtypex.TimestampToDateString(r.RecordTime),
|
||||
CreateTime: pgtypex.TimestampToDateTimeString(r.CreateTime),
|
||||
UpdateTime: pgtypex.TimestampToDateTimeString(r.UpdateTime),
|
||||
}
|
||||
|
||||
group.Records = append(group.Records, item)
|
||||
group.Records = append(group.Records, r)
|
||||
|
||||
// 收支累加
|
||||
amount := pgtypex.NumericToDecimal(r.Amount)
|
||||
if r.CategoryType.Int32 == 1 {
|
||||
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.Int32 == 0 {
|
||||
} else if r.CategoryType == 0 {
|
||||
expenseDec, _ := decimal.NewFromString(group.Expense)
|
||||
expenseDec = expenseDec.Add(amount)
|
||||
group.Expense = expenseDec.Round(2).StringFixed(2)
|
||||
@@ -103,14 +146,13 @@ func QueryMoneyRecord(ctx context.Context, c *app.RequestContext) {
|
||||
}
|
||||
}
|
||||
|
||||
// 对日期排序,最新在前
|
||||
// 对日期排序
|
||||
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])
|
||||
|
||||
@@ -89,7 +89,7 @@ func ListSpaceUsers(ctx context.Context, c *app.RequestContext) {
|
||||
response.Success(c).Data(resp).Send()
|
||||
}
|
||||
|
||||
// RemoveSpaceUser .
|
||||
// RemoveSpaceUser 移除空间用户
|
||||
// @router /space/user/remove [POST]
|
||||
func RemoveSpaceUser(ctx context.Context, c *app.RequestContext) {
|
||||
var req space.RemoveSpaceUserParams
|
||||
@@ -98,17 +98,17 @@ func RemoveSpaceUser(ctx context.Context, c *app.RequestContext) {
|
||||
return
|
||||
}
|
||||
|
||||
spaceId := req.SpaceId
|
||||
delUserId := req.UserId
|
||||
userID := middleware.GetUserID(ctx)
|
||||
delUserID := req.UserId
|
||||
currentUserID := middleware.GetUserID(ctx)
|
||||
|
||||
if userID == delUserId {
|
||||
// 不能删除自己
|
||||
if currentUserID == delUserID {
|
||||
response.Fail(c).Message("请退出空间或解散空间").Send()
|
||||
return
|
||||
}
|
||||
|
||||
// 当前操作者信息
|
||||
spaceUser, err := db.Queries.GetSpaceUserByUserId(ctx, userID)
|
||||
// 获取当前操作者所在空间及角色
|
||||
spaceUser, err := db.Queries.GetSpaceUserByUserId(ctx, currentUserID)
|
||||
if err != nil {
|
||||
if errorx.IsNotFound(err) {
|
||||
response.Fail(c).Message("权限不足").Send()
|
||||
@@ -118,36 +118,31 @@ func RemoveSpaceUser(ctx context.Context, c *app.RequestContext) {
|
||||
return
|
||||
}
|
||||
|
||||
if spaceUser.SpaceID != spaceId {
|
||||
response.Fail(c).Message("权限不足").Send()
|
||||
return
|
||||
}
|
||||
|
||||
// 检查权限
|
||||
if spaceUser.Role != 0 && spaceUser.Role != 1 {
|
||||
response.Fail(c).Message("权限不足").Send()
|
||||
return
|
||||
}
|
||||
|
||||
// 被踢用户是否在该空间
|
||||
targetUser, err := db.Queries.GetSpaceUserByUserId(ctx, delUserId)
|
||||
// 获取待删除用户的空间信息
|
||||
delUserSpace, err := db.Queries.GetSpaceByUserId(ctx, delUserID)
|
||||
if err != nil {
|
||||
if errorx.IsNotFound(err) {
|
||||
response.Success(c).Message("用户不在空间").Send()
|
||||
response.Fail(c).Message("待删除用户不存在或权限不足").Send()
|
||||
return
|
||||
}
|
||||
errorx.AddError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
if targetUser.SpaceID != spaceId {
|
||||
response.Fail(c).Message("用户不在该空间").Send()
|
||||
// 校验是否在同一个空间
|
||||
if spaceUser.SpaceID != delUserSpace.ID {
|
||||
response.Fail(c).Message("权限不足").Send()
|
||||
return
|
||||
}
|
||||
|
||||
// 删除
|
||||
err = db.Queries.DeleteSpaceUser(ctx, delUserId)
|
||||
|
||||
if err != nil {
|
||||
// 删除用户
|
||||
if err := db.Queries.DeleteSpaceUser(ctx, delUserID); err != nil {
|
||||
errorx.AddError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user