zhangqian
2023-10-19 c52ee4d80de792008522edfb3f06d67b20df4cb5
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
111
112
113
114
115
package model
 
import (
    "aps_crm/pkg/mysqlx"
    "gorm.io/gorm"
)
 
type (
    CollectionProjection struct {
        Id                        int     `json:"id" gorm:"column:id;primaryKey;autoIncrement;not null"`
        SaleChanceId              int     `json:"sale_chance_id" gorm:"column:sale_chance_id;type:int(11);comment:销售机会id"`
        Creator                   int     `json:"creator" gorm:"column:creator;type:int(11);comment:创建人"`
        Modifier                  int     `json:"modifier" gorm:"column:modifier;type:int(11);comment:修改人"`
        EstimatedCollectionDate   *string `json:"estimated_collection_date" gorm:"column:estimated_collection_date;type:datetime;comment:预计收款日期"`
        EstimatedCollectionAmount float64 `json:"estimated_collection_amount" gorm:"column:estimated_collection_amount;type:decimal(10,2);comment:预计收款金额"`
        gormModel
    }
 
    CollectionProjectionSearch struct {
        CollectionProjection
 
        Orm       *gorm.DB
        SearchMap map[string]interface{}
        OrderBy   string
        PageNum   int
        PageSize  int
    }
)
 
func (CollectionProjection) TableName() string {
    return "collection_projection"
}
 
func NewCollectionProjectionSearch() *CollectionProjectionSearch {
    return &CollectionProjectionSearch{
        Orm: mysqlx.GetDB(),
    }
}
 
func (slf *CollectionProjectionSearch) build() *gorm.DB {
    var db = slf.Orm.Model(&CollectionProjection{})
    if slf.Id != 0 {
        db = db.Where("id = ?", slf.Id)
    }
 
    if len(slf.SearchMap) > 0 {
        for key, value := range slf.SearchMap {
            switch v := value.(type) {
            case string:
                if key == "estimated_collection_date" {
                    db = db.Where(key+" = ?", v)
                }
            case int:
                if key == "client_id" || key == "sale_chance_id" {
                    db = db.Where(key+" = ?", v)
                }
            }
        }
    }
 
    return db
}
 
func (slf *CollectionProjectionSearch) Create(record *CollectionProjection) (err error) {
    var db = slf.build()
    err = db.Create(record).Error
    return
}
 
func (slf *CollectionProjectionSearch) Update(record *CollectionProjection) (err error) {
    var db = slf.build()
    err = db.Updates(record).Error
    return
}
 
func (slf *CollectionProjectionSearch) Delete() (err error) {
    var db = slf.build()
    err = db.Delete(&CollectionProjection{}).Error
    return
}
 
func (slf *CollectionProjectionSearch) Find() (int64, error, []*CollectionProjection) {
    var db = slf.build()
    var records = make([]*CollectionProjection, 0)
    var total int64
    if err := db.Count(&total).Error; err != nil {
        return total, err, records
    }
    if slf.PageNum > 0 && slf.PageSize > 0 {
        db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
    }
 
    err := db.Find(&records).Error
    return total, err, records
}
 
func (slf *CollectionProjectionSearch) SetID(id int) *CollectionProjectionSearch {
    slf.Id = id
    return slf
}
 
func (slf *CollectionProjectionSearch) SetSaleChanceId(saleChanceId int) *CollectionProjectionSearch {
    slf.SaleChanceId = saleChanceId
    return slf
}
 
func (slf *CollectionProjectionSearch) SetPage(page, size int) *CollectionProjectionSearch {
    slf.PageNum, slf.PageSize = page, size
    return slf
}
 
func (slf *CollectionProjectionSearch) SetSearchMap(data map[string]interface{}) *CollectionProjectionSearch {
    slf.SearchMap = data
    return slf
}