material_group_k3cloud_dao.go
1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package dao
import (
"fmt"
"strconv"
"strings"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/models"
)
type MaterialGroupK3cloudDao struct {
transactionContext *pgTransaction.TransactionContext
}
func NewMaterialGroupK3cloudDao(transactionContext *pgTransaction.TransactionContext) (*MaterialGroupK3cloudDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &MaterialGroupK3cloudDao{
transactionContext: transactionContext,
}, nil
}
}
//SyncDataMaterialGroupK3cloud 同步MaterialGroupK3cloud表 物料分组数据
func (d *MaterialGroupK3cloudDao) SyncDataMaterialGroupK3cloud(data []models.MaterialGroupK3cloud) error {
sqlValues := []string{}
var strTemp []string
for i := range data {
strTemp = make([]string, 0, 5)
strTemp = append(strTemp, strconv.Itoa(data[i].Id))
strTemp = append(strTemp, strconv.Itoa(data[i].ParentId))
strTemp = append(strTemp, `'`+data[i].Number+`'`)
strTemp = append(strTemp, `'`+data[i].Name+`'`)
strTemp = append(strTemp, strconv.Itoa(int(data[i].DataVersion)))
sqlValues = append(sqlValues, "("+strings.Join(strTemp, ",")+")")
}
var valueTemp []string
for i := 0; i < len(sqlValues); i += 100 {
if i <= len(sqlValues)-100 {
valueTemp = sqlValues[i : i+100]
} else {
valueTemp = sqlValues[i:]
}
sql := `INSERT INTO "manufacture"."material_group_k3cloud" (
"id","parent_id","number","name","data_version")
VALUES ` + strings.Join(valueTemp, ",") + `
ON conflict ( "id" ) DO
UPDATE
SET (
"parent_id","number","name","data_version") = (
EXCLUDED."parent_id",EXCLUDED."number",
EXCLUDED."name",EXCLUDED."data_version" )`
_, err := d.transactionContext.PgTx.Exec(sql)
if err != nil {
return err
}
}
return nil
}