From b331c9990a0396301e934daffe095f99d62d1c89 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期三, 27 九月 2023 15:58:49 +0800
Subject: [PATCH] 任务获取修改,状态2只返回已开始和已结束的

---
 model/procedures.go |   38 +++++++++++++++++++++++++++++++++++++-
 1 files changed, 37 insertions(+), 1 deletions(-)

diff --git a/model/procedures.go b/model/procedures.go
index 99e45f4..425a192 100644
--- a/model/procedures.go
+++ b/model/procedures.go
@@ -7,6 +7,7 @@
 	"encoding/json"
 	"fmt"
 	"gorm.io/gorm"
+	"gorm.io/gorm/clause"
 )
 
 type (
@@ -17,7 +18,7 @@
 		OrderID        string `gorm:"index;type:varchar(191);not null;comment:璁㈠崟ID" json:"-"`
 		DeviceID       string `gorm:"index;type:varchar(191);comment:璁惧ID" json:"deviceId"`
 		ProcedureID    string `gorm:"index;type:varchar(191);comment:宸ュ簭ID" json:"procedureId"`
-		Position       int    `gorm:"type:int;comment:宸ヤ綔浣嶇疆" json:"position"` //姣忎釜璁惧鍙兘鏈夊涓満浣嶅悓鏃剁敓浜э紝鐢╬osition琛ㄧず浣嶇疆
+		Channel        int32  `gorm:"index;comment:閫氶亾" json:"channel"` //閫氶亾
 		StartTime      int64  `gorm:"comment:璁″垝寮�濮嬫椂闂�" json:"startTime"`
 		EndTime        int64  `gorm:"comment:璁″垝缁撴潫鏃堕棿" json:"endTime"`
 		Status         ProcedureStatus
@@ -33,8 +34,10 @@
 		Orm          *gorm.DB
 		Preload      bool
 		StartTimeMax int64
+		EndTimeMin   int64
 		StatusNot    ProcedureStatus
 		ProcedureIds []string
+		Channels     []int32
 	}
 )
 
@@ -97,6 +100,11 @@
 	return slf
 }
 
+func (slf *ProceduresSearch) SetEndTimeMin(ts int64) *ProceduresSearch {
+	slf.EndTimeMin = ts
+	return slf
+}
+
 func (slf *ProceduresSearch) SetId(id int) *ProceduresSearch {
 	slf.ID = id
 	return slf
@@ -127,6 +135,11 @@
 	return slf
 }
 
+func (slf *ProceduresSearch) SetChannels(channels []int32) *ProceduresSearch {
+	slf.Channels = channels
+	return slf
+}
+
 func (slf *ProceduresSearch) build() *gorm.DB {
 	var db = slf.Orm.Model(&Procedures{})
 
@@ -154,6 +167,10 @@
 		db = db.Where("start_time <= ?", slf.StartTimeMax)
 	}
 
+	if slf.EndTimeMin != 0 {
+		db = db.Where("end_time > ?", slf.EndTimeMin)
+	}
+
 	if slf.Status != 0 {
 		db = db.Where("status = ?", slf.Status)
 	}
@@ -168,6 +185,10 @@
 
 	if slf.ProcedureID != "" {
 		db = db.Where("procedure_id = ?", slf.ProcedureID)
+	}
+
+	if len(slf.Channels) > 0 {
+		db = db.Where("channel in ?", slf.Channels)
 	}
 
 	return db
@@ -195,6 +216,21 @@
 	return nil
 }
 
+func (slf *ProceduresSearch) Upsert(record *Procedures) error {
+	var db = slf.build()
+	old, err := slf.First()
+	if err != gorm.ErrRecordNotFound && old.ID != 0 {
+		record.ID = old.ID
+	}
+	if err := db.Clauses(clause.OnConflict{
+		UpdateAll: true,
+	}).Create(&record).Error; err != nil {
+		return fmt.Errorf("save err: %v, record: %+v", err, record)
+	}
+
+	return nil
+}
+
 func (slf *ProceduresSearch) Updates(record *Procedures) error {
 	var db = slf.build()
 

--
Gitblit v1.8.0