翻译月份名称响应



我有一个名为CREATED_AT的表。我写了一个查询并收到这样的响应: Score D 27 01 17 02 80 03 55 06

这是我的查询:

SELECT SUM(AMOUNT) as scores,to_char(CREATED_AT, 'MONTH') as  d  
FROM C_SCORES 
WHERE USER_ID =201 and to_char(CREATED_AT, 'YYYY') ='1398' 
GROUP BY to_char(CREATED_AT, 'MONTH');

实体代码为:

    public class ScoreCategorizeWithMonth {
        private int Scores;
        private int month;
    private String monthfa;

 public String getMonthfa() {
        switch (month) {
            case 1:
                monthfa = "فروردین";
                break;
            case 2:
                monthfa = "اردیبهشت";
                break;
            case 3:
                monthfa = "خرداد";
                break;
            case 4:
                monthfa = "تیر";
                break;
            case 5:
                monthfa = "مرداد";
                break;
            case 6:
                monthfa = "شهریور";
                break;
            case 7:
                monthfa = "مهر";
                break;
            case 8:
                monthfa = "آبان";
                break;
            case 9:
                monthfa = "آذر";
                break;
            case 10:
                monthfa = "دی";
                break;
            case 11:
                monthfa = "بهمن";
                break;
            case 12:
                monthfa = "اسفند";
            default:
                monthfa = "";
        }

        return monthfa;
    }
    }

此代码在控制器中:

 @GetMapping(value = "score-Categorize-months")
    public List<ScoreCategorizeWithMonth> scoreCategorizedWithMonths()
    {
        return   iScoresSrv.scoreCategorizeWithMonths(userInfo.getUserId());
    }

我的问题是:

2-我的回答是数据库中存在的月份。如何显示月份,即使它不在数据库中

 {
    "scores": 27,
    "month": 1,
    "monthfa": "فروردین"
  },
  {
    "scores": 17,
    "month": 2,
    "monthfa": "اردیبهشت"
  },
  {
    "scores": 84,
    "month": 3,
    "monthfa": "خرداد"
  },
  {
    "scores": 55,
    "month": 6,
    "monthfa": "شهریور"
  } 

我还想显示 4..5 的月份,得分=0

谢谢

步骤2和3:您可以使用以下查询实现:

select sum(AMOUNT) as scores, month from  
(select AMOUNT ,to_char(CREATED_AT, 'MONTH') as month from C_SCORES WHERE USER_ID 
 =201 and to_char(CREATED_AT, 'YYYY') ='1398'
 union
 select 0 as AMOUNT, Month from
 (SELECT 1 AS MONTH
 UNION SELECT 2 AS MONTH
 UNION SELECT 3 AS MONTH
 UNION SELECT 4 AS MONTH
 UNION SELECT 5 AS MONTH
 UNION SELECT 6 AS MONTH
 UNION SELECT 7 AS MONTH
 UNION SELECT 8 AS MONTH
 UNION SELECT 9 AS MONTH
 UNION SELECT 10 AS MONTH
 UNION SELECT 11 AS MONTH
 UNION SELECT 12 AS MONTH) TBL1) TBL2
 group by month;

只需将 1-12 替换为您的月份值

您可以使用属性文件...

此链接提供了许多示例之一:http://zetcode.com/java/resourcebundle/

在该教程中,'words.properties'是默认和回退语言文件,words_de.propertieswords_sk.properties用于自定义翻译语言

在这一行中:source.setBasenames("messages/words");他试图将翻译文件传递给算法。

并用messageSource.getMessage("w1", null, Locale.GERMAN)他获取所需的语言。

请注意,words_sk.properties 中的sk是语言 iso 代码,如果波斯语是 fa ,英语是en ...

一些系统也使用本地化,他们定义了自定义字符和格式,我不确定此算法是否支持这些命名......你传递一个额外的参数:fa-IRen-US

老实说,我没有看得很深,但自从我几年前使用它以来,这就是原则。

我只是在java中添加sume代码来填充无效的索引。这很容易解决.但我有点困惑,没有一个答案对我没有帮助

 public List<ScoreCategorizedWithMonth> scoreCategorizeWithMonths(Long userId) {
        List<ScoreCategorizedWithMonth> monthCategory = scoreDao.scoreCategorizeWithMonths(userId);
        List<ScoreCategorizedWithMonth> result = new ArrayList<>();
        for (int i=1; i<=12;i++)
        {
            ScoreCategorizedWithMonth scoreCategorizedWithMonth = checkMonthCatIsExist(i, monthCategory);
            result.add(scoreCategorizedWithMonth);
        }
        return result;
    }
    private ScoreCategorizedWithMonth checkMonthCatIsExist(int month, List<ScoreCategorizedWithMonth> monthCategory) {
        for (ScoreCategorizedWithMonth cat:monthCategory) {
            if(cat.getMonth() == month){
                return cat;
            }
        }
        ScoreCategorizedWithMonth temp= new  ScoreCategorizedWithMonth();
        temp.setMonth(month);
        temp.setScores(0);
        return temp;
    }

相关内容

  • 没有找到相关文章