Просмотр исходного кода

更新收入记录控制器,新增团队ID和用户ID字段,完善创建和查询逻辑,增加必填字段验证和用户角色权限验证,支持根据团队和用户过滤收入记录。

wuyi 3 месяцев назад
Родитель
Сommit
0a0c125a63

+ 71 - 5
src/controllers/income-records.controller.ts

@@ -6,16 +6,61 @@ import {
   ListIncomeRecordsQuery,
   IncomeRecordParams
 } from '../dto/income-records.dto'
+import { UserRole } from '../entities/user.entity'
+import { TeamService } from '../services/team.service'
 
 export class IncomeRecordsController {
   private incomeRecordsService: IncomeRecordsService
+  private teamService: TeamService
 
   constructor(app: FastifyInstance) {
     this.incomeRecordsService = new IncomeRecordsService(app)
+    this.teamService = new TeamService(app)
   }
 
   async create(request: FastifyRequest<{ Body: CreateIncomeRecordBody }>, reply: FastifyReply) {
     try {
+      const {
+        teamId,
+        userId,
+        incomeAmount,
+        agentName,
+        incomeType,
+        orderType,
+        video,
+        price,
+        tipOrderId,
+        payChannel,
+        payNo
+      } = request.body
+
+      // 验证必填字段
+      if (
+        !teamId ||
+        !userId ||
+        !incomeAmount ||
+        !agentName ||
+        !incomeType ||
+        !orderType ||
+        !video ||
+        !price ||
+        !tipOrderId ||
+        !payChannel ||
+        !payNo
+      ) {
+        return reply
+          .code(400)
+          .send({
+            message:
+              'teamId、userId、incomeAmount、agentName、incomeType、orderType、video、price、tipOrderId、payChannel、payNo 为必填字段'
+          })
+      }
+
+      // 验证金额为正数
+      if (incomeAmount <= 0 || price <= 0) {
+        return reply.code(400).send({ message: 'incomeAmount 和 price 必须大于 0' })
+      }
+
       const incomeRecord = await this.incomeRecordsService.create(request.body)
       return reply.code(201).send(incomeRecord)
     } catch (error) {
@@ -35,6 +80,21 @@ export class IncomeRecordsController {
 
   async findAll(request: FastifyRequest<{ Querystring: ListIncomeRecordsQuery }>, reply: FastifyReply) {
     try {
+      const user = request.user
+      if (!user) {
+        return reply.code(403).send({ message: '用户未登录' })
+      }
+
+      // 根据用户角色设置查询条件
+      if (user.role === UserRole.TEAM) {
+        const team = await this.teamService.findByUserId(user.id)
+        if (team) {
+          request.query.teamId = team.id
+        }
+      } else if (user.role === UserRole.USER) {
+        request.query.userId = user.id
+      }
+
       const result = await this.incomeRecordsService.findAll(request.query)
       return reply.send(result)
     } catch (error) {
@@ -42,11 +102,14 @@ export class IncomeRecordsController {
     }
   }
 
-  async update(request: FastifyRequest<{ Params: IncomeRecordParams; Body: UpdateIncomeRecordBody }>, reply: FastifyReply) {
+  async update(
+    request: FastifyRequest<{ Params: IncomeRecordParams; Body: UpdateIncomeRecordBody }>,
+    reply: FastifyReply
+  ) {
     try {
       const { id } = request.params
       const updateData = { ...request.body, id }
-      
+
       try {
         await this.incomeRecordsService.findById(id)
       } catch (error) {
@@ -63,7 +126,7 @@ export class IncomeRecordsController {
   async delete(request: FastifyRequest<{ Params: IncomeRecordParams }>, reply: FastifyReply) {
     try {
       const { id } = request.params
-      
+
       try {
         await this.incomeRecordsService.findById(id)
       } catch (error) {
@@ -80,7 +143,7 @@ export class IncomeRecordsController {
   async hardDelete(request: FastifyRequest<{ Params: IncomeRecordParams }>, reply: FastifyReply) {
     try {
       const { id } = request.params
-      
+
       try {
         await this.incomeRecordsService.findById(id)
       } catch (error) {
@@ -94,7 +157,10 @@ export class IncomeRecordsController {
     }
   }
 
-  async getStatistics(request: FastifyRequest<{ Querystring: { startDate?: string; endDate?: string } }>, reply: FastifyReply) {
+  async getStatistics(
+    request: FastifyRequest<{ Querystring: { startDate?: string; endDate?: string } }>,
+    reply: FastifyReply
+  ) {
     try {
       const { startDate, endDate } = request.query
       const statistics = await this.incomeRecordsService.getStatistics(startDate, endDate)

+ 4 - 0
src/dto/income-records.dto.ts

@@ -3,6 +3,8 @@ import { IncomeType, OrderType } from '../entities/income-records.entity'
 import { Pagination } from './common.dto'
 
 export interface CreateIncomeRecordBody {
+  teamId: number
+  userId: number
   incomeAmount: number
   agentName: string
   incomeType: IncomeType
@@ -31,6 +33,8 @@ export interface UpdateIncomeRecordBody {
 }
 
 export interface ListIncomeRecordsQuery extends Pagination {
+  teamId?: number
+  userId?: number
   agentName?: string
   incomeType?: IncomeType
   orderType?: OrderType

+ 6 - 0
src/entities/income-records.entity.ts

@@ -19,6 +19,12 @@ export class IncomeRecords {
   @PrimaryGeneratedColumn()
   id: number
 
+  @Column({ nullable: true, default: 0 })
+  teamId: number
+
+  @Column({ nullable: true, default: 0 })
+  userId: number
+
   @Column({
     type: 'decimal',
     precision: 10,

+ 8 - 3
src/routes/income-records.routes.ts

@@ -1,8 +1,13 @@
 import { FastifyInstance } from 'fastify'
 import { IncomeRecordsController } from '../controllers/income-records.controller'
-import { authenticate, hasRole } from '../middlewares/auth.middleware'
+import { authenticate, hasAnyRole, hasRole } from '../middlewares/auth.middleware'
 import { UserRole } from '../entities/user.entity'
-import { CreateIncomeRecordBody, UpdateIncomeRecordBody, ListIncomeRecordsQuery, IncomeRecordParams } from '../dto/income-records.dto'
+import {
+  CreateIncomeRecordBody,
+  UpdateIncomeRecordBody,
+  ListIncomeRecordsQuery,
+  IncomeRecordParams
+} from '../dto/income-records.dto'
 
 export default async function incomeRecordsRoutes(fastify: FastifyInstance) {
   const incomeRecordsController = new IncomeRecordsController(fastify)
@@ -17,7 +22,7 @@ export default async function incomeRecordsRoutes(fastify: FastifyInstance) {
   // 获取收入记录列表
   fastify.get<{ Querystring: ListIncomeRecordsQuery }>(
     '/',
-    { onRequest: [authenticate, hasRole(UserRole.ADMIN)] },
+    { onRequest: [authenticate, hasAnyRole(UserRole.ADMIN, UserRole.TEAM, UserRole.USER)] },
     incomeRecordsController.findAll.bind(incomeRecordsController)
   )
 

+ 23 - 2
src/services/income-records.service.ts

@@ -12,7 +12,20 @@ export class IncomeRecordsService {
   }
 
   async create(data: CreateIncomeRecordBody): Promise<IncomeRecords> {
-    const incomeRecord = this.incomeRecordsRepository.create(data)
+    const incomeRecord = this.incomeRecordsRepository.create({
+      teamId: data.teamId,
+      userId: data.userId,
+      incomeAmount: data.incomeAmount,
+      agentName: data.agentName,
+      incomeType: data.incomeType,
+      orderType: data.orderType,
+      video: data.video,
+      price: data.price,
+      tipOrderId: data.tipOrderId,
+      payChannel: data.payChannel,
+      payNo: data.payNo,
+      source: data.source
+    })
     return this.incomeRecordsRepository.save(incomeRecord)
   }
 
@@ -21,10 +34,18 @@ export class IncomeRecordsService {
   }
 
   async findAll(query: ListIncomeRecordsQuery): Promise<PaginationResponse<IncomeRecords>> {
-    const { page, size, agentName, incomeType, orderType, payChannel, startDate, endDate } = query
+    const { page, size, teamId, userId, agentName, incomeType, orderType, payChannel, startDate, endDate } = query
     
     const where: any = {}
     
+    if (teamId) {
+      where.teamId = teamId
+    }
+    
+    if (userId) {
+      where.userId = userId
+    }
+    
     if (agentName) {
       where.agentName = Like(`%${agentName}%`)
     }