今天遇到一个问题,而之所以会遇到这种问题是因为数据库设计的问题.
数据表要保存一份年份的记录,如2016年值1,2017年值是20,……目前需求是保存到2026年,也就是未来十年的数据.不知谁设计的表,居然是设计了10个字段分别保存这十个数据!!!!!!取值时,在根据年份写了十个if语句来取值.额…….
现在问题来了,多条记录时,要取得多条记录中同一年份值最小的记录.即如果数据如下:
2016 | 2017 | 2018 |
---|---|---|
1 | 30 | 300 |
2 | 20 | 200 |
3 | 40 | 100 |
则取出数据为:2016:1,2017:20:2018:100.
数据表已被设计成这样,只能在此基础上想办法.
基本思路是:先取得年份列表,作为一级循环.
然后就得记录条数,作为二级循环.然后取得每年的数据列表,在排序取得最小值.
我这个解法中,最关键的就是,构建一个列表,这个列表在每次循环时都会变化,然后需要动态的排序取值.
这个取最小值的过程,我是利用List重写sort方法来实现的.
代码如下:
private BigDecimal getMin(List<BigDecimal> list, BigDecimal e) {
BigDecimal beginYearRent;
list.add(e);
//升序排序
Collections.sort(list, new Comparator<BigDecimal>() {
public int compare(BigDecimal o1, BigDecimal o2) {
return o1.compareTo(o2);
}
});
beginYearRent=list.get(0);
return beginYearRent;
}
至于外面如何调用这个方法,就简单做下说明如下,做了些许修改,只是为了更好的理解上面的问题:
/**
*
* @param ids 记录id
* @param yearRangeList 时间段
* @return
* @throws Exception
*/
public String calMgrStandard(String ids, List<String> yearRangeList) throws Exception {
String msg = "error";
msg = "";
String[] sIds = ids.split(",");
List<BigDecimal> list2015 = new ArrayList<BigDecimal>();
List<BigDecimal> list2016 = new ArrayList<BigDecimal>();
List<BigDecimal> list2017 = new ArrayList<BigDecimal>();
List<BigDecimal> list2018 = new ArrayList<BigDecimal>();
List<BigDecimal> list2019 = new ArrayList<BigDecimal>();
List<BigDecimal> list2020 = new ArrayList<BigDecimal>();
List<BigDecimal> list2021 = new ArrayList<BigDecimal>();
List<BigDecimal> list2022 = new ArrayList<BigDecimal>();
List<BigDecimal> list2023 = new ArrayList<BigDecimal>();
List<BigDecimal> list2024 = new ArrayList<BigDecimal>();
for (String yearRange : yearRangeList) {
String[] yearRanges = yearRange.split("~");
String[] beginDate = yearRanges[0].split("-");
String[] endDate = yearRanges[1].split("-");
int beginYear = Integer.valueOf(beginDate[0]);
BigDecimal beginYearRent = BigDecimal.ZERO;
BisStore store = null;
for (String id : sIds) {
if(StringUtils.isBlank(id)){
continue;
}
//取得记录
store = this.getEntity(id);
if(store == null){
continue;
}
BigDecimal[] djInfo = new BigDecimal[]{
store.getMgr2015(),
store.getMgr2016(),store.getMgr2017(),
store.getMgr2018(),store.getMgr2019(),
store.getMgr2020(),store.getMgr2021(), store.getMgr2022(),store.getMgr2023(),
store.getMgr2024()
};
if(2015==beginYear){
beginYearRent = getMin(list2015, djInfo[0]);
}else if(2016==beginYear){
beginYearRent = getMin(list2016, djInfo[1]);
}else if(2017==beginYear){
beginYearRent = getMin(list2017, djInfo[2]);
}else if(2018==beginYear){
beginYearRent = getMin(list2018, djInfo[3]);
}else if(2019==beginYear){
beginYearRent = getMin(list2019, djInfo[4]);
}else if(2020==beginYear){
beginYearRent = getMin(list2020, djInfo[5]);
}else if(2021==beginYear){
beginYearRent = getMin(list2021, djInfo[6]);
}else if(2022==beginYear){
beginYearRent = getMin(list2022, djInfo[7]);
}else if(2023==beginYear){
beginYearRent = getMin(list2023, djInfo[8]);
}else if(2024==beginYear){
beginYearRent = getMin(list2024, djInfo[9]);
}
}
msg += beginYearRent+"-";
}
return msg;
}
以上就是我对这个问题想到的解法.