From 7cd37a15e89cb806f33bb46b0fd791238fcd8a01 Mon Sep 17 00:00:00 2001 From: EricCheng <12955029+ericchengscut@user.noreply.gitee.com> Date: Wed, 17 Apr 2024 21:41:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=B4=A6=E5=8D=95=E7=B1=BB?= =?UTF-8?q?=EF=BC=88Invoice=EF=BC=89=E5=8F=8A=E5=85=B6=E5=A2=9E=E5=88=A0?= =?UTF-8?q?=E6=94=B9=E6=9F=A5=E4=B8=8E=E7=AE=80=E5=8D=95=E7=9A=84=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E7=BB=9F=E8=AE=A1=E6=96=B9=E6=B3=95=E3=80=82=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E7=9A=84Task=E7=B1=BB=E7=9A=84Controller=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/enums/InvoiceTypeEnum.java | 6 ++ .../newgroupshell/controller/InvoiceCtrl.java | 72 ++++++++++++++++++ .../newgroupshell/controller/TaskCtrl.java | 54 +++++++++++++ .../newgroupshell/mapper/InvoiceMapper.java | 34 +++++++++ .../newgroupshell/mapper/TaskMapper.java | 2 +- .../example/newgroupshell/pojo/Invoice.java | 61 +++++++++++++++ .../com/example/newgroupshell/pojo/Task.java | 2 +- .../newgroupshell/service/InvoiceService.java | 31 ++++++++ .../service/impl/InvoiceServiceImpl.java | 75 +++++++++++++++++++ src/main/resources/mapper/InvoiceMapper.xml | 72 ++++++++++++++++++ src/main/resources/mapper/TaskMapper.xml | 24 +++--- 11 files changed, 419 insertions(+), 14 deletions(-) create mode 100644 src/main/java/com/example/newgroupshell/common/enums/InvoiceTypeEnum.java create mode 100644 src/main/java/com/example/newgroupshell/controller/InvoiceCtrl.java create mode 100644 src/main/java/com/example/newgroupshell/controller/TaskCtrl.java create mode 100644 src/main/java/com/example/newgroupshell/mapper/InvoiceMapper.java create mode 100644 src/main/java/com/example/newgroupshell/pojo/Invoice.java create mode 100644 src/main/java/com/example/newgroupshell/service/InvoiceService.java create mode 100644 src/main/java/com/example/newgroupshell/service/impl/InvoiceServiceImpl.java create mode 100644 src/main/resources/mapper/InvoiceMapper.xml diff --git a/src/main/java/com/example/newgroupshell/common/enums/InvoiceTypeEnum.java b/src/main/java/com/example/newgroupshell/common/enums/InvoiceTypeEnum.java new file mode 100644 index 0000000..11422f4 --- /dev/null +++ b/src/main/java/com/example/newgroupshell/common/enums/InvoiceTypeEnum.java @@ -0,0 +1,6 @@ +package com.example.newgroupshell.common.enums; + +public enum InvoiceTypeEnum { + EXPENSE, + INCOME +} diff --git a/src/main/java/com/example/newgroupshell/controller/InvoiceCtrl.java b/src/main/java/com/example/newgroupshell/controller/InvoiceCtrl.java new file mode 100644 index 0000000..ff06e1a --- /dev/null +++ b/src/main/java/com/example/newgroupshell/controller/InvoiceCtrl.java @@ -0,0 +1,72 @@ +package com.example.newgroupshell.controller; + +import com.example.newgroupshell.pojo.Invoice; +import com.example.newgroupshell.service.InvoiceService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.math.BigDecimal; +import java.time.LocalDateTime; +import java.util.List; +import java.util.Map; + +@RestController +@RequestMapping("/api/invoice") +public class InvoiceCtrl { + + @Autowired + private InvoiceService invoiceService; + + @PostMapping + public Invoice addInvoice(@RequestBody Invoice record) { + return invoiceService.addInvoice(record); + } + + // 根据ID获取发票信息 + @GetMapping("/{id}") + public Invoice getInvoiceById(@PathVariable Integer id) { + return invoiceService.getInvoiceById(id); + } + + @GetMapping + public List getAllInvoices() { + return invoiceService.getAllInvoices(); + } + + @PutMapping("/{id}") + public Invoice updateInvoice(@PathVariable Integer id, @RequestBody Invoice record) { + record.setId(id); + return invoiceService.updateInvoice(record); + } + + @DeleteMapping("/{id}") + public String deleteInvoice(@PathVariable Integer id) { + return invoiceService.deleteInvoice(id); + } + + // 获取指定时间段内的总支出 + @GetMapping("/expenses") + public BigDecimal getExpensesByTime(@RequestParam LocalDateTime start, @RequestParam LocalDateTime end) { + return invoiceService.getExpensesByTime(start, end); + } + + // 获取指定时间段内的总收入 + @GetMapping("/incomes") + public BigDecimal getIncomesByTime(@RequestParam LocalDateTime start, @RequestParam LocalDateTime end) { + return invoiceService.getIncomesByTime(start, end); + } + + // 获取指定时间段内的净收入 + @GetMapping("/netIncome") + public BigDecimal getNetIncomeByTime(@RequestParam LocalDateTime start, @RequestParam LocalDateTime end) { + return invoiceService.getNetIncomeByTime(start, end); + } + + // 获取指定类别和时间段内的支出汇总 + @GetMapping("/expensesByCategory") + public Map getExpensesByCategory(@RequestParam String category, + @RequestParam LocalDateTime start, + @RequestParam LocalDateTime end) { + return invoiceService.getExpensesByCategory(category, start, end); + } +} diff --git a/src/main/java/com/example/newgroupshell/controller/TaskCtrl.java b/src/main/java/com/example/newgroupshell/controller/TaskCtrl.java new file mode 100644 index 0000000..c05dc6a --- /dev/null +++ b/src/main/java/com/example/newgroupshell/controller/TaskCtrl.java @@ -0,0 +1,54 @@ +package com.example.newgroupshell.controller; + +import com.example.newgroupshell.pojo.Task; +import com.example.newgroupshell.common.enums.TaskStatusEnum; +import com.example.newgroupshell.service.TaskService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/api/task") +public class TaskCtrl { + + @Autowired + private TaskService taskService; + + // 创建任务 + @PostMapping + public Task createTask(@RequestBody Task task) { + return taskService.createTask(task); + } + + // 更新任务 + @PutMapping("/{id}") + public Task updateTask(@PathVariable Integer id, @RequestBody Task task) { + task.setId(id); + return taskService.updateTask(task); + } + + // 删除任务 + @DeleteMapping("/{id}") + public boolean deleteTask(@PathVariable Integer id) { + return taskService.deleteTask(id); + } + + // 按创建者ID获取任务列表 + @GetMapping("/creator/{creatorId}") + public List listTasksByCreator(@PathVariable Integer creatorId) { + return taskService.listTasksByCreator(creatorId); + } + + // 按受托人ID获取任务列表 + @GetMapping("/assignee/{assigneeId}") + public List listTasksByAssignee(@PathVariable Integer assigneeId) { + return taskService.listTasksByAssignee(assigneeId); + } + + // 按状态获取任务列表 + @GetMapping("/status") + public List listTasksByStatus(@RequestParam TaskStatusEnum status) { + return taskService.listTasksByStatus(status); + } +} diff --git a/src/main/java/com/example/newgroupshell/mapper/InvoiceMapper.java b/src/main/java/com/example/newgroupshell/mapper/InvoiceMapper.java new file mode 100644 index 0000000..f6c1bcf --- /dev/null +++ b/src/main/java/com/example/newgroupshell/mapper/InvoiceMapper.java @@ -0,0 +1,34 @@ +package com.example.newgroupshell.mapper; + +import com.example.newgroupshell.common.enums.InvoiceTypeEnum; +import com.example.newgroupshell.pojo.Invoice; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; + +import java.math.BigDecimal; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.util.List; + +@Mapper +public interface InvoiceMapper { + Invoice insert(Invoice record); + + Invoice findById(@Param("id") Integer id); + + List findAllInvoice(); + + Invoice updateInvoiceById(Invoice record); + + String deleteById(@Param("id") Integer id); + + BigDecimal sumAmountByDate(@Param("type") InvoiceTypeEnum type, + @Param("start") LocalDateTime start, + @Param("end") LocalDateTime end); + + List findByCategoryAndDate(@Param("category") String category, + @Param("start") LocalDateTime start, + @Param("end") LocalDateTime end); + + +} diff --git a/src/main/java/com/example/newgroupshell/mapper/TaskMapper.java b/src/main/java/com/example/newgroupshell/mapper/TaskMapper.java index e1e1744..25d05ac 100644 --- a/src/main/java/com/example/newgroupshell/mapper/TaskMapper.java +++ b/src/main/java/com/example/newgroupshell/mapper/TaskMapper.java @@ -13,7 +13,7 @@ public interface TaskMapper { int updateTaskById(Task task); - int deleteTaskById(@Param("tid") Integer tid); + int deleteTaskById(@Param("id") Integer id); List findTasksByAssigneeId(@Param("assigneeId") Integer assigneeId); diff --git a/src/main/java/com/example/newgroupshell/pojo/Invoice.java b/src/main/java/com/example/newgroupshell/pojo/Invoice.java new file mode 100644 index 0000000..0d1e659 --- /dev/null +++ b/src/main/java/com/example/newgroupshell/pojo/Invoice.java @@ -0,0 +1,61 @@ +package com.example.newgroupshell.pojo; + +import com.example.newgroupshell.common.enums.InvoiceTypeEnum; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.ToString; + +import java.math.BigDecimal; +import java.time.LocalDateTime; + +/** + * Invoice class represents an invoice object with its attributes and methods. + * + * @author Tabnine + * @version 1.0 + */ +@Data +@ToString +@NoArgsConstructor +@AllArgsConstructor +public class Invoice { + /** + * The unique identifier for the invoice. + * 主键,自增 + */ + private Integer id; + + /** + * The amount of the invoice. + * 使用BigDecimal进行与财务相关的计算,避免出现浮点数运算误差 + */ + private BigDecimal amount; + + + private InvoiceTypeEnum type; + /** + * 非空 + */ + + /** + * The description of the invoice. + */ + private String description; + + /** + * The category of the invoice. + */ + private String category; + + /** + * The date and time when the invoice was created. + */ + private LocalDateTime createdTime; + + /** + * The user ID associated with the invoice. + * 外键引用user表的id + */ + private Integer userId; +} \ No newline at end of file diff --git a/src/main/java/com/example/newgroupshell/pojo/Task.java b/src/main/java/com/example/newgroupshell/pojo/Task.java index 38244cc..83f0c25 100644 --- a/src/main/java/com/example/newgroupshell/pojo/Task.java +++ b/src/main/java/com/example/newgroupshell/pojo/Task.java @@ -22,7 +22,7 @@ public class Task { /** * The unique identifier of the task. */ - private Integer tid; + private Integer id; /** * The title of the task. diff --git a/src/main/java/com/example/newgroupshell/service/InvoiceService.java b/src/main/java/com/example/newgroupshell/service/InvoiceService.java new file mode 100644 index 0000000..b345115 --- /dev/null +++ b/src/main/java/com/example/newgroupshell/service/InvoiceService.java @@ -0,0 +1,31 @@ +package com.example.newgroupshell.service; + +import com.example.newgroupshell.pojo.Invoice; +import org.springframework.stereotype.Service; + +import java.math.BigDecimal; +import java.time.LocalDateTime; +import java.util.List; +import java.util.Map; + +@Service +public interface InvoiceService { + + public Invoice addInvoice(Invoice record); + + public Invoice getInvoiceById(Integer id); + + public List getAllInvoices(); + + public Invoice updateInvoice(Invoice record); + + public String deleteInvoice(Integer id); + + public BigDecimal getExpensesByTime(LocalDateTime start, LocalDateTime end); + + BigDecimal getIncomesByTime(LocalDateTime start, LocalDateTime end); + + BigDecimal getNetIncomeByTime(LocalDateTime start, LocalDateTime end); + + Map getExpensesByCategory(String category, LocalDateTime start, LocalDateTime end); +} diff --git a/src/main/java/com/example/newgroupshell/service/impl/InvoiceServiceImpl.java b/src/main/java/com/example/newgroupshell/service/impl/InvoiceServiceImpl.java new file mode 100644 index 0000000..b8d57b2 --- /dev/null +++ b/src/main/java/com/example/newgroupshell/service/impl/InvoiceServiceImpl.java @@ -0,0 +1,75 @@ +package com.example.newgroupshell.service.impl; + +import com.example.newgroupshell.common.enums.InvoiceTypeEnum; +import com.example.newgroupshell.mapper.InvoiceMapper; +import com.example.newgroupshell.pojo.Invoice; +import com.example.newgroupshell.service.InvoiceService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.math.BigDecimal; +import java.time.LocalDateTime; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +@Service +public class InvoiceServiceImpl implements InvoiceService { + + @Autowired + private InvoiceMapper invoiceMapper; + + @Override + public Invoice addInvoice(Invoice record) { + return invoiceMapper.insert(record); + } + + //TODO:实现Controller类的函数 + @Override + public Invoice getInvoiceById(Integer id) { + return invoiceMapper.findById(id); + } + + @Override + public List getAllInvoices() { + return invoiceMapper.findAllInvoice(); + } + + @Override + public Invoice updateInvoice(Invoice record) { + return invoiceMapper.updateInvoiceById(record); + } + + @Override + public String deleteInvoice(Integer id) { + invoiceMapper.deleteById(id); + return "Record deleted successfully!"; + } + + //TODO:实现Controller类的函数 + @Override + public BigDecimal getExpensesByTime(LocalDateTime start, LocalDateTime end) { + return invoiceMapper.sumAmountByDate(InvoiceTypeEnum.EXPENSE, start, end); + } + + @Override + public BigDecimal getIncomesByTime(LocalDateTime start, LocalDateTime end) { + return invoiceMapper.sumAmountByDate(InvoiceTypeEnum.INCOME, start, end); + } + + @Override + public BigDecimal getNetIncomeByTime(LocalDateTime start, LocalDateTime end) { + BigDecimal income = invoiceMapper.sumAmountByDate(InvoiceTypeEnum.INCOME, start, end); + BigDecimal expenses = invoiceMapper.sumAmountByDate(InvoiceTypeEnum.EXPENSE, start, end); + return income.subtract(expenses); + } + + + //从数据库查询特定类别和时间范围内的发票,然后计算每个类别的总金额,并以Map形式返回结果 + @Override + public Map getExpensesByCategory(String category, LocalDateTime start, LocalDateTime end) { + List records = invoiceMapper.findByCategoryAndDate(category, start, end); + return records.stream().collect(Collectors.groupingBy(Invoice::getCategory, + Collectors.reducing(BigDecimal.ZERO, Invoice::getAmount, BigDecimal::add))); + } +} diff --git a/src/main/resources/mapper/InvoiceMapper.xml b/src/main/resources/mapper/InvoiceMapper.xml new file mode 100644 index 0000000..0d5d8b3 --- /dev/null +++ b/src/main/resources/mapper/InvoiceMapper.xml @@ -0,0 +1,72 @@ + + + + + + id, amount, type, description, category, created_time, user_id + + + + INSERT INTO invoice () + VALUES (#{id}, #{amount},#{type}, #{description}, #{category}, #{created_time}, #{user_id}) + + + + + + + + UPDATE invoice + + + amount = #{amount}, + + + type = #{type}, + + + description = #{description}, + + + category = #{category}, + + + created_time = #{createdTime}, + + + user_id = #{userId}, + + + WHERE id = #{id} + + + + DELETE + FROM invoice + WHERE id = #{id} + + + + + + + + diff --git a/src/main/resources/mapper/TaskMapper.xml b/src/main/resources/mapper/TaskMapper.xml index 70c6826..e709ec0 100644 --- a/src/main/resources/mapper/TaskMapper.xml +++ b/src/main/resources/mapper/TaskMapper.xml @@ -3,14 +3,14 @@ - tid, title, description,create_time, update_time,start_time, deadline, assignee_id, creator_id, priority, status + id, title, description,create_time, update_time,start_time, deadline, assignee_id, creator_id, priority, status - - INSERT INTO tasks + INSERT INTO task - tid, + id, title, description, create_time, @@ -23,7 +23,7 @@ status, - #{tid}, + #{id}, #{title}, #{description}, #{createdTime}, @@ -39,7 +39,7 @@ - UPDATE tasks + UPDATE task title = #{title}, @@ -72,34 +72,34 @@ status = #{status}, - WHERE tid = #{tid} + WHERE id = #{id} DELETE - FROM tasks - WHERE tid = #{tid} + FROM task + WHERE id = #{id} -- Gitee