|
@@ -54,6 +54,9 @@ class CalendarInfo(db.Model):
|
|
# 忌 (text)
|
|
# 忌 (text)
|
|
ji = db.Column(db.Text, nullable=True, comment='忌')
|
|
ji = db.Column(db.Text, nullable=True, comment='忌')
|
|
|
|
|
|
|
|
+ # 颜色 (varchar(10))
|
|
|
|
+ color = db.Column(db.String(10), nullable=True, comment='颜色')
|
|
|
|
+
|
|
def __init__(self, **kwargs):
|
|
def __init__(self, **kwargs):
|
|
super().__init__(**kwargs)
|
|
super().__init__(**kwargs)
|
|
|
|
|
|
@@ -77,7 +80,8 @@ class CalendarInfo(db.Model):
|
|
'jishen': self.jishen,
|
|
'jishen': self.jishen,
|
|
'yi': self.yi,
|
|
'yi': self.yi,
|
|
'xiongshen': self.xiongshen,
|
|
'xiongshen': self.xiongshen,
|
|
- 'ji': self.ji
|
|
|
|
|
|
+ 'ji': self.ji,
|
|
|
|
+ 'color': self.color
|
|
}
|
|
}
|
|
|
|
|
|
def to_json(self) -> str:
|
|
def to_json(self) -> str:
|
|
@@ -108,16 +112,34 @@ class CalendarInfo(db.Model):
|
|
except ValueError:
|
|
except ValueError:
|
|
yangli = None
|
|
yangli = None
|
|
|
|
|
|
|
|
+ # 从wuxing字段中判断五行元素并设置对应的颜色值
|
|
|
|
+ wuxing = data.get('wuxing', '') or ''
|
|
|
|
+ color = data.get('color') # 先获取字典中的color值
|
|
|
|
+
|
|
|
|
+ # 如果字典中没有color值,则根据wuxing字段判断五行元素设置颜色
|
|
|
|
+ if not color:
|
|
|
|
+ if '金' in wuxing:
|
|
|
|
+ color = '白'
|
|
|
|
+ elif '水' in wuxing:
|
|
|
|
+ color = '黑'
|
|
|
|
+ elif '木' in wuxing:
|
|
|
|
+ color = '绿'
|
|
|
|
+ elif '火' in wuxing:
|
|
|
|
+ color = '红'
|
|
|
|
+ elif '土' in wuxing:
|
|
|
|
+ color = '黄'
|
|
|
|
+
|
|
return cls(
|
|
return cls(
|
|
yangli=yangli, # type: ignore
|
|
yangli=yangli, # type: ignore
|
|
yinli=data.get('yinli'), # type: ignore
|
|
yinli=data.get('yinli'), # type: ignore
|
|
- wuxing=data.get('wuxing'), # type: ignore
|
|
|
|
|
|
+ wuxing=wuxing, # type: ignore
|
|
chongsha=data.get('chongsha'), # type: ignore
|
|
chongsha=data.get('chongsha'), # type: ignore
|
|
baiji=data.get('baiji'), # type: ignore
|
|
baiji=data.get('baiji'), # type: ignore
|
|
jishen=data.get('jishen'), # type: ignore
|
|
jishen=data.get('jishen'), # type: ignore
|
|
yi=data.get('yi'), # type: ignore
|
|
yi=data.get('yi'), # type: ignore
|
|
xiongshen=data.get('xiongshen'), # type: ignore
|
|
xiongshen=data.get('xiongshen'), # type: ignore
|
|
- ji=data.get('ji') # type: ignore
|
|
|
|
|
|
+ ji=data.get('ji'), # type: ignore
|
|
|
|
+ color=color # type: ignore
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
@@ -338,7 +360,8 @@ class CalendarService:
|
|
(CalendarInfo.jishen.contains(keyword)) |
|
|
(CalendarInfo.jishen.contains(keyword)) |
|
|
(CalendarInfo.yi.contains(keyword)) |
|
|
(CalendarInfo.yi.contains(keyword)) |
|
|
(CalendarInfo.xiongshen.contains(keyword)) |
|
|
(CalendarInfo.xiongshen.contains(keyword)) |
|
|
- (CalendarInfo.ji.contains(keyword))
|
|
|
|
|
|
+ (CalendarInfo.ji.contains(keyword)) |
|
|
|
|
+ (CalendarInfo.color.contains(keyword))
|
|
).limit(limit).all()
|
|
).limit(limit).all()
|
|
else:
|
|
else:
|
|
# 使用Flask-SQLAlchemy的db.session
|
|
# 使用Flask-SQLAlchemy的db.session
|
|
@@ -350,7 +373,8 @@ class CalendarService:
|
|
(CalendarInfo.jishen.contains(keyword)) |
|
|
(CalendarInfo.jishen.contains(keyword)) |
|
|
(CalendarInfo.yi.contains(keyword)) |
|
|
(CalendarInfo.yi.contains(keyword)) |
|
|
(CalendarInfo.xiongshen.contains(keyword)) |
|
|
(CalendarInfo.xiongshen.contains(keyword)) |
|
|
- (CalendarInfo.ji.contains(keyword))
|
|
|
|
|
|
+ (CalendarInfo.ji.contains(keyword)) |
|
|
|
|
+ (CalendarInfo.color.contains(keyword))
|
|
).limit(limit).all()
|
|
).limit(limit).all()
|
|
|
|
|
|
def fetch_calendar_from_api(self, yangli_date: date) -> Optional[dict]:
|
|
def fetch_calendar_from_api(self, yangli_date: date) -> Optional[dict]:
|
|
@@ -425,17 +449,35 @@ class CalendarService:
|
|
print(f"无效的日期格式: {yangli_str}")
|
|
print(f"无效的日期格式: {yangli_str}")
|
|
return None
|
|
return None
|
|
|
|
|
|
|
|
+ # 从wuxing字段中判断五行元素并设置对应的颜色值
|
|
|
|
+ wuxing = api_data.get('wuxing', '') or ''
|
|
|
|
+ color = api_data.get('color') # 先获取API中的color值
|
|
|
|
+
|
|
|
|
+ # 如果API中没有color值,则根据wuxing字段判断五行元素设置颜色
|
|
|
|
+ if not color:
|
|
|
|
+ if '金' in wuxing:
|
|
|
|
+ color = '白'
|
|
|
|
+ elif '水' in wuxing:
|
|
|
|
+ color = '黑'
|
|
|
|
+ elif '木' in wuxing:
|
|
|
|
+ color = '绿'
|
|
|
|
+ elif '火' in wuxing:
|
|
|
|
+ color = '红'
|
|
|
|
+ elif '土' in wuxing:
|
|
|
|
+ color = '黄'
|
|
|
|
+
|
|
# 创建CalendarInfo对象
|
|
# 创建CalendarInfo对象
|
|
calendar_info = CalendarInfo(
|
|
calendar_info = CalendarInfo(
|
|
yangli=yangli_date, # type: ignore
|
|
yangli=yangli_date, # type: ignore
|
|
yinli=api_data.get('yinli', ''), # type: ignore
|
|
yinli=api_data.get('yinli', ''), # type: ignore
|
|
- wuxing=api_data.get('wuxing'), # type: ignore
|
|
|
|
|
|
+ wuxing=wuxing, # type: ignore
|
|
chongsha=api_data.get('chongsha'), # type: ignore
|
|
chongsha=api_data.get('chongsha'), # type: ignore
|
|
baiji=api_data.get('baiji'), # type: ignore
|
|
baiji=api_data.get('baiji'), # type: ignore
|
|
jishen=api_data.get('jishen'), # type: ignore
|
|
jishen=api_data.get('jishen'), # type: ignore
|
|
yi=api_data.get('yi'), # type: ignore
|
|
yi=api_data.get('yi'), # type: ignore
|
|
xiongshen=api_data.get('xionshen'), # type: ignore # 注意API返回的是xionshen
|
|
xiongshen=api_data.get('xionshen'), # type: ignore # 注意API返回的是xionshen
|
|
- ji=api_data.get('ji') # type: ignore
|
|
|
|
|
|
+ ji=api_data.get('ji'), # type: ignore
|
|
|
|
+ color=color # type: ignore
|
|
)
|
|
)
|
|
|
|
|
|
# 保存到数据库
|
|
# 保存到数据库
|
|
@@ -525,7 +567,8 @@ def get_calendar_by_date(yangli_date: str, engine=None) -> dict:
|
|
"jishen": calendar_info.jishen,
|
|
"jishen": calendar_info.jishen,
|
|
"yi": calendar_info.yi,
|
|
"yi": calendar_info.yi,
|
|
"xiongshen": calendar_info.xiongshen,
|
|
"xiongshen": calendar_info.xiongshen,
|
|
- "ji": calendar_info.ji
|
|
|
|
|
|
+ "ji": calendar_info.ji,
|
|
|
|
+ "color": calendar_info.color
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else:
|
|
else:
|
|
@@ -555,7 +598,8 @@ def get_calendar_by_date(yangli_date: str, engine=None) -> dict:
|
|
"jishen": saved_calendar.jishen,
|
|
"jishen": saved_calendar.jishen,
|
|
"yi": saved_calendar.yi,
|
|
"yi": saved_calendar.yi,
|
|
"xiongshen": saved_calendar.xiongshen,
|
|
"xiongshen": saved_calendar.xiongshen,
|
|
- "ji": saved_calendar.ji
|
|
|
|
|
|
+ "ji": saved_calendar.ji,
|
|
|
|
+ "color": saved_calendar.color
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else:
|
|
else:
|