作者 庄敏学

同步数据表

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