from py_jftech import read, write, where, format_date from api import AssetRiskDateType as DateType __COLUMNS__ = { 'ard_id': 'id', 'ard_date': 'date', 'ard_type': 'type', 'ard_asset_id': 'asset_id', } @write def insert(asset_id, type: DateType, date): return f''' insert into asset_risk_dates(ard_asset_id, ard_type, ard_date) values ({asset_id}, {type.value}, '{format_date(date)}') ''' @read def get_list(asset_ids=None, max_date=None, min_date=None): sqls = [] if max_date: sqls.append(f"rap_date <= '{format_date(max_date)}'") if min_date: sqls.append(f"rap_date >= '{format_date(min_date)}'") return f''' select {','.join([f"`{x[0]}` as `{x[1]}`" for x in __COLUMNS__.items()])} from asset_risk_dates {where(*sqls, ard_asset_id=asset_ids)} order by ard_asset_id, ard_date ''' @read(one=True) def get_last_one(fund_id, date=None, type: DateType = None): kwargs = { 'ard_asset_id': fund_id, 'ard_type': type.value if type is not None else None } sql = f"ard_date <= '{format_date(date)}'" if date else None return f''' select {','.join([f"`{x[0]}` as `{x[1]}`" for x in __COLUMNS__.items()])} from asset_risk_dates {where(sql, **kwargs)} order by ard_date desc, ard_type asc limit 1 ''' @write def delete(day=None): if day: return f"delete from asset_risk_dates where ard_date >= '{format_date(day)}'" else: return 'truncate table asset_risk_dates'