财年场景解决方案

场景说明

当业务分析口径按财年、财季、财月统计,而不是按自然年、自然季度、自然月统计时,需要将用户提问中的时间条件映射到财务日历。

待补充

该场景的完整业务说明、建模方式与问答示例待补充。

财年 enrich 代码

源码位于:

packages/plugins/@yiwendata/yiask/docs/javascripts/enrich/calendar.js

async (logicform, normedLogicform, helperFunctions) => {
  const { getQueryWithKey, execute } = helperFunctions;

  if (logicform.calendar === 'fiscal') {
    // query
    {
      const timestampQueryValues = getQueryWithKey('日期');
      for (const timestampQuery of timestampQueryValues) {
        const timestampQueryValue = timestampQuery.queryFromLogicform;

        timestampQueryValue.日历 = {
          schema: 'calendar',
          query: {},
        };
        const calendarQuery = timestampQueryValue.日历.query;

        if (timestampQueryValue.日期.year) {
          // 复杂的暂不支持。
          if (typeof timestampQueryValue.日期.year !== 'number')
            throw new Error('复杂的日期表达暂不支持,请分成多个问题提问。');
          calendarQuery.财年 = timestampQueryValue.日期.year;
        }
        if (timestampQueryValue.日期.quarter) {
          // 复杂的暂不支持。
          if (typeof timestampQueryValue.日期.quarter !== 'number')
            throw new Error('复杂的日期表达暂不支持,请分成多个问题提问。');
          calendarQuery.财季 = timestampQueryValue.日期.quarter;
        }
        if (timestampQueryValue.日期.month) {
          // 复杂的暂不支持。
          if (typeof timestampQueryValue.日期.month !== 'number')
            throw new Error('复杂的日期表达暂不支持,请分成多个问题提问。');
          calendarQuery.财月 = timestampQueryValue.日期.month;
        }
        if (timestampQueryValue.日期.day) {
          // 复杂的暂不支持。
          if (typeof timestampQueryValue.日期.day !== 'number')
            throw new Error('复杂的日期表达暂不支持,请分成多个问题提问。');
          calendarQuery.财日 = timestampQueryValue.日期.day;
        }

        delete timestampQueryValue.日期;
      }
    }

    // groupby
    if (logicform.groupby) {
      logicform.groupby.forEach((gi) => {
        if (gi._id === '$year') {
          gi._id = '日历_财年展示';
          if (!logicform.sort) logicform.sort = { [gi.name || gi._id]: 1 };
        }

        if (gi._id === '$quarter') {
          gi._id = '日历_财季展示';
          if (!logicform.sort) logicform.sort = { [gi.name || gi._id]: 1 };
        }

        if (gi._id === '$month') {
          gi._id = '日历_财月展示';
          if (!logicform.sort) logicform.sort = { [gi.name || gi._id]: 1 };
        }
      });
    }
  }

  // 各种修改logicform,如logicform.query.日期 = {year: 2024}
  return logicform;
};