作者 庄敏学

同步数据表

@@ -2,14 +2,15 @@ package main @@ -2,14 +2,15 @@ package main
2 2
3 import ( 3 import (
4 "flag" 4 "flag"
  5 + "github.com/golang-jwt/jwt/v4/request"
  6 + "github.com/zeromicro/go-queue/kq"
5 "github.com/zeromicro/go-zero/core/logx" 7 "github.com/zeromicro/go-zero/core/logx"
  8 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/logic/consumer"
6 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/db" 9 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/db"
7 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/domain" 10 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/domain"
8 "net/http" 11 "net/http"
9 "strings" 12 "strings"
10 13
11 - "github.com/golang-jwt/jwt/v4/request"  
12 -  
13 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/config" 14 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/config"
14 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/handler" 15 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/handler"
15 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/svc" 16 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/svc"
@@ -56,7 +57,49 @@ func main() { @@ -56,7 +57,49 @@ func main() {
56 handler.RegisterHandlers(server, ctx) 57 handler.RegisterHandlers(server, ctx)
57 58
58 db.Migrate(ctx.DB) 59 db.Migrate(ctx.DB)
  60 + //启动消费队列
  61 + go startConsume(c)
59 62
60 logx.Infof("Starting server at %s:%d... \n", c.Host, c.Port) 63 logx.Infof("Starting server at %s:%d... \n", c.Host, c.Port)
61 server.Start() 64 server.Start()
62 } 65 }
  66 +
  67 +func startConsume(c config.Config) {
  68 + svcCtx := svc.NewServiceContext(c)
  69 + go func() {
  70 + //for {
  71 + // notice := &domain.ObjectNotice{
  72 + // CompanyId: 1594869884284571648,
  73 + // TableId: 1573,
  74 + // TableType: "主表",
  75 + // ObjectType: "导入模块",
  76 + // Event: "table.data.edit",
  77 + // TableAffectedList: []int{1573},
  78 + // DataChanged: true,
  79 + // StructChanged: true,
  80 + // MetaData: domain.ObjectNoticeMetaData{
  81 + // Module: 0,
  82 + // Status: 0,
  83 + // },
  84 + // }
  85 + // mBytes, _ := json.Marshal(notice)
  86 + // err := kq.NewPusher(c.KqConsumerConf.Brokers, c.KqConsumerConf.Topic).Push(string(mBytes))
  87 + // fmt.Println(err)
  88 + // time.Sleep(10 * 10 * time.Second)
  89 + //}
  90 + }()
  91 + go func() {
  92 +
  93 + }()
  94 + //kq.MustNewQueue(c.KqConsumerConf, consumer.NewByteNoticeLogic(svcCtx))
  95 + //for {
  96 + queue, err := kq.NewQueue(c.KqConsumerConf, consumer.NewByteNoticeLogic(svcCtx))
  97 + if err != nil {
  98 + panic(err)
  99 + } else {
  100 + queue.Start()
  101 + }
  102 + //time.Sleep(1 * time.Second)
  103 + //}
  104 +
  105 +}
@@ -26,4 +26,12 @@ ByteMetadata: @@ -26,4 +26,12 @@ ByteMetadata:
26 Name: ApiByteMetadata 26 Name: ApiByteMetadata
27 Host1: http://127.0.0.1:8080 27 Host1: http://127.0.0.1:8080
28 Host: http://character-library-metadata-bastion-test.fjmaimaimai.com 28 Host: http://character-library-metadata-bastion-test.fjmaimaimai.com
29 - Timeout: 0s  
  29 + Timeout: 0s
  30 +
  31 +KqConsumerConf:
  32 + Name: KqConsumer
  33 + Brokers:
  34 + - 192.168.100.221:9092
  35 + Group: sumifcc
  36 + Topic: sumifcc
  37 + Processors: 1
1 package config 1 package config
2 2
3 import ( 3 import (
  4 + "github.com/zeromicro/go-queue/kq"
4 "github.com/zeromicro/go-zero/core/stores/redis" 5 "github.com/zeromicro/go-zero/core/stores/redis"
5 "github.com/zeromicro/go-zero/rest" 6 "github.com/zeromicro/go-zero/rest"
6 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/pkg/config" 7 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/pkg/config"
@@ -10,9 +11,10 @@ import ( @@ -10,9 +11,10 @@ import (
10 type Config struct { 11 type Config struct {
11 rest.RestConf 12 rest.RestConf
12 config.Config 13 config.Config
13 - Redis redis.RedisConf `json:",optional"`  
14 - ByteMetadata ApiService  
15 - HostName string // 服务域名 14 + Redis redis.RedisConf `json:",optional"`
  15 + ByteMetadata ApiService
  16 + HostName string // 服务域名
  17 + KqConsumerConf kq.KqConf
16 } 18 }
17 19
18 type ApiService struct { 20 type ApiService struct {
  1 +package consumer
  2 +
  3 +import (
  4 + "context"
  5 + "encoding/json"
  6 + "fmt"
  7 + "github.com/zeromicro/go-zero/core/stores/redis"
  8 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/svc"
  9 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/types"
  10 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/db/transaction"
  11 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/domain"
  12 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/gateway/bytelib"
  13 +)
  14 +
  15 +type ByteNoticeLogic struct {
  16 + ctx context.Context
  17 + svcCtx *svc.ServiceContext
  18 + conn transaction.Conn
  19 +}
  20 +
  21 +func NewByteNoticeLogic(svcCtx *svc.ServiceContext) *ByteNoticeLogic {
  22 + return &ByteNoticeLogic{
  23 + ctx: context.Background(),
  24 + svcCtx: svcCtx,
  25 + conn: svcCtx.DefaultDBConn(),
  26 + }
  27 +}
  28 +
  29 +func (logic *ByteNoticeLogic) Consume(key, value string) error {
  30 + fmt.Println(key, value)
  31 + notice := &domain.ObjectNotice{}
  32 + err := json.Unmarshal([]byte(value), notice)
  33 + if err != nil {
  34 + return err
  35 + }
  36 + //保存推送消息
  37 + _, err = logic.svcCtx.ObjectNoticeRepository.Insert(logic.ctx, logic.conn, notice)
  38 + if err != nil {
  39 + return err
  40 + }
  41 + //处理消息
  42 + err = logic.handleNotice(notice)
  43 + //更新处理结果
  44 + if err != nil {
  45 + notice.Status = domain.ObjectNoticeStatusError
  46 + notice.Message = err.Error()
  47 + } else {
  48 + notice.Status = domain.ObjectNoticeStatusDone
  49 + notice.Message = "OK"
  50 + }
  51 + _, _ = logic.svcCtx.ObjectNoticeRepository.Update(logic.ctx, logic.conn, notice)
  52 + return err
  53 +}
  54 +
  55 +// handleNotice 处理消息
  56 +func (logic *ByteNoticeLogic) handleNotice(notice *domain.ObjectNotice) error {
  57 + //是否删除消息
  58 + if notice.IsDeletedEvent() {
  59 + return logic.handleDelete(notice)
  60 + }
  61 + accessToken, _ := types.TableAccessToken{CompanyId: notice.CompanyId}.GenerateToken()
  62 + //结构变更
  63 + if notice.StructChanged {
  64 + request := bytelib.ObjectTableSearchRequest{
  65 + Token: accessToken,
  66 + Module: bytelib.ModuleDigitalCenter,
  67 + }
  68 + if notice.ObjectType == "导入模块" {
  69 + request.TableTypes = []string{bytelib.MainTable, bytelib.SubTable, bytelib.SideTable}
  70 + }
  71 + if notice.ObjectType == "拆解模块" {
  72 + request.TableTypes = []string{bytelib.SchemaTable}
  73 + request.ReturnGroupItem = true
  74 + }
  75 + if notice.ObjectType == "计算模块" {
  76 + request.TableTypes = []string{bytelib.CalculateItem, bytelib.CalculateSet}
  77 + request.ReturnGroupItem = true
  78 + request.ExcludeTables = []int{0}
  79 + }
  80 + list, err := logic.svcCtx.ByteMetadataService.ObjectTableSearch(logic.ctx, request)
  81 + if err != nil {
  82 + return err
  83 + }
  84 + tableInfo, err := logic.svcCtx.ByteMetadataService.TableInfo(logic.ctx, &bytelib.TableInfoRequest{
  85 + Token: accessToken,
  86 + TableId: notice.TableId,
  87 + })
  88 + if err != nil {
  89 + return nil
  90 + }
  91 + if len(list.List) > 0 {
  92 + objectTables := make([]*domain.ObjectTable, 0)
  93 + for _, item := range list.List {
  94 + if item.TableId == notice.TableId {
  95 + objectTables = append(objectTables, &domain.ObjectTable{
  96 + Id: item.Id,
  97 + TableId: item.TableId,
  98 + Name: item.Name,
  99 + TableType: item.TableType,
  100 + CompanyId: notice.CompanyId,
  101 + ParentId: item.ParentId,
  102 + Flag: item.Flag,
  103 + Fields: tableInfo.Fields,
  104 + })
  105 + //父级节点
  106 + objectTables = append(objectTables, logic.getParents(notice.CompanyId, item.ParentId, list.List)...)
  107 + }
  108 + }
  109 + err = logic.saveTables(objectTables)
  110 + if err != nil {
  111 + return err
  112 + }
  113 + }
  114 + }
  115 + //数据变更
  116 + if notice.DataChanged {
  117 + tableDataPreview, err := logic.svcCtx.ByteMetadataService.TableDataPreview(logic.ctx, &bytelib.TableDataPreviewRequest{
  118 + Token: accessToken,
  119 + ObjectId: int64(notice.TableId),
  120 + ObjectType: bytelib.ObjectMetaTable,
  121 + Where: &bytelib.TableQueryWhere{
  122 + PageNumber: 1,
  123 + PageSize: bytelib.MaxPageSize,
  124 + },
  125 + UseCache: true,
  126 + HiddenData: false,
  127 + })
  128 + if err != nil {
  129 + return err
  130 + }
  131 + //加锁,避免图表在请求读取本地数据时报错
  132 + key := logic.svcCtx.Config.Name + ":bytelib:" + fmt.Sprintf("%v", tableDataPreview.ObjectId)
  133 + lock := redis.NewRedisLock(logic.svcCtx.Redis, key)
  134 + // 设置过期时间
  135 + lock.SetExpire(10 * 60)
  136 + acquire, err := lock.Acquire()
  137 + fmt.Println(acquire, err)
  138 + defer lock.Release()
  139 + err = transaction.UseTrans(logic.ctx, logic.conn.DB(), func(ctx context.Context, conn transaction.Conn) error {
  140 + return logic.svcCtx.ObjectTableDataRepository.InsertWithTableData(logic.ctx, conn, bytelib.TableData(tableDataPreview))
  141 + }, true)
  142 + fmt.Println(err)
  143 + }
  144 + return nil
  145 +}
  146 +
  147 +func (logic *ByteNoticeLogic) getParents(companyId int64, parentId int, list []*bytelib.Table) []*domain.ObjectTable {
  148 + result := make([]*domain.ObjectTable, 0)
  149 + for _, item := range list {
  150 + if item.Id == parentId {
  151 + result = append(result, &domain.ObjectTable{
  152 + Id: item.Id,
  153 + TableId: item.TableId,
  154 + Name: item.Name,
  155 + TableType: item.TableType,
  156 + CompanyId: companyId,
  157 + ParentId: item.ParentId,
  158 + Flag: item.Flag,
  159 + Fields: item.Fields,
  160 + })
  161 + if item.ParentId > 0 {
  162 + result = append(result, logic.getParents(companyId, item.ParentId, list)...)
  163 + }
  164 + }
  165 + }
  166 + return result
  167 +}
  168 +
  169 +// handleDelete 删除
  170 +func (logic *ByteNoticeLogic) handleDelete(notice *domain.ObjectNotice) error {
  171 + objectTable, err := logic.svcCtx.ObjectTableRepository.FindOneByTableId(logic.ctx, logic.conn, notice.TableId)
  172 + if err == nil && objectTable.Id > 0 {
  173 + objectTable.RemoteDeleted = 1
  174 + _, err := logic.svcCtx.ObjectTableRepository.Update(logic.ctx, logic.conn, objectTable)
  175 + if err != nil {
  176 + return err
  177 + }
  178 + }
  179 + return nil
  180 +}
  181 +
  182 +// saveTables 保存表结构
  183 +func (logic *ByteNoticeLogic) saveTables(tables []*domain.ObjectTable) error {
  184 + err := transaction.UseTrans(logic.ctx, logic.conn.DB(), func(ctx context.Context, conn transaction.Conn) error {
  185 + if len(tables) > 0 {
  186 + for _, item := range tables {
  187 + objectTable, err := logic.svcCtx.ObjectTableRepository.FindOne(logic.ctx, conn, item.Id)
  188 + if err == nil && objectTable.Id > 0 {
  189 + item.Id = objectTable.Id
  190 + _, err := logic.svcCtx.ObjectTableRepository.Update(logic.ctx, conn, item)
  191 + if err != nil {
  192 + return err
  193 + }
  194 + } else {
  195 + _, err := logic.svcCtx.ObjectTableRepository.Insert(logic.ctx, conn, item)
  196 + if err != nil {
  197 + return err
  198 + }
  199 + }
  200 + }
  201 + }
  202 + return nil
  203 + }, true)
  204 + return err
  205 +}
@@ -15,13 +15,16 @@ import ( @@ -15,13 +15,16 @@ import (
15 ) 15 )
16 16
17 type ServiceContext struct { 17 type ServiceContext struct {
18 - Config config.Config  
19 - DB *gorm.DB  
20 - Redis *redis.Redis  
21 - RedisCache gzcache.GZCache  
22 - ChartRepository domain.ChartRepository  
23 - ChartSettingRepository domain.ChartSettingRepository  
24 - AppPageRepository domain.AppPageRepository 18 + Config config.Config
  19 + DB *gorm.DB
  20 + Redis *redis.Redis
  21 + RedisCache gzcache.GZCache
  22 + ChartRepository domain.ChartRepository
  23 + ChartSettingRepository domain.ChartSettingRepository
  24 + AppPageRepository domain.AppPageRepository
  25 + ObjectNoticeRepository domain.ObjectNoticeRepository
  26 + ObjectTableRepository domain.ObjectTableRepository
  27 + ObjectTableDataRepository domain.ObjectTableDataRepository
25 28
26 ByteMetadataService bytelib.ByteMetadataService 29 ByteMetadataService bytelib.ByteMetadataService
27 } 30 }
@@ -33,13 +36,16 @@ func NewServiceContext(c config.Config) *ServiceContext { @@ -33,13 +36,16 @@ func NewServiceContext(c config.Config) *ServiceContext {
33 redis, _ := redis.NewRedis(redis.RedisConf{Host: c.Redis.Host, Pass: c.Redis.Pass, Type: "node"}) 36 redis, _ := redis.NewRedis(redis.RedisConf{Host: c.Redis.Host, Pass: c.Redis.Pass, Type: "node"})
34 37
35 return &ServiceContext{ 38 return &ServiceContext{
36 - Config: c,  
37 - DB: db,  
38 - RedisCache: redisCache,  
39 - Redis: redis,  
40 - ChartRepository: repository.NewChartRepository(cache.NewCachedRepository(mlCache)),  
41 - ChartSettingRepository: repository.NewChartSettingRepository(cache.NewCachedRepository(mlCache)),  
42 - AppPageRepository: repository.NewAppPageRepository(cache.NewCachedRepository(mlCache)), 39 + Config: c,
  40 + DB: db,
  41 + RedisCache: redisCache,
  42 + Redis: redis,
  43 + ChartRepository: repository.NewChartRepository(cache.NewCachedRepository(mlCache)),
  44 + ChartSettingRepository: repository.NewChartSettingRepository(cache.NewCachedRepository(mlCache)),
  45 + AppPageRepository: repository.NewAppPageRepository(cache.NewCachedRepository(mlCache)),
  46 + ObjectNoticeRepository: repository.NewObjectNoticeRepository(cache.NewCachedRepository(mlCache)),
  47 + ObjectTableRepository: repository.NewObjectTableRepository(cache.NewCachedRepository(mlCache)),
  48 + ObjectTableDataRepository: repository.NewObjectTableDataRepository(),
43 49
44 ByteMetadataService: bytelib.ByteMetadataService{ 50 ByteMetadataService: bytelib.ByteMetadataService{
45 Service: gateway.NewService(c.ByteMetadata.Name, c.ByteMetadata.Host, c.ByteMetadata.Timeout), 51 Service: gateway.NewService(c.ByteMetadata.Name, c.ByteMetadata.Host, c.ByteMetadata.Timeout),
1 package types 1 package types
  2 +
  3 +import (
  4 + "github.com/golang-jwt/jwt/v4"
  5 + "time"
  6 +)
  7 +
  8 +type TableAccessToken struct {
  9 + UserId int64 `json:"userId"`
  10 + CompanyId int64 `json:"companyId"`
  11 + UserName string `json:"userName"`
  12 + Phone string `json:"phone"`
  13 +}
  14 +
  15 +func (tk TableAccessToken) GenerateToken() (string, error) {
  16 + claims := make(jwt.MapClaims)
  17 + claims["exp"] = time.Now().Unix() + 3600
  18 + claims["iat"] = time.Now().Unix()
  19 + claims["UserId"] = tk.UserId
  20 + claims["CompanyId"] = tk.CompanyId
  21 + claims["UserName"] = tk.UserName
  22 + claims["Phone"] = tk.Phone
  23 + token := jwt.New(jwt.SigningMethodHS256)
  24 + token.Claims = claims
  25 + return token.SignedString([]byte("digital-platform"))
  26 +}
  1 +
  2 +syntax = "v1"
  3 +
  4 +info(
  5 + title: "xx实例"
  6 + desc: "xx实例"
  7 + author: "author"
  8 + email: "email"
  9 + version: "v1"
  10 +)
  11 +
  12 +@server(
  13 + prefix: object_field/v1
  14 + group: object_field
  15 + jwt: JwtAuth
  16 +)
  17 +service Core {
  18 + @handler getObjectField
  19 + post /object_field/:id (ObjectFieldGetRequest) returns (ObjectFieldGetResponse)
  20 + @handler saveObjectField
  21 + post /object_field (ObjectFieldSaveRequest) returns (ObjectFieldSaveResponse)
  22 + @handler deleteObjectField
  23 + delete /object_field/:id (ObjectFieldDeleteRequest) returns (ObjectFieldDeleteResponse)
  24 + @handler updateObjectField
  25 + put /object_field/:id (ObjectFieldUpdateRequest) returns (ObjectFieldUpdateResponse)
  26 + @handler searchObjectField
  27 + post /object_field/search (ObjectFieldSearchRequest) returns (ObjectFieldSearchResponse)
  28 +}
  29 +
  30 +type (
  31 + ObjectFieldGetRequest {
  32 + Id int64 `path:"id"`
  33 + }
  34 + ObjectFieldGetResponse struct{
  35 + ObjectField ObjectFieldItem `json:"object_field"`
  36 + }
  37 +
  38 + ObjectFieldSaveRequest struct{
  39 + ObjectField ObjectFieldItem `json:"object_field"`
  40 + }
  41 + ObjectFieldSaveResponse struct{}
  42 +
  43 + ObjectFieldDeleteRequest struct{
  44 + Id int64 `path:"id"`
  45 + }
  46 + ObjectFieldDeleteResponse struct{}
  47 +
  48 + ObjectFieldUpdateRequest struct{
  49 + Id int64 `path:"id"`
  50 + ObjectField ObjectFieldItem `json:"object_field"`
  51 + }
  52 + ObjectFieldUpdateResponse struct{}
  53 +
  54 + ObjectFieldSearchRequest struct{
  55 + Page int `json:"page"`
  56 + Size int `json:"size"`
  57 + }
  58 + ObjectFieldSearchResponse{
  59 + List []ObjectFieldItem `json:"list"`
  60 + Total int64 `json:"total"`
  61 + }
  62 + ObjectFieldItem struct{
  63 +
  64 + }
  65 +)
  1 +
  2 +syntax = "v1"
  3 +
  4 +info(
  5 + title: "xx实例"
  6 + desc: "xx实例"
  7 + author: "author"
  8 + email: "email"
  9 + version: "v1"
  10 +)
  11 +
  12 +@server(
  13 + prefix: object_notice/v1
  14 + group: object_notice
  15 + jwt: JwtAuth
  16 +)
  17 +service Core {
  18 + @handler getObjectNotice
  19 + post /object_notice/:id (ObjectNoticeGetRequest) returns (ObjectNoticeGetResponse)
  20 + @handler saveObjectNotice
  21 + post /object_notice (ObjectNoticeSaveRequest) returns (ObjectNoticeSaveResponse)
  22 + @handler deleteObjectNotice
  23 + delete /object_notice/:id (ObjectNoticeDeleteRequest) returns (ObjectNoticeDeleteResponse)
  24 + @handler updateObjectNotice
  25 + put /object_notice/:id (ObjectNoticeUpdateRequest) returns (ObjectNoticeUpdateResponse)
  26 + @handler searchObjectNotice
  27 + post /object_notice/search (ObjectNoticeSearchRequest) returns (ObjectNoticeSearchResponse)
  28 +}
  29 +
  30 +type (
  31 + ObjectNoticeGetRequest {
  32 + Id int64 `path:"id"`
  33 + }
  34 + ObjectNoticeGetResponse struct{
  35 + ObjectNotice ObjectNoticeItem `json:"object_notice"`
  36 + }
  37 +
  38 + ObjectNoticeSaveRequest struct{
  39 + ObjectNotice ObjectNoticeItem `json:"object_notice"`
  40 + }
  41 + ObjectNoticeSaveResponse struct{}
  42 +
  43 + ObjectNoticeDeleteRequest struct{
  44 + Id int64 `path:"id"`
  45 + }
  46 + ObjectNoticeDeleteResponse struct{}
  47 +
  48 + ObjectNoticeUpdateRequest struct{
  49 + Id int64 `path:"id"`
  50 + ObjectNotice ObjectNoticeItem `json:"object_notice"`
  51 + }
  52 + ObjectNoticeUpdateResponse struct{}
  53 +
  54 + ObjectNoticeSearchRequest struct{
  55 + Page int `json:"page"`
  56 + Size int `json:"size"`
  57 + }
  58 + ObjectNoticeSearchResponse{
  59 + List []ObjectNoticeItem `json:"list"`
  60 + Total int64 `json:"total"`
  61 + }
  62 + ObjectNoticeItem struct{
  63 +
  64 + }
  65 +)
  1 +
  2 +syntax = "v1"
  3 +
  4 +info(
  5 + title: "xx实例"
  6 + desc: "xx实例"
  7 + author: "author"
  8 + email: "email"
  9 + version: "v1"
  10 +)
  11 +
  12 +@server(
  13 + prefix: object_table/v1
  14 + group: object_table
  15 + jwt: JwtAuth
  16 +)
  17 +service Core {
  18 + @handler getObjectTable
  19 + post /object_table/:id (ObjectTableGetRequest) returns (ObjectTableGetResponse)
  20 + @handler saveObjectTable
  21 + post /object_table (ObjectTableSaveRequest) returns (ObjectTableSaveResponse)
  22 + @handler deleteObjectTable
  23 + delete /object_table/:id (ObjectTableDeleteRequest) returns (ObjectTableDeleteResponse)
  24 + @handler updateObjectTable
  25 + put /object_table/:id (ObjectTableUpdateRequest) returns (ObjectTableUpdateResponse)
  26 + @handler searchObjectTable
  27 + post /object_table/search (ObjectTableSearchRequest) returns (ObjectTableSearchResponse)
  28 +}
  29 +
  30 +type (
  31 + ObjectTableGetRequest {
  32 + Id int64 `path:"id"`
  33 + }
  34 + ObjectTableGetResponse struct{
  35 + ObjectTable ObjectTableItem `json:"object_table"`
  36 + }
  37 +
  38 + ObjectTableSaveRequest struct{
  39 + ObjectTable ObjectTableItem `json:"object_table"`
  40 + }
  41 + ObjectTableSaveResponse struct{}
  42 +
  43 + ObjectTableDeleteRequest struct{
  44 + Id int64 `path:"id"`
  45 + }
  46 + ObjectTableDeleteResponse struct{}
  47 +
  48 + ObjectTableUpdateRequest struct{
  49 + Id int64 `path:"id"`
  50 + ObjectTable ObjectTableItem `json:"object_table"`
  51 + }
  52 + ObjectTableUpdateResponse struct{}
  53 +
  54 + ObjectTableSearchRequest struct{
  55 + Page int `json:"page"`
  56 + Size int `json:"size"`
  57 + }
  58 + ObjectTableSearchResponse{
  59 + List []ObjectTableItem `json:"list"`
  60 + Total int64 `json:"total"`
  61 + }
  62 + ObjectTableItem struct{
  63 +
  64 + }
  65 +)
  1 +
  2 +syntax = "proto3";
  3 +
  4 +option go_package ="./pb";
  5 +
  6 +package pb;
  7 +
  8 +message ObjectFieldGetReq {
  9 + int64 Id = 1;
  10 +}
  11 +message ObjectFieldGetResp{
  12 + ObjectFieldItem User = 1;
  13 +}
  14 +
  15 +message ObjectFieldSaveReq {
  16 +
  17 +}
  18 +message ObjectFieldSaveResp{
  19 +
  20 +}
  21 +
  22 +message ObjectFieldDeleteReq {
  23 + int64 Id = 1;
  24 +}
  25 +message ObjectFieldDeleteResp{
  26 +
  27 +}
  28 +
  29 +message ObjectFieldUpdateReq {
  30 + int64 Id = 1;
  31 +}
  32 +message ObjectFieldUpdateResp{
  33 +
  34 +}
  35 +
  36 +message ObjectFieldSearchReq {
  37 + int64 PageNumber = 1;
  38 + int64 PageSize = 2;
  39 +}
  40 +message ObjectFieldSearchResp{
  41 + repeated ObjectFieldItem List =1;
  42 + int64 Total =2;
  43 +}
  44 +message ObjectFieldItem {
  45 +
  46 +}
  47 +
  48 +service ObjectFieldService {
  49 + rpc ObjectFieldGet(ObjectFieldGetReq) returns(ObjectFieldGetResp);
  50 + rpc ObjectFieldSave(ObjectFieldSaveReq) returns(ObjectFieldSaveResp);
  51 + rpc ObjectFieldDelete(ObjectFieldDeleteReq) returns(ObjectFieldDeleteResp);
  52 + rpc ObjectFieldUpdate(ObjectFieldUpdateReq) returns(ObjectFieldUpdateResp);
  53 + rpc ObjectFieldSearch(ObjectFieldSearchReq) returns(ObjectFieldSearchResp);
  54 +}
  1 +
  2 +syntax = "proto3";
  3 +
  4 +option go_package ="./pb";
  5 +
  6 +package pb;
  7 +
  8 +message ObjectNoticeGetReq {
  9 + int64 Id = 1;
  10 +}
  11 +message ObjectNoticeGetResp{
  12 + ObjectNoticeItem User = 1;
  13 +}
  14 +
  15 +message ObjectNoticeSaveReq {
  16 +
  17 +}
  18 +message ObjectNoticeSaveResp{
  19 +
  20 +}
  21 +
  22 +message ObjectNoticeDeleteReq {
  23 + int64 Id = 1;
  24 +}
  25 +message ObjectNoticeDeleteResp{
  26 +
  27 +}
  28 +
  29 +message ObjectNoticeUpdateReq {
  30 + int64 Id = 1;
  31 +}
  32 +message ObjectNoticeUpdateResp{
  33 +
  34 +}
  35 +
  36 +message ObjectNoticeSearchReq {
  37 + int64 PageNumber = 1;
  38 + int64 PageSize = 2;
  39 +}
  40 +message ObjectNoticeSearchResp{
  41 + repeated ObjectNoticeItem List =1;
  42 + int64 Total =2;
  43 +}
  44 +message ObjectNoticeItem {
  45 +
  46 +}
  47 +
  48 +service ObjectNoticeService {
  49 + rpc ObjectNoticeGet(ObjectNoticeGetReq) returns(ObjectNoticeGetResp);
  50 + rpc ObjectNoticeSave(ObjectNoticeSaveReq) returns(ObjectNoticeSaveResp);
  51 + rpc ObjectNoticeDelete(ObjectNoticeDeleteReq) returns(ObjectNoticeDeleteResp);
  52 + rpc ObjectNoticeUpdate(ObjectNoticeUpdateReq) returns(ObjectNoticeUpdateResp);
  53 + rpc ObjectNoticeSearch(ObjectNoticeSearchReq) returns(ObjectNoticeSearchResp);
  54 +}
  1 +
  2 +syntax = "proto3";
  3 +
  4 +option go_package ="./pb";
  5 +
  6 +package pb;
  7 +
  8 +message ObjectTableGetReq {
  9 + int64 Id = 1;
  10 +}
  11 +message ObjectTableGetResp{
  12 + ObjectTableItem User = 1;
  13 +}
  14 +
  15 +message ObjectTableSaveReq {
  16 +
  17 +}
  18 +message ObjectTableSaveResp{
  19 +
  20 +}
  21 +
  22 +message ObjectTableDeleteReq {
  23 + int64 Id = 1;
  24 +}
  25 +message ObjectTableDeleteResp{
  26 +
  27 +}
  28 +
  29 +message ObjectTableUpdateReq {
  30 + int64 Id = 1;
  31 +}
  32 +message ObjectTableUpdateResp{
  33 +
  34 +}
  35 +
  36 +message ObjectTableSearchReq {
  37 + int64 PageNumber = 1;
  38 + int64 PageSize = 2;
  39 +}
  40 +message ObjectTableSearchResp{
  41 + repeated ObjectTableItem List =1;
  42 + int64 Total =2;
  43 +}
  44 +message ObjectTableItem {
  45 +
  46 +}
  47 +
  48 +service ObjectTableService {
  49 + rpc ObjectTableGet(ObjectTableGetReq) returns(ObjectTableGetResp);
  50 + rpc ObjectTableSave(ObjectTableSaveReq) returns(ObjectTableSaveResp);
  51 + rpc ObjectTableDelete(ObjectTableDeleteReq) returns(ObjectTableDeleteResp);
  52 + rpc ObjectTableUpdate(ObjectTableUpdateReq) returns(ObjectTableUpdateResp);
  53 + rpc ObjectTableSearch(ObjectTableSearchReq) returns(ObjectTableSearchResp);
  54 +}
@@ -10,5 +10,7 @@ func Migrate(db *gorm.DB) { @@ -10,5 +10,7 @@ func Migrate(db *gorm.DB) {
10 &models.Chart{}, 10 &models.Chart{},
11 &models.ChartSetting{}, 11 &models.ChartSetting{},
12 &models.AppPage{}, 12 &models.AppPage{},
  13 + &models.ObjectNotice{},
  14 + &models.ObjectTable{},
13 ) 15 )
14 } 16 }
  1 +package models
  2 +
  3 +import (
  4 + "fmt"
  5 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/domain"
  6 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/gateway/bytelib"
  7 + "gorm.io/gorm"
  8 + "gorm.io/plugin/soft_delete"
  9 + "time"
  10 +)
  11 +
  12 +type ObjectField struct {
  13 + Id int64 `json:"id" gorm:"primaryKey"` // ID
  14 + Name string `json:"name"` //表名
  15 + Fields []*bytelib.Field `json:"fields"` //表字段
  16 + Version int `json:",omitempty"` //版本
  17 + IsDel soft_delete.DeletedAt `gorm:"softDelete:flag,DeletedAtField:DeletedAt"` //删除标记
  18 + CreatedAt int64 `json:",omitempty"` //创建时间
  19 + UpdatedAt int64 `json:",omitempty"` //更新时间
  20 + DeletedAt int64 `json:",omitempty"` //删除时间
  21 +}
  22 +
  23 +func (m *ObjectField) TableName() string {
  24 + return "object_field"
  25 +}
  26 +
  27 +func (m *ObjectField) BeforeCreate(tx *gorm.DB) (err error) {
  28 + m.CreatedAt = time.Now().Unix()
  29 + m.UpdatedAt = time.Now().Unix()
  30 + return
  31 +}
  32 +
  33 +func (m *ObjectField) BeforeUpdate(tx *gorm.DB) (err error) {
  34 + m.UpdatedAt = time.Now().Unix()
  35 + return
  36 +}
  37 +
  38 +func (m *ObjectField) CacheKeyFunc() string {
  39 + if m.Id == 0 {
  40 + return ""
  41 + }
  42 + return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
  43 +}
  44 +
  45 +func (m *ObjectField) CacheKeyFuncByObject(obj interface{}) string {
  46 + if v, ok := obj.(*ObjectField); ok {
  47 + return v.CacheKeyFunc()
  48 + }
  49 + return ""
  50 +}
  51 +
  52 +func (m *ObjectField) CachePrimaryKeyFunc() string {
  53 + if len("") == 0 {
  54 + return ""
  55 + }
  56 + return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key")
  57 +}
  1 +package models
  2 +
  3 +import (
  4 + "fmt"
  5 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/domain"
  6 + "gorm.io/gorm"
  7 + "gorm.io/plugin/soft_delete"
  8 + "time"
  9 +)
  10 +
  11 +type ObjectNotice struct {
  12 + Id int64 `json:"id" gorm:"primaryKey"` // ID
  13 + CompanyId int64 `json:"companyId"` //公司ID
  14 + TableId int64 `json:"tableId"` //表ID
  15 + TableType string `json:"tableType"` //表类型 导入模块(主表、附表、分表) 拆解(方案、子过程、计算表) 计算(计算项、计算集)
  16 + ObjectType string `json:"objectType"` //模块 导入模块、拆解模块、计算模块
  17 + Event string `json:"event"` //事件
  18 + TableAffectedList []int `json:"TableAffectedList" gorm:"serializer:json"` //级联影响到的表
  19 + DataChanged bool `json:"dataChanged"` //数据有更新
  20 + StructChanged bool `json:"structChanged"` //结构有更新
  21 + Status int `json:"status" gorm:"index:idx_object_notice_status"` //状态 1-等待处理 2-处理完成 3-处理失败
  22 + Message string `json:"message"` //错误信息
  23 + Retry int `json:"retry" gorm:"default:0"` //重试次数
  24 + MetaData domain.ObjectNoticeMetaData `json:"metaData" gorm:"serializer:json"` //模块信息
  25 + Version int `json:",omitempty"` //版本
  26 + IsDel soft_delete.DeletedAt `gorm:"softDelete:flag,DeletedAtField:DeletedAt"` //删除标记
  27 + CreatedAt int64 `json:",omitempty"` //创建时间
  28 + UpdatedAt int64 `json:",omitempty"` //更新时间
  29 + DeletedAt int64 `json:",omitempty"` //删除时间
  30 +}
  31 +
  32 +func (m *ObjectNotice) TableName() string {
  33 + return "object_notice"
  34 +}
  35 +
  36 +func (m *ObjectNotice) BeforeCreate(tx *gorm.DB) (err error) {
  37 + m.CreatedAt = time.Now().Unix()
  38 + m.UpdatedAt = time.Now().Unix()
  39 + return
  40 +}
  41 +
  42 +func (m *ObjectNotice) BeforeUpdate(tx *gorm.DB) (err error) {
  43 + m.UpdatedAt = time.Now().Unix()
  44 + return
  45 +}
  46 +
  47 +func (m *ObjectNotice) CacheKeyFunc() string {
  48 + if m.Id == 0 {
  49 + return ""
  50 + }
  51 + return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
  52 +}
  53 +
  54 +func (m *ObjectNotice) CacheKeyFuncByObject(obj interface{}) string {
  55 + if v, ok := obj.(*ObjectNotice); ok {
  56 + return v.CacheKeyFunc()
  57 + }
  58 + return ""
  59 +}
  60 +
  61 +func (m *ObjectNotice) CachePrimaryKeyFunc() string {
  62 + if len("") == 0 {
  63 + return ""
  64 + }
  65 + return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key")
  66 +}
  1 +package models
  2 +
  3 +import (
  4 + "fmt"
  5 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/domain"
  6 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/gateway/bytelib"
  7 + "gorm.io/gorm"
  8 + "gorm.io/plugin/soft_delete"
  9 + "time"
  10 +)
  11 +
  12 +type ObjectTable struct {
  13 + Id int `json:"id" gorm:"primaryKey"` // ID
  14 + TableId int `json:"tableId" gorm:"index:idx_object_table_id"` //表ID
  15 + Name string `json:"name"` //表名
  16 + TableType string `json:"tableType" gorm:"index:idx_object_table_type"` //模块
  17 + CompanyId int64 `json:"companyId" gorm:"index:idx_object_table_company"` //公司id
  18 + ParentId int `json:"parentId" gorm:"default:0"` //父id
  19 + Flag string `json:"flag"` //分组:Group 集合:Set
  20 + Version int `json:",omitempty"` //版本
  21 + Fields []*bytelib.Field `json:"fields" gorm:"serializer:json"` //表字段
  22 + RemoteDeleted int `json:"remoteDeleted"` //远端删除
  23 + IsDel soft_delete.DeletedAt `gorm:"softDelete:flag,DeletedAtField:DeletedAt"` //删除标记
  24 + CreatedAt int64 `json:",omitempty"` //创建时间
  25 + UpdatedAt int64 `json:",omitempty"` //更新时间
  26 + DeletedAt int64 `json:",omitempty"` //删除时间
  27 +}
  28 +
  29 +func (m *ObjectTable) TableName() string {
  30 + return "object_table"
  31 +}
  32 +
  33 +func (m *ObjectTable) BeforeCreate(tx *gorm.DB) (err error) {
  34 + m.CreatedAt = time.Now().Unix()
  35 + m.UpdatedAt = time.Now().Unix()
  36 + return
  37 +}
  38 +
  39 +func (m *ObjectTable) BeforeUpdate(tx *gorm.DB) (err error) {
  40 + m.UpdatedAt = time.Now().Unix()
  41 + return
  42 +}
  43 +
  44 +func (m *ObjectTable) CacheKeyFunc() string {
  45 + if m.Id == 0 {
  46 + return ""
  47 + }
  48 + return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
  49 +}
  50 +
  51 +func (m *ObjectTable) CacheKeyFuncByObject(obj interface{}) string {
  52 + if v, ok := obj.(*ObjectTable); ok {
  53 + return v.CacheKeyFunc()
  54 + }
  55 + return ""
  56 +}
  57 +
  58 +func (m *ObjectTable) CachePrimaryKeyFunc() string {
  59 + if len("") == 0 {
  60 + return ""
  61 + }
  62 + return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key")
  63 +}
  1 +package repository
  2 +
  3 +import (
  4 + "context"
  5 + "github.com/jinzhu/copier"
  6 + "github.com/pkg/errors"
  7 + "github.com/tiptok/gocomm/pkg/cache"
  8 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/db/models"
  9 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/db/transaction"
  10 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/domain"
  11 + "gorm.io/gorm"
  12 +)
  13 +
  14 +type ObjectFieldRepository struct {
  15 + *cache.CachedRepository
  16 +}
  17 +
  18 +func (repository *ObjectFieldRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.ObjectField) (*domain.ObjectField, error) {
  19 + var (
  20 + err error
  21 + m = &models.ObjectField{}
  22 + tx = conn.DB()
  23 + )
  24 + if m, err = repository.DomainModelToModel(dm); err != nil {
  25 + return nil, err
  26 + }
  27 + if tx = tx.Model(m).Save(m); tx.Error != nil {
  28 + return nil, tx.Error
  29 + }
  30 + dm.Id = m.Id
  31 + return repository.ModelToDomainModel(m)
  32 +
  33 +}
  34 +
  35 +func (repository *ObjectFieldRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.ObjectField) (*domain.ObjectField, error) {
  36 + var (
  37 + err error
  38 + m *models.ObjectField
  39 + tx = conn.DB()
  40 + )
  41 + if m, err = repository.DomainModelToModel(dm); err != nil {
  42 + return nil, err
  43 + }
  44 + queryFunc := func() (interface{}, error) {
  45 + tx = tx.Model(m).Updates(m)
  46 + return nil, tx.Error
  47 + }
  48 + if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
  49 + return nil, err
  50 + }
  51 + return repository.ModelToDomainModel(m)
  52 +}
  53 +
  54 +func (repository *ObjectFieldRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.ObjectField) (*domain.ObjectField, error) {
  55 + var (
  56 + err error
  57 + m *models.ObjectField
  58 + tx = transaction.DB()
  59 + )
  60 + if m, err = repository.DomainModelToModel(dm); err != nil {
  61 + return nil, err
  62 + }
  63 + oldVersion := dm.Version
  64 + m.Version += 1
  65 + queryFunc := func() (interface{}, error) {
  66 + tx = tx.Model(m).Select("*").Where("id = ?", m.Id).Where("version = ?", oldVersion).Updates(m)
  67 + if tx.RowsAffected == 0 {
  68 + return nil, domain.ErrUpdateFail
  69 + }
  70 + return nil, tx.Error
  71 + }
  72 + if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
  73 + return nil, err
  74 + }
  75 + return repository.ModelToDomainModel(m)
  76 +}
  77 +
  78 +func (repository *ObjectFieldRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.ObjectField) (*domain.ObjectField, error) {
  79 + var (
  80 + tx = conn.DB()
  81 + m = &models.ObjectField{Id: dm.Identify().(int64)}
  82 + )
  83 + queryFunc := func() (interface{}, error) {
  84 + tx = tx.Where("id = ?", m.Id).Delete(m)
  85 + return m, tx.Error
  86 + }
  87 + if _, err := repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
  88 + return dm, err
  89 + }
  90 + return repository.ModelToDomainModel(m)
  91 +}
  92 +
  93 +func (repository *ObjectFieldRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.ObjectField, error) {
  94 + var (
  95 + err error
  96 + tx = conn.DB()
  97 + m = new(models.ObjectField)
  98 + )
  99 + queryFunc := func() (interface{}, error) {
  100 + tx = tx.Model(m).Where("id = ?", id).First(m)
  101 + if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
  102 + return nil, domain.ErrNotFound
  103 + }
  104 + return m, tx.Error
  105 + }
  106 + cacheModel := new(models.ObjectField)
  107 + cacheModel.Id = id
  108 + if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
  109 + return nil, err
  110 + }
  111 + return repository.ModelToDomainModel(m)
  112 +}
  113 +
  114 +func (repository *ObjectFieldRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.ObjectField, error) {
  115 + var (
  116 + tx = conn.DB()
  117 + ms []*models.ObjectField
  118 + dms = make([]*domain.ObjectField, 0)
  119 + total int64
  120 + )
  121 + queryFunc := func() (interface{}, error) {
  122 + tx = tx.Model(&ms).Order("id desc")
  123 + if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
  124 + return dms, tx.Error
  125 + }
  126 + return dms, nil
  127 + }
  128 +
  129 + if _, err := repository.Query(queryFunc); err != nil {
  130 + return 0, nil, err
  131 + }
  132 +
  133 + for _, item := range ms {
  134 + if dm, err := repository.ModelToDomainModel(item); err != nil {
  135 + return 0, dms, err
  136 + } else {
  137 + dms = append(dms, dm)
  138 + }
  139 + }
  140 + return total, dms, nil
  141 +}
  142 +
  143 +func (repository *ObjectFieldRepository) ModelToDomainModel(from *models.ObjectField) (*domain.ObjectField, error) {
  144 + to := &domain.ObjectField{}
  145 + err := copier.Copy(to, from)
  146 + return to, err
  147 +}
  148 +
  149 +func (repository *ObjectFieldRepository) DomainModelToModel(from *domain.ObjectField) (*models.ObjectField, error) {
  150 + to := &models.ObjectField{}
  151 + err := copier.Copy(to, from)
  152 + return to, err
  153 +}
  154 +
  155 +func NewObjectFieldRepository(cache *cache.CachedRepository) domain.ObjectFieldRepository {
  156 + return &ObjectFieldRepository{CachedRepository: cache}
  157 +}
  1 +package repository
  2 +
  3 +import (
  4 + "context"
  5 + "github.com/jinzhu/copier"
  6 + "github.com/pkg/errors"
  7 + "github.com/tiptok/gocomm/pkg/cache"
  8 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/db/models"
  9 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/db/transaction"
  10 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/domain"
  11 + "gorm.io/gorm"
  12 +)
  13 +
  14 +type ObjectNoticeRepository struct {
  15 + *cache.CachedRepository
  16 +}
  17 +
  18 +func (repository *ObjectNoticeRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.ObjectNotice) (*domain.ObjectNotice, error) {
  19 + var (
  20 + err error
  21 + m = &models.ObjectNotice{}
  22 + tx = conn.DB()
  23 + )
  24 + if m, err = repository.DomainModelToModel(dm); err != nil {
  25 + return nil, err
  26 + }
  27 + if tx = tx.Model(m).Save(m); tx.Error != nil {
  28 + return nil, tx.Error
  29 + }
  30 + dm.Id = m.Id
  31 + return repository.ModelToDomainModel(m)
  32 +
  33 +}
  34 +
  35 +func (repository *ObjectNoticeRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.ObjectNotice) (*domain.ObjectNotice, error) {
  36 + var (
  37 + err error
  38 + m *models.ObjectNotice
  39 + tx = conn.DB()
  40 + )
  41 + if m, err = repository.DomainModelToModel(dm); err != nil {
  42 + return nil, err
  43 + }
  44 + queryFunc := func() (interface{}, error) {
  45 + tx = tx.Model(m).Updates(m)
  46 + return nil, tx.Error
  47 + }
  48 + if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
  49 + return nil, err
  50 + }
  51 + return repository.ModelToDomainModel(m)
  52 +}
  53 +
  54 +func (repository *ObjectNoticeRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.ObjectNotice) (*domain.ObjectNotice, error) {
  55 + var (
  56 + err error
  57 + m *models.ObjectNotice
  58 + tx = transaction.DB()
  59 + )
  60 + if m, err = repository.DomainModelToModel(dm); err != nil {
  61 + return nil, err
  62 + }
  63 + oldVersion := dm.Version
  64 + m.Version += 1
  65 + queryFunc := func() (interface{}, error) {
  66 + tx = tx.Model(m).Select("*").Where("id = ?", m.Id).Where("version = ?", oldVersion).Updates(m)
  67 + if tx.RowsAffected == 0 {
  68 + return nil, domain.ErrUpdateFail
  69 + }
  70 + return nil, tx.Error
  71 + }
  72 + if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
  73 + return nil, err
  74 + }
  75 + return repository.ModelToDomainModel(m)
  76 +}
  77 +
  78 +func (repository *ObjectNoticeRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.ObjectNotice) (*domain.ObjectNotice, error) {
  79 + var (
  80 + tx = conn.DB()
  81 + m = &models.ObjectNotice{Id: dm.Identify().(int64)}
  82 + )
  83 + queryFunc := func() (interface{}, error) {
  84 + tx = tx.Where("id = ?", m.Id).Delete(m)
  85 + return m, tx.Error
  86 + }
  87 + if _, err := repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
  88 + return dm, err
  89 + }
  90 + return repository.ModelToDomainModel(m)
  91 +}
  92 +
  93 +func (repository *ObjectNoticeRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.ObjectNotice, error) {
  94 + var (
  95 + err error
  96 + tx = conn.DB()
  97 + m = new(models.ObjectNotice)
  98 + )
  99 + queryFunc := func() (interface{}, error) {
  100 + tx = tx.Model(m).Where("id = ?", id).First(m)
  101 + if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
  102 + return nil, domain.ErrNotFound
  103 + }
  104 + return m, tx.Error
  105 + }
  106 + cacheModel := new(models.ObjectNotice)
  107 + cacheModel.Id = id
  108 + if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
  109 + return nil, err
  110 + }
  111 + return repository.ModelToDomainModel(m)
  112 +}
  113 +
  114 +func (repository *ObjectNoticeRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.ObjectNotice, error) {
  115 + var (
  116 + tx = conn.DB()
  117 + ms []*models.ObjectNotice
  118 + dms = make([]*domain.ObjectNotice, 0)
  119 + total int64
  120 + )
  121 + queryFunc := func() (interface{}, error) {
  122 + tx = tx.Model(&ms).Order("id desc")
  123 + if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
  124 + return dms, tx.Error
  125 + }
  126 + return dms, nil
  127 + }
  128 +
  129 + if _, err := repository.Query(queryFunc); err != nil {
  130 + return 0, nil, err
  131 + }
  132 +
  133 + for _, item := range ms {
  134 + if dm, err := repository.ModelToDomainModel(item); err != nil {
  135 + return 0, dms, err
  136 + } else {
  137 + dms = append(dms, dm)
  138 + }
  139 + }
  140 + return total, dms, nil
  141 +}
  142 +
  143 +func (repository *ObjectNoticeRepository) ModelToDomainModel(from *models.ObjectNotice) (*domain.ObjectNotice, error) {
  144 + to := &domain.ObjectNotice{}
  145 + err := copier.Copy(to, from)
  146 + return to, err
  147 +}
  148 +
  149 +func (repository *ObjectNoticeRepository) DomainModelToModel(from *domain.ObjectNotice) (*models.ObjectNotice, error) {
  150 + to := &models.ObjectNotice{}
  151 + err := copier.Copy(to, from)
  152 + return to, err
  153 +}
  154 +
  155 +func NewObjectNoticeRepository(cache *cache.CachedRepository) domain.ObjectNoticeRepository {
  156 + return &ObjectNoticeRepository{CachedRepository: cache}
  157 +}
  1 +package repository
  2 +
  3 +import (
  4 + "context"
  5 + "fmt"
  6 + "github.com/pkg/errors"
  7 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/db/transaction"
  8 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/domain"
  9 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/gateway/bytelib"
  10 + "strings"
  11 +)
  12 +
  13 +type ObjectTableDataRepository struct{}
  14 +
  15 +type TableColumnSQL struct {
  16 + Sql string `json:"sql"`
  17 + Column []string `json:"column"`
  18 + Data []map[string]string `json:"data"`
  19 +}
  20 +
  21 +// makeDropTableSQL 创建删除表SQL
  22 +func (repository *ObjectTableDataRepository) makeDropTableSQL(tableId int) (string, error) {
  23 + return fmt.Sprintf(`drop table if exists data."%v"`, tableId), nil
  24 +}
  25 +
  26 +// makeCreateTableSQL 创建表SQL
  27 +func (repository *ObjectTableDataRepository) makeCreateTableSQL(tableId int, fields []*bytelib.Field) (string, error) {
  28 + if len(fields) <= 0 {
  29 + return "", errors.New("缺少字段信息")
  30 + }
  31 + columns := make([]string, 0)
  32 + for _, item := range fields {
  33 + columns = append(columns, item.SQLName+" text ")
  34 + }
  35 + sql := `Create TABLE data."` + fmt.Sprintf("%v", tableId) + `" (` + strings.Join(columns, ",") + `);`
  36 + return sql, nil
  37 +}
  38 +
  39 +// makeInsertTableSQL 创建表数据SQL
  40 +func (repository *ObjectTableDataRepository) makeInsertTableSQL(tableId int, fields []*bytelib.Field, list []map[string]string) (*TableColumnSQL, error) {
  41 + tableColumnSQL := &TableColumnSQL{
  42 + Column: make([]string, 0),
  43 + Data: make([]map[string]string, 0),
  44 + }
  45 + if len(list) > 0 && len(fields) > 0 {
  46 + for _, field := range fields {
  47 + tableColumnSQL.Column = append(tableColumnSQL.Column, field.SQLName)
  48 + }
  49 + for _, item := range list {
  50 + values := make(map[string]string)
  51 + for _, field := range tableColumnSQL.Column {
  52 + if _, ok := item[field]; ok {
  53 + values[field] = item[field]
  54 + } else {
  55 + return tableColumnSQL, errors.New("未获取到字段值")
  56 + }
  57 + }
  58 + tableColumnSQL.Data = append(tableColumnSQL.Data, values)
  59 + }
  60 +
  61 + tableColumnSQL.Sql = fmt.Sprintf(`INSERT INTO data."%v" (%s) values (%s)`, tableId, strings.Join(tableColumnSQL.Column, ","), repository.sqlPlaceHoldersSnippet(tableColumnSQL.Column))
  62 + }
  63 + return tableColumnSQL, nil
  64 +}
  65 +
  66 +func (repository *ObjectTableDataRepository) sqlPlaceHoldersSnippet(sqlBuildFields []string) string {
  67 + var placeHolder []string
  68 + for i := 0; i < len(sqlBuildFields); i++ {
  69 + placeHolder = append(placeHolder, "?")
  70 + }
  71 + return strings.Join(placeHolder, ",")
  72 +}
  73 +
  74 +func (repository *ObjectTableDataRepository) DropTable(ctx context.Context, conn transaction.Conn, tableId int) error {
  75 + sql, err := repository.makeDropTableSQL(tableId)
  76 + if err != nil {
  77 + return err
  78 + }
  79 + return conn.DB().Exec(sql).Error
  80 +}
  81 +
  82 +// InsertWithTableData 写入表数据
  83 +func (repository *ObjectTableDataRepository) InsertWithTableData(ctx context.Context, conn transaction.Conn, tableDataPreview bytelib.TableData) error {
  84 + //删除表
  85 + err := repository.DropTable(ctx, conn, int(tableDataPreview.ObjectId))
  86 + if err != nil {
  87 + return err
  88 + }
  89 + //创建表
  90 + createTableSql, err := repository.makeCreateTableSQL(int(tableDataPreview.ObjectId), tableDataPreview.Fields)
  91 + if err != nil {
  92 + return err
  93 + }
  94 + err = conn.DB().Exec(createTableSql).Error
  95 + if err != nil {
  96 + return err
  97 + }
  98 + tableColumnSQL, err := repository.makeInsertTableSQL(int(tableDataPreview.ObjectId), tableDataPreview.Fields, tableDataPreview.Grid.List)
  99 + if err != nil {
  100 + return err
  101 + }
  102 + if len(tableColumnSQL.Column) > 0 && len(tableColumnSQL.Data) > 0 {
  103 + for _, item := range tableColumnSQL.Data {
  104 + if len(item) > 0 {
  105 + params := make([]interface{}, 0)
  106 + for _, m := range tableColumnSQL.Column {
  107 + if _, ok := item[m]; ok {
  108 + params = append(params, item[m])
  109 + } else {
  110 + params = append(params, "")
  111 + }
  112 + }
  113 + err = conn.DB().Exec(tableColumnSQL.Sql, params...).Error
  114 + if err != nil {
  115 + return err
  116 + }
  117 + }
  118 + }
  119 + }
  120 + return nil
  121 +}
  122 +
  123 +// FindAll 获取表内所有数据
  124 +func (repository *ObjectTableDataRepository) FindAll(ctx context.Context, conn transaction.Conn, tableId int) {
  125 +
  126 +}
  127 +
  128 +func NewObjectTableDataRepository() domain.ObjectTableDataRepository {
  129 + return &ObjectTableDataRepository{}
  130 +}
  1 +package repository
  2 +
  3 +import (
  4 + "context"
  5 + "github.com/jinzhu/copier"
  6 + "github.com/pkg/errors"
  7 + "github.com/tiptok/gocomm/pkg/cache"
  8 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/db/models"
  9 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/db/transaction"
  10 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/domain"
  11 + "gorm.io/gorm"
  12 +)
  13 +
  14 +type ObjectTableRepository struct {
  15 + *cache.CachedRepository
  16 +}
  17 +
  18 +func (repository *ObjectTableRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.ObjectTable) (*domain.ObjectTable, error) {
  19 + var (
  20 + err error
  21 + m = &models.ObjectTable{}
  22 + tx = conn.DB()
  23 + )
  24 + if m, err = repository.DomainModelToModel(dm); err != nil {
  25 + return nil, err
  26 + }
  27 + if tx = tx.Model(m).Save(m); tx.Error != nil {
  28 + return nil, tx.Error
  29 + }
  30 + dm.Id = m.Id
  31 + return repository.ModelToDomainModel(m)
  32 +
  33 +}
  34 +
  35 +func (repository *ObjectTableRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.ObjectTable) (*domain.ObjectTable, error) {
  36 + var (
  37 + err error
  38 + m *models.ObjectTable
  39 + tx = conn.DB()
  40 + )
  41 + if m, err = repository.DomainModelToModel(dm); err != nil {
  42 + return nil, err
  43 + }
  44 + queryFunc := func() (interface{}, error) {
  45 + tx = tx.Model(m).Updates(m)
  46 + return nil, tx.Error
  47 + }
  48 + if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
  49 + return nil, err
  50 + }
  51 + return repository.ModelToDomainModel(m)
  52 +}
  53 +
  54 +func (repository *ObjectTableRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.ObjectTable) (*domain.ObjectTable, error) {
  55 + var (
  56 + err error
  57 + m *models.ObjectTable
  58 + tx = transaction.DB()
  59 + )
  60 + if m, err = repository.DomainModelToModel(dm); err != nil {
  61 + return nil, err
  62 + }
  63 + oldVersion := dm.Version
  64 + m.Version += 1
  65 + queryFunc := func() (interface{}, error) {
  66 + tx = tx.Model(m).Select("*").Where("id = ?", m.Id).Where("version = ?", oldVersion).Updates(m)
  67 + if tx.RowsAffected == 0 {
  68 + return nil, domain.ErrUpdateFail
  69 + }
  70 + return nil, tx.Error
  71 + }
  72 + if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
  73 + return nil, err
  74 + }
  75 + return repository.ModelToDomainModel(m)
  76 +}
  77 +
  78 +func (repository *ObjectTableRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.ObjectTable) (*domain.ObjectTable, error) {
  79 + var (
  80 + tx = conn.DB()
  81 + m = &models.ObjectTable{Id: dm.Identify().(int)}
  82 + )
  83 + queryFunc := func() (interface{}, error) {
  84 + tx = tx.Where("id = ?", m.Id).Delete(m)
  85 + return m, tx.Error
  86 + }
  87 + if _, err := repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
  88 + return dm, err
  89 + }
  90 + return repository.ModelToDomainModel(m)
  91 +}
  92 +
  93 +func (repository *ObjectTableRepository) FindOne(ctx context.Context, conn transaction.Conn, id int) (*domain.ObjectTable, error) {
  94 + var (
  95 + err error
  96 + tx = conn.DB()
  97 + m = new(models.ObjectTable)
  98 + )
  99 + queryFunc := func() (interface{}, error) {
  100 + tx = tx.Model(m).Where("id = ?", id).First(m)
  101 + if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
  102 + return nil, domain.ErrNotFound
  103 + }
  104 + return m, tx.Error
  105 + }
  106 + cacheModel := new(models.ObjectTable)
  107 + cacheModel.Id = id
  108 + if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
  109 + return nil, err
  110 + }
  111 + return repository.ModelToDomainModel(m)
  112 +}
  113 +
  114 +func (repository *ObjectTableRepository) FindOneByTableId(ctx context.Context, conn transaction.Conn, tableId int) (*domain.ObjectTable, error) {
  115 + var (
  116 + err error
  117 + tx = conn.DB()
  118 + m = new(models.ObjectTable)
  119 + )
  120 + queryFunc := func() (interface{}, error) {
  121 + tx = tx.Model(m).Where("table_id = ?", tableId).First(m)
  122 + if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
  123 + return nil, domain.ErrNotFound
  124 + }
  125 + return m, tx.Error
  126 + }
  127 + if _, err = repository.Query(queryFunc); err != nil {
  128 + return nil, err
  129 + }
  130 + return repository.ModelToDomainModel(m)
  131 +}
  132 +
  133 +func (repository *ObjectTableRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.ObjectTable, error) {
  134 + var (
  135 + tx = conn.DB()
  136 + ms []*models.ObjectTable
  137 + dms = make([]*domain.ObjectTable, 0)
  138 + total int64
  139 + )
  140 + queryFunc := func() (interface{}, error) {
  141 + tx = tx.Model(&ms).Order("id desc")
  142 + if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
  143 + return dms, tx.Error
  144 + }
  145 + return dms, nil
  146 + }
  147 +
  148 + if _, err := repository.Query(queryFunc); err != nil {
  149 + return 0, nil, err
  150 + }
  151 +
  152 + for _, item := range ms {
  153 + if dm, err := repository.ModelToDomainModel(item); err != nil {
  154 + return 0, dms, err
  155 + } else {
  156 + dms = append(dms, dm)
  157 + }
  158 + }
  159 + return total, dms, nil
  160 +}
  161 +
  162 +func (repository *ObjectTableRepository) ModelToDomainModel(from *models.ObjectTable) (*domain.ObjectTable, error) {
  163 + to := &domain.ObjectTable{}
  164 + err := copier.Copy(to, from)
  165 + return to, err
  166 +}
  167 +
  168 +func (repository *ObjectTableRepository) DomainModelToModel(from *domain.ObjectTable) (*models.ObjectTable, error) {
  169 + to := &models.ObjectTable{}
  170 + err := copier.Copy(to, from)
  171 + return to, err
  172 +}
  173 +
  174 +func NewObjectTableRepository(cache *cache.CachedRepository) domain.ObjectTableRepository {
  175 + return &ObjectTableRepository{CachedRepository: cache}
  176 +}
  1 +package domain
  2 +
  3 +import (
  4 + "context"
  5 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/db/transaction"
  6 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/gateway/bytelib"
  7 + "gorm.io/plugin/soft_delete"
  8 +)
  9 +
  10 +type ObjectField struct {
  11 + Id int64 `json:"id"` // ID
  12 + Name string `json:"name"` //表名
  13 + Fields []*bytelib.Field `json:"fields"` //表字段
  14 + Version int `json:"version,omitempty"` //版本
  15 + IsDel soft_delete.DeletedAt `json:"isDel"` //删除标记
  16 + CreatedAt int64 `json:"createdAt,omitempty"` //创建时间
  17 + UpdatedAt int64 `json:"updatedAt,omitempty"` //更新时间
  18 + DeletedAt int64 `json:"deletedAt,omitempty"` //删除时间
  19 +}
  20 +
  21 +type ObjectFieldRepository interface {
  22 + Insert(ctx context.Context, conn transaction.Conn, dm *ObjectField) (*ObjectField, error)
  23 + Update(ctx context.Context, conn transaction.Conn, dm *ObjectField) (*ObjectField, error)
  24 + UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *ObjectField) (*ObjectField, error)
  25 + Delete(ctx context.Context, conn transaction.Conn, dm *ObjectField) (*ObjectField, error)
  26 + FindOne(ctx context.Context, conn transaction.Conn, id int64) (*ObjectField, error)
  27 + Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*ObjectField, error)
  28 +}
  29 +
  30 +func (m *ObjectField) Identify() interface{} {
  31 + if m.Id == 0 {
  32 + return nil
  33 + }
  34 + return m.Id
  35 +}
  1 +package domain
  2 +
  3 +import (
  4 + "context"
  5 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/db/transaction"
  6 + "gorm.io/plugin/soft_delete"
  7 +)
  8 +
  9 +type ObjectNotice struct {
  10 + Id int64 `json:"id"` // ID
  11 + CompanyId int64 `json:"companyId"` //公司ID
  12 + TableId int `json:"tableId"` //表ID
  13 + TableType string `json:"tableType"` //表类型 导入模块(主表、附表、分表) 拆解(方案、子过程、计算表) 计算(计算项、计算集)
  14 + ObjectType string `json:"objectType"` //模块 导入模块、拆解模块、计算模块
  15 + Event string `json:"event"` //事件
  16 + TableAffectedList []int `json:"TableAffectedList"` //级联影响到的表
  17 + DataChanged bool `json:"dataChanged"` //数据有更新
  18 + StructChanged bool `json:"structChanged"` //结构有更新
  19 + Status int `json:"status"` //状态 1-等待处理 2-处理完成 3-处理失败
  20 + UpdateTable int `json:"updateTable"` //更新表结构
  21 + UpdateData int `json:"updateData"` //更新表数据
  22 + Message string `json:"message"` //错误信息
  23 + Retry int `json:"retry"` //重试次数
  24 + MetaData ObjectNoticeMetaData `json:"metaData"` //模块信息
  25 + Version int `json:",omitempty"` //版本
  26 + IsDel soft_delete.DeletedAt `json:"isDel"` //删除标记
  27 + CreatedAt int64 `json:"createdAt,omitempty"` //创建时间
  28 + UpdatedAt int64 `json:"updatedAt,omitempty"` //更新时间
  29 + DeletedAt int64 `json:"deletedAt,omitempty"` //删除时间
  30 +}
  31 +
  32 +type ObjectNoticeMetaData struct {
  33 + Module int `json:"module"` // 导入模块判断是否 取消应用
  34 + Status int `json:"status"` // 拆解模块判断是否 取消
  35 +}
  36 +
  37 +var (
  38 + ObjectNoticeStatusWait = 1 //等待处理
  39 + ObjectNoticeStatusDone = 2 //处理完成
  40 + ObjectNoticeStatusError = 3 //处理失败
  41 +)
  42 +
  43 +type ObjectNoticeRepository interface {
  44 + Insert(ctx context.Context, conn transaction.Conn, dm *ObjectNotice) (*ObjectNotice, error)
  45 + Update(ctx context.Context, conn transaction.Conn, dm *ObjectNotice) (*ObjectNotice, error)
  46 + UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *ObjectNotice) (*ObjectNotice, error)
  47 + Delete(ctx context.Context, conn transaction.Conn, dm *ObjectNotice) (*ObjectNotice, error)
  48 + FindOne(ctx context.Context, conn transaction.Conn, id int64) (*ObjectNotice, error)
  49 + Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*ObjectNotice, error)
  50 +}
  51 +
  52 +func (m *ObjectNotice) Identify() interface{} {
  53 + if m.Id == 0 {
  54 + return nil
  55 + }
  56 + return m.Id
  57 +}
  58 +
  59 +// IsDeletedEvent 是否删除事件
  60 +func (m *ObjectNotice) IsDeletedEvent() bool {
  61 + deletedEvents := []string{
  62 + "table.delete",
  63 + "table.query.set.delete",
  64 + }
  65 + for _, event := range deletedEvents {
  66 + if m.Event == event {
  67 + return true
  68 + }
  69 + }
  70 + //导入模块 取消应用和应用于
  71 + if m.Event == "table.apply-on" {
  72 + if m.MetaData.Module&1 == 0 {
  73 + return true
  74 + }
  75 + }
  76 + //拆解模块 方案启用、禁用
  77 + //计算模块 计算项和计算集启用、禁用
  78 + if m.Event == "table.query.set.update.status" {
  79 + if m.MetaData.Status&1 == 0 {
  80 + return true
  81 + }
  82 + }
  83 + return false
  84 +}
  1 +package domain
  2 +
  3 +import (
  4 + "context"
  5 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/db/transaction"
  6 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/gateway/bytelib"
  7 + "gorm.io/plugin/soft_delete"
  8 +)
  9 +
  10 +type ObjectTable struct {
  11 + Id int `json:"id"` // ID
  12 + TableId int `json:"tableId" ` //表ID
  13 + Name string `json:"name"` //表名
  14 + TableType string `json:"tableType" ` //模块
  15 + CompanyId int64 `json:"companyId"` //公司id
  16 + ParentId int `json:"parentId"` //父id
  17 + Flag string `json:"flag"` //分组:Group 集合:Set
  18 + Fields []*bytelib.Field `json:"fields" gorm:"serializer:json"` //表字段
  19 + Version int `json:"version,omitempty"` //版本
  20 + RemoteDeleted int `json:"remoteDeleted"` //远端删除
  21 + IsDel soft_delete.DeletedAt `json:"isDel,omitempty"` //删除标记
  22 + CreatedAt int64 `json:"createdAt,omitempty"` //创建时间
  23 + UpdatedAt int64 `json:"updatedAt,omitempty"` //更新时间
  24 + DeletedAt int64 `json:"deletedAt,omitempty"` //删除时间
  25 +}
  26 +
  27 +type ObjectTableRepository interface {
  28 + Insert(ctx context.Context, conn transaction.Conn, dm *ObjectTable) (*ObjectTable, error)
  29 + Update(ctx context.Context, conn transaction.Conn, dm *ObjectTable) (*ObjectTable, error)
  30 + UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *ObjectTable) (*ObjectTable, error)
  31 + Delete(ctx context.Context, conn transaction.Conn, dm *ObjectTable) (*ObjectTable, error)
  32 + FindOne(ctx context.Context, conn transaction.Conn, id int) (*ObjectTable, error)
  33 + FindOneByTableId(ctx context.Context, conn transaction.Conn, tableId int) (*ObjectTable, error)
  34 + Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*ObjectTable, error)
  35 +}
  36 +
  37 +type ObjectTableDataRepository interface {
  38 + InsertWithTableData(ctx context.Context, conn transaction.Conn, tableDataPreview bytelib.TableData) error
  39 +}
  40 +
  41 +func (m *ObjectTable) Identify() interface{} {
  42 + if m.Id == 0 {
  43 + return nil
  44 + }
  45 + return m.Id
  46 +}
@@ -23,3 +23,7 @@ const ( @@ -23,3 +23,7 @@ const (
23 ObjectMetaTable = "MetaTable" 23 ObjectMetaTable = "MetaTable"
24 ObjectDBTable = "DBTable" 24 ObjectDBTable = "DBTable"
25 ) 25 )
  26 +
  27 +const (
  28 + MaxPageSize = 30000 //最大读取行数
  29 +)
  1 +CREATE TABLE `chart`
  2 +(
  3 + `id` bigint(0) NOT NULL COMMENT 'ID',
  4 + `pid` bigint(0) NOT NULL COMMENT '父级ID',
  5 + `type` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '类型',
  6 + `sort` int(0) NOT NULL COMMENT '排序',
  7 + `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '名称',
  8 + PRIMARY KEY (`id`) USING BTREE
  9 +) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
  10 +
  11 +CREATE TABLE `chart_setting`
  12 +(
  13 + `id` bigint(0) NOT NULL COMMENT 'ID ',
  14 + `chart_id` bigint(0) NOT NULL COMMENT '图表ID',
  15 + `property` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '属性',
  16 + `style` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '样式',
  17 + `series` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '系列值-数据绑定',
  18 + PRIMARY KEY (`id`) USING BTREE
  19 +) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
1 20
2 -CREATE TABLE `chart` (  
3 - `id` bigint(0) NOT NULL COMMENT 'ID',  
4 - `pid` bigint(0) NOT NULL COMMENT '父级ID',  
5 - `type` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '类型',  
6 - `sort` int(0) NOT NULL COMMENT '排序',  
7 - `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '名称',  
8 - PRIMARY KEY (`id`) USING BTREE 21 +CREATE TABLE `app_page`
  22 +(
  23 + `id` int(0) NOT NULL COMMENT '唯一标识',
  24 + PRIMARY KEY (`id`) USING BTREE
9 ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; 25 ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
10 26
11 -CREATE TABLE `chart_setting` (  
12 - `id` bigint(0) NOT NULL COMMENT 'ID ',  
13 - `chart_id` bigint(0) NOT NULL COMMENT '图表ID',  
14 - `property` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '属性',  
15 - `style` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '样式',  
16 - `series` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '系列值-数据绑定',  
17 - PRIMARY KEY (`id`) USING BTREE 27 +Create Table `object_table`
  28 +(
  29 + `id` int not null comment 'ID',
  30 + PRIMARY KEY (`id`) USING BTREE
18 ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; 31 ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
19 32
20 -CREATE TABLE `app_page` (  
21 - `id` int(0) NOT NULL COMMENT '唯一标识',  
22 - PRIMARY KEY (`id`) USING BTREE 33 +Create Table `object_field`
  34 +(
  35 + `id` int not null comment 'ID',
  36 + PRIMARY KEY (`id`) USING BTREE
23 ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; 37 ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
  38 +
  39 +Create Table `object_notice`
  40 +(
  41 + `id` int not null comment 'ID',
  42 + PRIMARY KEY (`id`) USING BTREE
  43 +) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
@@ -6,10 +6,14 @@ require ( @@ -6,10 +6,14 @@ require (
6 github.com/golang-jwt/jwt/v4 v4.5.0 6 github.com/golang-jwt/jwt/v4 v4.5.0
7 github.com/jinzhu/copier v0.4.0 7 github.com/jinzhu/copier v0.4.0
8 github.com/jinzhu/now v1.1.5 8 github.com/jinzhu/now v1.1.5
  9 + github.com/magiconair/properties v1.8.0
9 github.com/pkg/errors v0.9.1 10 github.com/pkg/errors v0.9.1
  11 + github.com/samber/lo v1.38.1
10 github.com/stretchr/testify v1.8.4 12 github.com/stretchr/testify v1.8.4
11 github.com/tiptok/gocomm v1.0.14 13 github.com/tiptok/gocomm v1.0.14
  14 + github.com/zeromicro/go-queue v1.1.8
12 github.com/zeromicro/go-zero v1.5.5 15 github.com/zeromicro/go-zero v1.5.5
  16 + golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17
13 google.golang.org/grpc v1.57.0 17 google.golang.org/grpc v1.57.0
14 gorm.io/driver/mysql v1.5.1 18 gorm.io/driver/mysql v1.5.1
15 gorm.io/driver/postgres v1.5.2 19 gorm.io/driver/postgres v1.5.2
@@ -37,7 +41,6 @@ require ( @@ -37,7 +41,6 @@ require (
37 github.com/garyburd/redigo v1.6.3 // indirect 41 github.com/garyburd/redigo v1.6.3 // indirect
38 github.com/gin-contrib/sse v0.1.0 // indirect 42 github.com/gin-contrib/sse v0.1.0 // indirect
39 github.com/gin-gonic/gin v1.5.0 // indirect 43 github.com/gin-gonic/gin v1.5.0 // indirect
40 - github.com/go-gota/gota v0.12.0 // indirect  
41 github.com/go-logr/logr v1.2.3 // indirect 44 github.com/go-logr/logr v1.2.3 // indirect
42 github.com/go-logr/stdr v1.2.2 // indirect 45 github.com/go-logr/stdr v1.2.2 // indirect
43 github.com/go-openapi/jsonpointer v0.19.6 // indirect 46 github.com/go-openapi/jsonpointer v0.19.6 // indirect
@@ -73,7 +76,6 @@ require ( @@ -73,7 +76,6 @@ require (
73 github.com/json-iterator/go v1.1.12 // indirect 76 github.com/json-iterator/go v1.1.12 // indirect
74 github.com/klauspost/compress v1.15.15 // indirect 77 github.com/klauspost/compress v1.15.15 // indirect
75 github.com/leodido/go-urn v1.1.0 // indirect 78 github.com/leodido/go-urn v1.1.0 // indirect
76 - github.com/magiconair/properties v1.8.0 // indirect  
77 github.com/mailru/easyjson v0.7.7 // indirect 79 github.com/mailru/easyjson v0.7.7 // indirect
78 github.com/mattn/go-colorable v0.1.13 // indirect 80 github.com/mattn/go-colorable v0.1.13 // indirect
79 github.com/mattn/go-isatty v0.0.17 // indirect 81 github.com/mattn/go-isatty v0.0.17 // indirect
@@ -93,7 +95,7 @@ require ( @@ -93,7 +95,7 @@ require (
93 github.com/prometheus/common v0.42.0 // indirect 95 github.com/prometheus/common v0.42.0 // indirect
94 github.com/prometheus/procfs v0.10.1 // indirect 96 github.com/prometheus/procfs v0.10.1 // indirect
95 github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect 97 github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
96 - github.com/samber/lo v1.38.1 // indirect 98 + github.com/segmentio/kafka-go v0.4.38 // indirect
97 github.com/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644 // indirect 99 github.com/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644 // indirect
98 github.com/spaolacci/murmur3 v1.1.0 // indirect 100 github.com/spaolacci/murmur3 v1.1.0 // indirect
99 github.com/spf13/afero v1.2.2 // indirect 101 github.com/spf13/afero v1.2.2 // indirect
@@ -121,7 +123,6 @@ require ( @@ -121,7 +123,6 @@ require (
121 go.uber.org/multierr v1.9.0 // indirect 123 go.uber.org/multierr v1.9.0 // indirect
122 go.uber.org/zap v1.24.0 // indirect 124 go.uber.org/zap v1.24.0 // indirect
123 golang.org/x/crypto v0.12.0 // indirect 125 golang.org/x/crypto v0.12.0 // indirect
124 - golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect  
125 golang.org/x/net v0.14.0 // indirect 126 golang.org/x/net v0.14.0 // indirect
126 golang.org/x/oauth2 v0.7.0 // indirect 127 golang.org/x/oauth2 v0.7.0 // indirect
127 golang.org/x/sys v0.11.0 // indirect 128 golang.org/x/sys v0.11.0 // indirect
@@ -137,7 +138,7 @@ require ( @@ -137,7 +138,7 @@ require (
137 gopkg.in/inf.v0 v0.9.1 // indirect 138 gopkg.in/inf.v0 v0.9.1 // indirect
138 gopkg.in/yaml.v2 v2.4.0 // indirect 139 gopkg.in/yaml.v2 v2.4.0 // indirect
139 gopkg.in/yaml.v3 v3.0.1 // indirect 140 gopkg.in/yaml.v3 v3.0.1 // indirect
140 - gorm.io/datatypes v1.2.0 // indirect 141 + gorm.io/driver/sqlite v1.4.3 // indirect
141 k8s.io/api v0.26.3 // indirect 142 k8s.io/api v0.26.3 // indirect
142 k8s.io/apimachinery v0.27.0-alpha.3 // indirect 143 k8s.io/apimachinery v0.27.0-alpha.3 // indirect
143 k8s.io/client-go v0.26.3 // indirect 144 k8s.io/client-go v0.26.3 // indirect
此 diff 太大无法显示。