作者 yangfu

refactor: 车间数据统计修改、修正批次同步批次号重复、时间蹉校正

@@ -91,7 +91,7 @@ func AutoFlushDeviceDailyRunningRecordOEE(ctx context.Context) error { @@ -91,7 +91,7 @@ func AutoFlushDeviceDailyRunningRecordOEE(ctx context.Context) error {
91 log.Logger.Error(err.Error()) 91 log.Logger.Error(err.Error())
92 continue 92 continue
93 } 93 }
94 - if err := redis.SaveDeviceDailyRunningRecord(v); err != nil { 94 + if err := redis.SaveDeviceDailyRunningRecord(v.ProductDate, v); err != nil {
95 log.Logger.Error(err.Error(), map[string]interface{}{"task": "定时刷新设备每日运行记录"}) 95 log.Logger.Error(err.Error(), map[string]interface{}{"task": "定时刷新设备每日运行记录"})
96 continue 96 continue
97 } 97 }
1 package syncdata 1 package syncdata
2 2
3 import ( 3 import (
  4 + "fmt"
4 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant" 5 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
5 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain" 6 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
6 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/domainService" 7 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/domainService"
@@ -523,16 +524,25 @@ func (srv *PullDataK3CloudService) SyncDataProductPlan(ptr *pgTransaction.Transa @@ -523,16 +524,25 @@ func (srv *PullDataK3CloudService) SyncDataProductPlan(ptr *pgTransaction.Transa
523 continue 524 continue
524 } 525 }
525 // 计划信息 526 // 计划信息
526 - plan, err = productPlanRepository.FindOne(map[string]interface{}{"companyId": cid, "orgId": oid, "batchNumber": v.BillNo}) 527 + plan, err = productPlanRepository.FindOne(map[string]interface{}{"companyId": cid, "orgId": oid, "erpBillNo": v.BillNo, "productCode": v.MaterialNumber})
527 if err == nil && plan != nil { 528 if err == nil && plan != nil {
528 - plan.UpdatedAt = time.Now() 529 + //plan.UpdatedAt = time.Now()
529 continue 530 continue
530 } 531 }
  532 + count, _, errFindPlan := productPlanRepository.Find(map[string]interface{}{"companyId": cid, "orgId": oid, "erpBillNo": v.BillNo, "productCode": v.MaterialNumber})
  533 + if errFindPlan != nil {
  534 + continue
  535 + }
  536 + /* ERP系统BillNO + MaterialNumber 唯一 会重复,需要做自增格式化处理*/
  537 + batchNumber := fmt.Sprintf("%v-1", v.BillNo)
  538 + if count >= 1 {
  539 + batchNumber = fmt.Sprintf("%v-%v", v.BillNo, count)
  540 + }
531 if err == domain.ErrorNotFound { 541 if err == domain.ErrorNotFound {
532 plan = &domain.ProductPlan{ 542 plan = &domain.ProductPlan{
533 CompanyId: cid, 543 CompanyId: cid,
534 OrgId: oid, 544 OrgId: oid,
535 - BatchNumber: v.BillNo, 545 + BatchNumber: batchNumber,
536 ProductDate: v.PlanStartDate, 546 ProductDate: v.PlanStartDate,
537 Workshop: workshop.CloneSample(), 547 Workshop: workshop.CloneSample(),
538 WorkOn: 0, 548 WorkOn: 0,
@@ -557,6 +567,7 @@ func (srv *PullDataK3CloudService) SyncDataProductPlan(ptr *pgTransaction.Transa @@ -557,6 +567,7 @@ func (srv *PullDataK3CloudService) SyncDataProductPlan(ptr *pgTransaction.Transa
557 ProductCode: product.ProductCode, 567 ProductCode: product.ProductCode,
558 ProductName: product.ProductName, 568 ProductName: product.ProductName,
559 DevotedUnit: "份", 569 DevotedUnit: "份",
  570 + ErpBillNo: v.BillNo,
560 }) 571 })
561 plan.PlanProductName = product.ProductName 572 plan.PlanProductName = product.ProductName
562 plan.Remark = v.Description 573 plan.Remark = v.Description
@@ -20,16 +20,16 @@ var MQTT_TOPIC = "/MQTT" @@ -20,16 +20,16 @@ var MQTT_TOPIC = "/MQTT"
20 //var MQTT_USER = "" 20 //var MQTT_USER = ""
21 //var MQTT_PASSWORD = "" 21 //var MQTT_PASSWORD = ""
22 22
23 -var MQTT_HOST = "175.24.122.87"  
24 -var MQTT_PORT = "1883"  
25 -var MQTT_USER = "user111"  
26 -var MQTT_PASSWORD = "user111" 23 +//var MQTT_HOST = "175.24.122.87"
  24 +//var MQTT_PORT = "1883"
  25 +//var MQTT_USER = "user111"
  26 +//var MQTT_PASSWORD = "user111"
27 27
28 //内网测试地址 28 //内网测试地址
29 -//var MQTT_HOST = "192.168.100.222"  
30 -//var MQTT_PORT = "1883"  
31 -//var MQTT_USER = "admin"  
32 -//var MQTT_PASSWORD = "123456" 29 +var MQTT_HOST = "47.97.5.102"
  30 +var MQTT_PORT = "6000"
  31 +var MQTT_USER = "admin"
  32 +var MQTT_PASSWORD = "123456"
33 33
34 func init() { 34 func init() {
35 if os.Getenv("MQTT_HOST") != "" { 35 if os.Getenv("MQTT_HOST") != "" {
@@ -78,6 +78,8 @@ func (deviceDailyRunningRecord *DeviceDailyRunningRecord) String() string { @@ -78,6 +78,8 @@ func (deviceDailyRunningRecord *DeviceDailyRunningRecord) String() string {
78 78
79 // 设备运行记录信息 79 // 设备运行记录信息
80 type DeviceRunningRecordInfo struct { 80 type DeviceRunningRecordInfo struct {
  81 + // 日期
  82 + ProductDate string `json:"productDate"`
81 // 设备类型 83 // 设备类型
82 DeviceType string `json:"deviceType"` 84 DeviceType string `json:"deviceType"`
83 // 当前状态 85 // 当前状态
@@ -12,4 +12,6 @@ type ProductPlanExt struct { @@ -12,4 +12,6 @@ type ProductPlanExt struct {
12 //ProductSpec *UnitQuantity `json:"productSpec,omitempty"` 12 //ProductSpec *UnitQuantity `json:"productSpec,omitempty"`
13 // 投入量规格 默认份 13 // 投入量规格 默认份
14 DevotedUnit string `json:"devotedUnit" ` 14 DevotedUnit string `json:"devotedUnit" `
  15 + // ERP系统里面的 bill_no
  16 + ErpBillNo string `json:"erpBillNo" `
15 } 17 }
@@ -164,7 +164,7 @@ from ts_product_list @@ -164,7 +164,7 @@ from ts_product_list
164 func (dao *DeviceDailyRunningRecordDao) DeviceRunningInfo(companyId, orgId int, deviceCode string, result interface{}) error { 164 func (dao *DeviceDailyRunningRecordDao) DeviceRunningInfo(companyId, orgId int, deviceCode string, result interface{}) error {
165 tx := dao.transactionContext.PgTx 165 tx := dao.transactionContext.PgTx
166 sql := fmt.Sprintf(` 166 sql := fmt.Sprintf(`
167 -select * from device_running_info(?,?,?); 167 +select * from manufacture.device_running_info(?,?,?);
168 `) 168 `)
169 if _, err := tx.Query(result, sql, deviceCode, companyId, orgId); err != nil { 169 if _, err := tx.Query(result, sql, deviceCode, companyId, orgId); err != nil {
170 return err 170 return err
@@ -110,7 +110,7 @@ func (ptr *PGWorkshopDataConsumeService) Consume(companyId, orgId int, record *d @@ -110,7 +110,7 @@ func (ptr *PGWorkshopDataConsumeService) Consume(companyId, orgId int, record *d
110 SendProductRecordStaticsJob(productRecord) 110 SendProductRecordStaticsJob(productRecord)
111 } 111 }
112 } 112 }
113 - if err = redis.SaveDeviceDailyRunningRecord(deviceDailyRecord); err != nil { 113 + if err = redis.SaveDeviceDailyRunningRecord(deviceRunningData.CollectionTime, deviceDailyRecord); err != nil {
114 return nil, err 114 return nil, err
115 } 115 }
116 return nil, nil 116 return nil, nil
@@ -293,6 +293,7 @@ func (ptr *PGWorkshopDataConsumeService) saveDeviceDailyRunningRecord(companyId, @@ -293,6 +293,7 @@ func (ptr *PGWorkshopDataConsumeService) saveDeviceDailyRunningRecord(companyId,
293 recordInfo.ProductPlanId = planId 293 recordInfo.ProductPlanId = planId
294 recordInfo.DeviceName = device.DeviceName 294 recordInfo.DeviceName = device.DeviceName
295 recordInfo.OrgName = device.Ext.OrgName 295 recordInfo.OrgName = device.Ext.OrgName
  296 + recordInfo.ProductDate = data.CollectionTime.Format("2006-01-02")
296 record = &domain.DeviceDailyRunningRecord{ 297 record = &domain.DeviceDailyRunningRecord{
297 CompanyId: companyId, 298 CompanyId: companyId,
298 OrgId: orgId, 299 OrgId: orgId,
@@ -32,9 +32,9 @@ func getDeviceDailyRunningRecord(client *redis.Client, key string) (*domain.Devi @@ -32,9 +32,9 @@ func getDeviceDailyRunningRecord(client *redis.Client, key string) (*domain.Devi
32 } 32 }
33 33
34 // 保存每日设备运行数据 34 // 保存每日设备运行数据
35 -func SaveDeviceDailyRunningRecord(record *domain.DeviceDailyRunningRecord) error { 35 +func SaveDeviceDailyRunningRecord(t time.Time, record *domain.DeviceDailyRunningRecord) error {
36 client := GetRedis() 36 client := GetRedis()
37 - key := DeviceDailyRunningRecordKey(record.ProductDate, record.DeviceCode) 37 + key := DeviceDailyRunningRecordKey(t, record.DeviceCode)
38 log.Logger.Debug(fmt.Sprintf("Redis Device:%v SET Key:%v Count:%v", record.DeviceCode, key, record.DeviceRunningRecordInfo.Count)) 38 log.Logger.Debug(fmt.Sprintf("Redis Device:%v SET Key:%v Count:%v", record.DeviceCode, key, record.DeviceRunningRecordInfo.Count))
39 recordData, err := json.Marshal(record) 39 recordData, err := json.Marshal(record)
40 result := client.Set(key, recordData, time.Hour*24*5) 40 result := client.Set(key, recordData, time.Hour*24*5)
@@ -151,6 +151,12 @@ func (repository *ProductPlanRepository) FindOne(queryOptions map[string]interfa @@ -151,6 +151,12 @@ func (repository *ProductPlanRepository) FindOne(queryOptions map[string]interfa
151 query.SetWhereByQueryOption("company_id = ?", "companyId") 151 query.SetWhereByQueryOption("company_id = ?", "companyId")
152 query.SetWhereByQueryOption("org_id = ?", "orgId") 152 query.SetWhereByQueryOption("org_id = ?", "orgId")
153 query.SetWhereByQueryOption("batch_number=?", "batchNumber") 153 query.SetWhereByQueryOption("batch_number=?", "batchNumber")
  154 + if v, ok := queryOptions["erpBillNo"]; ok && len(v.(string)) > 0 {
  155 + query.Where(fmt.Sprintf(`ext#>'{productPlanExt,erpBillNo}' = '"%v"'`, v))
  156 + }
  157 + if v, ok := queryOptions["productCode"]; ok && len(v.(string)) > 0 {
  158 + query.Where(fmt.Sprintf(`ext#>'{productPlanExt,productCode}' = '"%v"'`, v))
  159 + }
154 if v, ok := queryOptions["includeDeleted"]; ok && v.(bool) { 160 if v, ok := queryOptions["includeDeleted"]; ok && v.(bool) {
155 query.AllWithDeleted() 161 query.AllWithDeleted()
156 } 162 }
@@ -181,6 +187,12 @@ func (repository *ProductPlanRepository) Find(queryOptions map[string]interface{ @@ -181,6 +187,12 @@ func (repository *ProductPlanRepository) Find(queryOptions map[string]interface{
181 if v, ok := queryOptions["workshopName"]; ok && len(v.(string)) > 0 { 187 if v, ok := queryOptions["workshopName"]; ok && len(v.(string)) > 0 {
182 query.Where(fmt.Sprintf(`workshop->>'workshopName' like '%%%v%%'`, v)) 188 query.Where(fmt.Sprintf(`workshop->>'workshopName' like '%%%v%%'`, v))
183 } 189 }
  190 + if v, ok := queryOptions["erpBillNo"]; ok && len(v.(string)) > 0 {
  191 + query.Where(fmt.Sprintf(`ext#>'{productPlanExt,erpBillNo}' = '"%v"'`, v))
  192 + }
  193 + if v, ok := queryOptions["productCode"]; ok && len(v.(string)) > 0 {
  194 + query.Where(fmt.Sprintf(`ext#>'{productPlanExt,productCode}' = '"%v"'`, v))
  195 + }
184 query.SetWhereByQueryOption("plan_status=?", "planStatus") 196 query.SetWhereByQueryOption("plan_status=?", "planStatus")
185 query.SetOffsetAndLimit(domain.MaxQueryRow) 197 query.SetOffsetAndLimit(domain.MaxQueryRow)
186 query.SetOrderDirect("product_plan_id", "DESC") 198 query.SetOrderDirect("product_plan_id", "DESC")
  1 +package utils
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/linmadan/egglib-go/utils/json"
  6 + "testing"
  7 + "time"
  8 +)
  9 +
  10 +type User struct {
  11 + Age time.Time
  12 + AgeZero time.Time
  13 + AgeZeroFormat string
  14 +}
  15 +
  16 +func TestTimeParseInLocal(t *testing.T) {
  17 + timeStr := "2022-01-02 1:00:00"
  18 + tl, _ := time.ParseInLocation("2006-01-02 15:04:05", timeStr, time.Local)
  19 + u1 := User{
  20 + Age: tl,
  21 + AgeZero: GetZeroTime(tl),
  22 + }
  23 + u1.AgeZeroFormat = u1.AgeZero.Format("2006-01-02")
  24 +
  25 + fmt.Println(json.MarshalToString(u1), u1.Age.Local())
  26 +
  27 + u2 := User{
  28 + Age: tl,
  29 + AgeZero: GetZeroTimeWithLocal(tl, time.Local),
  30 + }
  31 + u2.AgeZeroFormat = u2.AgeZero.Format("2006-01-02")
  32 +
  33 + fmt.Println(json.MarshalToString(u2), u2.Age.Local())
  34 +}
@@ -55,7 +55,7 @@ func TestComputeTimeDuration(t *testing.T) { @@ -55,7 +55,7 @@ func TestComputeTimeDuration(t *testing.T) {
55 } 55 }
56 } 56 }
57 57
58 -const TIME_LAYOUT = "2006-01-02 03:04:05" 58 +const TIME_LAYOUT = "2006-01-02 15:04:05"
59 59
60 func TestTimeParse(t *testing.T) { 60 func TestTimeParse(t *testing.T) {
61 fmt.Println(int(-1)) 61 fmt.Println(int(-1))
@@ -64,11 +64,13 @@ func TestTimeParse(t *testing.T) { @@ -64,11 +64,13 @@ func TestTimeParse(t *testing.T) {
64 64
65 func timeParse() { 65 func timeParse() {
66 fmt.Println("0. now: ", time.Now()) 66 fmt.Println("0. now: ", time.Now())
67 - str := "2018-09-10 10:00:00" 67 + str := "2018-09-10 16:00:00"
68 fmt.Println("1. str: ", str) 68 fmt.Println("1. str: ", str)
69 - t, _ := time.Parse(TIME_LAYOUT, str)  
70 - fmt.Println("2. Parse time: ", t) 69 + t, err := time.Parse(TIME_LAYOUT, str)
  70 + fmt.Println("2. Parse time: ", t, err)
71 fmt.Println("2.1. Parse time: ", t.Local(), t.Local().Local()) 71 fmt.Println("2.1. Parse time: ", t.Local(), t.Local().Local())
  72 + parseInLocal, _ := time.ParseInLocation(TIME_LAYOUT, str, time.Local)
  73 + fmt.Println("2.2 parse in local", parseInLocal, parseInLocal.Local())
72 tStr := t.Format(TIME_LAYOUT) 74 tStr := t.Format(TIME_LAYOUT)
73 fmt.Println("3. Format time str: ", tStr) 75 fmt.Println("3. Format time str: ", tStr)
74 name, offset := t.Zone() 76 name, offset := t.Zone()