package entity_crud import ( "git.sxidc.com/go-framework/baize/binding" "git.sxidc.com/go-framework/baize/binding/request" "git.sxidc.com/go-framework/baize/binding/response" "git.sxidc.com/go-framework/baize/domain" ) func BindSimple[O any](binder *binding.Binder, crud *Simple[O]) { crud.bind(binder) } // Simple 实体CRUD的Bind参数 type Simple[O any] struct { // 使用的领域实体,注意是Entity类型 Entity domain.Entity // 表名 TableName string // 选择要使用的数据库Executor // DBExecutorOperations operations 数据库操作 // DBExecutorDataService data_service 数据服务 DBExecutorType string // URL领域相对路径,如/class,后面会自动补充 DomainPath string // 创建使用的DTO CreateDTO request.DTO // 创建是否使用事务 CreateNeedTx bool // 创建回调 CreateCallbacks *Callbacks[string] // 删除使用的DTO,注意是WithID类型 DeleteDTO request.WithID // 删除是否使用事务 DeleteNeedTx bool // 删除回调 DeleteCallbacks *Callbacks[any] // 更新使用的DTO,注意是WithID类型 UpdateDTO request.WithID // 根性是否使用事务 UpdateNeedTx bool // 更新回调 UpdateCallbacks *Callbacks[any] // 查询使用的DTO,注意是Query类型 QueryDTO request.Query // 查询条件构造回调 QueryConditionFieldCallback ConditionFieldCallback // 查询回调 QueryCallbacks *Callbacks[response.InfosData[O]] // 根据ID查询使用的DTO,注意是WithID类型 GetByIDDTO request.WithID // 根据ID查询回调 GetByIDCallbacks *Callbacks[O] } func (crud *Simple[O]) bind(binder *binding.Binder) { dbExecutor := binder.ChooseDBExecutor(crud.DBExecutorType) // 创建 if !crud.CreateNeedTx { binding.PostBind(binder, &binding.SimpleBindItem[string]{ Path: crud.DomainPath + "/create", ResponseFunc: response.SendIDResponse[string], DTO: crud.CreateDTO, Objects: []domain.Object{crud.Entity}, ServiceFunc: CreateEntity(crud.TableName, dbExecutor, crud.CreateCallbacks), }) } else { binding.PostBind(binder, &binding.SimpleBindItem[string]{ Path: crud.DomainPath + "/create", ResponseFunc: response.SendIDResponse[string], DTO: crud.CreateDTO, Objects: []domain.Object{crud.Entity}, ServiceFunc: CreateEntityTx(crud.TableName, dbExecutor, crud.CreateCallbacks), }) } // 删除 if !crud.DeleteNeedTx { binding.DeleteBind(binder, &binding.SimpleBindItem[any]{ Path: crud.DomainPath + "/:id/delete", ResponseFunc: response.SendMsgResponse, DTO: crud.DeleteDTO, Objects: []domain.Object{crud.Entity}, ServiceFunc: DeleteEntity(crud.TableName, dbExecutor, crud.DeleteCallbacks), }) } else { binding.DeleteBind(binder, &binding.SimpleBindItem[any]{ Path: crud.DomainPath + "/:id/delete", ResponseFunc: response.SendMsgResponse, DTO: crud.DeleteDTO, Objects: []domain.Object{crud.Entity}, ServiceFunc: DeleteEntityTx(crud.TableName, dbExecutor, crud.DeleteCallbacks), }) } // 修改 if !crud.UpdateNeedTx { binding.PutBind(binder, &binding.SimpleBindItem[any]{ Path: crud.DomainPath + "/update", ResponseFunc: response.SendMsgResponse, DTO: crud.UpdateDTO, Objects: []domain.Object{crud.Entity}, ServiceFunc: UpdateEntity(crud.TableName, dbExecutor, crud.UpdateCallbacks), }) } else { binding.PutBind(binder, &binding.SimpleBindItem[any]{ Path: crud.DomainPath + "/update", ResponseFunc: response.SendMsgResponse, DTO: crud.UpdateDTO, Objects: []domain.Object{crud.Entity}, ServiceFunc: UpdateEntityTx(crud.TableName, dbExecutor, crud.UpdateCallbacks), }) } // 查询 binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[O]]{ Path: crud.DomainPath + "/query", ResponseFunc: response.SendInfosResponse[O], DTO: crud.QueryDTO, Objects: []domain.Object{crud.Entity}, ServiceFunc: QueryEntities[O](crud.TableName, dbExecutor, crud.QueryCallbacks, crud.QueryConditionFieldCallback), }) // 通过ID获取 binding.GetBind(binder, &binding.SimpleBindItem[O]{ Path: crud.DomainPath + "/get", ResponseFunc: response.SendInfoResponse[O], DTO: crud.GetByIDDTO, Objects: []domain.Object{crud.Entity}, ServiceFunc: GetEntityByID[O](crud.TableName, dbExecutor, crud.GetByIDCallbacks), }) }