wuyi пре 1 година
родитељ
комит
e9f9b7edd9

+ 3 - 1
src/app.module.ts

@@ -17,6 +17,7 @@ import { RcsNumberModule } from './rcs-number/rcs-number.module'
 import { TaskModule } from './task/task.module'
 import { PhoneListModule } from './phone-list/phone-list.module'
 import { DeviceModule } from './device/device.module'
+import { BalanceModule } from './balance/balance.module'
 
 @Module({
     imports: [
@@ -73,7 +74,8 @@ import { DeviceModule } from './device/device.module'
         RcsNumberModule,
         TaskModule,
         PhoneListModule,
-        DeviceModule
+        DeviceModule,
+        BalanceModule
     ],
     controllers: [],
     providers: [

+ 29 - 0
src/balance/balance.controller.ts

@@ -0,0 +1,29 @@
+import { Controller, Get, Param } from '@nestjs/common'
+import { BalanceService } from './balance.service'
+
+@Controller('balance')
+export class BalanceController {
+    constructor(private readonly balanceService: BalanceService) {
+    }
+
+    @Get('/:id')
+    async getBalance(@Param('id') id: string) {
+        return await this.balanceService.findBalanceByUserId(parseInt(id))
+    }
+
+    @Get('/recharge/:id/:amount')
+    async recharge(@Param('id') id: string, @Param('amount') amount: number) {
+        return await this.balanceService.rechargeBalance(parseInt(id), amount)
+    }
+
+    @Get('/records/:id')
+    async findRecordsByUserId(@Param('id') id: string) {
+        return await this.balanceService.findRecordsByUserId(parseInt(id))
+    }
+
+    @Get('/updateRate/:id/:rate')
+    async updateRate(@Param('id') id: string, @Param('rate') rate: number) {
+        return await this.balanceService.updateRate(parseInt(id), rate)
+    }
+
+}

+ 17 - 0
src/balance/balance.module.ts

@@ -0,0 +1,17 @@
+import { Module } from '@nestjs/common'
+import { TypeOrmModule } from '@nestjs/typeorm'
+import { Balance } from './entities/balance.entities'
+import { BalanceRecord } from './entities/balance-record.entities'
+import { BalanceController } from './balance.controller'
+import { BalanceService } from './balance.service'
+import { UsersModule } from '../users/users.module'
+
+@Module({
+    imports: [
+        TypeOrmModule.forFeature([Balance,BalanceRecord]),
+        UsersModule
+    ],
+    controllers: [BalanceController],
+    providers: [BalanceService]
+})
+export class BalanceModule{}

+ 91 - 0
src/balance/balance.service.ts

@@ -0,0 +1,91 @@
+import { Injectable, Logger } from '@nestjs/common'
+import { Repository } from 'typeorm'
+import { Balance } from './entities/balance.entities'
+import { InjectRepository } from '@nestjs/typeorm'
+import { BalanceRecord, BalanceType } from './entities/balance-record.entities'
+import { UsersService } from '../users/users.service'
+
+@Injectable()
+export class BalanceService {
+
+    constructor(
+        @InjectRepository(Balance)
+        private balanceRepository: Repository<Balance>,
+        @InjectRepository(BalanceRecord)
+        private recordRepository: Repository<BalanceRecord>,
+        private readonly usersService: UsersService
+    ) {
+    }
+
+    async findBalanceByUserId(userId: number): Promise<Balance> {
+        return await this.balanceRepository.findOne({
+            select: {
+                userId: true,
+                currentBalance: true
+            },
+            where: {
+                userId: userId
+            }
+        })
+    }
+
+    async rechargeBalance(userId: number, amount: number): Promise<string> {
+        try {
+            // 获取余额
+            const balance = await this.balanceRepository.findOne({
+                where: {
+                    userId: userId
+                }
+            })
+
+            const currentBalanceNum = parseFloat(String(balance.currentBalance))
+
+            balance.currentBalance = currentBalanceNum + amount
+            balance.totalBalance = parseFloat(String(balance.totalBalance)) + amount
+
+            console.log('balance:', balance)
+            await this.balanceRepository.save(balance)
+
+            // 充值记录
+            const record = new BalanceRecord()
+            record.userId = userId
+            record.balanceId = balance.id
+            record.amount = amount
+            record.type = BalanceType.RECHARGE
+            await this.recordRepository.save(record)
+
+            return 'recharge success!'
+        } catch (e) {
+            Logger.error('Error recharge ', e, 'RcsService')
+            return 'recharge fail!'
+        }
+    }
+
+    async findRecordsByUserId(userId: number): Promise<BalanceRecord[]> {
+        return await this.recordRepository.find({
+            where: {
+                userId: userId
+            }
+        })
+    }
+
+    async updateRate(userId: number, rate: number): Promise<string> {
+        try {
+            const balance = await this.balanceRepository.findOne({
+                where: {
+                    userId: userId
+                }
+            })
+
+            balance.rate = rate
+            await this.balanceRepository.save(balance)
+
+            return 'update rate success!'
+        } catch (e) {
+            Logger.error('Error rate ', e, 'RcsService')
+            return 'update rate fail!'
+        }
+    }
+
+
+}

+ 36 - 0
src/balance/entities/balance-record.entities.ts

@@ -0,0 +1,36 @@
+import { Column, CreateDateColumn, Entity, PrimaryGeneratedColumn } from 'typeorm'
+import { number } from 'yup'
+
+export enum BalanceType {
+    RECHARGE = 'recharge',
+    CONSUMPTION = 'consumption',
+    REFUND = 'refund',
+    OTHER = 'other'
+}
+
+@Entity()
+export class BalanceRecord {
+
+    @PrimaryGeneratedColumn()
+    id: number
+
+    @Column()
+    userId: number
+
+    @Column({ nullable: true })
+    balanceId: number
+
+    @Column({ type: 'enum', enum: BalanceType, nullable: false, default: BalanceType.RECHARGE })
+    type: BalanceType
+
+    @Column('decimal', {
+        precision: 10,
+        scale: 2,
+        default: 0.00
+    })
+    amount: number
+
+    @CreateDateColumn()
+    createDate: Date
+
+}

+ 33 - 0
src/balance/entities/balance.entities.ts

@@ -0,0 +1,33 @@
+import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'
+
+@Entity()
+export class Balance {
+
+    @PrimaryGeneratedColumn()
+    id: number
+
+    @Column()
+    userId: number
+
+    @Column('decimal', {
+        precision: 10,
+        scale: 2,
+        default: 0.00
+    })
+    currentBalance: number
+
+    @Column('decimal', {
+        precision: 10,
+        scale: 2,
+        default: 0.00
+    })
+    totalBalance: number
+
+    @Column('decimal', {
+        precision: 2,
+        scale: 2,
+        default: 0.00
+    })
+    rate: number
+
+}

+ 23 - 0
src/statistics/statistics.controller.ts

@@ -0,0 +1,23 @@
+import {
+    Controller,
+    Put,
+    Get,
+    Body,
+    Param,
+    HttpStatus,
+    NotFoundException,
+    Delete,
+    BadRequestException,
+    Req,
+    Post
+} from '@nestjs/common'
+import { StatisticsService } from './statistics.service'
+
+@Controller('statistics')
+export class StatisticsController {
+
+    constructor(private readonly statisticsService: StatisticsService) {}
+
+
+
+}

+ 23 - 0
src/statistics/statistics.module.ts

@@ -0,0 +1,23 @@
+import { forwardRef, Module } from '@nestjs/common'
+import { StatisticsController } from './statistics.controller'
+import { StatisticsService } from './statistics.service'
+import { TypeOrmModule } from '@nestjs/typeorm'
+import { Task } from '../task/entities/task.entity'
+import { TaskItem } from '../task/entities/task-item.entity'
+import { EventsModule } from '../events/events.module'
+import { PhoneListModule } from '../phone-list/phone-list.module'
+import { DeviceModule } from '../device/device.module'
+
+@Module({
+    imports: [
+        TypeOrmModule.forFeature([TaskItem]),
+        forwardRef(() => EventsModule),
+        PhoneListModule,
+        DeviceModule
+    ],
+    controllers: [StatisticsController],
+    providers: [StatisticsService],
+    exports: [StatisticsService]
+})
+export class StatisticsModule {
+}

+ 31 - 0
src/statistics/statistics.service.ts

@@ -0,0 +1,31 @@
+import { forwardRef, Inject, Injectable } from '@nestjs/common'
+import { InjectRepository } from '@nestjs/typeorm'
+import { TaskItem } from '../task/entities/task-item.entity'
+import { Repository } from 'typeorm'
+import { Task } from '../task/entities/task.entity'
+
+@Injectable()
+export class StatisticsService {
+    constructor(
+        @InjectRepository(Task)
+        private taskRepository: Repository<Task>,
+        @InjectRepository(TaskItem)
+        private taskItemRepository: Repository<TaskItem>
+    ) {
+    }
+
+    async getStatistics() {
+        // 查询TaskItem状态为success的数据
+        let taskItems = await this.taskItemRepository.find({
+            where: {
+                status: 'success'
+            }
+        })
+
+        return {
+            month: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
+            value: []
+        }
+    }
+
+}

+ 9 - 0
src/statistics/vo/statistics.vo.ts

@@ -0,0 +1,9 @@
+export class StatisticsVo {
+
+    readonly year: string
+
+    readonly moth: string
+
+    readonly value: string
+
+}