From 732e1e344addd02a4fa56531c7ffd0319e2d5b6c Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期二, 28 十一月 2023 17:44:31 +0800
Subject: [PATCH] 进度更改兼容新旧模板

---
 service/report_work.go       |    2 
 service/progress.go          |   43 +++++++++++++++++----
 model/production_progress.go |   25 +++++++++---
 3 files changed, 53 insertions(+), 17 deletions(-)

diff --git a/model/production_progress.go b/model/production_progress.go
index 293a660..76fbd68 100644
--- a/model/production_progress.go
+++ b/model/production_progress.go
@@ -9,13 +9,15 @@
 type (
 	ProductionProgress struct {
 		gorm.Model
-		WorkOrderID      string `gorm:"index;type:varchar(191);not null" json:"workOrderID"`
-		OrderID          string `gorm:"index;type:varchar(191);not null" json:"orderID"`
-		ProcedureID      string `gorm:"type:varchar(191)" json:"procedureId"`
-		DeviceID         string `gorm:"type:varchar(191);not null" json:"deviceId"`
-		FinishedQuantity int64  `gorm:"type:int;not null" json:"finishedQuantity"`
-		Channel          int32  `gorm:"type:int" json:"channel"` //閫氶亾
-		TotalQuantity    int64  `gorm:"type:int;not null" json:"totalQuantity"`
+		ProceduresID       uint   `gorm:"index;type:varchar(191)" json:"proceduresId"` //procedures琛ㄧ殑id
+		WorkOrderID        string `gorm:"index;type:varchar(191);not null" json:"workOrderID"`
+		OrderID            string `gorm:"index;type:varchar(191);not null" json:"orderID"`
+		ProcedureID        string `gorm:"type:varchar(191)" json:"procedureId"`
+		ProductProcedureID string `gorm:"type:varchar(191);not null" json:"productProcedureID"` //浜у搧宸ュ簭id
+		DeviceID           string `gorm:"type:varchar(191);not null" json:"deviceId"`
+		FinishedQuantity   int64  `gorm:"type:int;not null" json:"finishedQuantity"`
+		Channel            int32  `gorm:"type:int" json:"channel"` //閫氶亾
+		TotalQuantity      int64  `gorm:"type:int;not null" json:"totalQuantity"`
 	}
 
 	ProductionProgressSearch struct {
@@ -64,6 +66,11 @@
 	return slf
 }
 
+func (slf *ProductionProgressSearch) SetProceduresId(proceduresId uint) *ProductionProgressSearch {
+	slf.ProceduresID = proceduresId
+	return slf
+}
+
 func (slf *ProductionProgressSearch) SetDeviceId(id string) *ProductionProgressSearch {
 	slf.DeviceID = id
 	return slf
@@ -107,6 +114,10 @@
 		db = db.Where("procedure_id = ?", slf.ProcedureID)
 	}
 
+	if slf.ProceduresID != 0 {
+		db = db.Where("procedures_id = ?", slf.ProceduresID)
+	}
+
 	if slf.DeviceID != "" {
 		db = db.Where("device_id = ?", slf.DeviceID)
 	}
diff --git a/service/progress.go b/service/progress.go
index ee41c05..4205413 100644
--- a/service/progress.go
+++ b/service/progress.go
@@ -14,15 +14,17 @@
 }
 
 func (slf ProgressService) Add(db *gorm.DB, procedure *model.Procedures, order *model.Order) error {
-	_, err := model.NewProductionProgressSearch(db).SetProcedureId(procedure.ProcedureID).SetWorkOrderId(procedure.WorkOrderID).First()
+	_, err := model.NewProductionProgressSearch(db).SetProceduresId(procedure.ID).First()
 	if err == gorm.ErrRecordNotFound {
 		progress := &model.ProductionProgress{
-			WorkOrderID:   procedure.WorkOrderID,
-			OrderID:       procedure.OrderID,
-			ProcedureID:   procedure.ProceduresInfo.ProcedureID,
-			DeviceID:      procedure.DeviceID,
-			TotalQuantity: order.Amount.IntPart(),
-			Channel:       procedure.Channel,
+			ProceduresID:       procedure.ID,
+			WorkOrderID:        procedure.WorkOrderID,
+			OrderID:            procedure.OrderID,
+			ProcedureID:        procedure.ProceduresInfo.ProcedureID,
+			ProductProcedureID: procedure.ProductProcedureID,
+			DeviceID:           procedure.DeviceID,
+			TotalQuantity:      order.Amount.IntPart(),
+			Channel:            procedure.Channel,
 		}
 		err := model.NewProductionProgressSearch(db).Create(progress)
 		if err != nil {
@@ -34,7 +36,7 @@
 	return nil
 }
 
-// UpdateProgress 浠呴檺plc鏁版嵁閲囬泦瀹氭椂浠诲姟鐢�
+// UpdateProgress 浠呴檺plc鏁版嵁閲囬泦瀹氭椂浠诲姟鐢�(缂哄皯宸ュ簭id鍙傛暟)
 func (slf ProgressService) UpdateProgress(deviceID string, channel int32, finishedQuantity int64) (err error) {
 	progressCache, err := slf.GetCurrentProgress(deviceID, channel)
 	if err != nil {
@@ -51,7 +53,7 @@
 	return nil
 }
 
-// GetCurrentProgress 浠呴檺plc鏁版嵁閲囬泦瀹氭椂浠诲姟鐢�
+// GetCurrentProgress 浠呴檺plc鏁版嵁閲囬泦瀹氭椂浠诲姟鐢�(缂哄皯宸ュ簭id鍙傛暟)
 func (slf ProgressService) GetCurrentProgress(deviceID string, channel int32) (progressCache *model.ProductionProgress, err error) {
 	var ok bool
 	progressCache, ok = ProgressCacheGet(deviceID, channel)
@@ -74,6 +76,29 @@
 	return
 }
 
+func (slf ProgressService) UpdateProgressByProceduresId(proceduresId uint, finishedQuantity int64) (err error) {
+	progress, err := slf.GetCurrentProgressByProceduresId(proceduresId)
+	if err != nil {
+		return err
+	}
+	if progress == nil {
+		return errors.New("progress not exists")
+	}
+	if finishedQuantity > progress.FinishedQuantity { //褰撴湁鍙樺寲鏃舵墠鏇存柊
+		progress.FinishedQuantity = finishedQuantity
+		return model.NewProductionProgressSearch(nil).SetId(progress.ID).Save(progress)
+	}
+	return nil
+}
+
+func (slf ProgressService) GetCurrentProgressByProceduresId(proceduresId uint) (progress *model.ProductionProgress, err error) {
+	progress, err = model.NewProductionProgressSearch(nil).SetProceduresId(proceduresId).First()
+	if err != nil {
+		return nil, err
+	}
+	return
+}
+
 // GetProgressList 鑾峰彇寰呭悓姝ヨ繘搴﹀伐搴�
 func (slf ProgressService) GetProgressList() (progressList []*model.ProductionProgress, err error) {
 	progressList, err = model.NewProductionProgressSearch(nil).SetUnFinished().SetOrder("id desc").SetPage(1, 100).FindNotTotal()
diff --git a/service/report_work.go b/service/report_work.go
index 4cbf466..1c9c50e 100644
--- a/service/report_work.go
+++ b/service/report_work.go
@@ -90,7 +90,7 @@
 		logx.Errorf("save report work transaction error: %v", err)
 		return err
 	}
-	_ = NewProgressService().UpdateProgress(conf.Conf.CurrentDeviceID, procedure.Channel, int64(params.ReportAmount))
+	_ = NewProgressService().UpdateProgressByProceduresId(procedure.ID, int64(params.ReportAmount))
 	return nil
 }
 

--
Gitblit v1.8.0