123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526 |
- 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, 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, e, make(map[string]any), err, i)
- }
- }
- prepared, err := callbackPrepareCreate(callbacks, c, e, i)
- if err != nil {
- return callbackOnCreateErrorReturn(callbacks, c, e, make(map[string]any), err, i)
- }
- err = domain.CheckFieldsForCreate(e, e.GetFieldMap())
- if err != nil {
- return callbackOnCreateErrorReturn(callbacks, c, 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, e, make(map[string]any), err, i)
- }
- err = domain.SetField(e, entity.FieldLastUpdateUserID, createUserID)
- if err != nil {
- return callbackOnCreateErrorReturn(callbacks, c, e, make(map[string]any), err, i)
- }
- }
- err = callbackBeforeCreate(callbacks, c, e, prepared, i, nil)
- if err != nil {
- return callbackOnCreateErrorReturn(callbacks, c, 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, e, prepared, err, i)
- }
- err = callbackAfterCreate(callbacks, c, e, prepared, i, nil)
- if err != nil {
- return callbackOnCreateErrorReturn(callbacks, c, e, prepared, err, i)
- }
- return callbackOnCreateSuccessReturn(callbacks, c, 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, e, i)
- if err != nil {
- return nil, callbackOnDeleteErrorReturn(callbacks, c, e, make(map[string]any), err, i)
- }
- err = domain.CheckFieldsForDelete(e, e.GetFieldMap())
- if err != nil {
- return nil, callbackOnDeleteErrorReturn(callbacks, c, e, make(map[string]any), err, i)
- }
- err = callbackBeforeDelete(callbacks, c, e, prepared, i, nil)
- if err != nil {
- return nil, callbackOnDeleteErrorReturn(callbacks, c, e, prepared, err, i)
- }
- err = database.DeleteEntity(dbExecutor, tableName, e)
- if err != nil {
- return nil, callbackOnDeleteErrorReturn(callbacks, c, e, prepared, err, i)
- }
- err = callbackAfterDelete(callbacks, c, e, prepared, i, nil)
- if err != nil {
- return nil, callbackOnDeleteErrorReturn(callbacks, c, e, prepared, err, i)
- }
- return nil, callbackOnDeleteSuccessReturn(callbacks, c, 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, e, make(map[string]any), err, i)
- }
- if !exist {
- err := errors.New(e.DomainCNName() + "不存在")
- return nil, callbackOnUpdateErrorReturn(callbacks, c, 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, e, make(map[string]any), err, i)
- }
- }
- prepared, err := callbackPrepareUpdate(callbacks, c, e, i)
- if err != nil {
- return nil, callbackOnUpdateErrorReturn(callbacks, c, e, make(map[string]any), err, i)
- }
- err = domain.CheckFieldsForUpdate(e, e.GetFieldMap())
- if err != nil {
- return nil, callbackOnUpdateErrorReturn(callbacks, c, e, make(map[string]any), err, i)
- }
- err = callbackBeforeUpdate(callbacks, c, e, prepared, i, nil)
- if err != nil {
- return nil, callbackOnUpdateErrorReturn(callbacks, c, e, prepared, err, i)
- }
- err = database.UpdateEntity(dbExecutor, tableName, e)
- if err != nil {
- return nil, callbackOnUpdateErrorReturn(callbacks, c, e, prepared, err, i)
- }
- err = callbackAfterUpdate(callbacks, c, e, prepared, i, nil)
- if err != nil {
- return nil, callbackOnUpdateErrorReturn(callbacks, c, e, prepared, err, i)
- }
- return nil, callbackOnUpdateSuccessReturn(callbacks, c, e, prepared, i)
- }
- }
- type ConditionFieldCallback func(conditions *sql.Conditions, fieldName string, columnName string, value any) (hasDeal bool, err error)
- func Query[O any](tableName string, orderBy string, callbacks *QueryCallbacks[O], conditionFieldCallback ConditionFieldCallback) 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
- }
- 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)
- }
- }
- }
- err = callbackBeforeQuery(callbacks, c, e, i)
- if err != nil {
- return callbackOnQueryErrorReturn(callbacks, c, 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, e, err, i)
- }
- err = callbackAfterQuery(callbacks, c, e, i)
- if err != nil {
- return callbackOnQueryErrorReturn(callbacks, c, e, err, i)
- }
- infos := make([]O, 0)
- err = sql.ParseSqlResult(results, &infos)
- if err != nil {
- return callbackOnQueryErrorReturn(callbacks, c, e, err, i)
- }
- output := response.InfosData[O]{
- Infos: infos,
- TotalCount: totalCount,
- PageNo: queryParams.GetPageNo(),
- }
- return callbackOnQuerySuccessReturn(callbacks, c, 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, e, err, i)
- }
- err = callbackBeforeGetByID(callbacks, c, e, i)
- if err != nil {
- return callbackOnGetByIDErrorReturn(callbacks, c, 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, e, err, i)
- }
- err = callbackAfterGetByID(callbacks, c, e, i)
- if err != nil {
- return callbackOnGetByIDErrorReturn(callbacks, c, e, err, i)
- }
- info := reflectutils.Zero[O]()
- err = sql.ParseSqlResult(result, &info)
- if err != nil {
- return callbackOnGetByIDErrorReturn(callbacks, c, e, err, i)
- }
- return callbackOnGetByIDSuccessReturn(callbacks, c, 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, 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, e, make(map[string]any), err, i)
- }
- }
- prepared, err := callbackPrepareCreate(callbacks, c, e, i)
- if err != nil {
- return callbackOnCreateErrorReturn(callbacks, c, e, make(map[string]any), err, i)
- }
- err = domain.CheckFieldsForCreate(e, e.GetFieldMap())
- if err != nil {
- return callbackOnCreateErrorReturn(callbacks, c, e, make(map[string]any), err, i)
- }
- err = database.Transaction(dbExecutor, func(tx database.Executor) error {
- err = callbackBeforeCreate(callbacks, c, 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, e, prepared, i, tx)
- if err != nil {
- return err
- }
- return nil
- })
- if err != nil {
- return callbackOnCreateErrorReturn(callbacks, c, e, prepared, err, i)
- }
- return callbackOnCreateSuccessReturn(callbacks, c, 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, e, i)
- if err != nil {
- return nil, callbackOnDeleteErrorReturn(callbacks, c, e, make(map[string]any), err, i)
- }
- err = domain.CheckFieldsForDelete(e, e.GetFieldMap())
- if err != nil {
- return nil, callbackOnDeleteErrorReturn(callbacks, c, e, make(map[string]any), err, i)
- }
- err = database.Transaction(dbExecutor, func(tx database.Executor) error {
- err = callbackBeforeDelete(callbacks, c, e, prepared, i, tx)
- if err != nil {
- return err
- }
- err = database.DeleteEntity(tx, tableName, e)
- if err != nil {
- return err
- }
- err = callbackAfterDelete(callbacks, c, e, prepared, i, tx)
- if err != nil {
- return err
- }
- return nil
- })
- if err != nil {
- return nil, callbackOnDeleteErrorReturn(callbacks, c, e, prepared, err, i)
- }
- return nil, callbackOnDeleteSuccessReturn(callbacks, c, 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, e, make(map[string]any), err, i)
- }
- if !exist {
- return nil, callbackOnUpdateErrorReturn(callbacks, c, 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, e, make(map[string]any), err, i)
- }
- }
- prepared, err := callbackPrepareUpdate(callbacks, c, e, i)
- if err != nil {
- return nil, callbackOnUpdateErrorReturn(callbacks, c, e, make(map[string]any), err, i)
- }
- err = domain.CheckFieldsForUpdate(e, e.GetFieldMap())
- if err != nil {
- return nil, callbackOnUpdateErrorReturn(callbacks, c, e, make(map[string]any), err, i)
- }
- err = database.Transaction(dbExecutor, func(tx database.Executor) error {
- err = callbackBeforeUpdate(callbacks, c, e, prepared, i, tx)
- if err != nil {
- return err
- }
- err = database.UpdateEntity(tx, tableName, e)
- if err != nil {
- return err
- }
- err = callbackAfterUpdate(callbacks, c, e, prepared, i, tx)
- if err != nil {
- return err
- }
- return nil
- })
- if err != nil {
- return nil, callbackOnUpdateErrorReturn(callbacks, c, e, prepared, err, i)
- }
- return nil, callbackOnUpdateSuccessReturn(callbacks, c, e, prepared, i)
- }
- }
|