mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-14 06:57:35 +08:00
add command transaction-tag-index-fix-transaction-time to fix transaction tag index which does not have transaction time
This commit is contained in:
@@ -547,6 +547,72 @@ func (l *UserDataCli) CheckTransactionAndAccount(c *cli.Context, username string
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// FixTransactionTagIndexWithTransactionTime fixes user transaction tag index data with transaction time
|
||||
func (l *UserDataCli) FixTransactionTagIndexWithTransactionTime(c *cli.Context, username string) (bool, error) {
|
||||
if username == "" {
|
||||
log.BootErrorf("[user_data.FixTransactionTagIndexWithTransactionTime] user name is empty")
|
||||
return false, errs.ErrUsernameIsEmpty
|
||||
}
|
||||
|
||||
uid, err := l.getUserIdByUsername(c, username)
|
||||
|
||||
if err != nil {
|
||||
log.BootErrorf("[user_data.FixTransactionTagIndexWithTransactionTime] error occurs when getting user id by user name")
|
||||
return false, err
|
||||
}
|
||||
|
||||
tagIndexs, err := l.tags.GetAllTagIdsOfAllTransactions(nil, uid)
|
||||
|
||||
if err != nil {
|
||||
log.BootErrorf("[user_data.FixTransactionTagIndexWithTransactionTime] failed to get tag index for user \"%s\", because %s", username, err.Error())
|
||||
return false, err
|
||||
}
|
||||
|
||||
invalidTagIndexs := make([]*models.TransactionTagIndex, 0, len(tagIndexs))
|
||||
|
||||
for i := 0; i < len(tagIndexs); i++ {
|
||||
tagIndex := tagIndexs[i]
|
||||
|
||||
if tagIndex.TransactionTime < 1 {
|
||||
invalidTagIndexs = append(invalidTagIndexs, tagIndex)
|
||||
}
|
||||
}
|
||||
|
||||
if len(invalidTagIndexs) < 1 {
|
||||
log.BootErrorf("[user_data.FixTransactionTagIndexWithTransactionTime] all user transaction tag index data has been checked, there is no problem with user data")
|
||||
return false, errs.ErrOperationFailed
|
||||
}
|
||||
|
||||
allTransactions, err := l.transactions.GetAllTransactions(nil, uid, pageCountForGettingTransactions, false)
|
||||
|
||||
if err != nil {
|
||||
log.BootErrorf("[user_data.FixTransactionTagIndexWithTransactionTime] failed to all transactions for user \"%s\", because %s", username, err.Error())
|
||||
return false, err
|
||||
}
|
||||
|
||||
transactionMap := l.transactions.GetTransactionMapByList(allTransactions)
|
||||
|
||||
for i := 0; i < len(invalidTagIndexs); i++ {
|
||||
tagIndex := invalidTagIndexs[i]
|
||||
transaction, exists := transactionMap[tagIndex.TransactionId]
|
||||
|
||||
if !exists {
|
||||
continue
|
||||
}
|
||||
|
||||
tagIndex.TransactionTime = transaction.TransactionTime
|
||||
}
|
||||
|
||||
err = l.tags.ModifyTagIndexTransactionTime(nil, uid, invalidTagIndexs)
|
||||
|
||||
if err != nil {
|
||||
log.BootErrorf("[user_data.FixTransactionTagIndexWithTransactionTime] failed to update transaction tag index for user \"%s\", because %s", username, err.Error())
|
||||
return false, err
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// ExportTransaction returns csv file content according user all transactions
|
||||
func (l *UserDataCli) ExportTransaction(c *cli.Context, username string, fileType string) ([]byte, error) {
|
||||
if username == "" {
|
||||
|
||||
@@ -9,4 +9,5 @@ var (
|
||||
ErrTransactionTagNameIsEmpty = NewNormalError(NormalSubcategoryTag, 2, http.StatusBadRequest, "transaction tag name is empty")
|
||||
ErrTransactionTagNameAlreadyExists = NewNormalError(NormalSubcategoryTag, 3, http.StatusBadRequest, "transaction tag name already exists")
|
||||
ErrTransactionTagInUseCannotBeDeleted = NewNormalError(NormalSubcategoryTag, 4, http.StatusBadRequest, "transaction tag is in use and cannot be deleted")
|
||||
ErrTransactionTagIndexNotFound = NewNormalError(NormalSubcategoryTag, 5, http.StatusBadRequest, "transaction tag index not found")
|
||||
)
|
||||
|
||||
@@ -342,6 +342,32 @@ func (s *TransactionTagService) ExistsTagName(c *core.Context, uid int64, name s
|
||||
return s.UserDataDB(uid).NewSession(c).Cols("name").Where("uid=? AND deleted=? AND name=?", uid, false, name).Exist(&models.TransactionTag{})
|
||||
}
|
||||
|
||||
// ModifyTagIndexTransactionTime updates transaction time of given transaction tag indexes
|
||||
func (s *TransactionTagService) ModifyTagIndexTransactionTime(c *core.Context, uid int64, tagIndexs []*models.TransactionTagIndex) error {
|
||||
if uid <= 0 {
|
||||
return errs.ErrUserIdInvalid
|
||||
}
|
||||
|
||||
for i := 0; i < len(tagIndexs); i++ {
|
||||
tagIndexs[i].UpdatedUnixTime = time.Now().Unix()
|
||||
}
|
||||
|
||||
return s.UserDataDB(uid).DoTransaction(c, func(sess *xorm.Session) error {
|
||||
for i := 0; i < len(tagIndexs); i++ {
|
||||
tagIndex := tagIndexs[i]
|
||||
updatedRows, err := sess.ID(tagIndex.TagIndexId).Cols("transaction_time", "updated_unix_time").Where("uid=? AND deleted=?", uid, false).Update(tagIndex)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
} else if updatedRows < 1 {
|
||||
return errs.ErrTransactionTagIndexNotFound
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// GetTagMapByList returns a transaction tag map by a list
|
||||
func (s *TransactionTagService) GetTagMapByList(tags []*models.TransactionTag) map[int64]*models.TransactionTag {
|
||||
tagMap := make(map[int64]*models.TransactionTag)
|
||||
|
||||
Reference in New Issue
Block a user