合并拉取请求 'master' (#1) 从 together/java-beta:master 到 master

已审核于:https://gitea.zhujinjie.me/qq1244/java-beta/pulls/1
This commit is contained in:
2025-06-20 02:29:24 +00:00
6 changed files with 138 additions and 0 deletions

13
pom.xml
View File

@@ -110,6 +110,19 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<!-- 添加POI依赖用于Excel操作 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.3</version>
</dependency>
</dependencies>
<build>

View File

@@ -1,16 +1,21 @@
package com.example.javatest.Controller.admin;
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.example.javatest.Entity.admin.Account;
import com.example.javatest.Mapper.admin.AccountMapper;
import com.example.javatest.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
@RestController
@RequestMapping("/admin/accounts")
@@ -179,4 +184,22 @@ public class AccountController {
Page<Account> resultPage = accountMapper.selectPage(page, queryWrapper);
return Result.success(resultPage);
}
@GetMapping("/statistics/cumulative-users-by-loop")
public Map<String, Object> getCumulativeUserCountByLoop() {
List<String> dates = new ArrayList<>();
List<Long> userCounts = new ArrayList<>();
LocalDate today = LocalDate.now();
for (int i = 0; i < 7; i++) {
LocalDate queryDate = today.minusDays(i);
LocalDateTime endOfDay = queryDate.plusDays(1).atStartOfDay();
Long totalCount = accountMapper.countTotalUpTo(endOfDay);
dates.add(queryDate.toString());
userCounts.add(totalCount);
}
return Map.of(
"dates", dates,
"userCounts", userCounts
);
}
}

View File

@@ -7,10 +7,19 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.javatest.Entity.admin.Dish;
import com.example.javatest.Mapper.admin.DishMapper;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.io.ByteArrayOutputStream;
import java.time.LocalDateTime;
import java.util.List;
@@ -120,4 +129,65 @@ public class DishController {
return dishMapper.selectList(new LambdaQueryWrapper<Dish>()
.orderByDesc(Dish::getCreatedAt));
}
/**
* 导出当前分类的菜品数据到Excel
* @param category 分类名称
* @return Excel文件响应
*/
@GetMapping("/export/{category}")
public ResponseEntity<byte[]> exportDishesToExcel(@PathVariable String category) {
// 查询当前分类的所有菜品
List<Dish> dishes = dishMapper.selectList(
new LambdaQueryWrapper<Dish>()
.eq(Dish::getCategory, category)
);
try (Workbook workbook = new XSSFWorkbook()) {
Sheet sheet = workbook.createSheet("菜品数据");
// 创建表头
String[] headers = {"菜品ID", "菜品名称", "分类", "价格", "库存", "状态", "描述", "累计销售", "标签"};
Row headerRow = sheet.createRow(0);
for (int i = 0; i < headers.length; i++) {
Cell cell = headerRow.createCell(i);
cell.setCellValue(headers[i]);
}
// 填充数据
int rowNum = 1;
for (Dish dish : dishes) {
Row row = sheet.createRow(rowNum++);
row.createCell(0).setCellValue(dish.getDishId());
row.createCell(1).setCellValue(dish.getDishName());
row.createCell(2).setCellValue(dish.getCategory());
row.createCell(3).setCellValue(dish.getPrice().doubleValue());
row.createCell(4).setCellValue(dish.getStock());
row.createCell(5).setCellValue(dish.getStatus());
row.createCell(6).setCellValue(dish.getDescription());
row.createCell(7).setCellValue(dish.getSales());
row.createCell(8).setCellValue(dish.getTag());
}
// 自动调整列宽
for (int i = 0; i < headers.length; i++) {
sheet.autoSizeColumn(i);
}
// 写入字节数组
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
workbook.write(outputStream);
// 设置响应头
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
responseHeaders.setContentDispositionFormData("attachment", "菜品数据_" + category + ".xlsx");
return ResponseEntity.ok()
.headers(responseHeaders)
.body(outputStream.toByteArray());
} catch (Exception e) {
throw new RuntimeException("导出Excel失败: " + e.getMessage());
}
}
}

View File

@@ -3,7 +3,13 @@ package com.example.javatest.Mapper.admin;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.javatest.Entity.admin.Account;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.time.LocalDateTime;
@Mapper
public interface AccountMapper extends BaseMapper<Account> {
@Select("SELECT COUNT(account_id) FROM account WHERE is_deleted = 0 AND created_at < #{endOfDay}")
Long countTotalUpTo(@Param("endOfDay") LocalDateTime endOfDay);
}

View File

@@ -0,0 +1,13 @@
package org.example.cgpro;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class CGproApplication {
public static void main(String[] args) {
SpringApplication.run(CGproApplication.class, args);
}
}

View File

@@ -0,0 +1,13 @@
package org.example.cgpro;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class CGproApplicationTests {
@Test
void contextLoads() {
}
}