我想将当前行与前一行进行比较,如果它们匹配,则进行计算。如果不是,则指定0.0我的代码只计算而不接受条件。
例如,我有一个表:oil由一个月、一个日期和一个指示符组成
它必须看起来像:
Month Values Date |formula |Growth
september 100 01.09.2020 |(100/nan - 1) * 100 |infinity
september 80 08.09.2020 |(80/100-1) * 100 |-20
september 100 15.09.2020 |(100/80-1) * 100 |25
october 118 01.10.2020 |september != october |0
october 120 08.10.2020 |(120/118-1) * 100 |1.69
october 123 15.10.2020 |(123/120-1) * 100 |2.5
november 122 01.11.2020 |october != november |0
november 122 08.11.2020 |(122/122-1) * 100 |0
我的班级模式:
@Entity
@Table(name = "oper_mon_oil_price")
public class OilModel {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "o_year")
private String oyear;
@Column(name = "o_month")
private String omonth;
@Column(name = "o_date_event")
private String odateEvent;
@Column(name = "o_period")
private String operiod;
@Column(name = "o_area")
private String oarea;
@Column(name = "o_unit")
private String ounit;
@Column(name = "o_ai_92")
public Double oai92;
@Column(name = "o_ai_95")
public Double oai95;
@Column(name = "o_diesel_winter")
private Double odieselWinter;
@Column(name = "o_diesel_fuel_offseason")
private Double odieselFuelOffseason;
@Column(name = "growth_ai_92")
public Double growthAi92;
@Column(name = "growth_ai_95")
public Double growthAi95;
@Column(name = "growth_diesel_winter")
public Double growthDieselWinter;
@Column(name = "growth_diesel_fuel_offseason")
public Double growthDieselFuelOffseason;
getters/setters
}
类存储库:
@Repository
@Mapper
public interface OilProdTovRepo {
@Select("SELECT * FROM oper_mon_prod_tov ORDER BY o_year ASC, o_month ASC, o_date_event ASC ")
@Results(value ={
@Result(property = "id",column = "id"),
@Result(property = "area",column = "o_area"),
@Result(property = "year",column = "o_year"),
@Result(property = "month",column = "o_month"),
@Result(property = "period",column = "o_period"),
@Result(property = "dateEvent",column = "o_date_event"),
@Result(property = "unit",column = "o_unit"),
@Result(property = "wheatFlourFirstGrade",column = "o_wheat_flour_first_grade"),
@Result(property = "wheatBread",column = "o_wheat_bread"),
@Result(property = "horns",column = "o_horns"),
@Result(property = "buckwheat",column = "o_buckwheat"),
@Result(property = "polishedRice",column = "o_polished_rice"),
@Result(property = "potato",column = "o_potato"),
@Result(property = "carrots",column = "o_carrots"),
@Result(property = "onion",column = "o_onion"),
@Result(property = "cabbageWhite",column = "o_cabbage_white"),
@Result(property = "sugar",column = "o_sugar"),
@Result(property = "oil",column = "o_oil"),
@Result(property = "beef",column = "o_beef"),
@Result(property = "chickenMeat",column = "o_chicken_meat"),
@Result(property = "milk",column = "o_milk"),
@Result(property = "kefir",column = "o_kefir"),
@Result(property = "butter",column = "o_butter"),
@Result(property = "egg",column = "o_egg"),
@Result(property = "salt",column = "o_salt"),
@Result(property = "cottageCheese",column = "o_cottage_cheese"),
@Result(property = "growthWheatFlourFirstGrade",column = "o_growth_wheat_flour_first_grade"),
})
List<OilProdTovModel> getOilProdTovModel();
@Update("UPDATE oper_mon_prod_tov SET o_growth_wheat_flour_first_grade=#{growthWheatFlourFirstGrade} WHERE id=#{id}")
void updateWheatFlourFirstGrade(OilProdTovModel oilProdTovModel);
}
以及主要的服务类:
@Service
public class OilProdTovService {
@Autowired
public OilProdTovRepo oilProdTovRepo;
public List<OilProdTovModel> allOilProdTov() {
List<OilProdTovModel> oilProdTovModels = oilProdTovRepo.getOilProdTovModel();
Double first = 0.0;
Double result;
for (OilProdTovModel oilProdTovModel : oilProdTovModels) {
if (oilProdTovModel.getMonth().equals("September")) {
Double wheatFlourFirstGrade = oilProdTovModel.getWheatFlourFirstGrade();
result = (wheatFlourFirstGrade / first - 1) * 100;
first = wheatFlourFirstGrade;
wheatFlourFirstGrade = result;
oilProdTovModel.setGrowthWheatFlourFirstGrade(wheatFlourFirstGrade);
} else if (oilProdTovModel.getMonth().equals("October")) {
Double wheatFlourFirstGrade = oilProdTovModel.getWheatFlourFirstGrade();
result = (wheatFlourFirstGrade / first - 1) * 100;
first = wheatFlourFirstGrade;
wheatFlourFirstGrade = result;
oilProdTovModel.setGrowthWheatFlourFirstGrade(wheatFlourFirstGrade);
}
else if (oilProdTovModel.getMonth().equals("November")) {
Double wheatFlourFirstGrade = oilProdTovModel.getWheatFlourFirstGrade();
result = (wheatFlourFirstGrade / first - 1) * 100;
first = wheatFlourFirstGrade;
wheatFlourFirstGrade = result;
oilProdTovModel.setGrowthWheatFlourFirstGrade(wheatFlourFirstGrade);
}
else {
oilProdTovModel.setGrowthWheatFlourFirstGrade(0.0);
}
oilProdTovRepo.updateWheatFlourFirstGrade(oilProdTovModel);
}
return oilProdTovRepo.getOilProdTovModel();
}
}
如何修改代码?
1条答案
按热度按时间p8h8hvxi1#
既然你似乎很难抓住列表中紧随其后的两个实体,那么你可以这样做:
使用“foreach”:
使用传统的for循环: