From f838162ed0ee7f2832924c2399eddd461760135a Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期五, 13 十月 2023 21:47:54 +0800
Subject: [PATCH] plc地址和工艺参数不再单独拉取,只serf主启动时拉取

---
 api/v1/task.go |   84 +++++++++++------------------------------
 1 files changed, 23 insertions(+), 61 deletions(-)

diff --git a/api/v1/task.go b/api/v1/task.go
index 44aa2fb..96a0b1a 100644
--- a/api/v1/task.go
+++ b/api/v1/task.go
@@ -4,21 +4,17 @@
 	"apsClient/conf"
 	"apsClient/constvar"
 	"apsClient/model"
-	"apsClient/model/common"
 	"apsClient/model/request"
 	"apsClient/model/response"
-	"apsClient/nsq"
 	"apsClient/pkg/contextx"
-	"apsClient/pkg/convertx"
 	"apsClient/pkg/ecode"
 	"apsClient/pkg/logx"
-	"apsClient/pkg/safe"
 	"apsClient/service"
 	"apsClient/service/plc_address"
 	"errors"
-	"fmt"
 	"github.com/gin-gonic/gin"
-	"gorm.io/gorm"
+	"github.com/jinzhu/gorm"
+	"github.com/spf13/cast"
 	"sort"
 	"sync"
 	"time"
@@ -144,7 +140,7 @@
 		ctx.Fail(ecode.ParamsErr)
 		return
 	}
-	id := convertx.Atoi(idx)
+	id := cast.ToUint(idx)
 	procedure, code := service.NewTaskService().GetProcedureById(id)
 	if code != ecode.OK {
 		ctx.Fail(code)
@@ -156,11 +152,19 @@
 		ctx.Fail(ecode.DBErr)
 		return
 	}
-
-	processModel, err := service.NewTaskService().GetProcessParams(procedure, order)
-	if err != nil || processModel == nil || processModel.ParamsMap == nil {
-		ctx.FailWithMsg(ecode.ParamsErr, "鏈幏鍙栧埌宸ヨ壓鍙傛暟锛岃鍦ㄥ伐鑹烘ā鍨嬪簱涓笂浼狅紒")
-		return
+	var processModel *model.ProcessModel
+	if procedure.ProcessModelNumber != "" {
+		processModel, err = service.GetProcessModelByNumber(procedure.ProcessModelNumber)
+		if err != nil || processModel == nil || processModel.ParamsMap == nil {
+			ctx.FailWithMsg(ecode.ParamsErr, "鏈幏鍙栧埌宸ヨ壓鍙傛暟锛岃鍦ㄥ伐鑹烘ā鍨嬪簱涓笂浼狅紒")
+			return
+		}
+	} else {
+		processModel, err = service.NewTaskService().GetProcessParams(procedure, order)
+		if err != nil || processModel == nil || processModel.ParamsMap == nil {
+			ctx.FailWithMsg(ecode.ParamsErr, "鏈幏鍙栧埌宸ヨ壓鍙傛暟锛岃鍦ㄥ伐鑹烘ā鍨嬪簱涓笂浼狅紒")
+			return
+		}
 	}
 
 	processParamsArr := make([]response.ProcessParams, 0, len(processModel.ParamsMap))
@@ -170,15 +174,6 @@
 			Value: v,
 		})
 	}
-
-	safe.Go(func() {
-		caller := nsq.NewCaller(fmt.Sprintf(constvar.NsqTopicGetPlcAddress, conf.Conf.NsqConf.NodeId), fmt.Sprintf(constvar.NsqTopicSendPlcAddress, conf.Conf.NsqConf.NodeId))
-		var addressResult common.ResponsePlcAddress
-		err := caller.Call(common.RequestPlcAddress{DeviceId: conf.Conf.System.DeviceId}, &addressResult, time.Second*3)
-		if err != nil {
-			logx.Infof("get plc address err: %v", err.Error())
-		}
-	})
 
 	resp := response.ProcessParamsResponse{
 		Number: processModel.Number,
@@ -204,7 +199,7 @@
 		ctx.Fail(ecode.ParamsErr)
 		return
 	}
-	id := convertx.Atoi(idx)
+	id := cast.ToUint(idx)
 	procedure, code := service.NewTaskService().GetProcedureById(id)
 	if code != ecode.OK {
 		ctx.Fail(code)
@@ -223,20 +218,6 @@
 		logx.Errorf("UpdateProcedureStatus err: %v", err.Error())
 		ctx.Fail(ecode.UnknownErr)
 		return
-	}
-
-	msg := &common.MsgTaskStatusUpdate{
-		WorkOrderId:  procedure.WorkOrderID,
-		ProcedureID:  procedure.ProceduresInfo.ProcedureID,
-		DeviceId:     procedure.ProceduresInfo.DeviceID,
-		IsProcessing: false,
-		IsFinish:     true,
-	}
-
-	caller := nsq.NewCaller(fmt.Sprintf(constvar.NsqTopicTaskProcedureStatusUpdate, conf.Conf.NsqConf.NodeId), "")
-	err = caller.Send(msg)
-	if err != nil {
-		logx.Errorf("send task status update msg error:%v", err.Error())
 	}
 
 	service.TaskFlagUnset(procedure.Channel)
@@ -284,11 +265,10 @@
 	}
 
 	err = model.WithTransaction(func(db *gorm.DB) error {
-		err = taskService.UpdateProcedureStatusAndChannel(db, params.ProcedureId, model.ProcedureStatusProcessing, params.Channel)
+		err = taskService.UpdateProcedureStatusAndChannel(db, params.ProcedureId, model.ProcedureStatusProcessing, procedure.Channel, processModel.Number)
 		if err != nil {
 			return err
 		}
-		procedure.Channel = params.Channel
 		err = taskService.UpdateOrderStatus(db, order.ID, model.OrderStatusProcessing)
 		if err != nil {
 			return err
@@ -301,7 +281,7 @@
 		return
 	}
 	plcConfig, code := service.NewDevicePlcService().GetDevicePlc()
-	if code != ecode.OK || plcConfig.Id == 0 {
+	if code != ecode.OK || plcConfig.ID == 0 {
 		ctx.FailWithMsg(ecode.NeedConfirmedErr, "璇峰厛閰嶇疆PLC")
 		return
 	}
@@ -310,12 +290,7 @@
 	if err != nil {
 		logx.Errorf("SendProcessParams: %v", err.Error())
 		err = model.WithTransaction(func(db *gorm.DB) error {
-			err = taskService.UpdateProcedureStatusAndChannel(db, params.ProcedureId, model.ProcedureStatusWaitProcess, params.Channel)
-			if err != nil {
-				return err
-			}
-			procedure.Channel = params.Channel
-			err = taskService.UpdateOrderStatus(db, order.ID, model.OrderStatusWaitProcess)
+			err = taskService.UpdateProcedureStatusAndChannel(db, params.ProcedureId, model.ProcedureStatusWaitProcess, procedure.Channel, "")
 			if err != nil {
 				return err
 			}
@@ -325,24 +300,10 @@
 		return
 	}
 	plcConfig.CurrentTryTimes = 0
-	err = service.PlcWrite(plcConfig, constvar.PlcStartAddressTypeTotalNumber, params.Channel, order.Amount.IntPart())
+	err = service.PlcWrite(plcConfig, constvar.PlcStartAddressTypeTotalNumber, procedure.Channel, order.Amount.IntPart())
 	if err != nil {
 		ctx.FailWithMsg(ecode.NeedConfirmedErr, "PLC璇锋眰澶辫触锛岃妫�鏌LC閰嶇疆")
 		return
-	}
-
-	msg := &common.MsgTaskStatusUpdate{
-		WorkOrderId:  procedure.WorkOrderID,
-		ProcedureID:  procedure.ProceduresInfo.ProcedureID,
-		DeviceId:     procedure.ProceduresInfo.DeviceID,
-		IsProcessing: true,
-		IsFinish:     false,
-	}
-
-	caller := nsq.NewCaller(fmt.Sprintf(constvar.NsqTopicTaskProcedureStatusUpdate, conf.Conf.NsqConf.NodeId), "")
-	err = caller.Send(msg)
-	if err != nil {
-		logx.Errorf("send task status update msg error:%v", err.Error())
 	}
 	service.TaskFlagSet(procedure.Channel)
 	ctx.Ok()
@@ -386,7 +347,7 @@
 	}
 
 	plcConfig, code := service.NewDevicePlcService().GetDevicePlc()
-	if code != ecode.OK || plcConfig.Id == 0 {
+	if code != ecode.OK || plcConfig.ID == 0 {
 		ctx.FailWithMsg(ecode.NeedConfirmedErr, "璇峰厛閰嶇疆PLC")
 		return
 	}
@@ -397,6 +358,7 @@
 		ctx.FailWithMsg(ecode.NeedConfirmedErr, "PLC璇锋眰澶辫触锛岃妫�鏌LC閰嶇疆锛�")
 		return
 	}
+	_ = taskService.UpdateProcedureStatusAndChannel(nil, params.ProcedureId, model.ProcedureStatusProcessing, procedure.Channel, processModel.Number)
 	ctx.Ok()
 }
 

--
Gitblit v1.8.0