rankingCalculator.dos 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. module fundit::rankingCalculator
  2. use fundit::sqlUtilities
  3. use fundit::operationDataPuller;
  4. use fundit::performanceDataPuller;
  5. use fundit::dataSaver
  6. /*
  7. * 汇集所有参与排名的指标信息
  8. *
  9. */
  10. def get_indicator_info() {
  11. ids = [1,
  12. 2, 6, 9, 10, 11, 12, 21, 50, 52, 59,
  13. 14, 15, 16, 17, 18, 19, 40, 58,
  14. 37, 38, 41, 42, 43, 44, 45, 46, 47, 48, 49,
  15. 33, 34, 35, 36,
  16. 66, 53, 54, 55, 56, 57
  17. ];
  18. names = ['ret',
  19. 'maxdrawdown', 'kurtosis', 'skewness', 'stddev', 'alpha', 'beta', 'downsidedev', 'maxdrawdown_months', 'maxdrawdown_recoverymonths', 'winrate',
  20. 'kapparatio', 'treynorratio', 'jensen', 'omegaratio', 'sharperatio', 'sortinoratio_MAR', 'calmarratio', 'sortinoratio',
  21. 'per_con', 'info_ratio', 'var', 'cvar', 'smddvar', 'smddcvar', 'smdd_lpm1', 'smdd_lpm2', 'smdd_downside_dev', 'tracking_error', 'm2',
  22. 'upsidecapture_ret', 'downsidecapture_ret', 'upsidecapture_ratio', 'downsidecapture_ratio',
  23. 'stability', 'jc_stddev', 'gzstyle_stddev', 'gzstrategy_stddev', 'zz_stddev', 'zx_stddev'
  24. ];
  25. is_ASCs = [false,
  26. true, true, false, true, false, false, true, true, true, false,
  27. false, false, false, false, false, false, false, false,
  28. false, false, true, true, true, true, true, true, true, true, false,
  29. false, false, false, true,
  30. true, true, true, true, true, true
  31. ];
  32. return table(names AS name, ids AS id, is_ASCs AS is_ASC);
  33. }
  34. /*
  35. * 自定义百分位计算
  36. *
  37. */
  38. defg perRank(x, is_ASC) {
  39. return (100 * x.rank(ascending=is_ASC, percent=true)).round(0);
  40. }
  41. /*
  42. * 动态生成用于排序的SQL脚本
  43. *
  44. * @param data_table <TABLE>: 指标横表
  45. * @param indicator_table <TABLE>: 指标表,有 id, name, is_ASC 字段
  46. *
  47. * TODO: portfolio, cf, manager, company,
  48. * TODO: bfi & category
  49. *
  50. */
  51. def gen_ranking_sql(entity_type, data_table, indicator_table) {
  52. ranking = iif(entity_type IN ['PL', 'CO'], create_mc_indicator_ranking(), create_entity_indicator_ranking());
  53. ranking_num = iif(entity_type IN ['PL', 'CO'], create_mc_indicator_ranking_num(), create_entity_indicator_ranking_num());
  54. if(entity_type IN ['PL', 'CO'])
  55. v_groupby = ['curve_type', 'strategy', 'category_id', 'end_date'];
  56. else
  57. v_groupby = ['category_id', 'end_date'];
  58. for(indicator in indicator_table) {
  59. // 与 MySQL 不同,这里统一把近4年和成立以来的排名去掉
  60. if(indicator.id == 1)
  61. v_trailing = ['1m', '3m', '6m', '1y', '2y', '3y', '5y', '10y', 'ytd'];
  62. else {
  63. v_trailing = ['6m', '1y', '2y', '3y', '5y', '10y', 'ytd'];
  64. v_missing_trailing = ['1m', '3m'];
  65. }
  66. // 绝对排名和百分位排名
  67. t_ranking = sql(select = (sqlCol(['entity_id'].join(v_groupby)), <indicator.id as indicator_id>,
  68. sqlCol(indicator.name + '_' + v_trailing,, 'indicator_' + v_trailing),
  69. sqlCol(indicator.name + '_' + v_trailing, rank{, indicator.is_ASC}, 'absrank_' + v_trailing),
  70. sqlCol(indicator.name + '_' + v_trailing, perRank{, indicator.is_ASC}, 'perrank_' + v_trailing)
  71. ),
  72. from = data_table,
  73. where = < category_id IS NOT NULL>,
  74. groupBy = sqlCol(v_groupby),
  75. groupFlag = 0 ).eval(); // context by
  76. // 为了满足表结构的要求, 非收益的指标要补上1m和3m的字段,虽然都是NULL
  77. if(indicator.id != 1) {
  78. v_tmp_col = ['indicator_' + v_missing_trailing, 'absrank_' + v_missing_trailing, 'perrank_' + v_missing_trailing].flatten();
  79. v_tmp_type = [take(DOUBLE, v_missing_trailing.size()), take(INT, v_missing_trailing.size()), take(INT, v_missing_trailing.size())].flatten();
  80. t_ranking.addColumn(v_tmp_col, v_tmp_type);
  81. }
  82. t_ranking.reorderColumns!(ranking.colNames());
  83. ranking.tableInsert(t_ranking);
  84. // 平均值、集合数量、各分位的阈值
  85. t_ranking_num = sql(select =(sqlCol(v_groupby),
  86. iif(entity_type IN ['PL', 'CO'], <indicator.id as indicator_id>, // 取消基金经理/公司的 raise_type 字段
  87. [sqlCol('raise_type', mean, 'raise_type'), <indicator.id as indicator_id>]),
  88. sqlCol(indicator.name + '_' + v_trailing, mean, 'avg_' + v_trailing),
  89. sqlCol(indicator.name + '_' + v_trailing, count, 'avg_' + v_trailing + '_cnt'),
  90. sqlCol(indicator.name + '_' + v_trailing, percentile{, iif(indicator.is_ASC, 5, 95)}, 'perrank_percent_5_' + v_trailing),
  91. sqlCol(indicator.name + '_' + v_trailing, percentile{, iif(indicator.is_ASC, 10, 90)}, 'perrank_percent_10_' + v_trailing),
  92. sqlCol(indicator.name + '_' + v_trailing, percentile{, iif(indicator.is_ASC, 25, 75)}, 'perrank_percent_25_' + v_trailing),
  93. sqlCol(indicator.name + '_' + v_trailing, percentile{, iif(indicator.is_ASC, 50, 50)}, 'perrank_percent_50_' + v_trailing),
  94. sqlCol(indicator.name + '_' + v_trailing, percentile{, iif(indicator.is_ASC, 75, 25)}, 'perrank_percent_75_' + v_trailing),
  95. sqlCol(indicator.name + '_' + v_trailing, percentile{, iif(indicator.is_ASC, 90, 10)}, 'perrank_percent_90_' + v_trailing),
  96. sqlCol(indicator.name + '_' + v_trailing, percentile{, iif(indicator.is_ASC, 95, 5)}, 'perrank_percent_95_' + v_trailing),
  97. sqlCol(indicator.name + '_' + v_trailing, iif(indicator.is_ASC, min, max), 'best_' + v_trailing),
  98. sqlCol(indicator.name + '_' + v_trailing, iif(indicator.is_ASC, max, min), 'worst_' + v_trailing)
  99. ),
  100. from = data_table,
  101. where = < category_id IS NOT NULL>,
  102. groupBy = sqlCol(v_groupby),
  103. groupFlag = 1).eval(); // group by
  104. // 为了满足表结构的要求, 非收益的指标要补上1m和3m的字段,虽然都是NULL
  105. if(indicator.id != 1) {
  106. v_tmp_col = ['avg_' + v_missing_trailing, 'avg_' + v_missing_trailing + '_cnt', 'perrank_percent_5_' + v_missing_trailing,
  107. 'perrank_percent_10_' + v_missing_trailing, 'perrank_percent_25_' + v_missing_trailing,
  108. 'perrank_percent_50_' + v_missing_trailing, 'perrank_percent_75_' + v_missing_trailing,
  109. 'perrank_percent_90_' + v_missing_trailing, 'perrank_percent_95_' + v_missing_trailing,
  110. 'best_' + v_missing_trailing, 'worst_' + v_missing_trailing
  111. ].flatten();
  112. v_tmp_type = [take(DOUBLE, v_missing_trailing.size()), take(INT, v_missing_trailing.size()), take(DOUBLE, v_missing_trailing.size()),
  113. take(DOUBLE, v_missing_trailing.size()), take(DOUBLE, v_missing_trailing.size()),
  114. take(DOUBLE, v_missing_trailing.size()), take(DOUBLE, v_missing_trailing.size()),
  115. take(DOUBLE, v_missing_trailing.size()), take(DOUBLE, v_missing_trailing.size()),
  116. take(DOUBLE, v_missing_trailing.size()),take(DOUBLE, v_missing_trailing.size())
  117. ].flatten();
  118. t_ranking_num.addColumn(v_tmp_col, v_tmp_type);
  119. }
  120. t_ranking_num.reorderColumns!(ranking_num.colNames());
  121. ranking_num.tableInsert(t_ranking_num);
  122. }
  123. return ranking, ranking_num;
  124. }
  125. /*
  126. * 运行排名SQL脚本
  127. *
  128. *
  129. */
  130. def run_ranking_sql(entity_type, ranking_by, mutable data_table, indicator_table) {
  131. // data_table = t
  132. // ranking_by = 'strategy'
  133. ret = array(ANY, 0);
  134. if(ranking_by == 'bfi') {
  135. UPDATE data_table SET category_id = factor_id;
  136. v_ranking = gen_ranking_sql(entity_type, data_table, indicator_table);
  137. ret.append!(v_ranking[0]); // ranking table
  138. ret.append!(v_ranking[1]); // ranking_num table
  139. } else {
  140. // 策略排名
  141. UPDATE data_table SET category_id = strategy$STRING;
  142. v_ranking = gen_ranking_sql(entity_type, data_table, indicator_table);
  143. ret.append!(v_ranking[0]); // ranking table
  144. ret.append!(v_ranking[1]); // ranking_num table
  145. // 基金经理和公司没有子策略排名
  146. if(! (entity_type IN ['PL', 'CO']) ) {
  147. // 子策略排名
  148. UPDATE data_table SET category_id = substrategy$STRING;
  149. v_ranking = gen_ranking_sql(entity_type, data_table, indicator_table);
  150. ret.append!(v_ranking[0]); // ranking table
  151. ret.append!(v_ranking[1]); // ranking_num table
  152. }
  153. }
  154. return ret;
  155. }
  156. /*
  157. * 为排名做数据准备
  158. *
  159. * TODO: 对少量组合做优化
  160. *
  161. * @return <VECTOR>: 包含两个表,一个指标数据表,一个是指标信息表
  162. *
  163. */
  164. def prepare_data_for_ranking(ranking_by, entity_type, entity_info, end_date, isFromMySQL=true) {
  165. // return
  166. table_desc = get_performance_table_description(entity_type);
  167. tb_data_return = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
  168. entity_id_name = table_desc.sec_id_col[0];
  169. // risk
  170. table_desc = get_risk_stats_table_description(entity_type);
  171. tb_data_risk_stats = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
  172. // risk adjusted return
  173. table_desc = get_riskadjret_stats_table_description(entity_type);
  174. tb_data_riskadjret_stats = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
  175. // others
  176. table_desc = get_indicator_table_description(entity_type);
  177. tb_data_indicator_stats = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
  178. // 做个大宽表
  179. if(entity_type IN ['PL', 'CO']) {
  180. matchingCols = [entity_id_name, 'curve_type', 'strategy', 'end_date'];
  181. matchingCols2 = [entity_id_name, 'curve_type', 'strategy', 'end_date', 'factor_id'];
  182. } else {
  183. matchingCols = [entity_id_name, 'end_date'];
  184. matchingCols2 = [entity_id_name, 'end_date', 'factor_id'];
  185. }
  186. tb_data = lj(lj(lj(tb_data_return, tb_data_indicator_stats, matchingCols), tb_data_risk_stats, matchingCols), tb_data_riskadjret_stats, matchingCols);
  187. if(ranking_by == 'bfi') {
  188. // 去掉被移到 fund_ty_bfi_bm_indicator 表中的重复字段
  189. v_dups = [38, 48, 11, 12, 59, 16];
  190. v_dup_col = EXEC name + suffix
  191. FROM cj(get_indicator_info(), table(['_6m', '_1y', '_2y', '_3y', '_5y', '_10y', '_ytd'] AS suffix))
  192. WHERE id IN v_dups;
  193. tb_data.dropColumns!(v_dup_col);
  194. // bfi table
  195. table_desc = get_bfi_by_category_group_table_description(entity_type);
  196. tb_bfi = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
  197. // bfi (as benchmark) indicator
  198. table_desc = get_bfi_indicator_table_description(entity_type);
  199. tb_data_bfi_indicator = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
  200. tb_data = lj(ej(tb_data, tb_bfi, matchingCols), tb_data_bfi_indicator, matchingCols2);
  201. v_indicator_id = [1, // 对应 fund_performance, 取消39(年化收益) 因为没有意义
  202. 41, 42, 49, // 对应 fund_indicator, 取消37 (per_con), 43, 44, 45, 46, 47 (smdd模型) 因为dolphin 未计算
  203. 2, 6, 9, 10, 21, // 对应 fund_risk_stats, 取消50, 52 因为 dolphin 未计算
  204. 14, 15, 17, 18, 40, 58, // 对应 fund_riskadjret_stats 取消19 (MAR Sortino ratio) 因为 dolphin 未计算
  205. 11, 12, 16, 33, 34, 35, 36, 38, 48, 59 // 对应 fund_ty_bfi_bm_indicator
  206. ]; // 取消 pf_fund_factor_stability 66 (stabiliy) 因为 dolphin 未计算
  207. // 取消 fund_rbsa_style 53, 54, 55, 56, 57(风格稳定性) 因为 dolphin 未计算
  208. } else {
  209. // upside/downside capture
  210. table_desc = get_capture_style_table_description(entity_type);
  211. tb_data_capture_stats = get_monthly_indicator_data(table_desc.table_name[0], end_date, isFromMySQL);
  212. tb_data = lj(tb_data, tb_data_capture_stats, matchingCols);
  213. v_indicator_id = [1, // 对应 fund_performance, 取消39(年化收益) 因为没有意义
  214. 38, 41, 42, 48, 49, // 对应 fund_indicator, 取消37 (per_con), 43, 44, 45, 46, 47 (smdd模型) 因为dolphin 未计算
  215. 2, 6, 9, 10, 11, 12, 21, 59, // 对应 fund_risk_stats, 取消50, 52 因为 dolphin 未计算
  216. 14, 15, 16, 17, 18, 40, 58, // 对应 fund_riskadjret_stats 取消19 (MAR Sortino ratio) 因为 dolphin 未计算
  217. 33, 34, 35, 36 // 对应 fund_style_stats
  218. ];
  219. }
  220. tb_data.rename!(entity_id_name, 'entity_id');
  221. if(entity_type IN ['PL', 'CO'])
  222. t = SELECT * FROM entity_info en
  223. INNER JOIN tb_data d ON en.entity_id = d.entity_id AND en.curve_type = d.curve_type AND en.strategy = d.strategy
  224. WHERE en.strategy IS NOT NULL;
  225. else
  226. t = SELECT * FROM entity_info en
  227. INNER JOIN tb_data d ON en.entity_id = d.entity_id
  228. WHERE en.strategy IS NOT NULL;
  229. if(ranking_by == 'bfi')
  230. UPDATE t SET category_id = factor_id;
  231. else if(ranking_by == 'substrategy')
  232. UPDATE t SET category_id = substrategy$STRING;
  233. else
  234. UPDATE t SET category_id = strategy$STRING;
  235. indicator_table = SELECT * FROM get_indicator_info() WHERE id IN v_indicator_id;
  236. return t, indicator_table;
  237. }
  238. /*
  239. * 通用指标排名计算
  240. *
  241. * @param ranking_by <STRING>: strategy, bfi
  242. *
  243. */
  244. def cal_indicator_ranking(ranking_by, entity_type, entity_info, end_date, isFromMySQL=true) {
  245. // 当前只对基金做排名, 其它类型参考基金排名做相对排名
  246. if(!(entity_type in ['MF', 'HF', 'PL', 'CO'])) return null;
  247. v = prepare_data_for_ranking(ranking_by, entity_type, entity_info, end_date, isFromMySQL);
  248. v_ranking_tables = run_ranking_sql(entity_type, ranking_by, v[0], v[1]);
  249. return v_ranking_tables;
  250. }
  251. /*
  252. * 将源指标表横表变竖表,以方便参考排名计算
  253. *
  254. *
  255. */
  256. def run_transformation_sql(entity_type, data_table, ranking_by, indicator_info) {
  257. // 只有 portfolio_id 是整型,其它的都是字符串
  258. is_id_integer = false;
  259. if(entity_type == 'PF') is_id_integer = true;
  260. tb_ranking = create_entity_indicator_ranking(is_id_integer);
  261. for(indicator in indicator_info) {
  262. // 只有收益需要1m, 3m
  263. if(indicator.id == 1)
  264. v_trailing = ['1m', '3m', '6m', '1y', '2y', '3y', '5y', '10y', 'ytd'];
  265. else {
  266. v_trailing = ['6m', '1y', '2y', '3y', '5y', '10y', 'ytd'];
  267. v_missing = ['1m', '3m'];
  268. }
  269. t = sql(select = (sqlCol(['entity_id', 'end_date', 'category_id']), <indicator.id as indicator_id>,
  270. sqlCol(indicator.name + '_' + v_trailing,, 'indicator_' + v_trailing)
  271. ),
  272. from = data_table
  273. ).eval();
  274. // 给非收益指标补上1m, 3m的三套指标
  275. if(indicator.id != 1 )
  276. {
  277. v_tmp_col = ['indicator_' + v_missing, 'absrank_' + v_missing, 'perrank_' + v_missing].flatten();
  278. v_tmp_type = [take(DOUBLE, v_missing.size()), take(INT, v_missing.size()), take(INT, v_missing.size())].flatten();
  279. t.addColumn(v_tmp_col, v_tmp_type);
  280. }
  281. // 给所有指标补上 absrank 和 perrank 两套指标
  282. v_tmp_col = ['absrank_' + v_trailing, 'perrank_' + v_trailing].flatten();
  283. v_tmp_type = [take(INT, v_trailing.size()), take(INT, v_trailing.size())].flatten();
  284. t.addColumn(v_tmp_col, v_tmp_type);
  285. INSERT INTO tb_ranking
  286. SELECT * FROM (sql(select = sqlCol(tb_ranking.colNames()),
  287. from = t).eval());
  288. }
  289. return tb_ranking;
  290. }
  291. /*
  292. * 将源风险指标表横表变竖表,以方便排名计算
  293. *
  294. *
  295. */
  296. def transform_data_for_ranking (entity_type, entity_info, end_date, ranking_by, isFromMySQL=true) {
  297. if(entity_info.isVoid() || entity_info.size() == 0) return null;
  298. v = prepare_data_for_ranking(ranking_by, entity_type, entity_info, end_date, isFromMySQL);
  299. tb_ranking = run_transformation_sql(entity_type, v[0], ranking_by, v[1]);
  300. return tb_ranking;
  301. }
  302. /*
  303. *
  304. * 参考某指定类排名,计算相对排名
  305. *
  306. * @param benchmark_ranking <TABLE>: 被参考的排名表,如公募混合基金
  307. * @param entity_ranking <TABLE>: 被计算的指标表,排名被填充在原表中
  308. * @param isFromMySQL <BOOL>
  309. *
  310. *
  311. * Example: cal_relative_ranking(get_fund_indicator_ranking(NULL, 2024.09M, 102, true),
  312. * transform_risk_stats_for_ranking('PF', get_entity_info('PF', NULL), 2024.09M, true),
  313. * true);
  314. */
  315. def cal_relative_ranking(benchmark_ranking, mutable entity_ranking, isFromMySQL=true) {
  316. v_trailing = ['1m', '3m', '6m', '1y', '2y', '3y', '5y', '10y', 'ytd'];
  317. for(tr in v_trailing) {
  318. indicator_val_col = 'indicator_' + tr;
  319. // 乘上100,000 是为了满足 window join 的字段必须是INT或DURATION
  320. tb_tmp = sql(select = (sqlCol(['entity_id', 'end_date', 'category_id', 'indicator_id']),
  321. sqlColAlias(makeCall(round, binaryExpr(sqlCol(indicator_val_col), 1000000, *), 0), indicator_val_col + '_int')),
  322. from = entity_ranking,
  323. where = < _$indicator_val_col is not null >,
  324. orderBy = sqlCol(['end_date', 'category_id', 'indicator_id', indicator_val_col])
  325. ).eval();
  326. tb_tmp2 = sql(select = (sqlCol(['end_date', 'category_id', 'indicator_id']),
  327. sqlColAlias(makeCall(round, binaryExpr(sqlCol(indicator_val_col), 1000000, *), 0), indicator_val_col + '_int'),
  328. sqlCol('absrank_' + tr), sqlCol('perrank_' + tr)
  329. ),
  330. from = benchmark_ranking,
  331. where = < _$indicator_val_col is not null >,
  332. orderBy = sqlCol(['end_date', 'category_id', 'indicator_id', indicator_val_col])
  333. ).eval();
  334. absrank_col = 'absrank_' + tr;
  335. perrank_col = 'perrank_' + tr;
  336. // 用 pwj 来找最接近的排名
  337. tb_tmp_ranking = sql(select = (sqlCol(['entity_id', 'end_date', 'category_id', 'indicator_id']),
  338. sqlCol(indicator_val_col + '_int'),
  339. sqlCol(['absrank_max', 'perrank_max'])),
  340. from = pwj(tb_tmp, tb_tmp2,
  341. window = 0:1,
  342. aggs = [<max(_$absrank_col) as 'absrank_max'>, <max(_$perrank_col) as 'perrank_max'>],
  343. matchingCols = ['end_date', 'category_id', 'indicator_id', indicator_val_col + '_int'])
  344. ).eval();
  345. // 计算的结果填入排名表
  346. sqlUpdate(table = entity_ranking,
  347. updates = [<absrank_max as _$absrank_col>, <perrank_max as _$perrank_col>],
  348. from = <ej(entity_ranking, tb_tmp_ranking, ['entity_id', 'end_date', 'category_id','indicator_id'])>
  349. ).eval();
  350. }
  351. }
  352. /*
  353. * 排名数据入库
  354. *
  355. * @param ranking_by <STRING>: 'strategy', 'bfi'
  356. * @param ranking_tables <VECTOR>: 当 ranking_by = 'strategy' 时包含4个数据表的向量,分别是一级策略排名,一级策略排名阈值,二级策略排名,二级策略排名阈值
  357. * ranking_by = 'bfi' 时包含2个数据表的向量,分别是bfi策略排名,bfi策略排名阈值
  358. */
  359. def save_ranking_tables(entity_type, ranking_by, ranking_tables) {
  360. if(ranking_tables.isVoid()) return;
  361. des_strategy = get_indicator_ranking_table_description(entity_type)[0];
  362. des_bfi = get_bfi_bm_indicator_ranking_table_description(entity_type)[0];
  363. entity_id_col = des_strategy.sec_id_col;
  364. t = ranking_tables[0];
  365. if(ranking_by == 'bfi') {
  366. source_table = des_bfi.table_name.strReplace('pfdb', 'raw_db');
  367. target_table = des_bfi.table_name.strReplace('pfdb', 'raw_db');
  368. category_id_col = 'factor_id';
  369. t.rename!(['entity_id', 'category_id'], [entity_id_col, category_id_col]);
  370. } else {
  371. source_table = des_strategy.table_name.strReplace('pfdb', 'raw_db');
  372. target_table = des_strategy.table_name.strReplace('pfdb', 'raw_db');
  373. category_id_col = 'strategy';
  374. // 基金经理和公司自带 strategy 字段,而 category_id 就是 strategy 的复制,所以要去掉
  375. // 基金和组合则没有这个字段,将 category_id 改名改回 strategy
  376. if(entity_type IN ['PL', 'CO'])
  377. t.rename!('entity_id', entity_id_col).dropColumns!('category_id');
  378. else
  379. t.rename!(['entity_id', 'category_id'], [entity_id_col, category_id_col]);
  380. }
  381. save_and_sync(t, source_table, target_table);
  382. t = ranking_tables[1];
  383. if(ranking_by == 'bfi') {
  384. t.rename!('category_id', category_id_col);
  385. } else {
  386. if(entity_type IN ['PL', 'CO'])
  387. t.dropColumns!('category_id');
  388. else
  389. t.rename!('category_id', category_id_col);
  390. }
  391. save_and_sync(t, source_table + '_num', target_table + '_num');
  392. // 基金有二级策略排名
  393. if(ranking_by == 'strategy' && entity_type IN ['HF', 'MF']) {
  394. source_table = source_table.strReplace('_ranking', '_substrategy_ranking');
  395. target_table = target_table.strReplace('_ranking', '_substrategy_ranking');
  396. category_id_col = 'substrategy';
  397. t = ranking_tables[2];
  398. save_and_sync(t.rename!(['entity_id', 'category_id'], [entity_id_col, category_id_col]), source_table, target_table);
  399. t = ranking_tables[3];
  400. save_and_sync(t.rename!('category_id', category_id_col), source_table + '_num', target_table + '_num');
  401. }
  402. }
  403. /*
  404. * 参考排名数据入库
  405. *
  406. * @param ranking_tables <TABLE>:
  407. */
  408. def save_relative_ranking_table(entity_type, ranking_table, ranking_by) {
  409. if(ranking_table.isVoid()) return;
  410. source_table = '';
  411. target_table = '';
  412. if(entity_type == 'PF') {
  413. entity_id_col = 'portfolio_id';
  414. if(ranking_by == 'strategy') {
  415. source_table = 'raw_db.pf_portfolio_indicator_ranking';
  416. target_table = 'raw_db.pf_portfolio_indicator_ranking';
  417. } else if(ranking_by == 'substrategy') {save_relative_ranking_table
  418. source_table = 'raw_db.pf_portfolio_indicator_substrategy_ranking';
  419. target_table = 'raw_db.pf_portfolio_indicator_substrategy_ranking';
  420. } else if(ranking_by == 'bfi') {
  421. source_table = 'raw_db.pf_portfolio_bfi_bm_indicator_ranking';
  422. target_table = 'raw_db.pf_portfolio_bfi_bm_indicator_ranking';
  423. }
  424. } else if(entity_type == 'CF') {
  425. entity_id_col = 'fund_id';
  426. source_table = 'raw_db.pf_cus_fund_indicator_ranking';
  427. target_table = 'raw_db.pf_cus_fund_indicator_ranking'
  428. }
  429. save_and_sync(ranking_table, source_table, target_table);
  430. }