作者 yangfu

Merge branch 'feat_v140'

package query
import (
"fmt"
"github.com/beego/beego/v2/core/validation"
"reflect"
"strings"
)
type StatisticsQuery struct {
}
func (q *StatisticsQuery) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (q *StatisticsQuery) ValidateQuery() error {
valid := validation.Validation{}
b, err := valid.Valid(q)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(q).Elem()
for _, validErr := range valid.Errors {
field, isExist := elem.FieldByName(validErr.Field)
if isExist {
return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
} else {
return fmt.Errorf(validErr.Message)
}
}
}
return nil
}
... ...
package service
import (
"github.com/linmadan/egglib-go/core/application"
"github.com/linmadan/egglib-go/utils/xtime"
"github.com/zeromicro/go-zero/core/fx"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/querySet/query"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
)
func (querySetService *QuerySetService) StatisticsQuery(ctx *domain.Context, query *query.StatisticsQuery) (interface{}, error) {
if err := query.ValidateQuery(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
querySetRepository, _, _ := factory.FastPgQuerySet(transactionContext, 0)
var (
total int64
totalToday int64
totalYesterday int64
totalEnable int64
platformTotal int64
platformTotalYesterday int64
)
fx.Parallel(func() {
total, _, err = querySetRepository.Find(map[string]interface{}{
"context": ctx,
"limit": 1,
"type": domain.SchemaTable.ToString(),
})
if err != nil {
return
}
totalEnable, _, err = querySetRepository.Find(map[string]interface{}{
"context": ctx,
"limit": 1,
"type": domain.SchemaTable.ToString(),
"status": domain.StatusOn,
})
if err != nil {
return
}
totalToday, _, err = querySetRepository.Find(map[string]interface{}{
"context": ctx,
"limit": 1,
"type": domain.SchemaTable.ToString(),
"beginTime": xtime.BeginningOfDay().Local(),
"endTime": xtime.BeginningOfDay().AddDate(0, 0, 1).Local(),
})
if err != nil {
return
}
totalYesterday, _, err = querySetRepository.Find(map[string]interface{}{
"context": ctx,
"limit": 1,
"type": domain.SchemaTable.ToString(),
"beginTime": xtime.BeginningOfDay().AddDate(0, 0, -1).Local(),
"endTime": xtime.BeginningOfDay().Local(),
})
if err != nil {
return
}
}, func() {
platformTotal, _, err = querySetRepository.Find(map[string]interface{}{
"limit": 1,
"type": domain.SchemaTable.ToString(),
})
if err != nil {
return
}
platformTotalYesterday, _, err = querySetRepository.Find(map[string]interface{}{
"limit": 1,
"type": domain.SchemaTable.ToString(),
"beginTime": xtime.BeginningOfDay().AddDate(0, 0, -1).Local(),
"endTime": xtime.BeginningOfDay().Local(),
})
if err != nil {
return
}
})
if err != nil {
return nil, factory.FastError(err)
}
return map[string]interface{}{
"schemaStatistics": map[string]interface{}{
"total": total,
"totalToday": totalToday,
"totalYesterday": totalYesterday,
"totalEnable": totalEnable,
},
"platformSchemaStatistics": map[string]interface{}{
"total": platformTotal,
"totalYesterday": platformTotalYesterday,
},
}, nil
}
... ...
package command
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type RowsDeleteCommand struct {
// 表Id
TableId int `cname:"表Id" json:"tableId" valid:"Required"`
Where domain.Where `json:"where"`
}
func (cmd *RowsDeleteCommand) Valid(validation *validation.Validation) {
}
func (cmd *RowsDeleteCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(cmd)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(cmd).Elem()
for _, validErr := range valid.Errors {
field, isExist := elem.FieldByName(validErr.Field)
if isExist {
return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
} else {
return fmt.Errorf(validErr.Message)
}
}
}
return nil
}
... ...
... ... @@ -97,6 +97,42 @@ func (tableService *TableService) RowEditV2(ctx *domain.Context, cmd *command.Ro
return struct{}{}, nil
}
func (tableService *TableService) RowsDelete(ctx *domain.Context, cmd *command.RowsDeleteCommand) (interface{}, error) {
if err := cmd.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
//if err := transactionContext.StartTransaction(); err != nil {
// return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
//}
//defer func() {
// transactionContext.RollbackTransaction()
//}()
var table *domain.Table
_, table, err = factory.FastPgTable(transactionContext, cmd.TableId)
if err != nil {
return nil, factory.FastError(err)
}
var options = starrocks.QueryOptions{
TableName: table.SQLName,
Select: []*domain.Field{domain.PK()}, //table.Fields(true),
Where: []starrocks.Condition{},
}
options.SetCondition(cmd.Where.Conditions)
total, err := starrocks.WrapDeleteFuncWithDB(starrocks.DB)(options)
//if err := transactionContext.CommitTransaction(); err != nil {
// return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
//}
return map[string]interface{}{
"rowsAffected": total,
}, nil
}
func MapArrayToFieldValues(list []map[string]string, table *domain.Table, dataTable *domain.DataTable, mustMatch bool) []*domain.FieldValues {
var result = make([]*domain.FieldValues, 0)
history := dto.ToFieldDataByPK(table, dataTable)
... ...
... ... @@ -266,6 +266,24 @@ func pin(name string) string {
return newPinyin.String()
}
func pinFull(name string) string {
prefixBuf := bytes.NewBuffer(nil)
runes := []rune(name)
for _, r := range runes {
if r > 0xFF {
if prefixBuf.Len() > 0 {
prefixBuf.WriteString("_")
}
prefixBuf.WriteString(pin(string(r)))
continue
}
//if isDigital(byte(r)) || isLetters(byte(r)){
prefixBuf.WriteRune(r)
//}
}
return prefixBuf.String()
}
func isDigital(b byte) bool {
return b >= byte('0') && b <= byte('9')
}
... ...
package domainService
import (
"github.com/stretchr/testify/assert"
"testing"
)
func Test_PinName(t *testing.T) {
inputs := []struct {
s string
want string
}{
{"a我们", "a_wo_men"},
{"a2我们", "a2_wo_men"},
{"1我们", "1_wo_men"},
{"10.我们", "10._wo_men"},
{"我们", "wo_men"},
{"z.我们", "z._wo_men"},
{"123", "123"},
{"年度", "nian_du"},
{"333", "333"},
{"yyy", "yyy"},
{"1.2.3", "1.2.3"},
}
for _, input := range inputs {
got := pinFull(input.s)
assert.Equal(t, input.want, got)
}
}
... ...
... ... @@ -41,7 +41,7 @@ func (ptr *QuerySetService) Create(ctx *domain.Context, qs *domain.QuerySet) (*d
Flag: qs.Flag,
Name: qs.Name,
ParentId: qs.ParentId,
PinName: pin(qs.Name),
PinName: pinFull(qs.Name),
Status: domain.StatusOn,
QuerySetInfo: &domain.QuerySetInfo{},
QueryComponents: make([]*domain.QueryComponent, 0),
... ... @@ -1032,7 +1032,7 @@ func copyQuerySet(qs *domain.QuerySet, t string, groupId int, name string) *doma
Type: t,
Flag: qs.Flag,
Name: name,
PinName: pin(name),
PinName: pinFull(name),
ParentId: groupId,
Status: qs.Status,
QuerySetInfo: qs.QuerySetInfo,
... ...
package domainService
import (
"fmt"
pG "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/pg"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/repository"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/log"
)
func Script() {
ScriptRenameQuerySetPinName()
}
func ScriptRenameQuerySetPinName() {
defer func() {
if p := recover(); p != nil {
log.Logger.Error(fmt.Sprintf("%v", p))
}
}()
var err error
transactionContext := pG.NewPGTransactionContext(pg.DB)
if err != nil {
return
}
if err := transactionContext.StartTransaction(); err != nil {
return
}
defer func() {
transactionContext.RollbackTransaction()
}()
querySetRepository, _ := repository.NewQuerySetRepository(transactionContext)
_, querySets, _ := querySetRepository.Find(map[string]interface{}{})
for i := range querySets {
pinName := pinFull(querySets[i].Name)
log.Logger.Info(fmt.Sprintf("%v %v -> %v", querySets[i].QuerySetId, querySets[i].Name, querySets[i]))
querySets[i].PinName = pinName
_, err := querySetRepository.Save(querySets[i])
if err != nil {
log.Logger.Error(err.Error(), map[string]interface{}{"qs": querySets[i]})
}
}
if err := transactionContext.CommitTransaction(); err != nil {
return
}
}
... ...
... ... @@ -5,6 +5,8 @@ import (
"fmt"
"github.com/go-pg/pg/v10"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/utils"
"strconv"
"time"
"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
... ... @@ -171,13 +173,25 @@ func (repository *QuerySetRepository) Find(queryOptions map[string]interface{})
query.Where("query_set_info->'BindTableId' in (?)", pg.In(utils.ToArrayString(v.([]int))))
}
if v, ok := queryOptions["sortByName"]; ok && len(v.(string)) > 0 {
query.SetOrderDirect("name", v.(string))//pin_name
query.SetOrderDirect("pin_name", v.(string)) //pin_name
} else if v, ok := queryOptions["sortByTime"]; ok && len(v.(string)) > 0 {
query.SetOrderDirect("created_at", v.(string))
} else {
query.SetOrderDirect("parent_id", "ASC")
query.SetOrderDirect("sort", "ASC")
}
if v, ok := queryOptions["beginTime"]; ok {
query.Where("created_at >= ?", v.(time.Time))
}
if v, ok := queryOptions["endTime"]; ok {
query.Where("created_at < ?", v.(time.Time))
}
if limit, ok := queryOptions["limit"]; ok {
limit, _ := strconv.ParseInt(fmt.Sprintf("%v", limit), 10, 64)
if limit > 0 {
query.Limit(int(limit))
}
}
if count, err := query.SelectAndCount(); err != nil {
return 0, querySets, err
} else {
... ...
... ... @@ -342,6 +342,43 @@ func WrapQueryFuncWithDB(db *gorm.DB) func(QueryOptions) (*sql.Rows, error) {
}
}
func WrapDeleteFuncWithDB(db *gorm.DB) func(QueryOptions) (int64, error) {
return func(params QueryOptions) (int64, error) {
query := db.Table(params.TableName)
queryWithoutLimitOffset(query, params)
//if params.Offset > 0 {
// query.Offset(params.Offset)
//}
//if params.Limit > 0 {
// query.Limit(params.Limit)
//}
//if params.Context != nil {
// query.Where(fmt.Sprintf("context->>'companyId'='%v'", params.Context.CompanyId))
//}
rows, err := query.Rows()
defer rows.Close()
if err != nil {
return 0, err
}
dataTable := &domain.DataTable{}
dataTable.Data, err = ScanRows(rows)
idList := make([]string, 0)
for _, row := range dataTable.Data {
if len(row) == 0 {
continue
}
idList = append(idList, row[0])
}
if len(idList) == 0 {
return 0, nil
}
c := Condition{}
sql := fmt.Sprintf("delete from %v where id in %v", params.TableName, c.InArgs(idList))
query = db.Exec(sql)
return int64(len(idList)), query.Error
}
}
func SetTable(query *gorm.DB, tableName string) {
query.Statement.Table = tableName
}
... ...
... ... @@ -154,3 +154,11 @@ func (controller *QuerySetController) CalculateSetExport() {
data, err := querySetService.CalculateSetExport(ParseContext(controller.BaseController), updateQuerySetCommand)
controller.Response(data, err)
}
func (controller *QuerySetController) QuerySetStatistics() {
querySetService := service.NewQuerySetService(nil)
q := &query.StatisticsQuery{}
Must(controller.Unmarshal(q))
data, err := querySetService.StatisticsQuery(ParseContext(controller.BaseController), q)
controller.Response(data, err)
}
... ...
... ... @@ -6,6 +6,8 @@ import (
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/table/query"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/table/service"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/domainService"
"strings"
filecommand "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/file/command"
fileservice "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/file/service"
... ... @@ -277,6 +279,14 @@ func (controller *TableController) RowEdit() {
controller.Response(data, err)
}
func (controller *TableController) RowsDelete() {
tableService := service.NewTableService(nil)
cmd := &command.RowsDeleteCommand{}
Must(controller.Unmarshal(cmd))
data, err := tableService.RowsDelete(ParseContext(controller.BaseController), cmd)
controller.Response(data, err)
}
func (controller *TableController) DependencyGraph() {
tableService := service.NewTableService(nil)
updateTableCommand := &query.GetTableQuery{}
... ... @@ -284,3 +294,12 @@ func (controller *TableController) DependencyGraph() {
data, err := tableService.DependencyGraph(ParseContext(controller.BaseController), updateTableCommand)
controller.Response(data, err)
}
func (controller *TableController) ExecScript() {
name := controller.GetString(":name")
switch strings.ToLower(name) {
case "qsrename":
domainService.ScriptRenameQuerySetPinName()
}
controller.Response(nil, nil)
}
... ...
... ... @@ -37,4 +37,6 @@ func init() {
web.Router("/data/query-sets/formula/calculate-item-preview", &controllers.QuerySetController{}, "Post:CalculateItemPreview")
web.Router("/data/query-sets/formula/calculate-item-export", &controllers.QuerySetController{}, "Post:CalculateItemExport")
web.Router("/data/query-sets/statistics", &controllers.QuerySetController{}, "Post:QuerySetStatistics")
}
... ...
... ... @@ -34,6 +34,7 @@ func init() {
web.Router("/data/tables/table-preview", tableController, "Post:TablePreview")
web.Router("/data/tables/row-edit", tableController, "Post:RowEdit")
web.Router("/data/tables/row-delete", tableController, "Post:RowsDelete")
web.Router("/data/field-optional-values", tableController, "Post:FieldOptionalValues")
web.Router("/data/table-object-search", tableController, "Post:TableObjectSearch")
... ... @@ -41,4 +42,5 @@ func init() {
web.Router("/business/db-table-preview", tableController, "Post:DBTablePreview")
web.Router("/data/table-preview", tableController, "Post:Preview")
web.Router("/data/tables/exec/:name", tableController, "Get:ExecScript")
}
... ...