package entity_crud import ( "git.sxidc.com/go-framework/baize/framework/binding" "git.sxidc.com/go-framework/baize/framework/core/api" "git.sxidc.com/go-framework/baize/framework/core/api/request" "git.sxidc.com/go-framework/baize/framework/core/api/response" "git.sxidc.com/go-framework/baize/framework/core/domain" "git.sxidc.com/go-framework/baize/framework/core/domain/entity" "git.sxidc.com/go-framework/baize/framework/core/infrastructure" "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database" "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/sql" "git.sxidc.com/go-framework/baize/framework/core/tag/sql/sql_mapping" "git.sxidc.com/go-tools/utils/reflectutils" "github.com/pkg/errors" "reflect" ) func Create(tableName string, needCreateUserID bool, callbacks *CreateCallbacks) binding.ServiceFunc[string] { return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (string, error) { object := objects[0] if object == nil { return "", errors.New("领域实体为空") } dbExecutor := i.DBExecutor() e, ok := objects[0].(entity.Entity) if !ok { return "", errors.New("需要传递领域对象应该为实体") } err := e.GenerateID() if err != nil { return callbackOnCreateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i) } if needCreateUserID { userInfo := c.GetUserInfo() err := domain.SetField(e, entity.FieldCreateUserID, userInfo.GetID()) if err != nil { return callbackOnCreateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i) } } prepared, err := callbackPrepareCreate(callbacks, c, params, e, i) if err != nil { return callbackOnCreateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i) } err = domain.CheckFieldsForCreate(e, e.GetFieldMap()) if err != nil { return callbackOnCreateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i) } if domain.HasField(e, entity.FieldCreateUserID) && domain.HasField(e, entity.FieldLastUpdateUserID) { createUserID, err := domain.Field[string](e, entity.FieldCreateUserID) if err != nil { return callbackOnCreateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i) } err = domain.SetField(e, entity.FieldLastUpdateUserID, createUserID) if err != nil { return callbackOnCreateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i) } } err = callbackBeforeCreate(callbacks, c, params, e, prepared, i, nil) if err != nil { return callbackOnCreateErrorReturn(callbacks, c, params, e, prepared, err, i) } err = database.InsertEntity(dbExecutor, tableName, e) if err != nil { if database.IsErrorDBRecordHasExist(err) { err = errors.New(e.DomainCNName() + "已存在") } return callbackOnCreateErrorReturn(callbacks, c, params, e, prepared, err, i) } err = callbackAfterCreate(callbacks, c, params, e, prepared, i, nil) if err != nil { return callbackOnCreateErrorReturn(callbacks, c, params, e, prepared, err, i) } return callbackOnCreateSuccessReturn(callbacks, c, params, e, prepared, i, e.GetID()) } } func Delete(tableName string, callbacks *DeleteCallbacks) binding.ServiceFunc[any] { return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) { object := objects[0] if object == nil { return nil, errors.New("领域实体为空") } dbExecutor := i.DBExecutor() e, ok := objects[0].(entity.Entity) if !ok { return nil, errors.New("需要传递领域对象应该为实体") } prepared, err := callbackPrepareDelete(callbacks, c, params, e, i) if err != nil { return nil, callbackOnDeleteErrorReturn(callbacks, c, params, e, make(map[string]any), err, i) } err = domain.CheckFieldsForDelete(e, e.GetFieldMap()) if err != nil { return nil, callbackOnDeleteErrorReturn(callbacks, c, params, e, make(map[string]any), err, i) } err = callbackBeforeDelete(callbacks, c, params, e, prepared, i, nil) if err != nil { return nil, callbackOnDeleteErrorReturn(callbacks, c, params, e, prepared, err, i) } err = database.DeleteEntity(dbExecutor, tableName, e) if err != nil { return nil, callbackOnDeleteErrorReturn(callbacks, c, params, e, prepared, err, i) } err = callbackAfterDelete(callbacks, c, params, e, prepared, i, nil) if err != nil { return nil, callbackOnDeleteErrorReturn(callbacks, c, params, e, prepared, err, i) } return nil, callbackOnDeleteSuccessReturn(callbacks, c, params, e, prepared, i) } } func Update(tableName string, needLastUpdateUserID bool, callbacks *UpdateCallbacks) binding.ServiceFunc[any] { return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) { object := objects[0] if object == nil { return nil, errors.New("领域实体为空") } dbExecutor := i.DBExecutor() e, ok := objects[0].(entity.Entity) if !ok { return nil, errors.New("需要传递领域对象应该为实体") } exist, err := database.CheckExist(dbExecutor, &sql.CheckExistExecuteParams{ TableName: tableName, Conditions: sql.NewConditions().Equal(entity.ColumnID, e.GetID()), }) if err != nil { return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i) } if !exist { err := errors.New(e.DomainCNName() + "不存在") return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i) } if needLastUpdateUserID { userInfo := c.GetUserInfo() err := domain.SetField(e, entity.FieldLastUpdateUserID, userInfo.GetID()) if err != nil { return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i) } } prepared, err := callbackPrepareUpdate(callbacks, c, params, e, i) if err != nil { return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i) } err = domain.CheckFieldsForUpdate(e, e.GetFieldMap()) if err != nil { return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i) } err = callbackBeforeUpdate(callbacks, c, params, e, prepared, i, nil) if err != nil { return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, prepared, err, i) } err = database.UpdateEntity(dbExecutor, tableName, e) if err != nil { return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, prepared, err, i) } err = callbackAfterUpdate(callbacks, c, params, e, prepared, i, nil) if err != nil { return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, prepared, err, i) } return nil, callbackOnUpdateSuccessReturn(callbacks, c, params, e, prepared, i) } } type ConditionFieldCallback func(conditions *sql.Conditions, fieldName string, columnName string, value any) (hasDeal bool, err error) type CustomCondition struct { Conditions *sql.Conditions OrderBy string } type FormCustomConditionFunc func(c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure) (*CustomCondition, error) func Query[O any](tableName string, orderBy string, callbacks *QueryCallbacks[O], conditionFieldCallback ConditionFieldCallback, formCustomConditionFunc FormCustomConditionFunc) binding.ServiceFunc[response.InfosData[O]] { return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[O], error) { errResponse := response.InfosData[O]{ Infos: make([]O, 0), } if params == nil { return errResponse, errors.New("请求参数为空") } object := objects[0] if object == nil { return errResponse, errors.New("领域实体为空") } dbExecutor := i.DBExecutor() queryParams, ok := params.(request.QueryRequestParams) if !ok { return errResponse, errors.New("请求参数不是Query接口") } e, ok := objects[0].(entity.Entity) if !ok { return errResponse, errors.New("需要传递领域对象应该为实体") } conditions := sql.NewConditions() fields, err := sql_mapping.DefaultUsage(e) if err != nil { return errResponse, err } if formCustomConditionFunc == nil { for _, field := range fields { hasDeal := false if conditionFieldCallback != nil { hasDeal, err = conditionFieldCallback(conditions, field.FieldName, field.ColumnName, field.Value) if err != nil { return errResponse, err } } if !hasDeal { fieldValue := reflect.ValueOf(field.Value) if !fieldValue.IsZero() { conditions.Equal(field.ColumnName, field.Value) } } } } else { customCondition, err := formCustomConditionFunc(c, params, e, i) if err != nil { return errResponse, err } conditions = customCondition.Conditions orderBy = customCondition.OrderBy } err = callbackBeforeQuery(callbacks, c, params, e, i) if err != nil { return callbackOnQueryErrorReturn(callbacks, c, params, e, err, i) } results, totalCount, err := database.Query(dbExecutor, &sql.QueryExecuteParams{ TableName: tableName, Conditions: conditions, OrderBy: orderBy, PageNo: queryParams.GetPageNo(), PageSize: queryParams.GetPageSize(), }) if err != nil { return callbackOnQueryErrorReturn(callbacks, c, params, e, err, i) } err = callbackAfterQuery(callbacks, c, params, e, i) if err != nil { return callbackOnQueryErrorReturn(callbacks, c, params, e, err, i) } infos := make([]O, 0) err = sql.ParseSqlResult(results, &infos) if err != nil { return callbackOnQueryErrorReturn(callbacks, c, params, e, err, i) } output := response.InfosData[O]{ Infos: infos, TotalCount: totalCount, PageNo: queryParams.GetPageNo(), } return callbackOnQuerySuccessReturn(callbacks, c, params, e, i, output) } } func GetByID[O any](tableName string, callbacks *GetByIDCallbacks[O]) binding.ServiceFunc[O] { return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (O, error) { outputZero := reflectutils.Zero[O]() object := objects[0] if object == nil { return outputZero, errors.New("领域实体为空") } dbExecutor := i.DBExecutor() e, ok := objects[0].(entity.Entity) if !ok { return outputZero, errors.New("需要传递领域对象应该为实体") } err := domain.CheckField(e, entity.FieldID, e.GetFieldMap()) if err != nil { return callbackOnGetByIDErrorReturn(callbacks, c, params, e, err, i) } err = callbackBeforeGetByID(callbacks, c, params, e, i) if err != nil { return callbackOnGetByIDErrorReturn(callbacks, c, params, e, err, i) } result, err := database.QueryOne(dbExecutor, &sql.QueryOneExecuteParams{ TableName: tableName, Conditions: sql.NewConditions().Equal(entity.ColumnID, e.GetID()), }) if err != nil { if database.IsErrorDBRecordNotExist(err) { err = errors.New(e.DomainCNName() + "不存在") } return callbackOnGetByIDErrorReturn(callbacks, c, params, e, err, i) } err = callbackAfterGetByID(callbacks, c, params, e, i) if err != nil { return callbackOnGetByIDErrorReturn(callbacks, c, params, e, err, i) } info := reflectutils.Zero[O]() err = sql.ParseSqlResult(result, &info) if err != nil { return callbackOnGetByIDErrorReturn(callbacks, c, params, e, err, i) } return callbackOnGetByIDSuccessReturn(callbacks, c, params, e, i, info) } } func CreateTx(tableName string, needCreateUserID bool, callbacks *CreateCallbacks) binding.ServiceFunc[string] { return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (string, error) { object := objects[0] if object == nil { return "", errors.New("领域实体为空") } dbExecutor := i.DBExecutor() e, ok := objects[0].(entity.Entity) if !ok { return "", errors.New("需要传递领域对象应该为实体") } err := e.GenerateID() if err != nil { return callbackOnCreateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i) } if needCreateUserID { userInfo := c.GetUserInfo() err := domain.SetField(e, entity.FieldCreateUserID, userInfo.GetID()) if err != nil { return callbackOnCreateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i) } } prepared, err := callbackPrepareCreate(callbacks, c, params, e, i) if err != nil { return callbackOnCreateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i) } err = domain.CheckFieldsForCreate(e, e.GetFieldMap()) if err != nil { return callbackOnCreateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i) } if domain.HasField(e, entity.FieldCreateUserID) && domain.HasField(e, entity.FieldLastUpdateUserID) { createUserID, err := domain.Field[string](e, entity.FieldCreateUserID) if err != nil { return callbackOnCreateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i) } err = domain.SetField(e, entity.FieldLastUpdateUserID, createUserID) if err != nil { return callbackOnCreateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i) } } err = database.Transaction(dbExecutor, func(tx database.Executor) error { err = callbackBeforeCreate(callbacks, c, params, e, prepared, i, tx) if err != nil { return err } err = database.InsertEntity(tx, tableName, e) if err != nil { if database.IsErrorDBRecordHasExist(err) { err = errors.New(e.DomainCNName() + "已存在") } return err } err = callbackAfterCreate(callbacks, c, params, e, prepared, i, tx) if err != nil { return err } return nil }) if err != nil { return callbackOnCreateErrorReturn(callbacks, c, params, e, prepared, err, i) } return callbackOnCreateSuccessReturn(callbacks, c, params, e, prepared, i, e.GetID()) } } func DeleteTx(tableName string, callbacks *DeleteCallbacks) binding.ServiceFunc[any] { return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) { object := objects[0] if object == nil { return nil, errors.New("领域实体为空") } dbExecutor := i.DBExecutor() e, ok := objects[0].(entity.Entity) if !ok { return nil, errors.New("需要传递领域对象应该为实体") } prepared, err := callbackPrepareDelete(callbacks, c, params, e, i) if err != nil { return nil, callbackOnDeleteErrorReturn(callbacks, c, params, e, make(map[string]any), err, i) } err = domain.CheckFieldsForDelete(e, e.GetFieldMap()) if err != nil { return nil, callbackOnDeleteErrorReturn(callbacks, c, params, e, make(map[string]any), err, i) } err = database.Transaction(dbExecutor, func(tx database.Executor) error { err = callbackBeforeDelete(callbacks, c, params, e, prepared, i, tx) if err != nil { return err } err = database.DeleteEntity(tx, tableName, e) if err != nil { return err } err = callbackAfterDelete(callbacks, c, params, e, prepared, i, tx) if err != nil { return err } return nil }) if err != nil { return nil, callbackOnDeleteErrorReturn(callbacks, c, params, e, prepared, err, i) } return nil, callbackOnDeleteSuccessReturn(callbacks, c, params, e, prepared, i) } } func UpdateTx(tableName string, needLastUpdateUserID bool, callbacks *UpdateCallbacks) binding.ServiceFunc[any] { return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) { object := objects[0] if object == nil { return nil, errors.New("领域实体为空") } dbExecutor := i.DBExecutor() e, ok := objects[0].(entity.Entity) if !ok { return nil, errors.New("需要传递领域对象应该为实体") } exist, err := database.CheckExist(dbExecutor, &sql.CheckExistExecuteParams{ TableName: tableName, Conditions: sql.NewConditions().Equal(entity.ColumnID, e.GetID()), }) if err != nil { return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i) } if !exist { return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, make(map[string]any), errors.New(e.DomainCNName()+"不存在"), i) } if needLastUpdateUserID { userInfo := c.GetUserInfo() err := domain.SetField(e, entity.FieldLastUpdateUserID, userInfo.GetID()) if err != nil { return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i) } } prepared, err := callbackPrepareUpdate(callbacks, c, params, e, i) if err != nil { return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i) } err = domain.CheckFieldsForUpdate(e, e.GetFieldMap()) if err != nil { return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i) } err = database.Transaction(dbExecutor, func(tx database.Executor) error { err = callbackBeforeUpdate(callbacks, c, params, e, prepared, i, tx) if err != nil { return err } err = database.UpdateEntity(tx, tableName, e) if err != nil { return err } err = callbackAfterUpdate(callbacks, c, params, e, prepared, i, tx) if err != nil { return err } return nil }) if err != nil { return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, prepared, err, i) } return nil, callbackOnUpdateSuccessReturn(callbacks, c, params, e, prepared, i) } }