prd_mo_k3cloud.go
6.4 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package dao
import (
"fmt"
"strconv"
"strings"
"time"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/models"
)
//
type PrdMoK3cloudDao struct {
transactionContext *pgTransaction.TransactionContext
}
func NewPrdMoK3cloudDao(transactionContext *pgTransaction.TransactionContext) (*PrdMoK3cloudDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &PrdMoK3cloudDao{
transactionContext: transactionContext,
}, nil
}
}
//SyncDataPrdMoK3cloud 同步PrdMoK3cloud表数据
func (d *PrdMoK3cloudDao) SyncDataPrdMoK3cloud(data []models.PrdMoK3cloud) error {
//插入或者更新
sqlValues := []string{}
var strTemp []string
for i := range data {
strTemp = make([]string, 0, 15)
strTemp = append(strTemp, `'`+data[i].RowId+`'`)
strTemp = append(strTemp, `'`+data[i].BillNo+`'`)
strTemp = append(strTemp, strconv.Itoa(data[i].WorkShopID))
strTemp = append(strTemp, `'`+data[i].WorkShopName+`'`)
strTemp = append(strTemp, strconv.Itoa(data[i].MaterialId))
strTemp = append(strTemp, `'`+data[i].MaterialName+`'`)
strTemp = append(strTemp, `'`+data[i].MaterialNumber+`'`)
strTemp = append(strTemp, `'`+data[i].PlanStartDate.Format("2006-01-02 15:04:05.999")+`'`)
strTemp = append(strTemp, `'`+data[i].PlanFinishDate.Format("2006-01-02 15:04:05.999")+`'`)
strTemp = append(strTemp, `'`+data[i].FDate.Format("2006-01-02 15:04:05.999")+`'`)
strTemp = append(strTemp, fmt.Sprintf("%.10f", data[i].Qty)) //15.5000000000
strTemp = append(strTemp, `'`+data[i].CreateDate.Format("2006-01-02 15:04:05.999")+`'`)
strTemp = append(strTemp, `'`+data[i].ModifyDate.Format("2006-01-02 15:04:05.999")+`'`)
strTemp = append(strTemp, strconv.Itoa(int(data[i].DataVersion)))
strTemp = append(strTemp, strconv.Itoa(data[i].PrdOrgId))
strTemp = append(strTemp, `'`+data[i].PrdOrgName+`'`)
strTemp = append(strTemp, `'`+data[i].UnitName+`'`)
strTemp = append(strTemp, strconv.Itoa(data[i].UnitId))
strTemp = append(strTemp, `'`+data[i].Specification+`'`)
strTemp = append(strTemp, `'`+data[i].Description+`'`)
//关联的计划管理表id ,使用 product_plan 产品表的自增序列表
strTemp = append(strTemp, "nextval('manufacture.product_plan_product_plan_id_seq'::regclass)")
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"."prd_mo_k3cloud" (
"row_id","bill_no","work_shop_id","work_shop_name",
"material_id","material_name","material_number",
"plan_start_date","plan_finish_date","f_date",
"qty","create_date","modify_date","data_version",
"prd_org_id","prd_org_name","unit_name","unit_id",
"specification","description","join_product_plan_id")
VALUES ` +
strings.Join(valueTemp, ",") +
` ON conflict ( "row_id" ) DO
UPDATE
SET (
"bill_no","work_shop_id","work_shop_name",
"material_id","material_name","material_number",
"plan_start_date","plan_finish_date","f_date",
"qty","create_date","modify_date","data_version",
"prd_org_id","prd_org_name","unit_name","unit_id",
"specification","description") = (
EXCLUDED."bill_no",EXCLUDED."work_shop_id",
EXCLUDED."work_shop_name",EXCLUDED."material_id",
EXCLUDED."material_name",EXCLUDED."material_number",
EXCLUDED."plan_start_date",EXCLUDED."plan_finish_date",
EXCLUDED."f_date",EXCLUDED."qty",EXCLUDED."create_date",
EXCLUDED."modify_date",EXCLUDED."data_version",
EXCLUDED."prd_org_id",EXCLUDED."prd_org_name",
EXCLUDED."unit_name",EXCLUDED."unit_id",
EXCLUDED."specification",EXCLUDED."description"
)`
_, err := d.transactionContext.PgTx.Exec(sql)
if err != nil {
return err
}
}
return nil
}
func (d *PrdMoK3cloudDao) GetLastVersion() (int64, error) {
var prdMoData []models.PrdMoK3cloud
err := d.transactionContext.PgTx.Model(&prdMoData).
Order("data_version DESC").
Limit(1).
Select()
if err != nil {
return 0, err
}
if len(prdMoData) == 0 {
return 0, nil
}
return prdMoData[0].DataVersion, nil
}
//SyncDataProductPlan 同步数据到
func (d *PrdMoK3cloudDao) SyncDataProductPlan(version int64, companyId, orgId int, orgName string) error {
sql := fmt.Sprintf(`
INSERT INTO "manufacture"."product_plan"(
"company_id","org_id",
"product_plan_id","batch_number","product_date","workshop",
"plan_product_name","plan_devoted","plan_status","remark",
"ext","created_at","updated_at"
)
SELECT ?,?,
prd_mo_k3cloud."join_product_plan_id",prd_mo_k3cloud."bill_no",
prd_mo_k3cloud."plan_start_date",
json_build_object('workshopId',COALESCE("workshop"."workshop_id",0),'workshopName',prd_mo_k3cloud."work_shop_name"),
prd_mo_k3cloud."material_name", json_build_object('unit',prd_mo_k3cloud."unit_name",'quantity',prd_mo_k3cloud."qty"),
2,prd_mo_k3cloud."description",
json_build_object('orgName','%v','productPlanExt',json_build_object('productId',prd_mo_k3cloud."join_product_plan_id",'productCode',prd_mo_k3cloud."material_number",'productName',prd_mo_k3cloud."material_name")),
now(),now()
FROM "manufacture"."prd_mo_k3cloud"
-- LEFT JOIN "manufacture"."material_k3cloud" ON "prd_mo_k3cloud"."material_id"="material_k3cloud"."material_id"
INNER JOIN "manufacture"."workshop" ON "workshop"."workshop_name" = "prd_mo_k3cloud"."work_shop_name"
WHERE prd_mo_k3cloud."data_version">=?
ON conflict ("product_plan_id") DO
UPDATE
SET (
"batch_number","product_date","workshop",
"plan_product_name","plan_devoted","plan_status","remark",
"ext","updated_at"
)=(
EXCLUDED."batch_number",
EXCLUDED."product_date",
"product_plan"."workshop"||EXCLUDED."workshop",
EXCLUDED."plan_product_name",
EXCLUDED."plan_devoted",
EXCLUDED."plan_status",
EXCLUDED."remark",
"product_plan"."ext"||EXCLUDED."ext",
EXCLUDED."updated_at"
)
`, orgName)
_, err := d.transactionContext.PgTx.Exec(sql, companyId, orgId, version)
return err
}
// 增量获取数据
func (d *PrdMoK3cloudDao) GetLatestData(fromTime time.Time) ([]*models.PrdMoK3cloud, error) {
m := new(models.PrdMoK3cloud)
result := make([]*models.PrdMoK3cloud, 0)
query := d.transactionContext.PgTx.Model(m)
query.Where("modify_date>=?", fromTime)
err := query.Select(&result)
if err != nil {
return nil, err
}
return result, nil
}