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 |  132 ++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 115 insertions(+), 17 deletions(-)

diff --git a/model/procedures.go b/model/procedures.go
index 5ac3185..425a192 100644
--- a/model/procedures.go
+++ b/model/procedures.go
@@ -1,22 +1,29 @@
 package model
 
 import (
-	"apsClient/model/request"
+	"apsClient/model/common"
 	"apsClient/pkg/logx"
 	"apsClient/pkg/sqlitex"
 	"encoding/json"
 	"fmt"
 	"gorm.io/gorm"
+	"gorm.io/gorm/clause"
 )
 
 type (
 	Procedures struct {
 		gorm.Model     `json:"-"`
-		ID             int                      `gorm:"primarykey"`
-		OrderID        string                   `gorm:"index;type:varchar(191);not null;comment:璁㈠崟ID" json:"-"`
-		Status         ProcedureStatus          `json:"-"`
-		ProcedureData  string                   `json:"-"`                  //request.ProductProcedure  json涓�
-		ProceduresInfo request.ProductProcedure `json:"procedure" gorm:"-"` //request.ProductProcedure  瀵硅薄
+		ID             int    `gorm:"primarykey"`
+		WorkOrderID    string `gorm:"index;type:varchar(191);not null;comment:宸ュ崟ID" json:"-"`
+		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"`
+		Channel        int32  `gorm:"index;comment:閫氶亾" json:"channel"` //閫氶亾
+		StartTime      int64  `gorm:"comment:璁″垝寮�濮嬫椂闂�" json:"startTime"`
+		EndTime        int64  `gorm:"comment:璁″垝缁撴潫鏃堕棿" json:"endTime"`
+		Status         ProcedureStatus
+		ProcedureData  string                  `json:"-"`                  //common.ProductProcedure  json涓�
+		ProceduresInfo common.ProductProcedure `json:"procedure" gorm:"-"` //common.ProductProcedure  瀵硅薄
 	}
 
 	ProceduresSearch struct {
@@ -27,27 +34,33 @@
 		Orm          *gorm.DB
 		Preload      bool
 		StartTimeMax int64
+		EndTimeMin   int64
+		StatusNot    ProcedureStatus
+		ProcedureIds []string
+		Channels     []int32
 	}
 )
 
 type ProcedureStatus int
 
 const (
-	ProcedureStatusUnFinished ProcedureStatus = 1
-	ProcedureStatusFinished   ProcedureStatus = 2
+	ProcedureStatusWaitProcess ProcedureStatus = 1
+	ProcedureStatusProcessing  ProcedureStatus = 2
+	ProcedureStatusFinished    ProcedureStatus = 3
 )
 
 func (slf *Procedures) TableName() string {
 	return "procedures"
 }
-func (slf *Procedures) AfterFind() {
-	var proceduresInfo request.ProductProcedure
+func (slf *Procedures) AfterFind(db *gorm.DB) error {
+	var proceduresInfo common.ProductProcedure
 	err := json.Unmarshal([]byte(slf.ProcedureData), &proceduresInfo)
 	if err != nil {
 		logx.Errorf("AfterFind Unmarshal err: %v", err.Error())
-		return
+		return err
 	}
 	slf.ProceduresInfo = proceduresInfo
+	return nil
 }
 
 func NewProceduresSearch(db *gorm.DB) *ProceduresSearch {
@@ -72,12 +85,23 @@
 	return slf
 }
 
-func (slf *ProceduresSearch) SetOrderId(orderId string) *ProceduresSearch {
-	slf.OrderID = orderId
+func (slf *ProceduresSearch) SetWorkOrderId(orderId string) *ProceduresSearch {
+	slf.WorkOrderID = orderId
 	return slf
 }
+
+func (slf *ProceduresSearch) SetProcedureId(id string) *ProceduresSearch {
+	slf.ProcedureID = id
+	return slf
+}
+
 func (slf *ProceduresSearch) SetStartTimeMax(ts int64) *ProceduresSearch {
 	slf.StartTimeMax = ts
+	return slf
+}
+
+func (slf *ProceduresSearch) SetEndTimeMin(ts int64) *ProceduresSearch {
+	slf.EndTimeMin = ts
 	return slf
 }
 
@@ -86,13 +110,33 @@
 	return slf
 }
 
+func (slf *ProceduresSearch) SetProcedureIds(procedureIds []string) *ProceduresSearch {
+	slf.ProcedureIds = procedureIds
+	return slf
+}
+
+func (slf *ProceduresSearch) SetDeviceId(id string) *ProceduresSearch {
+	slf.DeviceID = id
+	return slf
+}
+
 func (slf *ProceduresSearch) SetStatus(status ProcedureStatus) *ProceduresSearch {
 	slf.Status = status
 	return slf
 }
 
+func (slf *ProceduresSearch) SetStatusNot(status ProcedureStatus) *ProceduresSearch {
+	slf.StatusNot = status
+	return slf
+}
+
 func (slf *ProceduresSearch) SetPreload(preload bool) *ProceduresSearch {
 	slf.Preload = preload
+	return slf
+}
+
+func (slf *ProceduresSearch) SetChannels(channels []int32) *ProceduresSearch {
+	slf.Channels = channels
 	return slf
 }
 
@@ -103,8 +147,16 @@
 		db = db.Order(slf.Order)
 	}
 
-	if slf.OrderID != "" {
-		db = db.Where("order_id = ?", slf.OrderID)
+	if slf.ID != 0 {
+		db = db.Where("id = ?", slf.ID)
+	}
+
+	if slf.WorkOrderID != "" {
+		db = db.Where("work_order_id = ?", slf.WorkOrderID)
+	}
+
+	if slf.DeviceID != "" {
+		db = db.Where("device_id = ?", slf.DeviceID)
 	}
 
 	if slf.Preload {
@@ -115,8 +167,28 @@
 		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)
+	}
+
+	if slf.StatusNot != 0 {
+		db = db.Where("status <> ?", slf.StatusNot)
+	}
+
+	if len(slf.ProcedureIds) > 0 {
+		db = db.Where("procedure_id in ?", slf.ProcedureIds)
+	}
+
+	if slf.ProcedureID != "" {
+		db = db.Where("procedure_id = ?", slf.ProcedureID)
+	}
+
+	if len(slf.Channels) > 0 {
+		db = db.Where("channel in ?", slf.Channels)
 	}
 
 	return db
@@ -144,10 +216,25 @@
 	return nil
 }
 
-func (slf *ProceduresSearch) Save(record *Procedures) error {
+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()
 
-	if err := db.Save(record).Error; err != nil {
+	if err := db.Updates(record).Error; err != nil {
 		return fmt.Errorf("save err: %v, record: %+v", err, record)
 	}
 
@@ -221,6 +308,17 @@
 	return records, total, nil
 }
 
+func (slf *ProceduresSearch) Count() (int64, error) {
+	var (
+		total int64
+		db    = slf.build()
+	)
+	if err := db.Count(&total).Error; err != nil {
+		return total, fmt.Errorf("find count err: %v", err)
+	}
+	return total, nil
+}
+
 func (slf *ProceduresSearch) FindNotTotal() ([]*Procedures, error) {
 	var (
 		records = make([]*Procedures, 0)

--
Gitblit v1.8.0