from model import Wannianli, WannianliJieqi from tools import * from yixue import * def get_last_day_info(year: int): dt = Wannianli.select().where(Wannianli.nian == year, Wannianli.yue == 12, Wannianli.ri == 31) data = dt[0] if data.ri_gan == "": return None else: return data def _do_update_ri_by_year(year: int, start_rigan: str, start_rizhi: str): last_rigan = start_rigan last_rizhi = start_rizhi for month in range(1, 13): max_day = get_max_day(year, month) print("处理", year, month, "月数据") for day in range(1, max_day + 1): if month == 1 and day == 1: rigan = last_rigan rizhi = last_rizhi else: rigan = next_tiangan(last_rigan) rizhi = next_dizhi(last_rizhi) last_rigan = rigan last_rizhi = rizhi Wannianli.update({ "ri_gan": rigan, "ri_zhi": rizhi, }).where(Wannianli.nian == year, Wannianli.yue == month, Wannianli.ri == day).execute() print("update over last:", last_rigan, last_rizhi) def update_ri_by_year(year: int): start = Wannianli.select().where(Wannianli.nian == year, Wannianli.yue == 1, Wannianli.ri == 1) start_data = start[0] if start_data.ri_gan == "": print(year, "1月1日未配置初始值,从上一年取得数据") last_year = get_last_day_info(year - 1) if last_year is not None: print("上一年最后一天是", last_year.ri_gan, last_year.ri_zhi) next_rigan = next_tiangan(last_year.ri_gan) next_rizhi = next_dizhi(last_year.ri_zhi) print("新年第一天的开始是", next_rigan, next_rizhi) _do_update_ri_by_year(year, next_rigan, next_rizhi) else: print("上一年也没数据") else: _do_update_ri_by_year(year, start_data.ri_gan, start_data.ri_zhi) def get_wannianli_data(year: int, month: int, day: int): dt = Wannianli.select().where(Wannianli.nian == year, Wannianli.yue == month, Wannianli.ri == day) if len(dt) > 0: return dt[0] else: print(year, month, day, "has empty data") return None def get_jieqi_of_month(jieqi: WannianliJieqi, month: int): ary = [jieqi.xiaohan, jieqi.lichun, jieqi.jingzhe, jieqi.qingming, jieqi.lixia, jieqi.mangzhong, jieqi.xiaoshu, jieqi.liqiu, jieqi.bailu, jieqi.hanlu, jieqi.lidong, jieqi.daxue] return ary[month - 1] def _do_update_yue_by_year(year: int, month: int, start_yuegan: str, start_yuezhi: str): jieqi = WannianliJieqi.select().where(WannianliJieqi.nianfen == year) jieqi_day = get_jieqi_of_month(jieqi[0], month) print("处理", year, month, "月数据, 节气日期是:", jieqi_day) key_day = str(jieqi_day).split(".")[1] max_day = get_max_day(year, month) for day in range(1, max_day + 1): if day < int(key_day): yuegan = start_yuegan yuezhi = start_yuezhi else: yuegan = next_tiangan(start_yuegan) yuezhi = next_dizhi(start_yuezhi) # print(year, month, day, yuegan, yuezhi) Wannianli.update({ "yue_gan": yuegan, "yue_zhi": yuezhi, }).where(Wannianli.nian == year, Wannianli.yue == month, Wannianli.ri == day).execute() print("update month over ", year, month) def prepare_yue_data(year: int, month: int): if month == 1: print(year, month, "月份未配置初始值,从上年12月取得数据") last_month = get_wannianli_data(year - 1, 12, 31) else: print(year, month, "月份未配置初始值,从当年上个月取得数据") max_day = get_max_day(year, month - 1) last_month = get_wannianli_data(year, month - 1, max_day) if last_month.yue_gan == "": print("未取得合适的数据", year, month, ",请检查上年12月或本年上个月的数据是否已录入") return False else: _do_update_yue_by_year(year, month, last_month.yue_gan, last_month.yue_zhi) return True def update_yue_by_year(year: int): for month in range(1, 13): start = Wannianli.select().where(Wannianli.nian == year, Wannianli.yue == month, Wannianli.ri == 1) start_data = start[0] if start_data.yue_gan == "": if not prepare_yue_data(year, month): break else: if year == 1901 and month == 1: _do_update_yue_by_year(year, month, start_data.yue_gan, start_data.yue_zhi) else: if not prepare_yue_data(year, month): break def _do_update_nian_by_year(year: int, start_niangan: str, start_nianzhi: str): jieqi = WannianliJieqi.select().where(WannianliJieqi.nianfen == year) jieqi_key = str(jieqi[0].chuxi).split(".") jieqi_month = int(jieqi_key[0]) jieqi_day = int(jieqi_key[1]) for month in range(1, 13): max_day = get_max_day(year, month) for day in range(1, max_day + 1): niangan = "" nianzhi = "" if month < jieqi_month: niangan = start_niangan nianzhi = start_nianzhi elif month == jieqi_month: if day > jieqi_day: niangan = next_tiangan(start_niangan) nianzhi = next_dizhi(start_nianzhi) else: niangan = start_niangan nianzhi = start_nianzhi elif month > jieqi_month: niangan = next_tiangan(start_niangan) nianzhi = next_dizhi(start_nianzhi) Wannianli.update({ "nian_gan": niangan, "nian_zhi": nianzhi, }).where(Wannianli.nian == year, Wannianli.yue == month, Wannianli.ri == day).execute() print(year, month, "年数据更新完成") def update_nian_by_year(year: int): start = Wannianli.select().where(Wannianli.nian == year, Wannianli.yue == 1, Wannianli.ri == 1) start_data = start[0] if start_data.nian_gan == "": print("没有1月1日数据,从上一年取数") last_year = get_wannianli_data(year - 1, 12, 31) if last_year is not None: _do_update_nian_by_year(year, last_year.nian_gan, last_year.nian_zhi) else: _do_update_nian_by_year(year, start_data.nian_gan, start_data.nian_zhi) def get_hour_of_day(day_tiangan: str, hour: int): start_tiangan = get_tiangan_start_of_hour(day_tiangan) tiangan = get_tiangan_by_hour(start_tiangan, hour) dizhi = get_dizhi_by_hour(hour) return [tiangan, dizhi]