From 02a84fb6fb2a39bfe7fc5cf6c0137bbf231b17fe Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期四, 09 十一月 2023 14:14:17 +0800
Subject: [PATCH] 设备列表返回是否设置工艺参数字段

---
 service/cache_store.go |  127 +++++++++++++++++++++++++++++++++---------
 1 files changed, 99 insertions(+), 28 deletions(-)

diff --git a/service/cache_store.go b/service/cache_store.go
index 9cbd07b..2ac4594 100644
--- a/service/cache_store.go
+++ b/service/cache_store.go
@@ -1,10 +1,14 @@
 package service
 
 import (
+	"apsClient/conf"
+	"apsClient/constvar"
 	"apsClient/model"
-	"apsClient/model/response"
 	"fmt"
+	"github.com/jinzhu/gorm"
+	"github.com/spf13/cast"
 	"sync"
+	"time"
 )
 
 type CacheStore struct {
@@ -44,45 +48,112 @@
 }
 
 const (
-	PlcCacheKey             = "plc:%v:%v"
-	CurrentTaskCacheKey     = "current_task"
-	CurrentProgressCacheKey = "current_progress:%v"
+	PlcCacheKey             = "plc:%v:%v"                    //plc:channel:key
+	CurrentTaskCacheKey     = "current_task:%v"              //current_task:channel
+	CurrentProgressCacheKey = "current_progress:%v"          //current_progress:channel
+	PlcCacheKeyUpdateTime   = "finish_number_update_time:%v" //finish_number_update_time:channel
+	TaskStartTimeCache      = "task_start_time:%v"           //task_start_time:channel
+	TaskEndTimeCache        = "task_end_time:%v"             //task_end_time:channel
 )
 
-func PlcCacheGet(position int, key string) (interface{}, bool) {
-	return defaultCacheStore.Get(fmt.Sprintf(PlcCacheKey, position, key))
+func PlcCacheGet(channel int32, key string) (interface{}, bool) {
+	return defaultCacheStore.Get(fmt.Sprintf(PlcCacheKey, channel, key))
 }
 
-func PlcCacheSet(position int, key string, value interface{}) {
-	defaultCacheStore.Add(fmt.Sprintf(PlcCacheKey, position, key), value)
-}
-
-func TaskCacheSet(value *response.TaskData) {
-	defaultCacheStore.Add(CurrentTaskCacheKey, value)
-}
-
-func TaskCacheUnset() {
-	defaultCacheStore.Remove(CurrentTaskCacheKey)
-}
-
-func TaskCacheGet() (*response.TaskData, bool) {
-	if v, ok := defaultCacheStore.Get(CurrentTaskCacheKey); ok {
-		return v.(*response.TaskData), ok
+func PlcCacheSet(channel int32, key string, value interface{}) {
+	if key == constvar.PlcCacheKeyFinishNumber {
+		oldFinishNumber, exists := PlcCacheGet(channel, key)
+		if !exists || cast.ToInt(oldFinishNumber) != cast.ToInt(value) { //finishNumber鏈変簡鍙樺寲锛岃缃洿鏂版椂闂寸紦瀛�
+			FinishUpdateTimeSet(channel, time.Now().Unix())
+		}
 	}
-	return nil, false
+	defaultCacheStore.Add(fmt.Sprintf(PlcCacheKey, channel, key), value)
 }
 
-func ProgressCacheGet(position int) (*model.ProductionProgress, bool) {
-	if v, ok := defaultCacheStore.Get(fmt.Sprintf(CurrentProgressCacheKey, position)); ok {
+func FinishUpdateTimeGet(channel int32) interface{} {
+	val, ok := defaultCacheStore.Get(fmt.Sprintf(PlcCacheKeyUpdateTime, channel))
+	if ok {
+		return val
+	}
+	return 0
+}
+
+func TaskStartTimeSet(channel int32, ts int64) {
+	defaultCacheStore.Add(fmt.Sprintf(TaskStartTimeCache, channel), ts)
+}
+
+func TaskStartTimeGet(channel int32) int64 {
+	if v, ok := defaultCacheStore.Get(fmt.Sprintf(TaskStartTimeCache, channel)); ok {
+		return v.(int64)
+	}
+	procedure, err := model.NewProceduresSearch(nil).SetDeviceId(conf.Conf.CurrentDeviceID).SetStatus(model.ProcedureStatusProcessing).SetChannels([]int32{channel}).First() //杩涜涓换鍔�
+	if err == gorm.ErrRecordNotFound {
+		procedure, err = model.NewProceduresSearch(nil).SetDeviceId(conf.Conf.CurrentDeviceID).
+			SetStatus(model.ProcedureStatusFinished).SetChannels([]int32{channel}).SetOrder("real_end_time desc").First() //涓婁竴涓粨鏉熺殑浠诲姟
+		if err == gorm.ErrRecordNotFound { //杩涜涓拰缁撴潫鐨勯兘娌℃湁锛屽紑濮嬫椂闂村拰缁撴潫鏃堕棿閮借缃�0
+			TaskStartTimeSet(channel, int64(0))
+			TaskEndTimeSet(channel, int64(0))
+			return 0
+		} else {
+			TaskStartTimeSet(channel, procedure.RealStartTime)
+			TaskStartTimeSet(channel, procedure.RealEndTime)
+			return 0
+		}
+	} else {
+		TaskStartTimeSet(channel, procedure.RealStartTime)
+		TaskStartTimeSet(channel, int64(0))
+		return procedure.RealStartTime
+	}
+}
+
+func TaskEndTimeSet(channel int32, ts int64) {
+	defaultCacheStore.Add(fmt.Sprintf(TaskEndTimeCache, channel), ts)
+}
+
+func TaskEndTimeGet(channel int32) int64 {
+	if v, ok := defaultCacheStore.Get(fmt.Sprintf(TaskEndTimeCache, channel)); ok {
+		return v.(int64)
+	}
+	return 0
+}
+
+func FinishUpdateTimeSet(channel int32, value interface{}) {
+	defaultCacheStore.Add(fmt.Sprintf(PlcCacheKeyUpdateTime, channel), value)
+}
+
+func TaskFlagSet(channel int32, taskId int) {
+	defaultCacheStore.Add(fmt.Sprintf(CurrentTaskCacheKey, channel), taskId)
+}
+
+func TaskFlagUnset(channel int32) {
+	defaultCacheStore.Add(fmt.Sprintf(CurrentTaskCacheKey, channel), 0)
+}
+
+func TaskFlagGet(channel int32) bool {
+	if v, ok := defaultCacheStore.Get(fmt.Sprintf(CurrentTaskCacheKey, channel)); ok {
+		return v.(int) > 0
+	}
+	procedure, err := model.NewProceduresSearch(nil).SetDeviceId(conf.Conf.CurrentDeviceID).SetStatus(model.ProcedureStatusProcessing).SetChannels([]int32{channel}).First()
+	if err == gorm.ErrRecordNotFound {
+		defaultCacheStore.Add(fmt.Sprintf(CurrentTaskCacheKey, channel), 0)
+		return false
+	} else {
+		defaultCacheStore.Add(fmt.Sprintf(CurrentTaskCacheKey, channel), int(procedure.ID))
+		return true
+	}
+}
+
+func ProgressCacheGet(channel int32) (*model.ProductionProgress, bool) {
+	if v, ok := defaultCacheStore.Get(fmt.Sprintf(CurrentProgressCacheKey, channel)); ok {
 		return v.(*model.ProductionProgress), ok
 	}
 	return nil, false
 }
 
-func ProgressCacheSet(position int, value *model.ProductionProgress) {
-	defaultCacheStore.Add(fmt.Sprintf(CurrentProgressCacheKey, position), value)
+func ProgressCacheSet(channel int32, value *model.ProductionProgress) {
+	defaultCacheStore.Add(fmt.Sprintf(CurrentProgressCacheKey, channel), value)
 }
 
-func ProgressCacheUnset(position int) {
-	defaultCacheStore.Remove(fmt.Sprintf(CurrentProgressCacheKey, position))
+func ProgressCacheUnset(channel int32) {
+	defaultCacheStore.Remove(fmt.Sprintf(CurrentProgressCacheKey, channel))
 }

--
Gitblit v1.8.0