material_k3cloud_dao.go
5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package dao
import (
"fmt"
"strconv"
"strings"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/models"
)
type MaterialK3cloudDao struct {
transactionContext *pgTransaction.TransactionContext
}
func NewMaterialK3cloudDao(transactionContext *pgTransaction.TransactionContext) (*MaterialK3cloudDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &MaterialK3cloudDao{
transactionContext: transactionContext,
}, nil
}
}
//SyncDataMaterialK3cloud 同步MaterialK3cloud表数据
func (d *MaterialK3cloudDao) SyncDataMaterialK3cloud(data []models.MaterialK3cloud) error {
sqlValues := []string{}
var strTemp []string
for i := range data {
strTemp = make([]string, 0, 18)
strTemp = append(strTemp, strconv.Itoa(data[i].MaterialId))
strTemp = append(strTemp, `'`+data[i].Name+`'`)
strTemp = append(strTemp, `'`+data[i].Number+`'`)
strTemp = append(strTemp, `'`+data[i].Specification+`'`)
strTemp = append(strTemp, `'`+data[i].ForbidStatus+`'`)
strTemp = append(strTemp, strconv.Itoa(data[i].ErpClsId))
strTemp = append(strTemp, strconv.Itoa(data[i].BaseUnitId))
strTemp = append(strTemp, `'`+data[i].BaseUnitName+`'`)
if data[i].CreateDate.IsZero() {
strTemp = append(strTemp, `NULL`)
} else {
strTemp = append(strTemp, `'`+data[i].CreateDate.Format("2006-01-02 15:04:05.999")+`'`)
}
if data[i].ModifyDate.IsZero() {
strTemp = append(strTemp, `NULL`)
} else {
strTemp = append(strTemp, `'`+data[i].ModifyDate.Format("2006-01-02 15:04:05.999")+`'`)
}
if data[i].ForbidDate.IsZero() {
strTemp = append(strTemp, `NULL`)
} else {
strTemp = append(strTemp, `'`+data[i].ForbidDate.Format("2006-01-02 15:04:05.999")+`'`)
}
if data[i].ApproveDate.IsZero() {
strTemp = append(strTemp, `NULL`)
} else {
strTemp = append(strTemp, `'`+data[i].ApproveDate.Format("2006-01-02 15:04:05.999")+`'`)
}
strTemp = append(strTemp, strconv.Itoa(data[i].MaterialGroup))
strTemp = append(strTemp, `'`+data[i].MaterialGroupNumber+`'`)
strTemp = append(strTemp, `'`+data[i].MaterialGroupName+`'`)
strTemp = append(strTemp, strconv.Itoa(data[i].RefStatus))
//关联的产品表id ,使用 product 产品表的自增序列表
strTemp = append(strTemp, "nextval('manufacture.product_product_id_seq'::regclass)")
strTemp = append(strTemp, strconv.Itoa(int(data[i].DataVersion)))
strTemp = append(strTemp, strconv.Itoa(data[i].UseOrgId))
strTemp = append(strTemp, `'`+data[i].UseOrgName+`'`)
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_k3cloud" (
"material_id","name","number","specification","forbid_status",
"erp_cls_id","base_unit_id","base_unit_name","create_date",
"modify_date","forbid_date","approve_date","material_group",
"material_group_number","material_group_name","ref_status",
"join_product_id","data_version","use_org_id","use_org_name" )
VALUES ` + strings.Join(valueTemp, ",") +
` ON conflict ( material_id ) DO
UPDATE
SET (
"name","number","specification","forbid_status","erp_cls_id",
"base_unit_id","base_unit_name","create_date","modify_date",
"forbid_date","approve_date","material_group","material_group_number",
"material_group_name","ref_status","data_version","use_org_id","use_org_name" ) = (
EXCLUDED."name",EXCLUDED."number",EXCLUDED."specification",
EXCLUDED."forbid_status",EXCLUDED."erp_cls_id",EXCLUDED."base_unit_id",
EXCLUDED."base_unit_name",EXCLUDED."create_date",EXCLUDED."modify_date",
EXCLUDED."forbid_date",EXCLUDED."approve_date",EXCLUDED."material_group",
EXCLUDED."material_group_number",EXCLUDED."material_group_name",
EXCLUDED."ref_status",EXCLUDED."data_version",EXCLUDED."use_org_id",EXCLUDED."use_org_name")`
_, err := d.transactionContext.PgTx.Exec(sql)
if err != nil {
return err
}
}
return nil
}
//SyncDataProudct 同步MaterialK3cloud表数据到Proudct表
func (d *MaterialK3cloudDao) SyncDataProudct(version int64, orgName string) error {
// -- 插入或者更新
sql := fmt.Sprintf(`INSERT INTO "manufacture"."product"(
"company_id", "org_id", "product_id", "product_code", "product_name",
"product_category", "product_spec", "created_at", "updated_at","ext"
)
SELECT %v,%v,"join_product_id","number","name","material_group_name",
json_build_object('unit',specification),now(),now(),json_build_object('orgName','%v')
FROM "manufacture"."material_k3cloud" WHERE "data_version">=? AND "material_group_number" LIKE '05%%'
ON conflict ( product_id ) DO
UPDATE
SET (
"company_id", "org_id", "product_id", "product_code", "product_name",
"product_category", "product_spec", "updated_at")=(
EXCLUDED."company_id", EXCLUDED."org_id",EXCLUDED."product_id",
EXCLUDED."product_code",EXCLUDED."product_name",EXCLUDED."product_category",
EXCLUDED."product_spec", EXCLUDED."updated_at") `, constant.MANUFACTURE_DEFAULT_COMPANYID, constant.MANUFACTURE_DEFAULT_ORGID, orgName)
_, err := d.transactionContext.PgTx.Exec(sql, version)
return err
}
func (d *MaterialK3cloudDao) GetLastVersion() (int64, error) {
var materialData []models.MaterialK3cloud
err := d.transactionContext.PgTx.Model(&materialData).
Order("data_version DESC").
Limit(1).
Select()
if err != nil {
return 0, err
}
if len(materialData) == 0 {
return 0, nil
}
return materialData[0].DataVersion, nil
}