-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixed_asset.go
144 lines (135 loc) · 4.28 KB
/
fixed_asset.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
143
144
package mysql_model
import (
"github.com/XC-Zero/yinwan/pkg/client"
"github.com/XC-Zero/yinwan/pkg/utils/es_tool"
"github.com/pkg/errors"
"gorm.io/gorm"
)
// Currency 货币
type Currency struct {
BasicModel
// 货币名称
CurrencyName string `gorm:"type:varchar(50)"`
// 货币符号
CurrencySymbol string `gorm:"type:varchar(50)"`
// 对比人民币的汇率
CurrencyExchangeRate string `gorm:"type:varchar(20)"`
}
// FixedAsset 固定资产
// 固定资产类型存放于 TypeTree
type FixedAsset struct {
BasicModel
BookNameInfo
FixedAssetPicUrl *string `gorm:"type:varchar(500)" json:"fixed_asset_pic_url,omitempty" form:"fixed_asset_pic_url,omitempty" `
FixedAssetName string `gorm:"type:varchar(100);not null'" form:"fixed_asset_name" json:"fixed_asset_name"`
FixedAssetTypeID *int `gorm:"type:int;index" json:"fixed_asset_type_id,omitempty" form:"fixed_asset_type_id" cn:"固定资产类型ID"`
FixedAssetTypeName *string `gorm:"type:varchar(50)" json:"fixed_asset_type_name,omitempty" form:"fixed_asset_type_name" cn:"固定资产类型名称"`
DepreciationPeriod int `gorm:"type:int;not null" json:"depreciation_period" form:"depreciation_period" cn:"折旧期限(月)"`
TotalPrice *string `gorm:"type:varchar(20);not null" json:"total_price,omitempty" form:"total_price" cn:"原价"`
CurrentPrice *string `gorm:"type:varchar(20);not null" json:"current_price,omitempty" form:"current_price" cn:"残值"`
MonthlyDepreciationAmount *string `gorm:"type:varchar(20);not null" json:"monthly_depreciation_amount,omitempty" form:"monthly_depreciation_amount" cn:"每月折旧额"`
Remark *string `gorm:"type:varchar(200)" json:"remark" form:"remark" cn:"备注"`
}
func (p FixedAsset) TableCnName() string {
return "固定资产"
}
func (p FixedAsset) TableName() string {
return "fixed_assets"
}
func (p FixedAsset) Mapping() map[string]interface{} {
m := mapping{
"settings": mapping{},
"mappings": mapping{
"properties": mapping{
"rec_id": mapping{
"type": "keyword",
},
"fixed_asset_name": mapping{
"type": "text",
"analyzer": IK_SMART,
"search_analyzer": IK_SMART,
"fields": mapping{
"keyword": mapping{
"type": "keyword",
"ignore_above": 256,
},
},
},
"fixed_asset_amount": mapping{
"type": "text",
"analyzer": IK_SMART,
"search_analyzer": IK_SMART,
},
"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 FixedAsset) ToESDoc() map[string]interface{} {
return map[string]interface{}{
"rec_id": p.RecID,
"remark": p.Remark,
"created_at": p.CreatedAt,
"fixed_asset_amount": p.TotalPrice,
"fixed_asset_name": p.FixedAssetName,
"book_name": p.BookName,
"book_name_id": p.BookNameID,
}
}
func (p *FixedAsset) 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 *FixedAsset) 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 *FixedAsset) AfterDelete(tx *gorm.DB) error {
err := client.DeleteFromIndex(p, p.RecID, tx.Statement.Context)
if err != nil {
return err
}
return nil
}
// FixedAssetStatement 固定资产月度表
type FixedAssetStatement struct {
BasicModel
}
func (m FixedAssetStatement) TableName() string {
return "fixed_asset_statements"
}