|
|
@@ -1,6 +1,7 @@
|
|
|
package com.etotem.cfc.service;
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.etotem.cfc.entity.Food;
|
|
|
import com.etotem.cfc.entity.SeasonalFood;
|
|
|
@@ -25,23 +26,65 @@ public class FoodService {
|
|
|
private SeasonalFoodMapper seasonalFoodMapper;
|
|
|
|
|
|
public List<Food> listAll(String category, String status) {
|
|
|
+ return listAll(category, status, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<Food> listAll(String category, String status, List<Map<String, String>> sortSpecs) {
|
|
|
LambdaQueryWrapper<Food> wrapper = new LambdaQueryWrapper<Food>()
|
|
|
.eq(status != null, Food::getStatus, status)
|
|
|
- .eq(category != null, Food::getCategory, category)
|
|
|
- .orderByAsc(Food::getSortOrder);
|
|
|
+ .eq(category != null, Food::getCategory, category);
|
|
|
+ if (sortSpecs != null && !sortSpecs.isEmpty()) {
|
|
|
+ buildOrderBy(wrapper, sortSpecs);
|
|
|
+ } else {
|
|
|
+ wrapper.orderByAsc(Food::getSortOrder);
|
|
|
+ }
|
|
|
return foodMapper.selectList(wrapper);
|
|
|
}
|
|
|
|
|
|
+ private void buildOrderBy(LambdaQueryWrapper<Food> wrapper, List<Map<String, String>> sortSpecs) {
|
|
|
+ StringBuilder sb = new StringBuilder(" order by ");
|
|
|
+ for (int i = 0; i < sortSpecs.size(); i++) {
|
|
|
+ if (i > 0) sb.append(", ");
|
|
|
+ Map<String, String> spec = sortSpecs.get(i);
|
|
|
+ String col = spec.get("column");
|
|
|
+ if (col == null) continue;
|
|
|
+ boolean asc = !"desc".equalsIgnoreCase(spec.get("order"));
|
|
|
+ sb.append(colToSnake(col)).append(asc ? " asc" : " desc");
|
|
|
+ }
|
|
|
+ wrapper.last(sb.toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ private String colToSnake(String col) {
|
|
|
+ switch (col) {
|
|
|
+ case "name": return "name";
|
|
|
+ case "category": return "category";
|
|
|
+ case "calories": return "kcal_per_100g";
|
|
|
+ case "protein": return "protein";
|
|
|
+ case "carbs": return "carbs";
|
|
|
+ case "fat": return "fat";
|
|
|
+ case "fiber": return "fiber";
|
|
|
+ case "energyKj": return "energy_kj";
|
|
|
+ case "starch": return "starch";
|
|
|
+ case "cholesterol": return "cholesterol";
|
|
|
+ case "sortOrder": return "sort_order";
|
|
|
+ case "createdAt": return "created_at";
|
|
|
+ default: return col.replaceAll("([A-Z])", "_$1").toLowerCase();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 分页查询食材列表,支持分类、关键字、状态筛选
|
|
|
*/
|
|
|
- public Page<Food> listPage(long page, long size, String category, String keyword, String status) {
|
|
|
+ public Page<Food> listPage(long page, long size, String category, String keyword, String status, List<Map<String, String>> sortSpecs) {
|
|
|
LambdaQueryWrapper<Food> wrapper = new LambdaQueryWrapper<Food>()
|
|
|
.eq(status != null, Food::getStatus, status)
|
|
|
.eq(category != null && !category.isEmpty(), Food::getCategory, category)
|
|
|
- .like(keyword != null && !keyword.isEmpty(), Food::getName, keyword)
|
|
|
- .orderByAsc(Food::getSortOrder)
|
|
|
- .orderByAsc(Food::getId);
|
|
|
+ .like(keyword != null && !keyword.isEmpty(), Food::getName, keyword);
|
|
|
+ if (sortSpecs != null && !sortSpecs.isEmpty()) {
|
|
|
+ buildOrderBy(wrapper, sortSpecs);
|
|
|
+ } else {
|
|
|
+ wrapper.orderByAsc(Food::getSortOrder).orderByAsc(Food::getId);
|
|
|
+ }
|
|
|
return foodMapper.selectPage(new Page<>(page, size), wrapper);
|
|
|
}
|
|
|
|