Browse Source

修改Bug

yjp 3 months ago
parent
commit
90779a1ccc

+ 9 - 26
framework/core/infrastructure/database/database.go

@@ -292,43 +292,26 @@ func DeleteEntity(executor Executor, tableName string, e any) error {
 // 返回值:
 // - 错误
 func UpdateEntity(executor Executor, tableName string, e any) error {
-	_, err := UpdateEntityWithRowsAffected(executor, tableName, e)
-	if err != nil {
-		return err
-	}
-
-	return nil
-}
-
-// UpdateEntityWithRowsAffected 通过结构更新数据,返回影响行数
-// 参数:
-// - executor: 数据库基础设施接口
-// - tableName: 表名
-// - e: 结构,结构字段需要使用sqlmapping标注
-// 返回值:
-// - 影响行数
-// - 错误
-func UpdateEntityWithRowsAffected(executor Executor, tableName string, e any) (int64, error) {
 	if executor == nil {
-		return 0, errors.New("没有传递执行器")
+		return errors.New("没有传递执行器")
 	}
 
 	if strutils.IsStringEmpty(tableName) {
-		return 0, errors.New("没有传递表名")
+		return errors.New("没有传递表名")
 	}
 
 	if e == nil {
-		return 0, nil
+		return nil
 	}
 
 	entityType := reflect.TypeOf(e)
 	if !reflectutils.IsTypeStructOrStructPointer(entityType) {
-		return 0, errors.New("实体参数不是结构或结构指针")
+		return errors.New("实体参数不是结构或结构指针")
 	}
 
 	fields, err := sql_mapping.DefaultUsage(e)
 	if err != nil {
-		return 0, err
+		return err
 	}
 
 	now := time.Now().Local()
@@ -375,19 +358,19 @@ func UpdateEntityWithRowsAffected(executor Executor, tableName string, e any) (i
 
 	executeParamsMap, err := executeParams.Map()
 	if err != nil {
-		return 0, err
+		return err
 	}
 
 	args := make([]any, 0)
 	args = append(args, executeParams.TableRow.Values()...)
 	args = append(args, executeParams.Conditions.Args()...)
 
-	_, rowsAffected, err := executor.ExecuteRawSqlTemplateWithRowsAffected(sql.UpdateTpl, executeParamsMap, args...)
+	_, _, err = executor.ExecuteRawSqlTemplateWithRowsAffected(sql.UpdateTpl, executeParamsMap, args...)
 	if err != nil {
-		return 0, err
+		return err
 	}
 
-	return rowsAffected, nil
+	return nil
 }
 
 // Insert 插入数据

+ 14 - 4
framework/core/infrastructure/database/operations/operations.go

@@ -7,6 +7,7 @@ import (
 	"git.sxidc.com/go-tools/utils/template"
 	"github.com/pkg/errors"
 	"gorm.io/gorm"
+	"strings"
 	"time"
 )
 
@@ -177,10 +178,19 @@ func (op *Operations) ExecuteRawSqlTemplateWithRowsAffected(sqlStr string, execu
 		return nil, 0, errors.New(err.Error())
 	}
 
+	if strings.HasPrefix(strings.TrimSpace(parsedSql), "UPDATE") {
+		tx := op.db.Exec(parsedSql, args...)
+		if tx.Error != nil {
+			return nil, 0, errors.New(tx.Error.Error())
+		}
+
+		return make([]sql.Result, 0), tx.RowsAffected, nil
+	}
+
 	tableRows := make([]map[string]any, 0)
-	tx := op.db.Raw(parsedSql, args...).Scan(&tableRows)
-	if tx.Error != nil {
-		return nil, 0, errors.New(tx.Error.Error())
+	err = op.db.Raw(parsedSql, args...).Scan(&tableRows).Error
+	if err != nil {
+		return nil, 0, errors.New(err.Error())
 	}
 
 	results := make([]sql.Result, len(tableRows))
@@ -188,5 +198,5 @@ func (op *Operations) ExecuteRawSqlTemplateWithRowsAffected(sqlStr string, execu
 		results[i] = row
 	}
 
-	return results, tx.RowsAffected, nil
+	return results, 0, nil
 }