|
|
@@ -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)
|