-
Notifications
You must be signed in to change notification settings - Fork 0
/
payable.go
137 lines (130 loc) · 4.33 KB
/
payable.go
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package mysql_model
import (
"github.com/XC-Zero/yinwan/pkg/client"
_const "github.com/XC-Zero/yinwan/pkg/const"
"github.com/XC-Zero/yinwan/pkg/utils/es_tool"
"github.com/pkg/errors"
"gorm.io/gorm"
)
// Payable 应付
type Payable struct {
BasicModel
BookNameInfo
ProviderID *int `gorm:"type:int" json:"provider_id,omitempty" form:"provider_id,omitempty"`
ProviderName *string `gorm:"type:varchar(50)" json:"provider_name,omitempty" form:"provider_name"`
PayableDate *string `gorm:"type:varchar(50)" json:"payable_date,omitempty" form:"payable_date"`
PayableTotalAmount *string `gorm:"type:varchar(50);" json:"payable_total_amount,omitempty" form:"payable_total_amount"`
PayableActualAmount *string `gorm:"type:varchar(50)" json:"payable_actual_amount,omitempty" form:"payable_actual_amount"`
PayableDiscount *string `gorm:"type:varchar(50)" json:"payable_discount,omitempty" form:"payable_discount"`
PayableHedgingAmount *string `gorm:"type:varchar(50)" json:"payable_hedging_amount,omitempty" form:"payable_hedging_amount"`
PayableDebtAmount *string `gorm:"type:varchar(50)" json:"payable_debt_amount,omitempty" form:"payable_debt_amount"`
PayableStatus *_const.PayableStatus `gorm:"type:int" json:"payable_status,omitempty" form:"payable_status"`
PurchaseID *int `gorm:"type:int" json:"purchase_id,omitempty" form:"purchase_id,omitempty" cn:"关联采购单ID"`
CredentialID *int `gorm:"type:int" json:"credential_id,omitempty" form:"credential_id,omitempty"`
Remark *string `gorm:"type:varchar(200)" json:"remark,omitempty" form:"remark"`
}
func (p Payable) TableCnName() string {
return "应付"
}
func (p Payable) TableName() string {
return "payables"
}
func (p Payable) ToESDoc() map[string]interface{} {
return map[string]interface{}{
"rec_id": p.RecID,
"remark": p.Remark,
"book_name": p.BookName,
"book_name_id": p.BookNameID,
"purchase_id": p.PurchaseID,
"credential_id": p.CredentialID,
"provider_name": p.ProviderName,
"payable_total_amount": p.PayableTotalAmount,
}
}
func (p Payable) Mapping() map[string]interface{} {
m := mapping{
"settings": mapping{},
"mappings": mapping{
"properties": mapping{
"rec_id": mapping{
"type": "keyword",
},
"provider_name": mapping{
"type": "text",
"analyzer": IK_SMART,
"search_analyzer": IK_SMART,
"fields": mapping{
"keyword": mapping{
"type": "keyword",
"ignore_above": 256,
},
},
},
"payable_total_amount": mapping{
"type": "text",
"analyzer": IK_SMART,
"search_analyzer": IK_SMART,
},
"credential_id": mapping{
"type": "text",
"analyzer": IK_SMART,
"search_analyzer": IK_SMART,
},
"purchase_id": mapping{
"type": "keyword",
},
"remark": mapping{
"type": "text",
"analyzer": IK_SMART,
"search_analyzer": IK_SMART,
},
"created_at": mapping{
"type": "text",
},
"book_name": mapping{
"type": "keyword",
},
"book_name_id": mapping{
"type": "keyword",
},
},
},
}
return m
}
func (p *Payable) AfterCreate(tx *gorm.DB) error {
bookName := tx.Statement.Context.Value("book_name").(string)
bk, ok := client.ReadBookMap(bookName)
if !ok {
return errors.New("There is no book name!")
}
p.BookNameID = bk.StorageName
p.BookName = bk.BookName
err := client.PutIntoIndex(p)
if err != nil {
return err
}
return nil
}
// AfterUpdate 同步更新
func (p *Payable) AfterUpdate(tx *gorm.DB) error {
bookName := tx.Statement.Context.Value("book_name").(string)
bk, ok := client.ReadBookMap(bookName)
if !ok {
return errors.New("There is no book name!")
}
p.BookNameID = bk.StorageName
p.BookName = bk.BookName
err := client.UpdateIntoIndex(p, p.RecID, tx.Statement.Context, es_tool.ESDocToUpdateScript(p.ToESDoc()))
if err != nil {
return err
}
return nil
}
func (p *Payable) AfterDelete(tx *gorm.DB) error {
err := client.DeleteFromIndex(p, p.RecID, tx.Statement.Context)
if err != nil {
return err
}
return nil
}