yjp пре 1 година
родитељ
комит
76863b7ab6

+ 1 - 1
go.mod

@@ -3,7 +3,7 @@ module git.sxidc.com/go-framework/baize
 go 1.22.3
 
 require (
-	git.sxidc.com/go-tools/utils v1.5.9
+	git.sxidc.com/go-tools/utils v1.5.10
 	git.sxidc.com/service-supports/fserr v0.3.5
 	git.sxidc.com/service-supports/websocket v1.3.1
 	github.com/gin-gonic/gin v1.10.0

+ 2 - 2
go.sum

@@ -1,5 +1,5 @@
-git.sxidc.com/go-tools/utils v1.5.9 h1:8xo0ChP79KoM+wlOl09vaV+pTAtVv4uy7YInKI3V5b0=
-git.sxidc.com/go-tools/utils v1.5.9/go.mod h1:fkobAXFpOMTvkZ82TQXWcpsayePcyk/MS5TN6GTlRDg=
+git.sxidc.com/go-tools/utils v1.5.10 h1:mZmitAT0hQwfv6ZOFTHJ2Q1nfWY+GeF3TrV74WAxbrI=
+git.sxidc.com/go-tools/utils v1.5.10/go.mod h1:fkobAXFpOMTvkZ82TQXWcpsayePcyk/MS5TN6GTlRDg=
 git.sxidc.com/service-supports/fserr v0.3.5 h1:1SDC60r3FIDd2iRq/oHRLK4OMa1gf67h9B7kierKTUE=
 git.sxidc.com/service-supports/fserr v0.3.5/go.mod h1:8U+W/ulZIGVPFojV6cE18shkGXqvaICuzaxIJpOcBqI=
 git.sxidc.com/service-supports/fslog v0.5.9 h1:q2XIK2o/fk/qmByy4x5kKLC+k7kolT5LrXHcWRSffXQ=

+ 3 - 0
infrastructure/database/data_service/data_service.go

@@ -0,0 +1,3 @@
+package data_service
+
+type DataService struct{}

+ 500 - 0
infrastructure/database/database.go

@@ -0,0 +1,500 @@
+package database
+
+import (
+	"git.sxidc.com/go-framework/baize/infrastructure/database/sql"
+	"git.sxidc.com/go-framework/baize/tag/sql/sql_mapping"
+	"git.sxidc.com/go-tools/utils/reflectutils"
+	"git.sxidc.com/go-tools/utils/strutils"
+	"git.sxidc.com/service-supports/fserr"
+	"reflect"
+	"strings"
+	"time"
+)
+
+type Executor interface {
+	ExecuteRawSql(sql string, executeParams map[string]any) ([]sql.Result, error)
+	ExecuteSql(name string, executeParams map[string]any) ([]sql.Result, error)
+}
+
+const (
+	createdTimeFieldName     = "CreatedTime"
+	lastUpdatedTimeFieldName = "LastUpdatedTime"
+)
+
+func InsertEntity(executor Executor, tableName string, e any) error {
+	if executor == nil {
+		return fserr.New("没有传递执行器")
+	}
+
+	if strutils.IsStringEmpty(tableName) {
+		return fserr.New("没有传递表名")
+	}
+
+	if e == nil {
+		return nil
+	}
+
+	entityType := reflect.TypeOf(e)
+	if !reflectutils.IsTypeStructOrStructPointer(entityType) {
+		return fserr.New("实体参数不是结构或结构指针")
+	}
+
+	fields, err := sql_mapping.DefaultUsage(e)
+	if err != nil {
+		return err
+	}
+
+	executeParamsMap, err := sql.InsertExecuteParams{
+		TableName: tableName,
+		TableRow:  formInsertTableRow(fields),
+	}.Map()
+	if err != nil {
+		return err
+	}
+
+	_, err = executor.ExecuteRawSql(sql.InsertTpl, executeParamsMap)
+	if err != nil {
+		if strings.Contains(err.Error(), "SQLSTATE 23505") {
+			return ErrDBRecordHasExist
+		}
+
+		return err
+	}
+
+	return nil
+}
+
+func InsertEntityBatch(executor Executor, tableName string, es []any) error {
+	if executor == nil {
+		return fserr.New("没有传递执行器")
+	}
+
+	if strutils.IsStringEmpty(tableName) {
+		return fserr.New("没有传递表名")
+	}
+
+	tableRowBatch := make([]sql.TableRow, 0)
+
+	for _, e := range es {
+		if e == nil {
+			return nil
+		}
+
+		entityType := reflect.TypeOf(e)
+		if !reflectutils.IsTypeStructOrStructPointer(entityType) {
+			return fserr.New("实体参数不是结构或结构指针")
+		}
+
+		fields, err := sql_mapping.DefaultUsage(e)
+		if err != nil {
+			return err
+		}
+
+		tableRowBatch = append(tableRowBatch, *formInsertTableRow(fields))
+	}
+
+	executeParamsMap, err := sql.InsertBatchExecuteParams{
+		TableName:     tableName,
+		TableRowBatch: tableRowBatch,
+	}.Map()
+	if err != nil {
+		return err
+	}
+
+	_, err = executor.ExecuteRawSql(sql.InsertTpl, executeParamsMap)
+	if err != nil {
+		if strings.Contains(err.Error(), "SQLSTATE 23505") {
+			return ErrDBRecordHasExist
+		}
+
+		return err
+	}
+
+	return nil
+}
+
+func formInsertTableRow(fields []sql_mapping.Field) *sql.TableRow {
+	now := time.Now().Local()
+	tableRow := sql.NewTableRow()
+
+	for _, field := range fields {
+		fieldValue := reflect.ValueOf(field.Value)
+		if (field.FieldName == createdTimeFieldName || field.FieldName == lastUpdatedTimeFieldName) &&
+			reflectutils.IsValueTime(fieldValue) && fieldValue.IsZero() {
+			field.Value = now
+		}
+
+		tableRow.Add(field.ColumnName, field.Value)
+	}
+
+	return tableRow
+}
+
+func DeleteEntity(executor Executor, tableName string, e any) error {
+	if executor == nil {
+		return fserr.New("没有传递执行器")
+	}
+
+	if strutils.IsStringEmpty(tableName) {
+		return fserr.New("没有传递表名")
+	}
+
+	if e == nil {
+		return nil
+	}
+
+	entityType := reflect.TypeOf(e)
+	if !reflectutils.IsTypeStructOrStructPointer(entityType) {
+		return fserr.New("实体参数不是结构或结构指针")
+	}
+
+	fields, err := sql_mapping.DefaultUsage(e)
+	if err != nil {
+		return err
+	}
+
+	conditions := sql.NewConditions()
+	for _, field := range fields {
+		// 不是键,字段跳过
+		if !field.IsKey {
+			continue
+		}
+
+		conditions.Equal(field.ColumnName, field.Value)
+	}
+
+	executeParamsMap, err := sql.DeleteExecuteParams{
+		TableName:  tableName,
+		Conditions: conditions,
+	}.Map()
+	if err != nil {
+		return err
+	}
+
+	_, err = executor.ExecuteRawSql(sql.DeleteTpl, executeParamsMap)
+	if err != nil {
+		return err
+	}
+
+	return nil
+}
+
+func UpdateEntity(executor Executor, tableName string, e any) error {
+	if executor == nil {
+		return fserr.New("没有传递执行器")
+	}
+
+	if strutils.IsStringEmpty(tableName) {
+		return fserr.New("没有传递表名")
+	}
+
+	if e == nil {
+		return nil
+	}
+
+	entityType := reflect.TypeOf(e)
+	if !reflectutils.IsTypeStructOrStructPointer(entityType) {
+		return fserr.New("实体参数不是结构或结构指针")
+	}
+
+	fields, err := sql_mapping.DefaultUsage(e)
+	if err != nil {
+		return err
+	}
+
+	now := time.Now().Local()
+	tableRow := sql.NewTableRow()
+	conditions := sql.NewConditions()
+
+	for _, field := range fields {
+		// 不是键字段
+		// 不是更新时间字段
+		// 不更新的字段或者字段为零值且不能清空,跳过
+		if field.FieldName != lastUpdatedTimeFieldName &&
+			(!field.CanUpdate || (reflect.ValueOf(field.Value).IsZero() && !field.CanUpdateClear)) {
+			continue
+		}
+
+		fieldValue := reflect.ValueOf(field.Value)
+		if field.FieldName == lastUpdatedTimeFieldName &&
+			reflectutils.IsValueTime(fieldValue) && fieldValue.IsZero() {
+			field.Value = now
+		}
+
+		if field.IsKey {
+			conditions.Equal(field.ColumnName, field.Value)
+		} else {
+			tableRow.Add(field.ColumnName, field.Value)
+		}
+	}
+
+	executeParamsMap, err := sql.UpdateExecuteParams{
+		TableName:  tableName,
+		TableRow:   tableRow,
+		Conditions: conditions,
+	}.Map()
+	if err != nil {
+		return err
+	}
+
+	_, err = executor.ExecuteRawSql(sql.UpdateTpl, executeParamsMap)
+	if err != nil {
+		return err
+	}
+
+	return nil
+}
+
+func Insert(executor Executor, executeParams *sql.InsertExecuteParams) error {
+	if executor == nil {
+		return fserr.New("没有传递执行器")
+	}
+
+	if executeParams == nil {
+		return fserr.New("没有传递执行参数")
+	}
+
+	executeParamsMap, err := executeParams.Map()
+	if err != nil {
+		return err
+	}
+
+	_, err = executor.ExecuteRawSql(sql.InsertTpl, executeParamsMap)
+	if err != nil {
+		return err
+	}
+
+	return nil
+}
+
+func InsertBatch(executor Executor, executeParams *sql.InsertBatchExecuteParams) error {
+	if executor == nil {
+		return fserr.New("没有传递执行器")
+	}
+
+	if executeParams == nil {
+		return fserr.New("没有传递执行参数")
+	}
+
+	executeParamsMap, err := executeParams.Map()
+	if err != nil {
+		return err
+	}
+
+	_, err = executor.ExecuteRawSql(sql.InsertTpl, executeParamsMap)
+	if err != nil {
+		return err
+	}
+
+	return nil
+}
+
+func Delete(executor Executor, executeParams *sql.DeleteExecuteParams) error {
+	if executor == nil {
+		return fserr.New("没有传递执行器")
+	}
+
+	if executeParams == nil {
+		return fserr.New("没有传递执行参数")
+	}
+
+	executeParamsMap, err := executeParams.Map()
+	if err != nil {
+		return err
+	}
+
+	_, err = executor.ExecuteRawSql(sql.DeleteTpl, executeParamsMap)
+	if err != nil {
+		return err
+	}
+
+	return nil
+}
+
+func Update(executor Executor, executeParams *sql.UpdateExecuteParams) error {
+	if executor == nil {
+		return fserr.New("没有传递执行器")
+	}
+
+	if executeParams == nil {
+		return fserr.New("没有传递执行参数")
+	}
+
+	executeParamsMap, err := executeParams.Map()
+	if err != nil {
+		return err
+	}
+
+	_, err = executor.ExecuteRawSql(sql.UpdateTpl, executeParamsMap)
+	if err != nil {
+		return err
+	}
+
+	return nil
+}
+
+func Query(executor Executor, executeParams *sql.QueryExecuteParams) ([]sql.Result, int64, error) {
+	if executor == nil {
+		return nil, 0, fserr.New("没有传递执行器")
+	}
+
+	if executeParams == nil {
+		return nil, 0, fserr.New("没有传递执行参数")
+	}
+
+	queryExecuteParamsMap, err := executeParams.Map()
+	if err != nil {
+		return nil, 0, err
+	}
+
+	countExecuteParamsMap, err := sql.CountExecuteParams{
+		TableName:  executeParams.TableName,
+		Conditions: executeParams.Conditions,
+	}.Map()
+	if err != nil {
+		return nil, 0, err
+	}
+
+	tableRows, err := executor.ExecuteRawSql(sql.QueryTpl, queryExecuteParamsMap)
+	if err != nil {
+		return nil, 0, err
+	}
+
+	countTableRow, err := executor.ExecuteRawSql(sql.CountTpl, countExecuteParamsMap)
+	if err != nil {
+		return nil, 0, err
+	}
+
+	results := make([]sql.Result, len(tableRows))
+	for i, row := range tableRows {
+		results[i] = row
+	}
+
+	return results, int64(countTableRow[0]["count"].(float64)), nil
+}
+
+func QueryOne(executor Executor, executeParams *sql.QueryOneExecuteParams) (sql.Result, error) {
+	if executor == nil {
+		return nil, fserr.New("没有传递执行器")
+	}
+
+	if executeParams == nil {
+		return nil, fserr.New("没有传递执行参数")
+	}
+
+	executeParamsMap, err := executeParams.Map()
+	if err != nil {
+		return nil, err
+	}
+
+	tableRows, err := executor.ExecuteRawSql(sql.QueryTpl, executeParamsMap)
+	if err != nil {
+		return nil, err
+	}
+
+	if tableRows == nil || len(tableRows) == 0 {
+		return nil, ErrDBRecordNotExist
+	}
+
+	return tableRows[0], nil
+}
+
+func Count(executor Executor, executeParams *sql.CountExecuteParams) (int64, error) {
+	if executor == nil {
+		return 0, fserr.New("没有传递执行器")
+	}
+
+	if executeParams == nil {
+		return 0, fserr.New("没有传递执行参数")
+	}
+
+	executeParamsMap, err := executeParams.Map()
+	if err != nil {
+		return 0, err
+	}
+
+	tableRows, err := executor.ExecuteRawSql(sql.CountTpl, executeParamsMap)
+	if err != nil {
+		return 0, err
+	}
+
+	return int64(tableRows[0]["count"].(float64)), nil
+}
+
+func CheckExist(executor Executor, executeParams *sql.CheckExistExecuteParams) (bool, error) {
+	if executor == nil {
+		return false, fserr.New("没有传递执行器")
+	}
+
+	if executeParams == nil {
+		return false, fserr.New("没有传递执行参数")
+	}
+
+	executeParamsMap, err := executeParams.Map()
+	if err != nil {
+		return false, err
+	}
+
+	tableRows, err := executor.ExecuteRawSql(sql.CountTpl, executeParamsMap)
+	if err != nil {
+		return false, err
+	}
+
+	return int64(tableRows[0]["count"].(float64)) > 0, nil
+}
+
+func CheckHasOnlyOne(executor Executor, executeParams *sql.CheckHasOnlyOneExecuteParams) (bool, error) {
+	if executor == nil {
+		return false, fserr.New("没有传递执行器")
+	}
+
+	if executeParams == nil {
+		return false, fserr.New("没有传递执行参数")
+	}
+
+	executeParamsMap, err := executeParams.Map()
+	if err != nil {
+		return false, err
+	}
+
+	tableRows, err := executor.ExecuteRawSql(sql.CountTpl, executeParamsMap)
+	if err != nil {
+		return false, err
+	}
+
+	return int64(tableRows[0]["count"].(float64)) == 1, nil
+}
+
+func ExecuteRawSql(executor Executor, sql string, executeParams map[string]any) ([]sql.Result, error) {
+	if executor == nil {
+		return nil, fserr.New("没有传递执行器")
+	}
+
+	if strutils.IsStringEmpty(sql) {
+		return nil, fserr.New("没有sql")
+	}
+
+	tableRows, err := executor.ExecuteRawSql(sql, executeParams)
+	if err != nil {
+		return nil, err
+	}
+
+	return tableRows, nil
+}
+
+func ExecuteSql(executor Executor, name string, executeParams map[string]any) ([]sql.Result, error) {
+	if executor == nil {
+		return nil, fserr.New("没有传递执行器")
+	}
+
+	if strutils.IsStringEmpty(name) {
+		return nil, fserr.New("没有sql资源名称")
+	}
+
+	tableRows, err := executor.ExecuteSql(name, executeParams)
+	if err != nil {
+		return nil, err
+	}
+
+	return tableRows, nil
+}

+ 3 - 0
infrastructure/database/db_operations/db_operations.go

@@ -0,0 +1,3 @@
+package db_operations
+
+type DBOperations struct{}

+ 19 - 0
infrastructure/database/error.go

@@ -0,0 +1,19 @@
+package database
+
+import (
+	"errors"
+	"strings"
+)
+
+var (
+	ErrDBRecordHasExist = errors.New("记录已存在")
+	ErrDBRecordNotExist = errors.New("记录不存在")
+)
+
+func IsErrorDBRecordHasExist(err error) bool {
+	return strings.Contains(err.Error(), "记录已存在")
+}
+
+func IsErrorDBRecordNotExist(err error) bool {
+	return strings.Contains(err.Error(), "记录不存在")
+}

+ 19 - 19
infrastructure/database/sql_tpl/condition.go → infrastructure/database/sql/condition.go

@@ -1,4 +1,4 @@
-package sql_tpl
+package sql
 
 type Conditions struct {
 	Conditions []string
@@ -16,12 +16,12 @@ func (conditions *Conditions) AddCondition(condition string) *Conditions {
 	return conditions
 }
 
-func (conditions *Conditions) Equal(columnName string, value any, opts ...AfterParsedStrValueOption) *Conditions {
+func (conditions *Conditions) Equal(columnName string, value any) *Conditions {
 	if conditions.err != nil {
 		return conditions
 	}
 
-	parsedValue, err := parseValue(value, opts...)
+	parsedValue, err := toSqlValue(value)
 	if err != nil {
 		conditions.err = err
 		return conditions
@@ -32,12 +32,12 @@ func (conditions *Conditions) Equal(columnName string, value any, opts ...AfterP
 	return conditions
 }
 
-func (conditions *Conditions) Like(columnName string, value string, opts ...AfterParsedStrValueOption) *Conditions {
+func (conditions *Conditions) Like(columnName string, value string) *Conditions {
 	if conditions.err != nil {
 		return conditions
 	}
 
-	parsedValue, err := parseValue(value, opts...)
+	parsedValue, err := toSqlValue(value)
 	if err != nil {
 		conditions.err = err
 		return conditions
@@ -48,12 +48,12 @@ func (conditions *Conditions) Like(columnName string, value string, opts ...Afte
 	return conditions
 }
 
-func (conditions *Conditions) In(columnName string, value any, opts ...AfterParsedStrValueOption) *Conditions {
+func (conditions *Conditions) In(columnName string, value any) *Conditions {
 	if conditions.err != nil {
 		return conditions
 	}
 
-	parsedValue, err := parseSliceValues(value, opts...)
+	parsedValue, err := toSqlValues(value)
 	if err != nil {
 		conditions.err = err
 		return conditions
@@ -64,12 +64,12 @@ func (conditions *Conditions) In(columnName string, value any, opts ...AfterPars
 	return conditions
 }
 
-func (conditions *Conditions) NotIn(columnName string, value any, opts ...AfterParsedStrValueOption) *Conditions {
+func (conditions *Conditions) NotIn(columnName string, value any) *Conditions {
 	if conditions.err != nil {
 		return conditions
 	}
 
-	parsedValue, err := parseSliceValues(value, opts...)
+	parsedValue, err := toSqlValues(value)
 	if err != nil {
 		conditions.err = err
 		return conditions
@@ -80,12 +80,12 @@ func (conditions *Conditions) NotIn(columnName string, value any, opts ...AfterP
 	return conditions
 }
 
-func (conditions *Conditions) Not(columnName string, value any, opts ...AfterParsedStrValueOption) *Conditions {
+func (conditions *Conditions) Not(columnName string, value any) *Conditions {
 	if conditions.err != nil {
 		return conditions
 	}
 
-	parsedValue, err := parseValue(value, opts...)
+	parsedValue, err := toSqlValue(value)
 	if err != nil {
 		conditions.err = err
 		return conditions
@@ -96,12 +96,12 @@ func (conditions *Conditions) Not(columnName string, value any, opts ...AfterPar
 	return conditions
 }
 
-func (conditions *Conditions) LessThan(columnName string, value any, opts ...AfterParsedStrValueOption) *Conditions {
+func (conditions *Conditions) LessThan(columnName string, value any) *Conditions {
 	if conditions.err != nil {
 		return conditions
 	}
 
-	parsedValue, err := parseValue(value, opts...)
+	parsedValue, err := toSqlValue(value)
 	if err != nil {
 		conditions.err = err
 		return conditions
@@ -112,12 +112,12 @@ func (conditions *Conditions) LessThan(columnName string, value any, opts ...Aft
 	return conditions
 }
 
-func (conditions *Conditions) LessThanAndEqual(columnName string, value any, opts ...AfterParsedStrValueOption) *Conditions {
+func (conditions *Conditions) LessThanAndEqual(columnName string, value any) *Conditions {
 	if conditions.err != nil {
 		return conditions
 	}
 
-	parsedValue, err := parseValue(value, opts...)
+	parsedValue, err := toSqlValue(value)
 	if err != nil {
 		conditions.err = err
 		return conditions
@@ -128,12 +128,12 @@ func (conditions *Conditions) LessThanAndEqual(columnName string, value any, opt
 	return conditions
 }
 
-func (conditions *Conditions) GreaterThan(columnName string, value any, opts ...AfterParsedStrValueOption) *Conditions {
+func (conditions *Conditions) GreaterThan(columnName string, value any) *Conditions {
 	if conditions.err != nil {
 		return conditions
 	}
 
-	parsedValue, err := parseValue(value, opts...)
+	parsedValue, err := toSqlValue(value)
 	if err != nil {
 		conditions.err = err
 		return conditions
@@ -144,12 +144,12 @@ func (conditions *Conditions) GreaterThan(columnName string, value any, opts ...
 	return conditions
 }
 
-func (conditions *Conditions) GreaterThanAndEqual(columnName string, value any, opts ...AfterParsedStrValueOption) *Conditions {
+func (conditions *Conditions) GreaterThanAndEqual(columnName string, value any) *Conditions {
 	if conditions.err != nil {
 		return conditions
 	}
 
-	parsedValue, err := parseValue(value, opts...)
+	parsedValue, err := toSqlValue(value)
 	if err != nil {
 		conditions.err = err
 		return conditions

+ 180 - 0
infrastructure/database/sql/result.go

@@ -0,0 +1,180 @@
+package sql
+
+import (
+	"fmt"
+	"git.sxidc.com/go-tools/utils/reflectutils"
+	"strings"
+	"time"
+)
+
+const (
+	sqlResultTimeMicroFormat = "2006-01-02T15:04:05.000000+08:00"
+	sqlResultTimeMilliFormat = "2006-01-02T15:04:05.000+08:00"
+	sqlResultTimeSecFormat   = "2006-01-02T15:04:05+08:00"
+)
+
+type Result map[string]any
+
+func (result Result) ColumnValueStringAsTime(columnName string) time.Time {
+	value, ok := result[columnName].(string)
+	if !ok {
+		return time.Time{}
+	}
+
+	var layout string
+
+	if strings.HasSuffix(value, ".000000+08:00") {
+		layout = sqlResultTimeMicroFormat
+	} else if strings.HasSuffix(value, ".000+08:00") {
+		layout = sqlResultTimeMilliFormat
+	} else {
+		layout = sqlResultTimeSecFormat
+	}
+
+	t, err := time.ParseInLocation(layout, value, time.Local)
+	if err != nil {
+		return time.Time{}
+	}
+
+	return t
+}
+
+func (result Result) ColumnValueBool(columnName string) bool {
+	value, err := reflectutils.ToBool(result[columnName])
+	if err != nil {
+		fmt.Println(err)
+		return false
+	}
+
+	return value
+}
+
+func (result Result) ColumnValueString(columnName string) string {
+	value, err := reflectutils.ToString(result[columnName])
+	if err != nil {
+		fmt.Println(err)
+		return ""
+	}
+
+	return value
+}
+
+func (result Result) ColumnValueInt(columnName string) int {
+	value, err := reflectutils.ToInt64(result[columnName])
+	if err != nil {
+		fmt.Println(err)
+		return 0
+	}
+
+	return int(value)
+}
+
+func (result Result) ColumnValueInt8(columnName string) int8 {
+	value, err := reflectutils.ToInt64(result[columnName])
+	if err != nil {
+		fmt.Println(err)
+		return 0
+	}
+
+	return int8(value)
+}
+
+func (result Result) ColumnValueInt16(columnName string) int16 {
+	value, err := reflectutils.ToInt64(result[columnName])
+	if err != nil {
+		fmt.Println(err)
+		return 0
+	}
+
+	return int16(value)
+}
+
+func (result Result) ColumnValueInt32(columnName string) int32 {
+	value, err := reflectutils.ToInt64(result[columnName])
+	if err != nil {
+		fmt.Println(err)
+		return 0
+	}
+
+	return int32(value)
+}
+
+func (result Result) ColumnValueInt64(columnName string) int64 {
+	value, err := reflectutils.ToInt64(result[columnName])
+	if err != nil {
+		fmt.Println(err)
+		return 0
+	}
+
+	return value
+}
+
+func (result Result) ColumnValueUint(columnName string) uint {
+	value, err := reflectutils.ToUint64(result[columnName])
+	if err != nil {
+		fmt.Println(err)
+		return 0
+	}
+
+	return uint(value)
+}
+
+func (result Result) ColumnValueUint8(columnName string) uint8 {
+	value, err := reflectutils.ToUint64(result[columnName])
+	if err != nil {
+		fmt.Println(err)
+		return 0
+	}
+
+	return uint8(value)
+}
+
+func (result Result) ColumnValueUint16(columnName string) uint16 {
+	value, err := reflectutils.ToUint64(result[columnName])
+	if err != nil {
+		fmt.Println(err)
+		return 0
+	}
+
+	return uint16(value)
+}
+
+func (result Result) ColumnValueUint32(columnName string) uint32 {
+	value, err := reflectutils.ToUint64(result[columnName])
+	if err != nil {
+		fmt.Println(err)
+		return 0
+	}
+
+	return uint32(value)
+}
+
+func (result Result) ColumnValueUint64(columnName string) uint64 {
+	value, err := reflectutils.ToUint64(result[columnName])
+	if err != nil {
+		fmt.Println(err)
+		return 0
+	}
+
+	return value
+}
+
+func (result Result) ColumnValueFloat32(columnName string) float32 {
+	value, err := reflectutils.ToFloat64(result[columnName])
+	if err != nil {
+		fmt.Println(err)
+		return 0
+	}
+
+	return float32(value)
+}
+
+func (result Result) ColumnValueFloat64(columnName string) float64 {
+	value, err := reflectutils.ToFloat64(result[columnName])
+	if err != nil {
+		fmt.Println(err)
+		return 0
+	}
+
+	return value
+}

+ 3 - 3
infrastructure/database/sql_tpl/table_row.go → infrastructure/database/sql/table_row.go

@@ -1,4 +1,4 @@
-package sql_tpl
+package sql
 
 type TableRow struct {
 	columnValues []columnValue
@@ -16,12 +16,12 @@ func NewTableRow() *TableRow {
 	}
 }
 
-func (tableRow *TableRow) Add(column string, value any, opts ...AfterParsedStrValueOption) *TableRow {
+func (tableRow *TableRow) Add(column string, value any) *TableRow {
 	if tableRow.err != nil {
 		return tableRow
 	}
 
-	parsedValue, err := parseValue(value, opts...)
+	parsedValue, err := toSqlValue(value)
 	if err != nil {
 		tableRow.err = err
 		return tableRow

+ 1 - 1
infrastructure/database/sql_tpl/sql_tpl.go → infrastructure/database/sql/template.go

@@ -1,4 +1,4 @@
-package sql_tpl
+package sql
 
 import (
 	"git.sxidc.com/service-supports/fserr"

+ 66 - 0
infrastructure/database/sql/value.go

@@ -0,0 +1,66 @@
+package sql
+
+import (
+	"git.sxidc.com/go-tools/utils/reflectutils"
+	"git.sxidc.com/service-supports/fserr"
+	"reflect"
+	"strings"
+)
+
+func toSqlValue(value any) (string, error) {
+	if value == nil {
+		return "", nil
+	}
+
+	valueValue := reflect.ValueOf(value)
+
+	if valueValue.Kind() == reflect.Slice {
+		return "", fserr.New("请使用toSqlValues处理slice类型")
+	}
+
+	switch v := reflectutils.PointerValueElem(valueValue).Interface().(type) {
+	case string:
+		return "'" + v + "'", nil
+	default:
+		var retStr string
+		err := reflectutils.AssignStringValue(v, reflect.ValueOf(retStr))
+		if err != nil {
+			return "", err
+		}
+
+		return retStr, nil
+	}
+}
+
+func toSqlValues(values any) (string, error) {
+	if values == nil {
+		return "()", nil
+	}
+
+	sliceValue := reflect.ValueOf(values)
+
+	if sliceValue.IsZero() {
+		return "()", nil
+	}
+
+	if sliceValue.Kind() != reflect.Slice {
+		return "", fserr.New("传递的不是slice")
+	}
+
+	parsedValues := make([]string, 0)
+	for i := 0; i < sliceValue.Len(); i++ {
+		value := sliceValue.Index(i).Interface()
+		parsedValue, err := toSqlValue(value)
+		if err != nil {
+			return "", err
+		}
+
+		parsedValues = append(parsedValues, parsedValue)
+	}
+
+	if len(parsedValues) == 0 {
+		return "()", nil
+	}
+
+	return "(" + strings.Join(parsedValues, ",") + ")", nil
+}

+ 0 - 190
infrastructure/database/sql_tpl/value.go

@@ -1,190 +0,0 @@
-package sql_tpl
-
-import (
-	"git.sxidc.com/go-tools/utils/encoding"
-	"git.sxidc.com/go-tools/utils/strutils"
-	"git.sxidc.com/service-supports/fserr"
-	"reflect"
-	"strconv"
-	"strings"
-	"time"
-)
-
-const (
-	timeWriteFormat = time.DateTime + ".000000 +08:00"
-)
-
-type AfterParsedStrValueOption func(strValue string) (string, error)
-
-func WithAESKey(aesKey string) AfterParsedStrValueOption {
-	return func(strValue string) (string, error) {
-		if strutils.IsStringEmpty(strValue) {
-			return "''", nil
-		}
-
-		encrypted, err := encoding.AESEncrypt(strValue, aesKey)
-		if err != nil {
-			return "", err
-		}
-
-		return "'" + encrypted + "'", nil
-	}
-}
-
-func parseValue(value any, opts ...AfterParsedStrValueOption) (string, error) {
-	valueValue := reflect.ValueOf(value)
-
-	if !valueValue.IsValid() {
-		return "", fserr.New("无效值")
-	}
-
-	if valueValue.Kind() == reflect.Pointer && valueValue.IsNil() {
-		return "", fserr.New("空值")
-	}
-
-	if valueValue.Kind() == reflect.Pointer {
-		valueValue = valueValue.Elem()
-	}
-
-	var parsedValue string
-
-	switch v := valueValue.Interface().(type) {
-	case string:
-		parsedValue = v
-
-		if opts == nil || len(opts) == 0 {
-			return "'" + parsedValue + "'", nil
-		}
-	case bool:
-		parsedValue = strconv.FormatBool(v)
-
-		if opts == nil || len(opts) == 0 {
-			return parsedValue, nil
-		}
-	case time.Time:
-		parsedValue = v.Format(timeWriteFormat)
-
-		if opts == nil || len(opts) == 0 {
-			return "'" + parsedValue + "'", nil
-		}
-	case int:
-		parsedValue = strconv.Itoa(v)
-
-		if opts == nil || len(opts) == 0 {
-			return parsedValue, nil
-		}
-	case int8:
-		parsedValue = strconv.FormatInt(int64(v), 10)
-
-		if opts == nil || len(opts) == 0 {
-			return parsedValue, nil
-		}
-	case int16:
-		parsedValue = strconv.FormatInt(int64(v), 10)
-
-		if opts == nil || len(opts) == 0 {
-			return parsedValue, nil
-		}
-	case int32:
-		parsedValue = strconv.FormatInt(int64(v), 10)
-
-		if opts == nil || len(opts) == 0 {
-			return parsedValue, nil
-		}
-	case int64:
-		parsedValue = strconv.FormatInt(v, 10)
-
-		if opts == nil || len(opts) == 0 {
-			return parsedValue, nil
-		}
-	case uint:
-		parsedValue = strconv.FormatUint(uint64(v), 10)
-
-		if opts == nil || len(opts) == 0 {
-			return parsedValue, nil
-		}
-	case uint8:
-		parsedValue = strconv.FormatUint(uint64(v), 10)
-
-		if opts == nil || len(opts) == 0 {
-			return parsedValue, nil
-		}
-	case uint16:
-		parsedValue = strconv.FormatUint(uint64(v), 10)
-
-		if opts == nil || len(opts) == 0 {
-			return parsedValue, nil
-		}
-	case uint32:
-		parsedValue = strconv.FormatUint(uint64(v), 10)
-
-		if opts == nil || len(opts) == 0 {
-			return parsedValue, nil
-		}
-	case uint64:
-		parsedValue = strconv.FormatUint(v, 10)
-
-		if opts == nil || len(opts) == 0 {
-			return parsedValue, nil
-		}
-	default:
-		return "", fserr.New("不支持的类型")
-	}
-
-	for _, opt := range opts {
-		innerParsedValue, err := opt(parsedValue)
-		if err != nil {
-			return "", err
-		}
-
-		parsedValue = innerParsedValue
-	}
-
-	return parsedValue, nil
-}
-
-func parseSliceValues(values any, opts ...AfterParsedStrValueOption) (string, error) {
-	sliceValue := reflect.ValueOf(values)
-
-	if !sliceValue.IsValid() {
-		return "", fserr.New("无效值")
-	}
-
-	if sliceValue.Kind() == reflect.Pointer && sliceValue.IsNil() {
-		return "()", nil
-	}
-
-	if sliceValue.Kind() == reflect.Pointer {
-		sliceValue = sliceValue.Elem()
-	}
-
-	if sliceValue.Kind() != reflect.Slice {
-		return "", fserr.New("传递的不是slice")
-	}
-
-	parsedValues := make([]string, 0)
-	for i := 0; i < sliceValue.Len(); i++ {
-		value := sliceValue.Index(i).Interface()
-		parsedValue, err := parseValue(value)
-		if err != nil {
-			return "", err
-		}
-
-		for _, opt := range opts {
-			innerParsedValue, err := opt(parsedValue)
-			if err != nil {
-				return "", err
-			}
-
-			parsedValue = innerParsedValue
-		}
-
-		parsedValues = append(parsedValues, parsedValue)
-	}
-
-	if len(parsedValues) == 0 {
-		return "()", nil
-	}
-
-	return "(" + strings.Join(parsedValues, ",") + ")", nil
-}

+ 19 - 0
infrastructure/infrastructure.go

@@ -0,0 +1,19 @@
+package infrastructure
+
+import (
+	"git.sxidc.com/go-framework/baize/infrastructure/database/data_service"
+	"git.sxidc.com/go-framework/baize/infrastructure/database/db_operations"
+)
+
+type Infrastructure struct {
+	dbOperations *db_operations.DBOperations
+	dataService  *data_service.DataService
+}
+
+func (i Infrastructure) GetDBOperations() *db_operations.DBOperations {
+	return i.dbOperations
+}
+
+func (i Infrastructure) GetDataService() *data_service.DataService {
+	return i.dataService
+}

+ 28 - 36
tag/sql/sql_mapping/usage.go

@@ -15,11 +15,12 @@ const (
 )
 
 type Field struct {
+	FieldName      string
 	ColumnName     string
 	IsKey          bool
 	CanUpdate      bool
 	CanUpdateClear bool
-	SqlValue       string
+	Value          any
 }
 
 func DefaultUsage(e any) ([]Field, error) {
@@ -35,20 +36,22 @@ func DefaultUsage(e any) ([]Field, error) {
 
 func defaultCallback(fields *[]Field) OnParsedFieldTagFunc {
 	return func(fieldName string, entityFieldElemValue reflect.Value, tag *Tag) error {
-		field := &Field{
+		field := Field{
+			FieldName:      fieldName,
 			ColumnName:     tag.Name,
 			IsKey:          tag.IsKey,
 			CanUpdate:      tag.CanUpdate,
 			CanUpdateClear: tag.CanUpdateClear,
-			SqlValue:       "",
 		}
 
 		if entityFieldElemValue.IsZero() {
+			reflectutils.Zero(&entityFieldElemValue)
+			field.Value = entityFieldElemValue.Interface()
+			*fields = append(*fields, field)
 			return nil
 		}
 
 		entityFieldKind := reflectutils.GroupValueKind(entityFieldElemValue)
-		var entityFieldValue any
 
 		switch entityFieldKind {
 		case reflect.Struct:
@@ -56,47 +59,36 @@ func defaultCallback(fields *[]Field) OnParsedFieldTagFunc {
 				return fserr.New(fieldName + " Error: 解析tag内部错误,除time.Time类型之外的结构被回调")
 			}
 
-			entityFieldValue = entityFieldElemValue.Interface().(time.Time).Format(timeWriteFormat)
+			strValue, err := dealStringValue(entityFieldElemValue.Interface().(time.Time).Format(timeWriteFormat), tag)
+			if err != nil {
+				return err
+			}
+
+			field.Value = strValue
 		case reflect.Slice:
 			if !reflectutils.IsSliceValueOf(entityFieldElemValue, reflect.String) {
 				return fserr.New(fieldName + " Error: slice仅支持[]string")
 			}
 
-			entityFieldValue = strings.Join(entityFieldElemValue.Interface().([]string), tag.JoinWith)
-		default:
-			entityFieldValue = entityFieldElemValue.Interface()
-		}
-
-		sqlValue, err := toSqlValue(entityFieldValue, tag)
-		if err != nil {
-			return err
-		}
-
-		field.SqlValue = sqlValue
-		*fields = append(*fields, *field)
-
-		return nil
-	}
-}
-
-func toSqlValue(value any, tag *Tag) (string, error) {
-	switch v := value.(type) {
-	case string:
-		retValue, err := dealStringValue(v, tag)
-		if err != nil {
-			return "", err
-		}
+			strValue, err := dealStringValue(strings.Join(entityFieldElemValue.Interface().([]string), tag.JoinWith), tag)
+			if err != nil {
+				return err
+			}
 
-		return "'" + retValue + "'", nil
-	default:
-		var retValue string
+			field.Value = strValue
+		case reflect.String:
+			strValue, err := dealStringValue(entityFieldElemValue.Interface().(string), tag)
+			if err != nil {
+				return err
+			}
 
-		err := reflectutils.AssignStringValue(v, reflect.ValueOf(retValue))
-		if err != nil {
-			return "", err
+			field.Value = strValue
+		default:
+			field.Value = entityFieldElemValue.Interface()
 		}
 
-		return retValue, nil
+		*fields = append(*fields, field)
+		return nil
 	}
 }
 

+ 0 - 1
tag/sql/sql_result/usage.go

@@ -113,7 +113,6 @@ func defaultCallback(result map[string]any) OnParsedFieldTagFunc {
 				fieldName, tag.Name, entityFieldElemValue.Type().String())
 		}
 	}
-
 }
 
 func parseTimeStringResult(timeStr string) (time.Time, error) {