Skip to content

Commit

Permalink
修复交易类型顺序变更与预期不符的问题,以及加入GetList接口
Browse files Browse the repository at this point in the history
  • Loading branch information
小滋润 committed Apr 16, 2024
1 parent a7da085 commit 9c635a7
Show file tree
Hide file tree
Showing 8 changed files with 308 additions and 277 deletions.
4 changes: 2 additions & 2 deletions api/request/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ type CategoryGetTree struct {
}

type CategoryGetList struct {
AccountId uint `binding:"required"`
IncomeExpense constant.IncomeExpense `binding:"required"`
AccountId uint `binding:"required"`
IncomeExpense *constant.IncomeExpense `binding:"omitempty"`
}

type CategoryMapping struct {
Expand Down
73 changes: 61 additions & 12 deletions api/response/category.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
package response

import (
"KeepAccount/global"
"KeepAccount/global/constant"
categoryModel "KeepAccount/model/category"
"KeepAccount/util/dataType"
"github.com/pkg/errors"
)

func CategoryModelToResponse(category *categoryModel.Category) *CategoryOne {
if category == nil {
return &CategoryOne{}
}
return &CategoryOne{
Id: category.ID,
Name: category.Name,
Icon: category.Icon,
IncomeExpense: category.IncomeExpense,
}
}

type CategoryOne struct {
Id uint
Name string
Expand All @@ -33,13 +23,72 @@ func (co *CategoryOne) SetData(category categoryModel.Category) error {
return nil
}

type CategoryDetail struct {
Id uint
Name string
Icon string
FatherId uint
FatherName string
IncomeExpense constant.IncomeExpense
}

func (cd *CategoryDetail) SetData(category categoryModel.Category, father categoryModel.Father) error {
cd.Id = category.ID
cd.Name = category.Name
cd.Icon = category.Icon
cd.FatherId = father.ID
cd.FatherName = father.Name
cd.IncomeExpense = category.IncomeExpense
return nil
}

type CategoryDetailList []CategoryDetail

func (cdl *CategoryDetailList) SetData(categoryList dataType.Slice[uint, categoryModel.Category]) error {
*cdl = make(CategoryDetailList, len(categoryList), len(categoryList))
if len(categoryList) == 0 {
return nil
}

fatherIds := categoryList.ExtractValues(func(category categoryModel.Category) uint { return category.FatherId })
var fatherList dataType.Slice[uint, categoryModel.Father]
err := global.GvaDb.Where("id IN (?)", fatherIds).Find(&fatherList).Error
if err != nil {
return err
}
fatherMap := fatherList.ToMap(func(father categoryModel.Father) uint { return father.ID })

for i, category := range categoryList {
err = (*cdl)[i].SetData(category, fatherMap[category.FatherId])
if err != nil {
return err
}
}
return nil
}

type FatherOne struct {
Id uint
Name string
IncomeExpense constant.IncomeExpense
Children []CategoryOne
}

func (fo *FatherOne) SetData(father categoryModel.Father, categoryList []categoryModel.Category) error {
fo.Id = father.ID
fo.Name = father.Name
fo.IncomeExpense = father.IncomeExpense
fo.Children = make([]CategoryOne, len(categoryList), len(categoryList))
var err error
for i, category := range categoryList {
err = fo.Children[i].SetData(category)
if err != nil {
return err
}
}
return nil
}

type CategoryTree struct {
Tree []FatherOne
}
Expand Down
159 changes: 63 additions & 96 deletions api/v1/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (catApi *CategoryApi) CreateOne(ctx *gin.Context) {
}
father := categoryModel.Father{}
err := father.SelectById(requestData.FatherId)
if pass := checkFunc.AccountBelong(father.AccountID, ctx); false == pass {
if pass := checkFunc.AccountBelong(father.AccountId, ctx); false == pass {
return
}
if err != nil {
Expand Down Expand Up @@ -73,78 +73,70 @@ func (catApi *CategoryApi) CreateOneFather(ctx *gin.Context) {
}

func (catApi *CategoryApi) MoveCategory(ctx *gin.Context) {
var category categoryModel.Category
var previous *categoryModel.Category
err := global.GvaDb.First(&category, ctx.Param("id")).Error
if handelError(err, ctx) {
return
}
var requestData request.CategoryMoveCategory
if err := ctx.ShouldBindJSON(&requestData); err != nil {
response.FailToParameter(ctx, err)
return
}
if requestData.Previous != nil {
err = global.GvaDb.First(previous, requestData.Previous).Error
category, pass := contextFunc.GetCategoryByParam(ctx)
if !pass {
return
}
txFunc := func(tx *gorm.DB) error {
user, err := contextFunc.GetUser(ctx)
if err != nil {
response.FailToError(ctx, err)
return
return err
}
if previous.ID == category.ID {
response.FailWithMessage("数据异常", ctx)
return
var previous *categoryModel.Category
if requestData.Previous != nil {
previous = &categoryModel.Category{}
err = global.GvaDb.First(previous, requestData.Previous).Error
if err != nil {
return err
}
}
}

father := categoryModel.Father{}
if requestData.FatherId != nil {
err = global.GvaDb.First(&father, requestData.FatherId).Error
if err != nil {
response.FailToError(ctx, err)
return
var father *categoryModel.Father
if requestData.FatherId != nil {
father = &categoryModel.Father{}
err = global.GvaDb.First(father, requestData.FatherId).Error
if err != nil {
return err
}
}
return categoryService.MoveCategory(category, previous, father, user, tx)
}
err = global.GvaDb.Transaction(
func(tx *gorm.DB) error {
return categoryService.MoveCategory(category, previous, father, tx)
},
)

if err != nil {
response.FailToError(ctx, err)
err := global.GvaDb.Transaction(txFunc)
if responseError(err, ctx) {
return
}
response.Ok(ctx)
}

func (catApi *CategoryApi) MoveFather(ctx *gin.Context) {
var father categoryModel.Father
var previous *categoryModel.Father
err := global.GvaDb.First(&father, ctx.Param("id")).Error
if handelError(err, ctx) {
return
}
var requestData request.CategoryMoveFather
if err = ctx.ShouldBindJSON(&requestData); err != nil {
if err := ctx.ShouldBindJSON(&requestData); err != nil {
response.FailToParameter(ctx, err)
return
}
if requestData.Previous != nil {
err = global.GvaDb.First(previous, requestData.Previous).Error
txFunc := func(tx *gorm.DB) error {
var father categoryModel.Father
err := global.GvaDb.First(&father, ctx.Param("id")).Error
if err != nil {
response.FailToError(ctx, err)
return
return err
}
if previous.ID == father.ID {
response.FailWithMessage("数据异常", ctx)
return

var previous *categoryModel.Father
if requestData.Previous != nil {
previous = &categoryModel.Father{}
err = global.GvaDb.First(previous, requestData.Previous).Error
if err != nil {
return err
}
}
return categoryService.MoveFather(father, previous, tx)
}
err = global.GvaDb.Transaction(
func(tx *gorm.DB) error {
return categoryService.MoveFather(father, previous, tx)
},
)
err := global.GvaDb.Transaction(txFunc)
if handelError(err, ctx) {
return
}
Expand Down Expand Up @@ -210,29 +202,20 @@ func (catApi *CategoryApi) GetTree(ctx *gin.Context) {
response.FailToError(ctx, err)
return
}
categorySequence, err := categoryService.GetSequenceCategory(account, requestData.IncomeExpense)
if err != nil {
response.FailToError(ctx, err)
return
}
// 响应
var responseTree response.CategoryTree
var responseChildren []response.CategoryOne
for _, father := range fatherSequence {
responseChildren = make([]response.CategoryOne, 0)
if categorySequence[father.ID] != nil {
for _, category := range *categorySequence[father.ID] {
responseChildren = append(
responseChildren,
*response.CategoryModelToResponse(&category),
)
}
var categoryList []categoryModel.Category
responseTree.Tree = make([]response.FatherOne, len(fatherSequence), len(fatherSequence))

for i, father := range fatherSequence {
categoryList, err = categoryService.GetSequenceCategoryByFather(father)
if responseError(err, ctx) {
return
}
err = responseTree.Tree[i].SetData(father, categoryList)
if responseError(err, ctx) {
return
}
responseTree.Tree = append(
responseTree.Tree,
response.FatherOne{
Name: father.Name, Id: father.ID, IncomeExpense: father.IncomeExpense, Children: responseChildren,
},
)
}
response.OkWithData(responseTree, ctx)
}
Expand Down Expand Up @@ -265,7 +248,7 @@ func (catApi *CategoryApi) DeleteFather(ctx *gin.Context) {
response.FailToError(ctx, err)
return
}
if pass := checkFunc.AccountBelong(father.AccountID, ctx); pass == false {
if pass := checkFunc.AccountBelong(father.AccountId, ctx); pass == false {
return
}
err = global.GvaDb.Transaction(
Expand All @@ -285,43 +268,27 @@ func (catApi *CategoryApi) DeleteFather(ctx *gin.Context) {
}

func (catApi *CategoryApi) GetList(ctx *gin.Context) {
var requestData request.CategoryGetTree
var requestData request.CategoryGetList
var err error
if err = ctx.ShouldBindJSON(&requestData); err != nil {
response.FailToParameter(ctx, err)
return
}
var account accountModel.Account
err = global.GvaDb.First(&account, requestData.AccountId).Error
if err != nil {
response.FailToError(ctx, errors.Wrap(err, ""))
account, _, pass := checkFunc.AccountBelongAndGet(requestData.AccountId, ctx)
if false == pass {
return
}
fatherSequence, err := categoryService.GetSequenceFather(account, requestData.IncomeExpense)
if err != nil {
response.FailToError(ctx, err)
categoryList, err := categoryModel.NewDao().GetListByAccount(account)
if responseError(err, ctx) {
return
}
categorySequence, err := categoryService.GetSequenceCategory(account, requestData.IncomeExpense)
if err != nil {
response.FailToError(ctx, err)

var responseData response.CategoryDetailList
err = responseData.SetData(categoryList)
if responseError(err, ctx) {
return
}
var responseTree response.TwoLevelTree
var responseChildren []response.NameId
for _, father := range fatherSequence {
responseChildren = make([]response.NameId, 0)
if categorySequence[father.ID] != nil {
for _, category := range *categorySequence[father.ID] {
responseChildren = append(responseChildren, response.NameId{Name: category.Name, Id: category.ID})
}
}
responseTree.Tree = append(
responseTree.Tree,
response.Father{NameId: response.NameId{Name: father.Name, Id: father.ID}, Children: responseChildren},
)
}
response.OkWithData(responseTree, ctx)
response.OkWithData(response.List[response.CategoryDetail]{List: responseData}, ctx)
}

func (catApi *CategoryApi) MappingCategory(ctx *gin.Context) {
Expand Down
Loading

0 comments on commit 9c635a7

Please sign in to comment.