forked from babyname/fate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wuge.go
291 lines (264 loc) · 6.37 KB
/
wuge.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package fate
import (
"github.com/goextension/log"
"github.com/google/uuid"
"github.com/xormsharp/xorm"
)
// WuGe ...
type WuGe struct {
tianGe int
renGe int
diGe int
waiGe int
zongGe int
}
// ZongGe ...
func (ge *WuGe) ZongGe() int {
return ge.zongGe
}
// WaiGe ...
func (ge *WuGe) WaiGe() int {
return ge.waiGe
}
// DiGe ...
func (ge *WuGe) DiGe() int {
return ge.diGe
}
// RenGe ...
func (ge *WuGe) RenGe() int {
return ge.renGe
}
// TianGe ...
func (ge *WuGe) TianGe() int {
return ge.tianGe
}
//CalcWuGe 计算五格
func CalcWuGe(l1, l2, f1, f2 int) *WuGe {
return &WuGe{
tianGe: tianGe(l1, l2, f1, f2),
renGe: renGe(l1, l2, f1, f2),
diGe: diGe(l1, l2, f1, f2),
waiGe: waiGe(l1, l2, f1, f2),
zongGe: zongGe(l1, l2, f1, f2),
}
}
//tianGe input the ScienceStrokes with last name
//天格(复姓)姓的笔画相加
//天格(单姓)姓的笔画上加一
func tianGe(l1, l2, _, _ int) int {
if l2 == 0 {
return l1 + 1
}
return l1 + l2
}
//renGe input the ScienceStrokes with name
//人格(复姓)姓氏的第二字的笔画加名的第一字
//人格(复姓单名)姓的第二字加名
//人格(单姓单名)姓加名
// 人格(单姓复名)姓加名的第一字
func renGe(l1, l2, f1, _ int) int {
//人格(复姓)姓氏的第二字的笔画加名的第一字
//人格(复姓单名)姓的第二字加名
if l2 != 0 {
return l2 + f1
}
return l1 + f1
}
//diGe input the ScienceStrokes with name
//地格(复姓复名,单姓复名)名字相加
//地格(复姓单名,单姓单名)名字+1
func diGe(_, _, f1, f2 int) int {
if f2 == 0 {
return f1 + 1
}
return f1 + f2
}
//waiGe input the ScienceStrokes with name
//外格(复姓单名)姓的第一字加笔画数一
//外格(复姓复名)姓的第一字和名的最后一定相加的笔画数
//外格(单姓复名)一加名的最后一个字
//外格(单姓单名)一加一
func waiGe(l1, l2, _, f2 int) (n int) {
//单姓单名
if l2 == 0 && f2 == 0 {
n = 1 + 1
}
//单姓复名
if l2 == 0 && f2 != 0 {
n = 1 + f2
}
//复姓单名
if l2 != 0 && f2 == 0 {
n = l1 + 1
}
//复姓复名
if l2 != 0 && f2 != 0 {
n = l1 + f2
}
return n
}
//zongGe input the ScienceStrokes with name
//总格,姓加名的笔画总数 数理五行分类
func zongGe(l1, l2, f1, f2 int) int {
//归1
zg := (l1 + l2 + f1 + f2) - 1
if zg < 0 {
zg = zg + 81
}
return zg%81 + 1
}
//Check 格检查
func (ge *WuGe) Check(ss ...string) bool {
v := map[string]bool{}
if ss == nil {
ss = append(ss, "吉", "半吉")
}
//ignore:tianGe
v[GetDaYan(ge.diGe).Lucky] = false
v[GetDaYan(ge.renGe).Lucky] = false
v[GetDaYan(ge.waiGe).Lucky] = false
v[GetDaYan(ge.zongGe).Lucky] = false
for l := range v {
for i := range ss {
if ss[i] == l {
v[l] = true
break
}
}
}
for l := range v {
if v[l] == false {
return false
}
}
return true
}
//WuGeLucky ...
type WuGeLucky struct {
ID string `xorm:"id pk"`
LastStroke1 int `xorm:"last_stroke_1"`
LastStroke2 int `xorm:"last_stroke_2"`
FirstStroke1 int `xorm:"first_stroke_1"`
FirstStroke2 int `xorm:"first_stroke_2"`
TianGe int `xorm:"tian_ge"`
TianDaYan string `xorm:"tian_da_yan"`
RenGe int `xorm:"ren_ge"`
RenDaYan string `xorm:"ren_da_yan"`
DiGe int `xorm:"di_ge"`
DiDaYan string `xorm:"di_da_yan"`
WaiGe int `xorm:"wai_ge"`
WaiDaYan string `xorm:"wai_da_yan"`
ZongGe int `xorm:"zong_ge"`
ZongDaYan string `xorm:"zong_da_yan"`
ZongLucky bool `xorm:"zong_lucky"`
ZongSex bool `xorm:"zong_sex"`
ZongMax bool `xorm:"zong_max"`
}
// BeforeInsert ...
func (w *WuGeLucky) BeforeInsert() {
w.ID = uuid.Must(uuid.NewUUID()).String()
}
func countWuGeLucky(engine *xorm.Engine) (n int64, e error) {
return engine.Table(&WuGeLucky{}).Count()
}
func insertOrUpdateWuGeLucky(engine *xorm.Engine, lucky *WuGeLucky) (n int64, e error) {
session := engine.Where("last_stroke_1 = ?", lucky.LastStroke1).
Where("last_stroke_2 = ?", lucky.LastStroke2).
Where("first_stroke_1 = ?", lucky.FirstStroke1).
Where("first_stroke_2 = ?", lucky.FirstStroke2)
n, e = session.Clone().Count(&WuGeLucky{})
if e != nil {
return n, e
}
log.Infow("lucky", lucky)
if n == 0 {
n, e = engine.InsertOne(lucky)
return
}
return session.Clone().Update(lucky)
}
// WuGeMax ...
const WuGeMax = 32
func initWuGe(lucky chan<- *WuGeLucky) {
defer func() {
close(lucky)
}()
var wuge *WuGe
for l1 := 1; l1 <= WuGeMax; l1++ {
for l2 := 0; l2 <= WuGeMax; l2++ {
for f1 := 1; f1 <= WuGeMax; f1++ {
for f2 := 1; f2 <= WuGeMax; f2++ {
wuge = CalcWuGe(l1, l2, f1, f2)
lucky <- &WuGeLucky{
ID: "",
LastStroke1: l1,
LastStroke2: l2,
FirstStroke1: f1,
FirstStroke2: f2,
TianGe: wuge.tianGe,
TianDaYan: GetDaYan(wuge.tianGe).Lucky,
RenGe: wuge.renGe,
RenDaYan: GetDaYan(wuge.renGe).Lucky,
DiGe: wuge.diGe,
DiDaYan: GetDaYan(wuge.diGe).Lucky,
WaiGe: wuge.waiGe,
WaiDaYan: GetDaYan(wuge.waiGe).Lucky,
ZongGe: wuge.zongGe,
ZongDaYan: GetDaYan(wuge.zongGe).Lucky,
ZongLucky: wuge.Check(),
ZongSex: isSex(wuge.zongGe, wuge.waiGe, wuge.renGe, wuge.diGe),
ZongMax: GetDaYan(wuge.zongGe).IsMax(),
}
}
}
}
}
}
func getStroke(character *Character) int {
if character.ScienceStroke != 0 {
return character.ScienceStroke
} else if character.KangXiStroke != 0 {
return character.KangXiStroke
} else if character.Stroke != 0 {
return character.Stroke
} else if character.SimpleTotalStroke != 0 {
return character.SimpleTotalStroke
} else if character.TraditionalTotalStroke != 0 {
return character.TraditionalTotalStroke
}
return 0
}
func isSex(dys ...int) bool {
for _, dy := range dys {
if GetDaYan(dy).Sex {
return true
}
}
return false
}
func filterWuGe(eng *xorm.Engine, last []*Character, wg chan<- *WuGeLucky) error {
defer func() {
close(wg)
}()
l1 := getStroke(last[0])
l2 := 0
if len(last) == 2 {
l2 = getStroke(last[1])
}
s := eng.Where("last_stroke_1 =?", l1).
And("last_stroke_2 =?", l2).
And("zong_lucky = ?", 1)
rows, e := s.Rows(&WuGeLucky{})
if e != nil {
return e
}
for rows.Next() {
var tmp WuGeLucky
e := rows.Scan(&tmp)
if e != nil {
return e
}
wg <- &tmp
}
return nil
}