Spaces:
Sleeping
Sleeping
File size: 3,069 Bytes
73746a8 426f2a4 73746a8 f45e448 73746a8 426f2a4 cbed33a 426f2a4 cbed33a 426f2a4 73746a8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Course } from '../entities/course.entity';
import { Order, OrderStatus, OrderType } from '../entities/order.entity';
import { CreateCourseDto } from './dto/create-course.dto';
@Injectable()
export class AdminService {
constructor(
@InjectRepository(Course)
private courseRepository: Repository<Course>,
@InjectRepository(Order)
private orderRepository: Repository<Order>,
) {}
async createCourse(createCourseDto: CreateCourseDto) {
const course = this.courseRepository.create(createCourseDto);
return this.courseRepository.save(course);
}
async updateCourse(id: number, updateData: Partial<CreateCourseDto>) {
await this.courseRepository.update(id, updateData);
return this.courseRepository.findOne({ where: { id } });
}
async deleteCourse(id: number) {
await this.courseRepository.delete(id);
}
async getOrders() {
return this.orderRepository.find({
relations: ['user', 'course'],
order: { createdAt: 'DESC' },
});
}
async getStatistics(startDate?: string, endDate?: string) {
const query = this.orderRepository.createQueryBuilder('order')
.select('order.orderType', 'orderType')
.addSelect('SUM(order.amount)', 'total')
.where('order.status = :status', { status: OrderStatus.PAID });
if (startDate) {
query.andWhere('order.createdAt >= :startDate', { startDate: new Date(startDate) });
}
if (endDate) {
const end = new Date(endDate);
end.setHours(23, 59, 59, 999);
query.andWhere('order.createdAt <= :endDate', { endDate: end });
}
const results = await query.groupBy('order.orderType').getRawMany();
const stats = {
vipAmount: 0,
donationAmount: 0,
purchaseAmount: 0,
};
results.forEach(row => {
const total = Number(row.total) || 0;
if (row.orderType === OrderType.VIP) {
stats.vipAmount += total;
} else if (row.orderType === OrderType.DONATION) {
stats.donationAmount += total;
} else if (row.orderType === OrderType.PURCHASE) {
stats.purchaseAmount += total;
}
});
return stats;
}
async getStatisticsDetails(type: string, startDate?: string, endDate?: string) {
const query = this.orderRepository.createQueryBuilder('order')
.leftJoinAndSelect('order.user', 'user')
.leftJoinAndSelect('order.course', 'course')
.where('order.status = :status', { status: OrderStatus.PAID });
if (type && type !== 'all') {
query.andWhere('order.orderType = :type', { type });
}
if (startDate) {
query.andWhere('order.createdAt >= :startDate', { startDate: new Date(startDate) });
}
if (endDate) {
const end = new Date(endDate);
end.setHours(23, 59, 59, 999);
query.andWhere('order.createdAt <= :endDate', { endDate: end });
}
return query.orderBy('order.createdAt', 'DESC').getMany();
}
}
|