add
wangpengfei
2023-07-18 5fac03fb857cf9a160e1736a25de2c5f95f5e44f
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
package model
 
import (
    "aps_crm/pkg/mysqlx"
    "gorm.io/gorm"
)
 
type (
    Contract struct {
        Id          int       `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
        ClientId    int       `json:"clientId" gorm:"column:client_id;type:int;comment:客户id"`
        MemberId    int       `json:"memberId" gorm:"column:member_id;type:int;comment:负责人id"`
        Number      string    `json:"number" gorm:"column:number;type:varchar(255);comment:合同编号"`
        QuotationId int       `json:"quotationId" gorm:"column:quotation_id;type:int;comment:报价单id"`
        Quotation   Quotation `json:"quotation" gorm:"foreignKey:QuotationId;references:Id"`
        StatusId    int       `json:"statusId" gorm:"column:status_id;type:int;comment:合同状态"`
        File        string    `json:"file" gorm:"column:file;type:varchar(255);comment:合同文件"`
        gorm.Model  `json:"-"`
    }
 
    ContractSearch struct {
        Contract
        Orm *gorm.DB
    }
)
 
func (Contract) TableName() string {
    return "contract"
}
 
func NewContractSearch() *ContractSearch {
    return &ContractSearch{
        Orm: mysqlx.GetDB(),
    }
}
 
func (slf *ContractSearch) build() *gorm.DB {
    var db = slf.Orm.Model(&Contract{})
    if slf.Id != 0 {
        db = db.Where("id = ?", slf.Id)
    }
 
    return db
}
 
func (slf *ContractSearch) Create(record *Contract) error {
    var db = slf.build()
    return db.Create(record).Error
}
 
func (slf *ContractSearch) Delete() error {
    var db = slf.build()
    return db.Delete(&Contract{}).Error
}
 
func (slf *ContractSearch) Update(record *Contract) error {
    var db = slf.build()
    return db.Updates(record).Error
}
 
func (slf *ContractSearch) Find() (*Contract, error) {
    var db = slf.build()
    var record = &Contract{}
    err := db.First(record).Error
    return record, err
}
 
func (slf *ContractSearch) FindAll() ([]*Contract, error) {
    var db = slf.build()
    var records = make([]*Contract, 0)
    err := db.Preload("Quotation").Find(&records).Error
    return records, err
}
 
func (slf *ContractSearch) SetId(id int) *ContractSearch {
    slf.Id = id
    return slf
}