- 使用自定义的getXingZuoByDate方法替代solar.getXingZuo() - 确保星座日期范围与显示的日期范围一致 - 白羊座: 3月21日-4月19日 - 金牛座: 4月20日-5月20日 - 双子座: 5月21日-6月21日 - 巨蟹座: 6月22日-7月22日 - 狮子座: 7月23日-8月22日 - 处女座: 8月23日-9月22日 - 天秤座: 9月23日-10月23日 - 天蝎座: 10月24日-11月22日 - 射手座: 11月23日-12月21日 - 摩羯座: 12月22日-1月19日 - 水瓶座: 1月20日-2月18日 - 双鱼座: 2月19日-3月20日
447 lines
21 KiB
Java
447 lines
21 KiB
Java
package xuan.controller;
|
|
|
|
import com.nlf.calendar.Lunar;
|
|
import com.nlf.calendar.Solar;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.util.*;
|
|
|
|
@RestController
|
|
@RequestMapping("/api/tools")
|
|
public class ToolController {
|
|
|
|
@PostMapping("/lunar")
|
|
public Map<String, Object> lunarQuery(@RequestBody Map<String, Object> params) {
|
|
Map<String, Object> result = new HashMap<>();
|
|
try {
|
|
String date = params.get("date").toString();
|
|
int type = params.containsKey("type") ? Integer.parseInt(params.get("type").toString()) : 0;
|
|
|
|
Map<String, Object> data = new HashMap<>();
|
|
|
|
if (type == 0) {
|
|
Solar solar = Solar.fromYmdHms(
|
|
Integer.parseInt(date.substring(0, 4)),
|
|
Integer.parseInt(date.substring(5, 7)),
|
|
Integer.parseInt(date.substring(8, 10)),
|
|
params.containsKey("hour") ? Integer.parseInt(params.get("hour").toString()) : 0,
|
|
params.containsKey("minute") ? Integer.parseInt(params.get("minute").toString()) : 0,
|
|
0
|
|
);
|
|
Lunar lunar = solar.getLunar();
|
|
|
|
data.put("solar", solar.toString());
|
|
data.put("lunar", lunar.toString());
|
|
data.put("lunarYear", lunar.getYearInGanZhi());
|
|
data.put("lunarMonth", lunar.getMonthInGanZhi());
|
|
data.put("lunarDay", lunar.getDayInGanZhi());
|
|
data.put("lunarYearName", lunar.getYearShengXiao() + "年");
|
|
data.put("lunarMonthName", lunar.getMonthInChinese());
|
|
data.put("lunarDayName", lunar.getDayInChinese());
|
|
data.put("festival", lunar.getFestivals());
|
|
data.put("jieQi", lunar.getJieQi());
|
|
data.put("week", solar.getWeekInChinese());
|
|
data.put("constellation", solar.getXingZuo());
|
|
} else {
|
|
int year = Integer.parseInt(params.get("lunarYear").toString());
|
|
int month = Integer.parseInt(params.get("lunarMonth").toString());
|
|
int day = Integer.parseInt(params.get("lunarDay").toString());
|
|
boolean isLeap = params.containsKey("isLeap") && Boolean.parseBoolean(params.get("isLeap").toString());
|
|
|
|
Lunar lunar = isLeap ? Lunar.fromYmd(year, -month, day) : Lunar.fromYmd(year, month, day);
|
|
Solar solar = lunar.getSolar();
|
|
|
|
data.put("lunar", lunar.toString());
|
|
data.put("solar", solar.toString());
|
|
data.put("solarYear", solar.getYear());
|
|
data.put("solarMonth", solar.getMonth());
|
|
data.put("solarDay", solar.getDay());
|
|
}
|
|
|
|
result.put("success", true);
|
|
result.put("data", data);
|
|
} catch (Exception e) {
|
|
result.put("success", false);
|
|
result.put("error", e.getMessage());
|
|
}
|
|
return result;
|
|
}
|
|
|
|
@PostMapping("/ganzhi")
|
|
public Map<String, Object> ganZhiQuery(@RequestBody Map<String, Object> params) {
|
|
Map<String, Object> result = new HashMap<>();
|
|
try {
|
|
String date = params.get("date").toString();
|
|
int hour = params.containsKey("hour") ? Integer.parseInt(params.get("hour").toString()) : 0;
|
|
|
|
Solar solar = Solar.fromYmdHms(
|
|
Integer.parseInt(date.substring(0, 4)),
|
|
Integer.parseInt(date.substring(5, 7)),
|
|
Integer.parseInt(date.substring(8, 10)),
|
|
hour, 0, 0
|
|
);
|
|
Lunar lunar = solar.getLunar();
|
|
|
|
Map<String, Object> data = new HashMap<>();
|
|
data.put("yearGanZhi", lunar.getYearInGanZhi());
|
|
data.put("monthGanZhi", lunar.getMonthInGanZhi());
|
|
data.put("dayGanZhi", lunar.getDayInGanZhi());
|
|
data.put("hourGanZhi", lunar.getTimeInGanZhi());
|
|
data.put("yearGan", lunar.getYearGan());
|
|
data.put("yearZhi", lunar.getYearZhi());
|
|
data.put("monthGan", lunar.getMonthGan());
|
|
data.put("monthZhi", lunar.getMonthZhi());
|
|
data.put("dayGan", lunar.getDayGan());
|
|
data.put("dayZhi", lunar.getDayZhi());
|
|
data.put("hourGan", lunar.getTimeGan());
|
|
data.put("hourZhi", lunar.getTimeZhi());
|
|
|
|
result.put("success", true);
|
|
result.put("data", data);
|
|
} catch (Exception e) {
|
|
result.put("success", false);
|
|
result.put("error", e.getMessage());
|
|
}
|
|
return result;
|
|
}
|
|
|
|
@PostMapping("/shengxiao")
|
|
public Map<String, Object> shengXiaoQuery(@RequestBody Map<String, Object> params) {
|
|
Map<String, Object> result = new HashMap<>();
|
|
try {
|
|
int year = Integer.parseInt(params.get("year").toString());
|
|
|
|
String[] shengXiaoList = {"鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"};
|
|
String[] ganList = {"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"};
|
|
String[] zhiList = {"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"};
|
|
|
|
int shengXiaoIndex = (year - 4) % 12;
|
|
if (shengXiaoIndex < 0) shengXiaoIndex += 12;
|
|
String shengXiao = shengXiaoList[shengXiaoIndex];
|
|
|
|
int ganIndex = (year - 4) % 10;
|
|
if (ganIndex < 0) ganIndex += 10;
|
|
int zhiIndex = (year - 4) % 12;
|
|
if (zhiIndex < 0) zhiIndex += 12;
|
|
String ganZhi = ganList[ganIndex] + zhiList[zhiIndex];
|
|
|
|
Map<String, Object> data = new HashMap<>();
|
|
data.put("year", year);
|
|
data.put("shengXiao", shengXiao);
|
|
|
|
Map<String, String> shengXiaoInfo = new HashMap<>();
|
|
shengXiaoInfo.put("鼠", "子鼠,五行属水,聪明机智,适应力强");
|
|
shengXiaoInfo.put("牛", "丑牛,五行属土,勤劳踏实,责任心强");
|
|
shengXiaoInfo.put("虎", "寅虎,五行属木,勇敢自信,领导力强");
|
|
shengXiaoInfo.put("兔", "卯兔,五行属木,温柔善良,心思细腻");
|
|
shengXiaoInfo.put("龙", "辰龙,五行属土,气宇轩昂,有远大理想");
|
|
shengXiaoInfo.put("蛇", "巳蛇,五行属火,智慧深沉,洞察力强");
|
|
shengXiaoInfo.put("马", "午马,五行属火,热情奔放,行动力强");
|
|
shengXiaoInfo.put("羊", "未羊,五行属土,温文尔雅,富有同情心");
|
|
shengXiaoInfo.put("猴", "申猴,五行属金,聪明伶俐,多才多艺");
|
|
shengXiaoInfo.put("鸡", "酉鸡,五行属金,精明能干,善于表达");
|
|
shengXiaoInfo.put("狗", "戌狗,五行属土,忠诚可靠,正义感强");
|
|
shengXiaoInfo.put("猪", "亥猪,五行属水,善良宽厚,乐观豁达");
|
|
|
|
data.put("info", shengXiaoInfo.getOrDefault(shengXiao, "未知生肖"));
|
|
data.put("ganZhi", ganZhi);
|
|
|
|
result.put("success", true);
|
|
result.put("data", data);
|
|
} catch (Exception e) {
|
|
result.put("success", false);
|
|
result.put("error", e.getMessage());
|
|
}
|
|
return result;
|
|
}
|
|
|
|
@PostMapping("/xingzuo")
|
|
public Map<String, Object> xingZuoQuery(@RequestBody Map<String, Object> params) {
|
|
Map<String, Object> result = new HashMap<>();
|
|
try {
|
|
int month = Integer.parseInt(params.get("month").toString());
|
|
int day = Integer.parseInt(params.get("day").toString());
|
|
|
|
String xingZuo = getXingZuoByDate(month, day);
|
|
|
|
Map<String, Object> data = new HashMap<>();
|
|
data.put("month", month);
|
|
data.put("day", day);
|
|
data.put("xingZuo", xingZuo);
|
|
|
|
Map<String, Map<String, String>> xingZuoInfo = new HashMap<>();
|
|
|
|
Map<String, String> aries = new HashMap<>();
|
|
aries.put("dateRange", "3月21日-4月19日");
|
|
aries.put("element", "火象星座");
|
|
aries.put("character", "热情、冲动、自信");
|
|
aries.put("desc", "白羊座是黄道十二宫的第一宫,代表着新的开始和生命力。");
|
|
xingZuoInfo.put("白羊座", aries);
|
|
|
|
Map<String, String> taurus = new HashMap<>();
|
|
taurus.put("dateRange", "4月20日-5月20日");
|
|
taurus.put("element", "土象星座");
|
|
taurus.put("character", "稳重、务实、固执");
|
|
taurus.put("desc", "金牛座是黄道十二宫的第二宫,代表着物质和财富。");
|
|
xingZuoInfo.put("金牛座", taurus);
|
|
|
|
Map<String, String> gemini = new HashMap<>();
|
|
gemini.put("dateRange", "5月21日-6月21日");
|
|
gemini.put("element", "风象星座");
|
|
gemini.put("character", "聪明、善变、好奇");
|
|
gemini.put("desc", "双子座是黄道十二宫的第三宫,代表着沟通和交流。");
|
|
xingZuoInfo.put("双子座", gemini);
|
|
|
|
Map<String, String> cancer = new HashMap<>();
|
|
cancer.put("dateRange", "6月22日-7月22日");
|
|
cancer.put("element", "水象星座");
|
|
cancer.put("character", "敏感、顾家、情绪化");
|
|
cancer.put("desc", "巨蟹座是黄道十二宫的第四宫,代表着家庭和情感。");
|
|
xingZuoInfo.put("巨蟹座", cancer);
|
|
|
|
Map<String, String> leo = new HashMap<>();
|
|
leo.put("dateRange", "7月23日-8月22日");
|
|
leo.put("element", "火象星座");
|
|
leo.put("character", "自信、慷慨、霸道");
|
|
leo.put("desc", "狮子座是黄道十二宫的第五宫,代表着创造力和自我表达。");
|
|
xingZuoInfo.put("狮子座", leo);
|
|
|
|
Map<String, String> virgo = new HashMap<>();
|
|
virgo.put("dateRange", "8月23日-9月22日");
|
|
virgo.put("element", "土象星座");
|
|
virgo.put("character", "细心、完美主义、挑剔");
|
|
virgo.put("desc", "处女座是黄道十二宫的第六宫,代表着分析和服务。");
|
|
xingZuoInfo.put("处女座", virgo);
|
|
|
|
Map<String, String> libra = new HashMap<>();
|
|
libra.put("dateRange", "9月23日-10月23日");
|
|
libra.put("element", "风象星座");
|
|
libra.put("character", "优雅、公正、犹豫");
|
|
libra.put("desc", "天秤座是黄道十二宫的第七宫,代表着平衡和和谐。");
|
|
xingZuoInfo.put("天秤座", libra);
|
|
|
|
Map<String, String> scorpio = new HashMap<>();
|
|
scorpio.put("dateRange", "10月24日-11月22日");
|
|
scorpio.put("element", "水象星座");
|
|
scorpio.put("character", "神秘、执着、多疑");
|
|
scorpio.put("desc", "天蝎座是黄道十二宫的第八宫,代表着转化和重生。");
|
|
xingZuoInfo.put("天蝎座", scorpio);
|
|
|
|
Map<String, String> sagittarius = new HashMap<>();
|
|
sagittarius.put("dateRange", "11月23日-12月21日");
|
|
sagittarius.put("element", "火象星座");
|
|
sagittarius.put("character", "乐观、自由、粗心");
|
|
sagittarius.put("desc", "射手座是黄道十二宫的第九宫,代表着探索和哲学。");
|
|
xingZuoInfo.put("射手座", sagittarius);
|
|
|
|
Map<String, String> capricorn = new HashMap<>();
|
|
capricorn.put("dateRange", "12月22日-1月19日");
|
|
capricorn.put("element", "土象星座");
|
|
capricorn.put("character", "务实、有责任感、悲观");
|
|
capricorn.put("desc", "摩羯座是黄道十二宫的第十宫,代表着成就和责任。");
|
|
xingZuoInfo.put("摩羯座", capricorn);
|
|
|
|
Map<String, String> aquarius = new HashMap<>();
|
|
aquarius.put("dateRange", "1月20日-2月18日");
|
|
aquarius.put("element", "风象星座");
|
|
aquarius.put("character", "独立、创新、叛逆");
|
|
aquarius.put("desc", "水瓶座是黄道十二宫的第十一宫,代表着创新和人道主义。");
|
|
xingZuoInfo.put("水瓶座", aquarius);
|
|
|
|
Map<String, String> pisces = new HashMap<>();
|
|
pisces.put("dateRange", "2月19日-3月20日");
|
|
pisces.put("element", "水象星座");
|
|
pisces.put("character", "浪漫、敏感、优柔寡断");
|
|
pisces.put("desc", "双鱼座是黄道十二宫的最后一宫,代表着灵性和直觉。");
|
|
xingZuoInfo.put("双鱼座", pisces);
|
|
|
|
Map<String, String> defaultInfo = new HashMap<>();
|
|
defaultInfo.put("desc", "未知星座");
|
|
|
|
Map<String, String> info = xingZuoInfo.getOrDefault(xingZuo, defaultInfo);
|
|
data.put("dateRange", info.get("dateRange"));
|
|
data.put("element", info.get("element"));
|
|
data.put("character", info.get("character"));
|
|
data.put("desc", info.get("desc"));
|
|
|
|
result.put("success", true);
|
|
result.put("data", data);
|
|
} catch (Exception e) {
|
|
result.put("success", false);
|
|
result.put("error", e.getMessage());
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private String getXingZuoByDate(int month, int day) {
|
|
if ((month == 3 && day >= 21) || (month == 4 && day <= 19)) {
|
|
return "白羊座";
|
|
} else if ((month == 4 && day >= 20) || (month == 5 && day <= 20)) {
|
|
return "金牛座";
|
|
} else if ((month == 5 && day >= 21) || (month == 6 && day <= 21)) {
|
|
return "双子座";
|
|
} else if ((month == 6 && day >= 22) || (month == 7 && day <= 22)) {
|
|
return "巨蟹座";
|
|
} else if ((month == 7 && day >= 23) || (month == 8 && day <= 22)) {
|
|
return "狮子座";
|
|
} else if ((month == 8 && day >= 23) || (month == 9 && day <= 22)) {
|
|
return "处女座";
|
|
} else if ((month == 9 && day >= 23) || (month == 10 && day <= 23)) {
|
|
return "天秤座";
|
|
} else if ((month == 10 && day >= 24) || (month == 11 && day <= 22)) {
|
|
return "天蝎座";
|
|
} else if ((month == 11 && day >= 23) || (month == 12 && day <= 21)) {
|
|
return "射手座";
|
|
} else if ((month == 12 && day >= 22) || (month == 1 && day <= 19)) {
|
|
return "摩羯座";
|
|
} else if ((month == 1 && day >= 20) || (month == 2 && day <= 18)) {
|
|
return "水瓶座";
|
|
} else {
|
|
return "双鱼座";
|
|
}
|
|
}
|
|
|
|
@PostMapping("/wuxing")
|
|
public Map<String, Object> wuXingQuery(@RequestBody Map<String, Object> params) {
|
|
Map<String, Object> result = new HashMap<>();
|
|
try {
|
|
String date = params.get("date").toString();
|
|
int hour = params.containsKey("hour") ? Integer.parseInt(params.get("hour").toString()) : 0;
|
|
|
|
Solar solar = Solar.fromYmdHms(
|
|
Integer.parseInt(date.substring(0, 4)),
|
|
Integer.parseInt(date.substring(5, 7)),
|
|
Integer.parseInt(date.substring(8, 10)),
|
|
hour, 0, 0
|
|
);
|
|
Lunar lunar = solar.getLunar();
|
|
|
|
String yearGan = lunar.getYearGan();
|
|
String monthGan = lunar.getMonthGan();
|
|
String dayGan = lunar.getDayGan();
|
|
String hourGan = lunar.getTimeGan();
|
|
|
|
Map<String, String> ganWuXing = new HashMap<>();
|
|
ganWuXing.put("甲", "木"); ganWuXing.put("乙", "木");
|
|
ganWuXing.put("丙", "火"); ganWuXing.put("丁", "火");
|
|
ganWuXing.put("戊", "土"); ganWuXing.put("己", "土");
|
|
ganWuXing.put("庚", "金"); ganWuXing.put("辛", "金");
|
|
ganWuXing.put("壬", "水"); ganWuXing.put("癸", "水");
|
|
|
|
Map<String, Object> data = new HashMap<>();
|
|
data.put("yearGan", yearGan);
|
|
data.put("yearWuXing", ganWuXing.get(yearGan));
|
|
data.put("monthGan", monthGan);
|
|
data.put("monthWuXing", ganWuXing.get(monthGan));
|
|
data.put("dayGan", dayGan);
|
|
data.put("dayWuXing", ganWuXing.get(dayGan));
|
|
data.put("hourGan", hourGan);
|
|
data.put("hourWuXing", ganWuXing.get(hourGan));
|
|
|
|
Map<String, Integer> wuXingCount = new HashMap<>();
|
|
wuXingCount.put("金", 0);
|
|
wuXingCount.put("木", 0);
|
|
wuXingCount.put("水", 0);
|
|
wuXingCount.put("火", 0);
|
|
wuXingCount.put("土", 0);
|
|
|
|
for (String gan : Arrays.asList(yearGan, monthGan, dayGan, hourGan)) {
|
|
String wx = ganWuXing.get(gan);
|
|
wuXingCount.put(wx, wuXingCount.get(wx) + 1);
|
|
}
|
|
|
|
String[] zhiList = {lunar.getYearZhi(), lunar.getMonthZhi(), lunar.getDayZhi(), lunar.getTimeZhi()};
|
|
Map<String, String> zhiWuXing = new HashMap<>();
|
|
zhiWuXing.put("子", "水"); zhiWuXing.put("丑", "土");
|
|
zhiWuXing.put("寅", "木"); zhiWuXing.put("卯", "木");
|
|
zhiWuXing.put("辰", "土"); zhiWuXing.put("巳", "火");
|
|
zhiWuXing.put("午", "火"); zhiWuXing.put("未", "土");
|
|
zhiWuXing.put("申", "金"); zhiWuXing.put("酉", "金");
|
|
zhiWuXing.put("戌", "土"); zhiWuXing.put("亥", "水");
|
|
|
|
for (String zhi : zhiList) {
|
|
String wx = zhiWuXing.get(zhi);
|
|
wuXingCount.put(wx, wuXingCount.get(wx) + 1);
|
|
}
|
|
|
|
data.put("wuXingCount", wuXingCount);
|
|
|
|
List<String> missing = new ArrayList<>();
|
|
for (Map.Entry<String, Integer> entry : wuXingCount.entrySet()) {
|
|
if (entry.getValue() == 0) {
|
|
missing.add(entry.getKey());
|
|
}
|
|
}
|
|
data.put("missing", missing);
|
|
|
|
Map<String, String> wuXingInfo = new HashMap<>();
|
|
wuXingInfo.put("金", "金主义,其性刚,其情烈。金盛之人骨肉相称,面方白净,眉高眼深。");
|
|
wuXingInfo.put("木", "木主仁,其性直,其情和。木盛之人丰姿秀丽,骨骼修长,手足细腻。");
|
|
wuXingInfo.put("水", "水主智,其性聪,其情善。水盛之人面黑有采,语言清和,为人深思熟虑。");
|
|
wuXingInfo.put("火", "火主礼,其性急,其情恭。火盛之人头小脚长,上尖下阔,浓眉小耳。");
|
|
wuXingInfo.put("土", "土主信,其性重,其情厚。土盛之人圆腰廓鼻,眉清目秀,口才声重。");
|
|
|
|
data.put("wuXingInfo", wuXingInfo);
|
|
|
|
result.put("success", true);
|
|
result.put("data", data);
|
|
} catch (Exception e) {
|
|
result.put("success", false);
|
|
result.put("error", e.getMessage());
|
|
}
|
|
return result;
|
|
}
|
|
|
|
@PostMapping("/jiri")
|
|
public Map<String, Object> jiRiQuery(@RequestBody Map<String, Object> params) {
|
|
Map<String, Object> result = new HashMap<>();
|
|
try {
|
|
String date = params.get("date").toString();
|
|
|
|
Solar solar = Solar.fromYmd(
|
|
Integer.parseInt(date.substring(0, 4)),
|
|
Integer.parseInt(date.substring(5, 7)),
|
|
Integer.parseInt(date.substring(8, 10))
|
|
);
|
|
Lunar lunar = solar.getLunar();
|
|
|
|
Map<String, Object> data = new HashMap<>();
|
|
data.put("solar", solar.toString());
|
|
data.put("lunar", lunar.toString());
|
|
data.put("ganZhi", lunar.getDayInGanZhi());
|
|
data.put("xingZuo", solar.getXingZuo());
|
|
data.put("week", solar.getWeekInChinese());
|
|
|
|
List<String> yi = lunar.getDayYi();
|
|
List<String> ji = lunar.getDayJi();
|
|
data.put("yi", yi);
|
|
data.put("ji", ji);
|
|
|
|
String chong = lunar.getDayChong();
|
|
data.put("chong", chong);
|
|
data.put("chongGan", lunar.getDayChongGan());
|
|
data.put("sha", lunar.getDaySha());
|
|
|
|
String jieQi = lunar.getJieQi();
|
|
data.put("jieQi", jieQi != null ? jieQi : "无");
|
|
|
|
List<String> festivals = lunar.getFestivals();
|
|
data.put("festivals", festivals);
|
|
|
|
List<String> solarFestivals = solar.getFestivals();
|
|
data.put("solarFestivals", solarFestivals);
|
|
|
|
boolean isGoodDay = yi.size() > ji.size();
|
|
data.put("isGoodDay", isGoodDay);
|
|
data.put("suggestion", isGoodDay ? "今日宜多行善事,适合办理重要事务" : "今日宜静不宜动,适合修身养性");
|
|
|
|
result.put("success", true);
|
|
result.put("data", data);
|
|
} catch (Exception e) {
|
|
result.put("success", false);
|
|
result.put("error", e.getMessage());
|
|
}
|
|
return result;
|
|
}
|
|
}
|