mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-15 23:47:33 +08:00
batch create nonexistent transaction tags when import transaction
This commit is contained in:
@@ -101,6 +101,47 @@ func (a *TransactionTagsApi) TagCreateHandler(c *core.WebContext) (any, *errs.Er
|
||||
return tagResp, nil
|
||||
}
|
||||
|
||||
// TagCreateBatchHandler saves some new transaction tags by request parameters for current user
|
||||
func (a *TransactionTagsApi) TagCreateBatchHandler(c *core.WebContext) (any, *errs.Error) {
|
||||
var tagCreateBatchReq models.TransactionTagCreateBatchRequest
|
||||
err := c.ShouldBindJSON(&tagCreateBatchReq)
|
||||
|
||||
if err != nil {
|
||||
log.Warnf(c, "[transaction_tags.TagCreateBatchHandler] parse request failed, because %s", err.Error())
|
||||
return nil, errs.NewIncompleteOrIncorrectSubmissionError(err)
|
||||
}
|
||||
|
||||
uid := c.GetCurrentUid()
|
||||
|
||||
maxOrderId, err := a.tags.GetMaxDisplayOrder(c, uid)
|
||||
|
||||
if err != nil {
|
||||
log.Errorf(c, "[transaction_tags.TagCreateBatchHandler] failed to get max display order for user \"uid:%d\", because %s", uid, err.Error())
|
||||
return nil, errs.Or(err, errs.ErrOperationFailed)
|
||||
}
|
||||
|
||||
tags := a.createNewTagModels(uid, &tagCreateBatchReq, maxOrderId+1)
|
||||
|
||||
err = a.tags.CreateTags(c, uid, tags, tagCreateBatchReq.SkipExists)
|
||||
|
||||
if err != nil {
|
||||
log.Errorf(c, "[transaction_tags.TagCreateBatchHandler] failed to create tags for user \"uid:%d\", because %s", uid, err.Error())
|
||||
return nil, errs.Or(err, errs.ErrOperationFailed)
|
||||
}
|
||||
|
||||
log.Infof(c, "[transaction_tags.TagCreateBatchHandler] user \"uid:%d\" has created tags successfully", uid)
|
||||
|
||||
tagResps := make(models.TransactionTagInfoResponseSlice, len(tags))
|
||||
|
||||
for i := 0; i < len(tags); i++ {
|
||||
tagResps[i] = tags[i].ToTransactionTagInfoResponse()
|
||||
}
|
||||
|
||||
sort.Sort(tagResps)
|
||||
|
||||
return tagResps, nil
|
||||
}
|
||||
|
||||
// TagModifyHandler saves an existed transaction tag by request parameters for current user
|
||||
func (a *TransactionTagsApi) TagModifyHandler(c *core.WebContext) (any, *errs.Error) {
|
||||
var tagModifyReq models.TransactionTagModifyRequest
|
||||
@@ -230,3 +271,15 @@ func (a *TransactionTagsApi) createNewTagModel(uid int64, tagCreateReq *models.T
|
||||
DisplayOrder: order,
|
||||
}
|
||||
}
|
||||
|
||||
func (a *TransactionTagsApi) createNewTagModels(uid int64, tagCreateBatchReq *models.TransactionTagCreateBatchRequest, order int32) []*models.TransactionTag {
|
||||
tags := make([]*models.TransactionTag, len(tagCreateBatchReq.Tags))
|
||||
|
||||
for i := 0; i < len(tagCreateBatchReq.Tags); i++ {
|
||||
tagCreateReq := tagCreateBatchReq.Tags[i]
|
||||
tag := a.createNewTagModel(uid, tagCreateReq, order+int32(i))
|
||||
tags[i] = tag
|
||||
}
|
||||
|
||||
return tags
|
||||
}
|
||||
|
||||
@@ -23,6 +23,12 @@ type TransactionTagCreateRequest struct {
|
||||
Name string `json:"name" binding:"required,notBlank,max=64"`
|
||||
}
|
||||
|
||||
// TransactionTagCreateBatchRequest represents all parameters of transaction tag batch creation request
|
||||
type TransactionTagCreateBatchRequest struct {
|
||||
Tags []*TransactionTagCreateRequest `json:"tags" binding:"required"`
|
||||
SkipExists bool `json:"skipExists"`
|
||||
}
|
||||
|
||||
// TransactionTagModifyRequest represents all parameters of transaction tag modification request
|
||||
type TransactionTagModifyRequest struct {
|
||||
Id int64 `json:"id,string" binding:"required,min=1"`
|
||||
@@ -59,6 +65,19 @@ type TransactionTagInfoResponse struct {
|
||||
Hidden bool `json:"hidden"`
|
||||
}
|
||||
|
||||
// FillFromOtherTag fills all the fields in this current tag from other transaction tag
|
||||
func (t *TransactionTag) FillFromOtherTag(tag *TransactionTag) {
|
||||
t.TagId = tag.TagId
|
||||
t.Uid = tag.Uid
|
||||
t.Deleted = tag.Deleted
|
||||
t.Name = tag.Name
|
||||
t.DisplayOrder = tag.DisplayOrder
|
||||
t.Hidden = tag.Hidden
|
||||
t.CreatedUnixTime = tag.CreatedUnixTime
|
||||
t.UpdatedUnixTime = tag.UpdatedUnixTime
|
||||
t.DeletedUnixTime = tag.DeletedUnixTime
|
||||
}
|
||||
|
||||
// ToTransactionTagInfoResponse returns a view-object according to database model
|
||||
func (t *TransactionTag) ToTransactionTagInfoResponse() *TransactionTagInfoResponse {
|
||||
return &TransactionTagInfoResponse{
|
||||
|
||||
@@ -222,6 +222,76 @@ func (s *TransactionTagService) CreateTag(c core.Context, tag *models.Transactio
|
||||
})
|
||||
}
|
||||
|
||||
// CreateTags saves a few transaction tag models to database
|
||||
func (s *TransactionTagService) CreateTags(c core.Context, uid int64, tags []*models.TransactionTag, skipExists bool) error {
|
||||
if uid <= 0 {
|
||||
return errs.ErrUserIdInvalid
|
||||
}
|
||||
|
||||
allTagNames := make([]string, len(tags))
|
||||
|
||||
for i := 0; i < len(tags); i++ {
|
||||
allTagNames[i] = tags[i].Name
|
||||
}
|
||||
|
||||
var existTags []*models.TransactionTag
|
||||
err := s.UserDataDB(uid).NewSession(c).Where("uid=? AND deleted=?", uid, false).In("name", allTagNames).Find(&existTags)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
} else if !skipExists && len(existTags) > 0 {
|
||||
return errs.ErrTransactionTagNameAlreadyExists
|
||||
}
|
||||
|
||||
existsNameTagMap := make(map[string]*models.TransactionTag, len(existTags))
|
||||
|
||||
for i := 0; i < len(existTags); i++ {
|
||||
tag := existTags[i]
|
||||
existsNameTagMap[tag.Name] = tag
|
||||
}
|
||||
|
||||
newTags := make([]*models.TransactionTag, 0, len(tags)-len(existTags))
|
||||
|
||||
for i := 0; i < len(tags); i++ {
|
||||
tag := tags[i]
|
||||
existsTag, exists := existsNameTagMap[tag.Name]
|
||||
|
||||
if exists {
|
||||
tag.FillFromOtherTag(existsTag)
|
||||
continue
|
||||
}
|
||||
|
||||
newTags = append(newTags, tag)
|
||||
}
|
||||
|
||||
tagUuids := s.GenerateUuids(uuid.UUID_TYPE_TAG_INDEX, uint16(len(newTags)))
|
||||
|
||||
if len(tagUuids) < len(newTags) {
|
||||
return errs.ErrSystemIsBusy
|
||||
}
|
||||
|
||||
for i := 0; i < len(newTags); i++ {
|
||||
tag := newTags[i]
|
||||
tag.TagId = tagUuids[i]
|
||||
tag.Deleted = false
|
||||
tag.CreatedUnixTime = time.Now().Unix()
|
||||
tag.UpdatedUnixTime = time.Now().Unix()
|
||||
}
|
||||
|
||||
return s.UserDataDB(uid).DoTransaction(c, func(sess *xorm.Session) error {
|
||||
for i := 0; i < len(newTags); i++ {
|
||||
tag := newTags[i]
|
||||
_, err := sess.Insert(tag)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// ModifyTag saves an existed transaction tag model to database
|
||||
func (s *TransactionTagService) ModifyTag(c core.Context, tag *models.TransactionTag) error {
|
||||
if tag.Uid <= 0 {
|
||||
|
||||
Reference in New Issue
Block a user