首页>代码>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);
    }
}
最近下载更多
半夏bx  LV15 9月8日
唐僧洗头爱飘柔  LV22 7月10日
ewan007  LV30 7月2日
欠踹de背影  LV25 6月30日
最代码官方  LV168 6月29日
最近浏览更多
Coffee_冬孓  LV12 10月17日
sos0531  LV8 10月13日
310834626  LV3 10月11日
不想起  LV10 10月10日
vincemokea  LV9 10月9日
wyx065747  LV67 10月1日
Molly123  LV8 9月27日
arrce11 9月26日
暂无贡献等级
xxyylu  LV2 9月22日
xiaotao555555  LV3 9月21日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友