log_service.go 14.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
package domainService

import (
	"fmt"
	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/repository"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/starrocks"
	"strings"
	"time"
)

type PGLogService struct {
	transactionContext *pgTransaction.TransactionContext
}

func NewPGLogService(transactionContext *pgTransaction.TransactionContext) (*PGLogService, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	} else {
		return &PGLogService{
			transactionContext: transactionContext,
		}, nil
	}
}

func FastLog(transactionContext *pgTransaction.TransactionContext, logType domain.LogType, sourceId int, logEntry Log) error {
	logService, _ := NewPGLogService(transactionContext)
	return logService.Log(logType, sourceId, logEntry)
}

func (ptr *PGLogService) Log(logType domain.LogType, sourceId int, logEntry Log) error {
	logRepository, _ := repository.NewLogRepository(ptr.transactionContext)
	entry := logEntry.Entry()
	entry.OperationType = logEntry.OperateType()
	log := &domain.Log{
		LogType:       logType.ToString(),
		SourceId:      sourceId,
		Entry:         &entry,
		ObjectName:    entry.ObjectName,
		ObjectType:    domain.EnumsDescription(domain.ObjectTypeMap, entry.ObjectType),
		OperationType: domain.EnumsDescription(domain.OperationTypeMap, entry.OperationType),
		Content:       logEntry.Content(),
		OperatorName:  entry.OperatorName,
		CreatedAt:     time.Now(),
		Context:       logEntry.Context(),
		LogTime:       entry.LogTime,
	}
	if entry.OperationType == domain.UnKnown.ToString() {
		return nil
	}
	if logEntry.LogType() != "" {
		log.LogType = logEntry.LogType()
	}

	if v, ok := logEntry.Context().GetValue(domain.ContextWithLogLevel); ok {
		log.Entry.Level = string(v.(domain.LogLevel))
	}
	if v, ok := logEntry.Context().GetValue(domain.ContextWithLogMsg); ok {
		log.Entry.Error = v.(string)
	}
	_, err := logRepository.Save(log)
	return err
}

type FastSourceLog struct {
	LogType  domain.LogType
	SourceId int
	LogEntry Log
}

func NewFastSourceLog(logType domain.LogType, sourceId int, logEntry Log) FastSourceLog {
	return FastSourceLog{
		LogType:  logType,
		SourceId: sourceId,
		LogEntry: logEntry,
	}
}

func (ptr *PGLogService) NewLogEntry() domain.LogEntry {
	return domain.LogEntry{}
}

type Log interface {
	Content() string
	Entry() domain.LogEntry
	Context() *domain.Context
	OperateType() string
	LogType() string
}

var _ Log = (*FileUploadSuccessLog)(nil)

// FileUploadSuccessLog 1.1文件上传成功
type FileUploadSuccessLog struct {
	domain.LogEntry
}

func (l *FileUploadSuccessLog) Content() string {
	return fmt.Sprintf("上传成功")
}

// FileUploadFailLog 1.2文件上传失败
type FileUploadFailLog struct {
	domain.LogEntry
	Reason string
}

func (l *FileUploadFailLog) Content() string {
	return fmt.Sprintf("上传失败,失败原因:%s", l.Reason)
}

// FileVerifyLog 2.文件校验
type FileVerifyLog struct {
	domain.LogEntry
	// 错误信息
	Errors []string
	// 记录数
	Total int
}

func (l *FileVerifyLog) Content() string {
	msg := fmt.Sprintf("校验完成,共计%d条记录 ", l.Total)
	if len(l.Errors) > 0 {
		msg += fmt.Sprintf("存在%v条报错", len(l.Errors))
	}
	return msg
}

// GenerateMainTableLog 3.主表生成日志
type GenerateMainTableLog struct {
	domain.LogEntry
	// 表名
	TableName string
	// 文件名
	FileName string
}

func (l *GenerateMainTableLog) Content() string {
	msg := fmt.Sprintf("来源校验文件:%v", l.FileName)
	return msg
}

// SpiltMainTableLog 4.主表拆分
type SpiltMainTableLog struct {
	domain.LogEntry
	Reserve []*domain.Field
	Delete  []*domain.Field
	Add     []*domain.Field
	// 表名
	SourceTableName string
}

func (l *SpiltMainTableLog) Content() string {
	var msg string
	msg += fmt.Sprintf("来源表:%v", l.SourceTableName)
	msg += l.makeMsg(" 删除字段", l.Delete)
	msg += l.makeMsg(" 保留字段", l.Reserve)
	msg += l.makeMsg(" 添加字段", l.Add)
	return msg
}

func (l *SpiltMainTableLog) makeMsg(title string, fields []*domain.Field) string {
	if len(l.fieldNames(fields)) > 0 {
		return fmt.Sprintf("%s: %s ", title, strings.Join(l.fieldNames(fields), "、"))
	}
	return ""
}

func (l *SpiltMainTableLog) fieldNames(fields []*domain.Field) []string {
	names := make([]string, 0)
	for _, f := range fields {
		names = append(names, f.Name)
	}
	return names
}

// SubTableEditLog 5.分表编辑
type SubTableEditLog struct {
	domain.LogEntry

	Reserve []*domain.Field
	Delete  []*domain.Field
	Add     []*domain.Field
}

func (l *SubTableEditLog) Content() string {
	var msg string
	msg = "分表编辑 "
	msg += l.makeMsg("删除字段", l.Delete)
	msg += l.makeMsg("保留字段", l.Reserve)
	msg += l.makeMsg("添加字段", l.Add)
	return msg
}

func (l *SubTableEditLog) makeMsg(title string, fields []*domain.Field) string {
	if len(l.fieldNames(fields)) > 0 {
		return fmt.Sprintf("%s: %s ", title, strings.Join(l.fieldNames(fields), "、"))
	}
	return ""
}

func (l *SubTableEditLog) fieldNames(fields []*domain.Field) []string {
	names := make([]string, 0)
	for _, f := range fields {
		names = append(names, f.Name)
	}
	return names
}

// CopyTableLog 6.表复制日志
type CopyTableLog struct {
	domain.LogEntry
	// 表名
	SourceTableName string
}

func (l *CopyTableLog) Content() string {
	msg := fmt.Sprintf("来源表:%v", l.SourceTableName)
	return msg
}

// RowAddLog 7.编辑记录
type RowAddLog struct {
	domain.LogEntry
}

func (l *RowAddLog) Content() string {
	msg := fmt.Sprintf("新增行数据")
	return msg
}

type RowUpdateLog struct {
	domain.LogEntry
	FieldValue []*domain.FieldValue
	Where      domain.Where
	Number     int
}

func (l *RowUpdateLog) Content() string {
	change := ""
	for _, f := range l.FieldValue {
		if f.OldValue != f.Value {
			change += fmt.Sprintf("【%v】字段的值从“%v”更改为“%v”;", f.Field.Name, f.OldValue, f.Value)
		}
	}
	if len(change) == 0 {
		return "更新数据内容"
	}
	msg := fmt.Sprintf("更改数据内容:%v", change)
	return msg
}

type RowRemoveLog struct {
	domain.LogEntry
	DeleteRowCount int
	Where          domain.Where
}

func (l *RowRemoveLog) Content() string {
	index := l.DeleteRowCount
	msg := fmt.Sprintf("删除%v行数据;", index)
	filters := make([]string, 0)
	inArgs := func(args []string) string {
		return strings.Join(args, "、")
	}
	for _, c := range l.Where.Conditions {
		if len(c.In) > 0 {
			filters = append(filters, fmt.Sprintf("【%v】 包含 %v", c.Field.Name, inArgs(starrocks.ArrayInterfaceToString(c.In))))
		}
		if len(c.Ex) > 0 {
			filters = append(filters, fmt.Sprintf("【%v】 不包含 %v", c.Field.Name, inArgs(starrocks.ArrayInterfaceToString(c.Ex))))
		}
	}
	if len(filters) > 0 {
		msg += "筛选件:" + strings.Join(filters, "|")
	}
	return msg
}

// DeleteTableLog 8.表删除日志
type DeleteTableLog struct {
	domain.LogEntry
	// 表名
	SourceTableName string
	RowCount        int
	SubTables       []*domain.Table
}

func (l *DeleteTableLog) Content() string {
	msg := fmt.Sprintf("共计%v条数据", l.RowCount)
	var tables []string
	for _, t := range l.SubTables {
		tables = append(tables, t.Name+"分表")
	}
	if len(tables) > 0 {
		msg += fmt.Sprintf(",(存在分表)同步删除%s", strings.Join(tables, "/"))
	}
	return msg
}

// AppendDataToTableLog 9.数据追加日志
type AppendDataToTableLog struct {
	domain.LogEntry
	Table     *domain.Table
	File      *domain.File
	RowCount  int
	SubTables []*domain.Table
}

func (l *AppendDataToTableLog) Content() string {
	msg := fmt.Sprintf("来源文件:%v校验文件,导入成功%v条,目标表单:%v", l.File.FileInfo.Name, l.RowCount, l.Table.Name)
	var tables []string
	for _, t := range l.SubTables {
		tables = append(tables, t.Name+"分表")
	}
	if len(tables) > 0 {
		msg += fmt.Sprintf(",关联更新%s", strings.Join(tables, "/"))
	}
	return msg
}

/* *********************************************步骤日志************************************************** */

type ExcelTableEditLog struct {
	domain.LogEntry
	// 操作名称
	OperateName string
	// 操作列
	ProcessFields []*domain.Field
}

func (l *ExcelTableEditLog) Content() string {
	fieldsName := make([]string, 0)
	for _, f := range l.ProcessFields {
		fieldsName = append(fieldsName, fmt.Sprintf("【%v】", f.Name))
	}
	msg := fmt.Sprintf("%v:%v", l.OperateName, strings.Join(fieldsName, "、"))
	return msg
}

/* *********************************************拆解模块************************************************** */

type CreateQuerySetLog struct {
	domain.LogEntry
	Qs *domain.QuerySet
}

func (l *CreateQuerySetLog) OperateType() string {
	if l.Qs.Type == domain.SchemaTable.ToString() && l.Qs.Flag == domain.FlagSet {
		return domain.CreateSchema.ToString()
	}
	if l.Qs.Type == domain.SubProcessTable.ToString() && l.Qs.Flag == domain.FlagSet {
		return domain.CreateSubProcess.ToString()
	}
	if l.Qs.Type == domain.CalculateItem.ToString() && l.Qs.Flag == domain.FlagSet {
		return domain.CreateFormula.ToString()
	}
	if l.Qs.Type == domain.CalculateTable.ToString() && l.Qs.Flag == domain.FlagSet {
		return domain.CreateFormula.ToString()
	}
	if l.Qs.Type == domain.CalculateSet.ToString() && l.Qs.Flag == domain.FlagSet {
		return domain.CreateFormula.ToString()
	}
	return domain.UnKnown.ToString()
}

func (l *CreateQuerySetLog) LogType() string {
	if l.Qs.Type == domain.SchemaTable.ToString() {
		return domain.QuerySetLog.ToString()
	}
	if l.Qs.Type == domain.SubProcessTable.ToString() {
		return domain.QuerySetLog.ToString()
	}
	if l.Qs.Type == domain.CalculateTable.ToString() {
		return domain.QuerySetLog.ToString()
	}
	return domain.FormulaLog.ToString()
}

func (l *CreateQuerySetLog) Content() string {
	return "新增成功"
}

type RenameQuerySetLog struct {
	Qs *domain.QuerySet
	domain.LogEntry
	OldName string
	NewName string
}

func (l *RenameQuerySetLog) OperateType() string {
	return domain.RenameQuerySet.ToString()
}

func (l *RenameQuerySetLog) LogType() string {
	if l.Qs.Type == domain.SchemaTable.ToString() {
		return domain.QuerySetLog.ToString()
	}
	if l.Qs.Type == domain.SubProcessTable.ToString() {
		return domain.QuerySetLog.ToString()
	}
	if l.Qs.Type == domain.CalculateTable.ToString() {
		return domain.QuerySetLog.ToString()
	}
	return domain.FormulaLog.ToString()
}

func (l *RenameQuerySetLog) Content() string {
	return fmt.Sprintf(`"%s"重命名为"%v"`, l.OldName, l.NewName)
}

type DeleteQuerySetLog struct {
	domain.LogEntry
	DeleteList []*domain.QuerySet
}

func (l *DeleteQuerySetLog) OperateType() string {
	if len(l.DeleteList) == 0 {
		return domain.UnKnown.ToString()
	}
	return domain.DeleteQuerySet.ToString()
}

func (l *DeleteQuerySetLog) LogType() string {
	if len(l.DeleteList) == 0 {
		return ""
	}
	qs := l.DeleteList[0]
	if qs.Type == domain.SchemaTable.ToString() || qs.Type == domain.SubProcessTable.ToString() {
		return domain.QuerySetLog.ToString()
	}
	if qs.Type == domain.CalculateTable.ToString() {
		return domain.QuerySetLog.ToString()
	}
	return domain.FormulaLog.ToString()
}

func (l *DeleteQuerySetLog) Content() string {
	names := make([]string, 0)
	for i := range l.DeleteList {
		names = append(names, "\""+l.DeleteList[i].Name+"\"")
	}
	t := domain.EnumsDescription(domain.ObjectTypeMap, l.DeleteList[0].Type)
	if l.DeleteList[0].Flag == domain.FlagGroup {
		t += "分组"
	}
	return fmt.Sprintf(`%s%s删除成功`, t, strings.Join(names, "、"))
}

type CopyQuerySetLog struct {
	domain.LogEntry
	From *domain.QuerySet
	To   *domain.QuerySet
}

func (l *CopyQuerySetLog) OperateType() string {
	return domain.CopyQuerySet.ToString()
}

func (l *CopyQuerySetLog) LogType() string {
	qs := l.From
	if qs.Type == domain.SchemaTable.ToString() || qs.Type == domain.SubProcessTable.ToString() {
		return domain.QuerySetLog.ToString()
	}
	if qs.Type == domain.CalculateTable.ToString() {
		return domain.QuerySetLog.ToString()
	}
	return domain.FormulaLog.ToString()
}

func (l *CopyQuerySetLog) Content() string {
	if l.LogType() == domain.QuerySetLog.ToString() {
		return fmt.Sprintf(`%s"%s"复制为%s"%s"`, domain.EnumsDescription(domain.ObjectTypeMap, l.From.Type), l.From.Name,
			domain.EnumsDescription(domain.ObjectTypeMap, l.To.Type), l.To.Name)
	}
	return fmt.Sprintf(`"%s"复制为"%s"`, l.From.Name, l.To.Name)
}

type EditQuerySetConditionLog struct {
	domain.LogEntry
	OperationType domain.OperationType
	Sources       []string
	SourceTargets [][]string
}

func (l *EditQuerySetConditionLog) OperateType() string {
	return l.OperationType.ToString()
}

func (l *EditQuerySetConditionLog) Content() string {
	if l.OperationType == domain.AddSetCondition {
		return fmt.Sprintf("新增条件:%v", strings.Join(l.Sources, ";"))
	}
	if l.OperationType == domain.EditSetCondition {
		items := make([]string, 0)
		for _, sourceTarget := range l.SourceTargets {
			items = append(items, fmt.Sprintf("%v 修改为 %v", sourceTarget[0], sourceTarget[1]))
		}
		return fmt.Sprintf("编辑条件:%v", strings.Join(items, ";"))
	}
	return "删除条件:" + strings.Join(l.Sources, ";")
}

type EditSelectConditionLog struct {
	domain.LogEntry
	OperationType domain.OperationType // 编辑类型 1:add 2.edit 3.delete
	Sources       []string
	SourceTargets [][]string
}

func (l *EditSelectConditionLog) OperateType() string {
	return l.OperationType.ToString()
}

func (l *EditSelectConditionLog) Content() string {
	if l.OperationType == domain.AddSelectCondition {
		return fmt.Sprintf("新增拆分规则:%v", strings.Join(l.Sources, ";"))
	}
	if l.OperationType == domain.EditSelectCondition {
		items := make([]string, 0)
		for _, sourceTarget := range l.SourceTargets {
			items = append(items, fmt.Sprintf("%v 修改为 %v", sourceTarget[0], sourceTarget[1]))
		}
		return fmt.Sprintf("编辑拆分规则:%v", strings.Join(items, ";"))
	}
	return "删除拆分规则:" + strings.Join(l.Sources, ";")
}

type EditFormulaLog struct {
	domain.LogEntry
	OperationType domain.OperationType // 编辑类型 1:add 2.edit 3.delete
	Old           string
	New           string
	Msg           string
}

func (l *EditFormulaLog) OperateType() string {
	return l.OperationType.ToString()
}

func (l *EditFormulaLog) Content() string {
	if len(l.Msg) > 0 {
		return l.Msg
	}
	if len(l.Old) == 0 {
		return fmt.Sprintf("修改为 %v", l.New)
	}
	return fmt.Sprintf("%v 修改为 %v", l.Old, l.New)
}