-
Notifications
You must be signed in to change notification settings - Fork 0
/
receivable.go
142 lines (134 loc) · 4.72 KB
/
receivable.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
138
139
140
141
142
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"
)
// Receivable 应收
type Receivable struct {
BasicModel
BookNameInfo
CustomerID *int `gorm:"type:int" json:"customer_id,omitempty" form:"customer_id,omitempty"`
CustomerName *string `gorm:"type:varchar(50)" json:"customer_name,omitempty" form:"customer_name"`
ReceivableDate *string `gorm:"type:varchar(50)" json:"receivable_date,omitempty" form:"receivable_date" cn:"应收日期"`
ReceivableTotalAmount *string `gorm:"type:varchar(50)" json:"receivable_total_amount,omitempty" form:"receivable_total_amount" cn:"账单总金额"`
ReceivableActualAmount *string `gorm:"type:varchar(50)" json:"receivable_actual_amount,omitempty" form:"receivable_actual_amount" cn:"实际收款"`
ReceivableDiscount *string `gorm:"type:varchar(50)" json:"receivable_discount,omitempty" form:"receivable_discount" cn:"零头优惠"`
ReceivableHedgingAmount *string `gorm:"type:varchar(50)" json:"receivable_hedging_amount,omitempty" form:"receivable_hedging_amount" cn:"红冲金额"`
ReceivableDebtAmount *string `gorm:"type:varchar(50)" json:"receivable_debt_amount,omitempty" form:"receivable_debt_amount" cn:"欠款金额"`
ReceivableStatus *_const.ReceivableStatus `gorm:"type:int" json:"receivable_status,omitempty" form:"receivable_status" cn:"收款状态"`
CredentialID *int `gorm:"type:int" json:"credential_id,omitempty" form:"credential_id" cn:"关联凭证ID"`
TransactionID *int `gorm:"type:int" json:"transaction_id,omitempty" form:"transaction_id,omitempty" cn:"关联销售单ID"`
Remark *string `gorm:"type:varchar(200)" json:"remark,omitempty" form:"remark"`
}
func (p Receivable) TableCnName() string {
return "应收"
}
func (p Receivable) TableName() string {
return "receivables"
}
func (p Receivable) Mapping() map[string]interface{} {
m := mapping{
"settings": mapping{},
"mappings": mapping{
"properties": mapping{
"rec_id": mapping{
"type": "keyword",
},
"customer_name": mapping{
"type": "text",
"analyzer": IK_SMART,
"search_analyzer": IK_SMART,
"fields": mapping{
"keyword": mapping{
"type": "keyword",
"ignore_above": 256,
},
},
},
"receivable_total_amount": mapping{
"type": "text",
"analyzer": IK_SMART,
"search_analyzer": IK_SMART,
},
"credential_id": mapping{
"type": "text",
"analyzer": IK_SMART,
"search_analyzer": IK_SMART,
},
"transaction_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 Receivable) ToESDoc() map[string]interface{} {
return map[string]interface{}{
"rec_id": p.RecID,
"remark": p.Remark,
"book_name": p.BookName,
"book_name_id": p.BookNameID,
"transaction_id": p.TransactionID,
"credential_id": p.CredentialID,
"customer_name": p.CustomerName,
"receivable_total_amount": p.ReceivableTotalAmount,
}
}
func (p *Receivable) 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 *Receivable) 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 *Receivable) AfterDelete(tx *gorm.DB) error {
err := client.DeleteFromIndex(p, p.RecID, tx.Statement.Context)
if err != nil {
return err
}
return nil
}
func (p *Receivable) UpdateStatus() {
if p.ReceivableDebtAmount == nil {
}
}