From 8324f872ef3a4d0c978a9b1d062800c6a1701c12 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期五, 01 十二月 2023 09:58:17 +0800
Subject: [PATCH] fix

---
 service/cache_store.go |  122 ++++++++++++++++++++++++++++++----------
 1 files changed, 92 insertions(+), 30 deletions(-)

diff --git a/service/cache_store.go b/service/cache_store.go
index 9cbd07b..877217a 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,103 @@
 }
 
 const (
-	PlcCacheKey             = "plc:%v:%v"
-	CurrentTaskCacheKey     = "current_task"
-	CurrentProgressCacheKey = "current_progress:%v"
+	PlcCacheKey             = "plc:%v:%v:%v"                    //plc:deviceID:channel:key 缂撳瓨鍔犲伐鏁版垨鐩爣鏁�
+	CurrentTaskCacheKey     = "current_task:%v:%v"              //current_task:deviceID:channel 缂撳瓨褰撳墠浠诲姟id
+	CurrentProgressCacheKey = "current_progress:%v:%v"          //current_progress:deviceId:channel
+	PlcCacheKeyUpdateTime   = "finish_number_update_time:%v:%v" //finish_number_update_time:deviceID:channel
+	TaskStartTimeCache      = "task_start_time:%v:%v"           //task_start_time:deviceID:channel
+	TaskEndTimeCache        = "task_end_time:%v:%v"             //task_end_time:deviceID:channel
 )
 
-func PlcCacheGet(position int, key string) (interface{}, bool) {
-	return defaultCacheStore.Get(fmt.Sprintf(PlcCacheKey, position, key))
+func PlcCacheGet(deviceId string, channel int32, key string) (interface{}, bool) {
+	return defaultCacheStore.Get(fmt.Sprintf(PlcCacheKey, deviceId, 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(deviceId string, channel int32, key string, value interface{}) {
+	if key == constvar.PlcCacheKeyFinishNumber {
+		oldFinishNumber, exists := PlcCacheGet(deviceId, channel, key)
+		if !exists || cast.ToInt(oldFinishNumber) != cast.ToInt(value) { //finishNumber鏈変簡鍙樺寲锛岃缃洿鏂版椂闂寸紦瀛�
+			FinishUpdateTimeSet(deviceId, channel, time.Now().Unix())
+		}
 	}
-	return nil, false
+	defaultCacheStore.Add(fmt.Sprintf(PlcCacheKey, deviceId, channel, key), value)
 }
 
-func ProgressCacheGet(position int) (*model.ProductionProgress, bool) {
-	if v, ok := defaultCacheStore.Get(fmt.Sprintf(CurrentProgressCacheKey, position)); ok {
-		return v.(*model.ProductionProgress), ok
+// FinishUpdateTimeGet 鐢ㄤ簬鍒ゆ柇plc鐘舵�侊紝瓒呰繃澶氬皯鏃堕棿鏈洿鏂拌涓哄緟鏈�
+func FinishUpdateTimeGet(deviceId string, channel int32) interface{} {
+	val, ok := defaultCacheStore.Get(fmt.Sprintf(PlcCacheKeyUpdateTime, deviceId, channel))
+	if ok {
+		return val
 	}
-	return nil, false
+	return 0
 }
 
-func ProgressCacheSet(position int, value *model.ProductionProgress) {
-	defaultCacheStore.Add(fmt.Sprintf(CurrentProgressCacheKey, position), value)
+func FinishUpdateTimeSet(deviceId string, channel int32, value interface{}) {
+	defaultCacheStore.Add(fmt.Sprintf(PlcCacheKeyUpdateTime, deviceId, channel), value)
 }
 
-func ProgressCacheUnset(position int) {
-	defaultCacheStore.Remove(fmt.Sprintf(CurrentProgressCacheKey, position))
+func TaskStartTimeSet(deviceID string, channel int32, ts int64) {
+	defaultCacheStore.Add(fmt.Sprintf(TaskStartTimeCache, deviceID, channel), ts)
+}
+
+// TaskStartTimeGet 鐢ㄤ簬鍓嶇灞曠ず宸ュ簭杩愯鏃堕棿
+func TaskStartTimeGet(deviceId string, channel int32) int64 {
+	if v, ok := defaultCacheStore.Get(fmt.Sprintf(TaskStartTimeCache, deviceId, 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(deviceId, channel, int64(0))
+			TaskEndTimeSet(deviceId, channel, int64(0))
+			return 0
+		} else {
+			TaskStartTimeSet(deviceId, channel, procedure.RealStartTime)
+			TaskEndTimeSet(deviceId, channel, procedure.RealEndTime)
+			return 0
+		}
+	} else {
+		TaskStartTimeSet(deviceId, channel, procedure.RealStartTime)
+		TaskEndTimeSet(deviceId, channel, int64(0))
+		return procedure.RealStartTime
+	}
+}
+
+func TaskEndTimeSet(deviceID string, channel int32, ts int64) {
+	defaultCacheStore.Add(fmt.Sprintf(TaskEndTimeCache, deviceID, channel), ts)
+}
+
+func TaskEndTimeGet(deviceID string, channel int32) int64 {
+	if v, ok := defaultCacheStore.Get(fmt.Sprintf(TaskEndTimeCache, deviceID, channel)); ok {
+		return v.(int64)
+	}
+	return 0
+}
+
+func TaskFlagSet(deviceID string, channel int32, taskId int) {
+	defaultCacheStore.Add(fmt.Sprintf(CurrentTaskCacheKey, deviceID, channel), taskId)
+}
+
+func TaskFlagUnset(deviceID string, channel int32) {
+	defaultCacheStore.Add(fmt.Sprintf(CurrentTaskCacheKey, deviceID, channel), 0)
+}
+
+func TaskFlagGet(deviceID string, channel int32) bool {
+	if v, ok := defaultCacheStore.Get(fmt.Sprintf(CurrentTaskCacheKey, deviceID, 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, deviceID, channel), 0)
+		return false
+	} else {
+		defaultCacheStore.Add(fmt.Sprintf(CurrentTaskCacheKey, deviceID, channel), int(procedure.ID))
+		return true
+	}
+}
+
+func ProgressCacheUnset(deviceID string, channel int32) {
+	defaultCacheStore.Remove(fmt.Sprintf(CurrentProgressCacheKey, deviceID, channel))
 }

--
Gitblit v1.8.0