1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package model
 
import (
    "aps_crm/pkg/mysqlx"
    "gorm.io/gorm"
    "time"
)
 
type (
    SalesReturn struct {
        Id                int       `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
        ClientId          int       `json:"clientId" gorm:"column:client_id;type:int;comment:客户id"`
        Number            string    `json:"number" gorm:"column:number;type:varchar(255);comment:退货单号"`
        Repository        string    `json:"repository" gorm:"column:repository;type:varchar(255);comment:仓库"`
        MemberId          int       `json:"memberId" gorm:"column:member_id;type:int;comment:负责人id"`
        ReturnDate        time.Time `json:"returnDate" gorm:"column:return_date;type:datetime;comment:退货日期"`
        SalesReturnStatus int       `json:"salesReturnStatus" gorm:"column:sales_return_status;type:int;comment:退货状态"`
        Reason            string    `json:"reason" gorm:"column:reason;type:varchar(255);comment:退货原因"`
        Products          []Product `json:"products" gorm:"many2many:salesReturn_product;"`
    }
 
    SalesReturnSearch struct {
        SalesReturn
 
        Orm      *gorm.DB
        Keyword  string
        OrderBy  string
        PageNum  int
        PageSize int
    }
)
 
func (SalesReturn) TableName() string {
    return "sales_return"
}
 
func NewSalesReturnSearch() *SalesReturnSearch {
    return &SalesReturnSearch{
        Orm: mysqlx.GetDB(),
    }
}
 
func (slf *SalesReturnSearch) build() *gorm.DB {
    var db = slf.Orm.Model(&SalesReturn{})
    if slf.Keyword != "" {
        db = db.Where("name LIKE ?", "%"+slf.Keyword+"%")
    }
    if slf.Id != 0 {
        db = db.Where("id = ?", slf.Id)
    }
 
    return db
}
 
func (slf *SalesReturnSearch) Create(record *SalesReturn) error {
    var db = slf.build()
    return db.Create(record).Error
}
 
func (slf *SalesReturnSearch) Delete() error {
    var db = slf.build()
    return db.Delete(&SalesReturn{}).Error
}
 
func (slf *SalesReturnSearch) Update(record *SalesReturn) error {
    var db = slf.build()
    return db.Updates(record).Error
}
 
func (slf *SalesReturnSearch) Find() (*SalesReturn, error) {
    var db = slf.build()
    var record = new(SalesReturn)
    err := db.First(record).Error
    return record, err
}
 
func (slf *SalesReturnSearch) FindAll() ([]*SalesReturn, int64, error) {
    var db = slf.build()
    var records = make([]*SalesReturn, 0)
    var total int64
    if err := db.Count(&total).Error; err != nil {
        return records, total, err
    }
    if slf.PageNum > 0 && slf.PageSize > 0 {
        db = db.Limit(slf.PageSize).Offset((slf.PageNum - 1) * slf.PageSize)
    }
 
    err := db.Preload("Products").Find(&records).Error
    return records, total, err
}
 
func (slf *SalesReturnSearch) SetId(id int) *SalesReturnSearch {
    slf.Id = id
    return slf
}
 
func (slf *SalesReturnSearch) SetKeyword(keyword string) *SalesReturnSearch {
    slf.Keyword = keyword
    return slf
}
 
func (slf *SalesReturnSearch) SetPage(page, size int) *SalesReturnSearch {
    slf.PageNum, slf.PageSize = page, size
    return slf
}
 
func (slf *SalesReturnSearch) SetOrder(order string) *SalesReturnSearch {
    slf.OrderBy = order
    return slf
}