From e99cc5acf40323379cf538bc5f08228a42aa2bee Mon Sep 17 00:00:00 2001
From: liujiandao <274878379@qq.com>
Date: 星期二, 09 四月 2024 17:52:26 +0800
Subject: [PATCH] 产量登记表2

---
 models/yield_register.go |   30 ++++++++++++++++++++++++++----
 1 files changed, 26 insertions(+), 4 deletions(-)

diff --git a/models/yield_register.go b/models/yield_register.go
index 421d2a8..c5ec8c7 100644
--- a/models/yield_register.go
+++ b/models/yield_register.go
@@ -36,8 +36,8 @@
 		TwoBack                 string                 `json:"twoBack" gorm:"type:varchar(255);comment:绗簩鍥�"`
 		ThreeBack               string                 `json:"threeBack" gorm:"type:varchar(255);comment:绗笁鍥�"`
 		Total                   decimal.Decimal        `json:"total" gorm:"type:decimal(12,2);comment:鎬讳骇閲�"`
-		Items                   []*YieldRegisterItem   `json:"items" gorm:"foreignKey:YieldRegisterId;references:Id"`
-		Circles                 []*YieldRegisterCircle `json:"circles" gorm:"foreignKey:YieldRegisterId;references:Id"`
+		Items                   []*YieldRegisterItem   `json:"items" gorm:"foreignKey:YieldRegisterId"`
+		Circles                 []*YieldRegisterCircle `json:"circles" gorm:"foreignKey:YieldRegisterId"`
 	}
 	YieldRegisterSearch struct {
 		YieldRegister
@@ -71,11 +71,20 @@
 	return slf
 }
 
+func (slf *YieldRegisterSearch) SetNumber(number string) *YieldRegisterSearch {
+	slf.Number = number
+	return slf
+}
+
 func (slf *YieldRegisterSearch) build() *gorm.DB {
 	db := slf.Orm.Table(slf.TableName())
 
 	if slf.Preload {
 		db = db.Model(YieldRegister{}).Preload("Items").Preload("Circles")
+	}
+
+	if slf.Number != "" {
+		db = db.Where("number = ?", slf.Number)
 	}
 
 	return db
@@ -91,6 +100,19 @@
 	return nil
 }
 
+func (slf *YieldRegisterSearch) First() (*YieldRegister, error) {
+	var (
+		record = new(YieldRegister)
+		db     = slf.build()
+	)
+
+	if err := db.First(record).Error; err != nil {
+		return record, err
+	}
+
+	return record, nil
+}
+
 func (slf *YieldRegisterSearch) Find() ([]*YieldRegister, int64, error) {
 	var (
 		records = make([]*YieldRegister, 0)
@@ -104,7 +126,7 @@
 	if slf.PageNum*slf.PageSize > 0 {
 		db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
 	}
-	if err := db.Find(&records).Error; err != nil {
+	if err := db.Order("updated_at desc").Find(&records).Error; err != nil {
 		return records, total, fmt.Errorf("find records err: %v", err)
 	}
 
@@ -127,7 +149,7 @@
 func (slf *YieldRegisterSearch) Save(record *YieldRegister) error {
 	var db = slf.build()
 
-	if err := db.Save(record).Error; err != nil {
+	if err := db.Omit("CreatedAt").Save(record).Error; err != nil {
 		return fmt.Errorf("save err: %v, record: %+v", err, record)
 	}
 

--
Gitblit v1.8.0