首页>代码>springboot开发微信记事本小程序,实际运行地址搜,记事本随手记就好了>/untitled4/src/main/java/com/rgzn/app/controller/BillController.java
package com.rgzn.app.controller; import com.rgzn.app.common.ApiResponse; import com.rgzn.app.entity.Bill; import com.rgzn.app.service.BillService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.HashMap; import java.util.List; import java.util.Map; @RestController @RequestMapping("/api/bill") public class BillController { @Autowired private BillService billService; @PostMapping public ApiResponse createBill(@RequestBody Bill bill) { Bill createdBill = billService.createBill(bill); return ApiResponse.success(createdBill); } @GetMapping("/{id}") public ApiResponse getBill(@PathVariable Long id) { Bill bill = billService.getBillById(id); return bill != null ? ApiResponse.success(bill) : ApiResponse.error("账单不存在"); } // 此方法已不再使用,因为我们现在使用openid // @GetMapping("/user/{userId}") // public ApiResponse getUserBills(@PathVariable Long userId) { // return ApiResponse.success(billService.getBillsByUserId(userId)); // } @PutMapping("/{id}") public ApiResponse updateBill(@PathVariable Long id, @RequestBody Bill bill) { bill.setId(id); boolean success = billService.updateBill(bill); return success ? ApiResponse.success(null) : ApiResponse.error("更新失败"); } @DeleteMapping("/{id}") public ApiResponse deleteBill(@PathVariable Long id) { boolean success = billService.deleteBill(id); return success ? ApiResponse.success(null) : ApiResponse.error("删除失败"); } /** * 获取用户指定月份的账单统计数据 * @param openid 用户的openid * @param year 年份 * @param month 月份 * @return 包含月度结余、月度收入和月度支出的统计数据 */ @GetMapping("/statistics/{openid}") public ApiResponse getMonthlyStatistics( @PathVariable String openid, @RequestParam int year, @RequestParam int month) { Map<String, Object> statistics = billService.getMonthlyStatisticsByOpenid(openid, year, month); return ApiResponse.success(statistics); } /** * 根据openid获取账单列表,可选按月份筛选,按修改时间倒序排序 * @param openid 用户的openid * @param year 可选的年份参数 * @param month 可选的月份参数 * @return 符合条件的账单列表 */ @GetMapping("/list/{openid}") public ApiResponse getBillsByOpenid( @PathVariable String openid, @RequestParam(required = false) Integer year, @RequestParam(required = false) Integer month) { if (year != null && month != null) { return ApiResponse.success(billService.getBillsByOpenidAndMonth(openid, year, month)); } else { return ApiResponse.success(billService.getBillsByOpenidOrderByUpdateTimeDesc(openid)); } } /** * 获取用户指定年份的所有月份账单统计数据 * @param openid 用户的openid * @param year 年份 * @return 包含每个月的结余、收入和支出的统计数据列表 */ @GetMapping("/yearly-statistics/{openid}") public ApiResponse getYearlyStatistics( @PathVariable String openid, @RequestParam int year) { List<Map<String, Object>> yearlyStatistics = billService.getYearlyStatisticsByOpenid(openid, year); return ApiResponse.success(yearlyStatistics); } }