From f4508a84236a4aff1c7b5bfa17a14a8ff95728ba Mon Sep 17 00:00:00 2001
From: yinbentan <yinbentan@live.com>
Date: 星期一, 08 七月 2024 09:45:10 +0800
Subject: [PATCH] 接口调用报错处理

---
 models/attribute.go |   75 ++++++++++++++++++++++++++++++++++---
 1 files changed, 69 insertions(+), 6 deletions(-)

diff --git a/models/attribute.go b/models/attribute.go
index cd342fa..8f1a368 100644
--- a/models/attribute.go
+++ b/models/attribute.go
@@ -1,6 +1,7 @@
 package models
 
 import (
+	"encoding/json"
 	"fmt"
 	"gorm.io/gorm"
 	"wms/pkg/mysqlx"
@@ -10,9 +11,12 @@
 	// Attribute 鍔ㄦ�佸睘鎬ц〃
 	Attribute struct {
 		gorm.Model
-		Name     string   `json:"name"`
-		DataType DataType `json:"data_type"`
-		Value    string   `json:"value" gorm:"-"`
+		Name         string     `gorm:"type:varchar(100);not null;default:''" json:"name"` //灞炴�у悕绉�
+		DataType     DataType   `gorm:"type:tinyint;not null;default:0" json:"dataType"`   //鍊肩被鍨嬶紙1瀛楃涓� 2 int 3 涓嬫媺妗� 锛�
+		EntityType   EntityType `gorm:"type:tinyint;not null;default:0" json:"entityType"` //缁欒皝鐢ㄧ殑 1 鐗╂枡锛堜骇鍝侊級
+		SelectValues []string   `json:"selectValues" gorm:"-"`                             //dateType=3鏃剁敤
+		SelectValue  string     `json:"-" gorm:"type:text;"`
+		Value        string     `json:"value" gorm:"-"` //浠嶢ttributeValue鍙栧埌鐨剉alue
 	}
 
 	AttributeSearch struct {
@@ -21,17 +25,61 @@
 		PageNum  int
 		PageSize int
 		Orm      *gorm.DB
+		Keyword  string
+		IDs      []uint
 	}
 )
 
 type DataType int
 
 const (
-	DateTypeProduct = 1
+	DataTypeString = 1 //鎵嬪~瀛楃涓�
+	DataTypeInt    = 2 //鎵嬪~鏁村瀷
+	DataTypeSelect = 3 //涓嬫媺妗�
+)
+
+type EntityType int
+
+const (
+	EntityTypeProduct = 1
 )
 
 func (slf *Attribute) TableName() string {
 	return "wms_attribute"
+}
+
+func (slf *Attribute) BeforeCreate(tx *gorm.DB) (err error) {
+	if len(slf.SelectValues) != 0 {
+		bts, err := json.Marshal(slf.SelectValues)
+		if err != nil {
+			return err
+		}
+		slf.SelectValue = string(bts)
+	}
+	return nil
+}
+
+func (slf *Attribute) BeforeUpdate(tx *gorm.DB) (err error) {
+	if len(slf.SelectValues) != 0 {
+		bts, err := json.Marshal(slf.SelectValues)
+		if err != nil {
+			return err
+		}
+		slf.SelectValue = string(bts)
+	}
+	return nil
+}
+
+func (slf *Attribute) AfterFind(tx *gorm.DB) (err error) {
+	if slf.SelectValue != "" {
+		var list []string
+		err = json.Unmarshal([]byte(slf.SelectValue), &list)
+		if err != nil {
+			return err
+		}
+		slf.SelectValues = list
+	}
+	return nil
 }
 
 func NewAttributeSearch() *AttributeSearch {
@@ -57,9 +105,16 @@
 	slf.ID = id
 	return slf
 }
-
+func (slf *AttributeSearch) SetIDs(ids []uint) *AttributeSearch {
+	slf.IDs = ids
+	return slf
+}
 func (slf *AttributeSearch) SetName(name string) *AttributeSearch {
 	slf.Name = name
+	return slf
+}
+func (slf *AttributeSearch) SetEntityType(entityType EntityType) *AttributeSearch {
+	slf.EntityType = entityType
 	return slf
 }
 
@@ -69,14 +124,22 @@
 	if slf.ID != 0 {
 		db = db.Where("id = ?", slf.ID)
 	}
-
+	if len(slf.IDs) != 0 {
+		db = db.Where("id in ?", slf.IDs)
+	}
 	if slf.Order != "" {
 		db = db.Order(slf.Order)
+	}
+	if slf.EntityType != 0 {
+		db = db.Where("entity_type = ?", slf.EntityType)
 	}
 
 	if slf.Name != "" {
 		db = db.Where("name = ?", slf.Name)
 	}
+	if slf.Keyword != "" {
+		db = db.Where("id like ? or data_type like ? ", fmt.Sprintf("%%%v%%", slf.Keyword), fmt.Sprintf("%%%v%%", slf.Keyword))
+	}
 
 	return db
 }

--
Gitblit v1.8.0