首页>代码>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);
    }
}
最近下载更多
唐僧洗头爱飘柔  LV22 7月10日
ewan007  LV30 7月2日
欠踹de背影  LV25 6月30日
最代码官方  LV168 6月29日
最近浏览更多
lipanlong  LV10 前天
Rucoding  LV8 8月15日
冰枫xjz8  LV31 8月11日
一个小兵  LV1 8月2日
gzxsvip  LV27 7月30日
260858883  LV5 7月25日
yimaoermao  LV1 7月25日
Mirage无衣 7月25日
暂无贡献等级
wonderfulvv  LV4 7月24日
hulewang  LV8 7月23日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友