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 0000000000000000000000000000000000000000..11422f4cba23282ae8e2f2622ccf2648e2fbf830 --- /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 0000000000000000000000000000000000000000..ff06e1a8ab2dba45b8ee6cf39dd418cda50b020d --- /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 0000000000000000000000000000000000000000..c05dc6afe6e7ecf09f43a8ec0f095c9cce1a73ca --- /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 0000000000000000000000000000000000000000..f6c1bcf3ebc5178079d188e0e75895b4b34473e4 --- /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 8291fadec2d5705e34f75559536c66a40a03ad85..dac4b4c4c618dbe2cd41373047466e0a5c316c06 100644 --- a/src/main/java/com/example/newgroupshell/mapper/TaskMapper.java +++ b/src/main/java/com/example/newgroupshell/mapper/TaskMapper.java @@ -7,17 +7,16 @@ import org.apache.ibatis.annotations.*; import java.util.List; @Mapper -public interface TaskMapper -{ - int insertTask(Task task); - - int updateTaskById(Task task); - - int deleteTaskById(@Param("tid") Integer tid); - - List findTasksByAssigneeId(@Param("assigneeId") Integer assigneeId); - - List findTasksByCreatorId(@Param("creatorId") Integer creatorId); - - List findTasksByStatus(@Param("status") TaskStatusEnum status); +public interface TaskMapper { + int insertTask(Task task); + + int updateTaskById(Task task); + + int deleteTaskById(@Param("id") Integer id); + + List findTasksByAssigneeId(@Param("assigneeId") Integer assigneeId); + + List findTasksByCreatorId(@Param("creatorId") Integer creatorId); + + List findTasksByStatus(@Param("status") TaskStatusEnum status); } \ No newline at end of file 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 0000000000000000000000000000000000000000..0d1e659fa1c67610cea9b1984947b18abd4f5e39 --- /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/service/InvoiceService.java b/src/main/java/com/example/newgroupshell/service/InvoiceService.java new file mode 100644 index 0000000000000000000000000000000000000000..b34511509281b7fb5978e050018f9297c51b8171 --- /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 0000000000000000000000000000000000000000..b8d57b28f2ff1ccabda9037edadc0622f69c5424 --- /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 0000000000000000000000000000000000000000..0d5d8b38622c8e07d46dbf3a372086037c6cd1f2 --- /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 3b04bb091476c3bb8705c08c23ade16047e3a84b..390a7af5f04916c34264125f70190ad2873d88e1 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 task - tid, + id, title, description, create_time, @@ -23,7 +23,7 @@ status, - #{tid}, + #{id}, #{title}, #{description}, #{createdTime}, @@ -73,14 +73,14 @@ status = #{status}, - WHERE id = #{tid} + WHERE id = #{id} DELETE FROM task - WHERE id = #{tid} + WHERE id = #{id}