作者 yangfu

feat: row batch delete

  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
  6 + "reflect"
  7 + "strings"
  8 +
  9 + "github.com/beego/beego/v2/core/validation"
  10 +)
  11 +
  12 +type RowsDeleteCommand struct {
  13 + // 表Id
  14 + TableId int `cname:"表Id" json:"tableId" valid:"Required"`
  15 + Where domain.Where `json:"where"`
  16 +}
  17 +
  18 +func (cmd *RowsDeleteCommand) Valid(validation *validation.Validation) {
  19 +}
  20 +
  21 +func (cmd *RowsDeleteCommand) ValidateCommand() error {
  22 + valid := validation.Validation{}
  23 + b, err := valid.Valid(cmd)
  24 + if err != nil {
  25 + return err
  26 + }
  27 + if !b {
  28 + elem := reflect.TypeOf(cmd).Elem()
  29 + for _, validErr := range valid.Errors {
  30 + field, isExist := elem.FieldByName(validErr.Field)
  31 + if isExist {
  32 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  33 + } else {
  34 + return fmt.Errorf(validErr.Message)
  35 + }
  36 + }
  37 + }
  38 + return nil
  39 +}
@@ -97,6 +97,42 @@ func (tableService *TableService) RowEditV2(ctx *domain.Context, cmd *command.Ro @@ -97,6 +97,42 @@ func (tableService *TableService) RowEditV2(ctx *domain.Context, cmd *command.Ro
97 return struct{}{}, nil 97 return struct{}{}, nil
98 } 98 }
99 99
  100 +func (tableService *TableService) RowsDelete(ctx *domain.Context, cmd *command.RowsDeleteCommand) (interface{}, error) {
  101 + if err := cmd.ValidateCommand(); err != nil {
  102 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  103 + }
  104 + transactionContext, err := factory.CreateTransactionContext(nil)
  105 + if err != nil {
  106 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  107 + }
  108 + //if err := transactionContext.StartTransaction(); err != nil {
  109 + // return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  110 + //}
  111 + //defer func() {
  112 + // transactionContext.RollbackTransaction()
  113 + //}()
  114 +
  115 + var table *domain.Table
  116 + _, table, err = factory.FastPgTable(transactionContext, cmd.TableId)
  117 + if err != nil {
  118 + return nil, factory.FastError(err)
  119 + }
  120 +
  121 + var options = starrocks.QueryOptions{
  122 + TableName: table.SQLName,
  123 + Select: []*domain.Field{domain.PK()}, //table.Fields(true),
  124 + Where: []starrocks.Condition{},
  125 + }
  126 + options.SetCondition(cmd.Where.Conditions)
  127 + total, err := starrocks.WrapDeleteFuncWithDB(starrocks.DB)(options)
  128 + //if err := transactionContext.CommitTransaction(); err != nil {
  129 + // return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  130 + //}
  131 + return map[string]interface{}{
  132 + "rowsAffected": total,
  133 + }, nil
  134 +}
  135 +
100 func MapArrayToFieldValues(list []map[string]string, table *domain.Table, dataTable *domain.DataTable, mustMatch bool) []*domain.FieldValues { 136 func MapArrayToFieldValues(list []map[string]string, table *domain.Table, dataTable *domain.DataTable, mustMatch bool) []*domain.FieldValues {
101 var result = make([]*domain.FieldValues, 0) 137 var result = make([]*domain.FieldValues, 0)
102 history := dto.ToFieldDataByPK(table, dataTable) 138 history := dto.ToFieldDataByPK(table, dataTable)
@@ -342,6 +342,43 @@ func WrapQueryFuncWithDB(db *gorm.DB) func(QueryOptions) (*sql.Rows, error) { @@ -342,6 +342,43 @@ func WrapQueryFuncWithDB(db *gorm.DB) func(QueryOptions) (*sql.Rows, error) {
342 } 342 }
343 } 343 }
344 344
  345 +func WrapDeleteFuncWithDB(db *gorm.DB) func(QueryOptions) (int64, error) {
  346 + return func(params QueryOptions) (int64, error) {
  347 + query := db.Table(params.TableName)
  348 + queryWithoutLimitOffset(query, params)
  349 + //if params.Offset > 0 {
  350 + // query.Offset(params.Offset)
  351 + //}
  352 + //if params.Limit > 0 {
  353 + // query.Limit(params.Limit)
  354 + //}
  355 + //if params.Context != nil {
  356 + // query.Where(fmt.Sprintf("context->>'companyId'='%v'", params.Context.CompanyId))
  357 + //}
  358 + rows, err := query.Rows()
  359 + defer rows.Close()
  360 + if err != nil {
  361 + return 0, err
  362 + }
  363 + dataTable := &domain.DataTable{}
  364 + dataTable.Data, err = ScanRows(rows)
  365 + idList := make([]string, 0)
  366 + for _, row := range dataTable.Data {
  367 + if len(row) == 0 {
  368 + continue
  369 + }
  370 + idList = append(idList, row[0])
  371 + }
  372 + if len(idList) == 0 {
  373 + return 0, nil
  374 + }
  375 + c := Condition{}
  376 + sql := fmt.Sprintf("delete from %v where id in %v", params.TableName, c.InArgs(idList))
  377 + query = db.Exec(sql)
  378 + return int64(len(idList)), query.Error
  379 + }
  380 +}
  381 +
345 func SetTable(query *gorm.DB, tableName string) { 382 func SetTable(query *gorm.DB, tableName string) {
346 query.Statement.Table = tableName 383 query.Statement.Table = tableName
347 } 384 }
@@ -279,6 +279,14 @@ func (controller *TableController) RowEdit() { @@ -279,6 +279,14 @@ func (controller *TableController) RowEdit() {
279 controller.Response(data, err) 279 controller.Response(data, err)
280 } 280 }
281 281
  282 +func (controller *TableController) RowsDelete() {
  283 + tableService := service.NewTableService(nil)
  284 + cmd := &command.RowsDeleteCommand{}
  285 + Must(controller.Unmarshal(cmd))
  286 + data, err := tableService.RowsDelete(ParseContext(controller.BaseController), cmd)
  287 + controller.Response(data, err)
  288 +}
  289 +
282 func (controller *TableController) DependencyGraph() { 290 func (controller *TableController) DependencyGraph() {
283 tableService := service.NewTableService(nil) 291 tableService := service.NewTableService(nil)
284 updateTableCommand := &query.GetTableQuery{} 292 updateTableCommand := &query.GetTableQuery{}
@@ -34,6 +34,7 @@ func init() { @@ -34,6 +34,7 @@ func init() {
34 34
35 web.Router("/data/tables/table-preview", tableController, "Post:TablePreview") 35 web.Router("/data/tables/table-preview", tableController, "Post:TablePreview")
36 web.Router("/data/tables/row-edit", tableController, "Post:RowEdit") 36 web.Router("/data/tables/row-edit", tableController, "Post:RowEdit")
  37 + web.Router("/data/tables/row-delete", tableController, "Post:RowsDelete")
37 38
38 web.Router("/data/field-optional-values", tableController, "Post:FieldOptionalValues") 39 web.Router("/data/field-optional-values", tableController, "Post:FieldOptionalValues")
39 web.Router("/data/table-object-search", tableController, "Post:TableObjectSearch") 40 web.Router("/data/table-object-search", tableController, "Post:TableObjectSearch")