Skip to content

Commit

Permalink
feat:add 补充说明字段更新
Browse files Browse the repository at this point in the history
  • Loading branch information
mao888 committed Feb 14, 2023
1 parent 897e996 commit 5f1091d
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 5 deletions.
6 changes: 3 additions & 3 deletions project/data-sync/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package main

import "github.com/mao888/golang-guide/project/data-sync/mongo-to-mysql/art_need"

func main() {
// mongo_to_mysql.RunGame()

Expand Down Expand Up @@ -97,5 +95,7 @@ func main() {

// 美术需求默认描述表
// Mongo/plat_console/artneeds.main_desc 数据迁移到 ARK cruiser_console/base_desc_template
art_need.RunBaseDescTemplate()
//art_need.RunBaseDescTemplate()

//
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"go.mongodb.org/mongo-driver/bson"
)

// 1000361

func RunBaseDescTemplate() {
// 1、建立连接
db := db2.MongoClient.Database("plat_console")
Expand Down
2 changes: 1 addition & 1 deletion project/data-sync/mongo-to-mysql/art_need/bean_mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ type ArtNeedDrafts struct {
UpdateTime *time.Time `json:"update_time" bson:"update_time"`
}

type ArtNeedLogs struct {
type MArtNeedLogs struct {
ID int `json:"_id" bson:"_id"`
CompanyId int `json:"company_id" bson:"company_id"` //公司id
GameId int `json:"game_id" bson:"game_id"` //游戏id
Expand Down
2 changes: 1 addition & 1 deletion project/data-sync/mongo-to-mysql/art_need/bean_mysql.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package art_need

// ArtNeed 美术需求主表 mapped from table cruiser_console <art_need>
// ArtNeed 美术需求主表 mapped from table cruiser_console <art_needs>
type ArtNeed struct {
ID int32 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"` // 需求id
GameID string `gorm:"column:game_id;not null" json:"game_id"` // 游戏ID
Expand Down
57 changes: 57 additions & 0 deletions project/data-sync/mongo-to-mysql/art_need/supply_desc.go
Original file line number Diff line number Diff line change
@@ -1 +1,58 @@
package art_need

import (
"context"
"fmt"
"github.com/mao888/go-utils/constants"
db2 "github.com/mao888/golang-guide/project/data-sync/db"
"go.mongodb.org/mongo-driver/bson"
"sort"
)

func RunSupplyDesc() {
// 1、建立连接
db := db2.MongoClient.Database("plat_console")
coll := db.Collection("artneedlogs")

// 2、从mysql查询数据
artNeeds := make([]*ArtNeed, 0)
err := db2.MySQLClientCruiser.Table("art_needs").Find(artNeeds).Error
if err != nil {
fmt.Println("从mysql查询数据 错误:", err)
return
}
fmt.Println(artNeeds)

// 3、将mongo数据装入切片
for _, need := range artNeeds {

// 根据需求id去mongo中查询日志
mArtNeedLogs := make([]*MArtNeedLogs, 0)
err := coll.Find(context.TODO(), bson.M{"artneed_id": need.ID}).All(&mArtNeedLogs)
if err != nil {
fmt.Println("Mongo查询错误:", err)
return
}
if len(mArtNeedLogs) == constants.NumberZero {
continue
}
// 根据时间降序
sort.Slice(mArtNeedLogs, func(i, j int) bool {
return mArtNeedLogs[i].UpdateTime.Unix() > mArtNeedLogs[j].UpdateTime.Unix() // 降序
})
//
for _, log := range mArtNeedLogs {
// SupplyDesc 补充说明 为空跳过
if log.SupplyDesc == constants.EmptyString {
continue
}
// SupplyDesc 补充说明 不为空,则更新当前需求的补充说明
err = db2.MySQLClientCruiser.Table("art_needs").Where("id = ?", need.ID).
UpdateColumn("extra_desc", log.SupplyDesc).Error
if err != nil {
fmt.Println("更新数据 错误:", err)
return
}
}
}
}

0 comments on commit 5f1091d

Please sign in to comment.