From 8826db43bd7ac5c050fd5a3dfff4c1076b3b0139 Mon Sep 17 00:00:00 2001 From: Administrator <625580134@qq.com> Date: Mon, 17 May 2021 11:55:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/chickens/chickens.controller.ts | 32 ++++++------ src/chickens/chickens.module.ts | 8 ++- src/chickens/chickens.service.ts | 6 +-- src/common/utils/login.interceptor.ts | 2 +- src/goods/goods.controller.ts | 2 +- src/goods/goods.service.ts | 4 +- .../invitedstatistics.controller.ts | 40 +++++++------- src/orderlog/dto/create-orderlog.dto.ts | 8 ++- src/orderlog/entities/orderlog.entity.ts | 6 +++ src/orderlog/orderlog.controller.ts | 24 ++++----- src/user/dto/update-user.dto.ts | 3 +- src/user/user.module.ts | 6 ++- src/user/user.service.ts | 52 +++++++++++++++---- 13 files changed, 118 insertions(+), 75 deletions(-) diff --git a/src/chickens/chickens.controller.ts b/src/chickens/chickens.controller.ts index d5e5296..18f468a 100644 --- a/src/chickens/chickens.controller.ts +++ b/src/chickens/chickens.controller.ts @@ -51,21 +51,21 @@ export class ChickensController { return SuccessResult(init); } - //小鸡信息查询 - @ApiOperation({ summary: '小鸡信息查询' }) - @UsePipes(new HttpValidationPipe()) - @UseInterceptors(LoggingInterceptor) - @ApiImplicitQuery({ - name: 'userid', - required: true, - description: '当前用户ID', - type: Number, - }) - @Get() - async query(@Query('userid') userid: number) { - //小鸡数据初始化 - const res = await this.chickensService.chickensInfo(userid); - return SuccessResult(res); - } + // //小鸡信息查询 + // @ApiOperation({ summary: '小鸡信息查询' }) + // @UsePipes(new HttpValidationPipe()) + // @UseInterceptors(LoggingInterceptor) + // @ApiImplicitQuery({ + // name: 'userid', + // required: true, + // description: '当前用户ID', + // type: Number, + // }) + // @Get() + // async query(@Query('userid') userid: number) { + // //小鸡数据初始化 + // const res = await this.chickensService.chickensInfo(userid); + // return SuccessResult(res); + // } } diff --git a/src/chickens/chickens.module.ts b/src/chickens/chickens.module.ts index a6e4c07..b2c6aef 100644 --- a/src/chickens/chickens.module.ts +++ b/src/chickens/chickens.module.ts @@ -7,14 +7,18 @@ import { UserModule } from 'src/user/user.module'; import { CacheService } from 'src/redis/redis'; import { OrderlogService } from 'src/orderlog/orderlog.service'; import { OrderlogEntity } from 'src/orderlog/entities/orderlog.entity'; +import { UserService } from 'src/user/user.service'; +import { UserEntity } from 'src/user/entities/user.entity'; +import { GoodsService } from 'src/goods/goods.service'; +import { GoodsEntity } from 'src/goods/entities/good.entity'; @Module({ imports: [ - TypeOrmModule.forFeature([ChickenEntity,OrderlogEntity]), + TypeOrmModule.forFeature([ChickenEntity,OrderlogEntity,UserEntity,GoodsEntity]), UserModule, ], controllers: [ChickensController], exports: [ChickensService], - providers: [ChickensService, CacheService,OrderlogService] + providers: [ChickensService, CacheService,OrderlogService,UserService,GoodsService] }) export class ChickensModule { } diff --git a/src/chickens/chickens.service.ts b/src/chickens/chickens.service.ts index 3e26900..50b3883 100644 --- a/src/chickens/chickens.service.ts +++ b/src/chickens/chickens.service.ts @@ -30,12 +30,11 @@ export class ChickensService { //小鸡信息 const info = await this.chickensRepository.createQueryBuilder(prefix) .select().where(`userid=:userid`, { userid: userid }).getRawOne(); - console.log(info); if (!info) return CustomResult(CODE.CODE_PLAYER_INFO_ERR); //小鸡穿戴套装信息 if (info.chick_ck_suitid == 0) info['suit_info'] = {}; const suit = await this.orderService.paidGoodsInfo(info.chick_suitid, info.chick_userid); - console.log(suit); + info['suit_info'] = suit; return info; } @@ -43,7 +42,4 @@ export class ChickensService { return `This action updates a #${id} chicken`; } - remove(id: number) { - return `This action removes a #${id} chicken`; - } } diff --git a/src/common/utils/login.interceptor.ts b/src/common/utils/login.interceptor.ts index 766afac..17b2a23 100644 --- a/src/common/utils/login.interceptor.ts +++ b/src/common/utils/login.interceptor.ts @@ -15,7 +15,7 @@ export class LoggingInterceptor implements NestInterceptor { const userid = request.query.userid; const token = await this.cacheRedis.get(getUserTokenKey(userid)); - console.log(token) + const now = Date.now(); if (!token) { return throwError(new HttpException('登录信息错误', HttpStatus.SEE_OTHER)) diff --git a/src/goods/goods.controller.ts b/src/goods/goods.controller.ts index daabb16..cb37ab9 100644 --- a/src/goods/goods.controller.ts +++ b/src/goods/goods.controller.ts @@ -51,7 +51,7 @@ export class GoodsController { }) @Get(':id') async findOne(@Query('id') id: number) { - return this.goodsService.findOne(+id, GoodsStatus.HIDE); + return this.goodsService.findOne(+id, [GoodsStatus.SELLING,GoodsStatus.SELLEND]); } } diff --git a/src/goods/goods.service.ts b/src/goods/goods.service.ts index ebe25c2..4225177 100644 --- a/src/goods/goods.service.ts +++ b/src/goods/goods.service.ts @@ -40,10 +40,10 @@ export class GoodsService { } /** 查询商品详情 */ - async findOne(id: number, status?: string) { + async findOne(id: number, status?: string[]) { let findBuilder = this.goodsRepository.createQueryBuilder(prefix); //商品状态(API接口排除隐藏的) - if (status) findBuilder = findBuilder.andWhere(`status != :status`, { status: status }); + if (status) findBuilder = findBuilder.andWhere(`status IN (:status)`, { status: status }); const returnData = await findBuilder.select().andWhere(`id=:id`, { id: id }).getRawOne(); if (!returnData) return CustomResult(CODE.CODE_GOODS_INFO_ERR); return SuccessResult(returnData); diff --git a/src/invitedstatistics/invitedstatistics.controller.ts b/src/invitedstatistics/invitedstatistics.controller.ts index 1f87320..6ccebe1 100644 --- a/src/invitedstatistics/invitedstatistics.controller.ts +++ b/src/invitedstatistics/invitedstatistics.controller.ts @@ -9,28 +9,28 @@ import { ApiTags } from '@nestjs/swagger'; export class InvitedstatisticsController { constructor(private readonly invitedstatisticsService: InvitedstatisticsService) {} - @Post() - create(@Body() createInvitedstatisticDto: CreateInvitedstatisticDto) { - return this.invitedstatisticsService.create(createInvitedstatisticDto); - } + // @Post() + // create(@Body() createInvitedstatisticDto: CreateInvitedstatisticDto) { + // return this.invitedstatisticsService.create(createInvitedstatisticDto); + // } - @Get() - findAll() { - return this.invitedstatisticsService.findAll(); - } + // @Get() + // findAll() { + // return this.invitedstatisticsService.findAll(); + // } - @Get(':id') - findOne(@Param('id') id: string) { - return this.invitedstatisticsService.findOne(+id); - } + // @Get(':id') + // findOne(@Param('id') id: string) { + // return this.invitedstatisticsService.findOne(+id); + // } - @Patch(':id') - update(@Param('id') id: string, @Body() updateInvitedstatisticDto: UpdateInvitedstatisticDto) { - return this.invitedstatisticsService.update(+id, updateInvitedstatisticDto); - } + // @Patch(':id') + // update(@Param('id') id: string, @Body() updateInvitedstatisticDto: UpdateInvitedstatisticDto) { + // return this.invitedstatisticsService.update(+id, updateInvitedstatisticDto); + // } - @Delete(':id') - remove(@Param('id') id: string) { - return this.invitedstatisticsService.remove(+id); - } + // @Delete(':id') + // remove(@Param('id') id: string) { + // return this.invitedstatisticsService.remove(+id); + // } } diff --git a/src/orderlog/dto/create-orderlog.dto.ts b/src/orderlog/dto/create-orderlog.dto.ts index f29e045..d14bf39 100644 --- a/src/orderlog/dto/create-orderlog.dto.ts +++ b/src/orderlog/dto/create-orderlog.dto.ts @@ -1,4 +1,4 @@ -import { IsNotEmpty, IsInt, IsIn } from "class-validator"; +import { IsNotEmpty, IsInt, IsIn, IsString } from "class-validator"; import { GoodsTypes } from "src/common/const"; export class CreateOrderlogDto { @@ -22,6 +22,12 @@ export class CreateOrderlogDto { @IsInt() goods_attribute: number; + @IsString() + goods_desc: string; + + @IsString() + goods_url: string; + buy_time?: Date; create_time?: Date; update_time?: Date; diff --git a/src/orderlog/entities/orderlog.entity.ts b/src/orderlog/entities/orderlog.entity.ts index 7119079..5745fdb 100644 --- a/src/orderlog/entities/orderlog.entity.ts +++ b/src/orderlog/entities/orderlog.entity.ts @@ -28,6 +28,12 @@ export class OrderlogEntity { @Column({ comment: '商品属性(如:饲料(克),背景/道具/套装(天)', type: 'bigint' }) goods_attribute: number; + @Column({ comment: '商品描述', type: 'varchar', default: '' }) + goods_desc: string; + + @Column({ comment: '商品地址(图片路径)', type: 'varchar', default: '' }) + goods_url: string; + @CreateDateColumn({ comment: '购买时间', type: 'timestamp' }) buy_time: Date; diff --git a/src/orderlog/orderlog.controller.ts b/src/orderlog/orderlog.controller.ts index fa2ae0f..d97c7cc 100644 --- a/src/orderlog/orderlog.controller.ts +++ b/src/orderlog/orderlog.controller.ts @@ -9,19 +9,19 @@ import { ApiTags } from '@nestjs/swagger'; export class OrderlogController { constructor(private readonly orderlogService: OrderlogService) {} - @Post() - create(@Body() createOrderlogDto: CreateOrderlogDto) { - return this.orderlogService.create(createOrderlogDto); - } + // @Post() + // create(@Body() createOrderlogDto: CreateOrderlogDto) { + // return this.orderlogService.create(createOrderlogDto); + // } - @Patch(':id') - update(@Param('id') id: string, @Body() updateOrderlogDto: UpdateOrderlogDto) { - return this.orderlogService.update(+id, updateOrderlogDto); - } + // @Patch(':id') + // update(@Param('id') id: string, @Body() updateOrderlogDto: UpdateOrderlogDto) { + // return this.orderlogService.update(+id, updateOrderlogDto); + // } - @Delete(':id') - remove(@Param('id') id: string) { - return this.orderlogService.remove(+id); - } + // @Delete(':id') + // remove(@Param('id') id: string) { + // return this.orderlogService.remove(+id); + // } } diff --git a/src/user/dto/update-user.dto.ts b/src/user/dto/update-user.dto.ts index 32fa38c..0397aad 100644 --- a/src/user/dto/update-user.dto.ts +++ b/src/user/dto/update-user.dto.ts @@ -4,9 +4,8 @@ import { UserOnlieStatus } from 'src/common/const'; import { CreateUserDto } from './create-user.dto'; export class UpdateUserDto extends PartialType(CreateUserDto) { - @IsNotEmpty({ message: '用户状态不能为空' }) @IsIn(Object.values(UserOnlieStatus)) - online_status: string; + online_status?: string; update_time?:Date; } diff --git a/src/user/user.module.ts b/src/user/user.module.ts index 1eac3bd..9302087 100644 --- a/src/user/user.module.ts +++ b/src/user/user.module.ts @@ -9,14 +9,16 @@ import { OrderlogEntity } from 'src/orderlog/entities/orderlog.entity'; import { OrderlogService } from 'src/orderlog/orderlog.service'; import { ConfigModule } from '@nestjs/config'; import { CacheService } from 'src/redis/redis'; +import { ChickensService } from 'src/chickens/chickens.service'; +import { ChickenEntity } from 'src/chickens/entities/chicken.entity'; @Module({ imports: [ ConfigModule, - TypeOrmModule.forFeature([UserEntity, GoodsEntity, OrderlogEntity]) + TypeOrmModule.forFeature([UserEntity, GoodsEntity, OrderlogEntity,ChickenEntity]) ], controllers: [UserController], exports: [UserService], - providers: [UserService, GoodsService, OrderlogService,CacheService] + providers: [UserService, GoodsService, OrderlogService,CacheService,ChickensService] }) export class UserModule { } diff --git a/src/user/user.service.ts b/src/user/user.service.ts index 17ea307..61cd66a 100644 --- a/src/user/user.service.ts +++ b/src/user/user.service.ts @@ -1,5 +1,6 @@ import { Injectable, Logger } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; +import { ChickensService } from 'src/chickens/chickens.service'; import { CODE } from 'src/common/code'; import { GoodsStatus, GoodsTypes, UserOnlieStatus } from 'src/common/const'; import { ExternalApiService } from 'src/common/utils/external-api'; @@ -9,7 +10,7 @@ import { CreateOrderlogDto } from 'src/orderlog/dto/create-orderlog.dto'; import { OrderlogService } from 'src/orderlog/orderlog.service'; import { getUserShoppingKey, getUserTokenKey } from 'src/redis/keys'; import { CacheService } from 'src/redis/redis'; -import { Repository } from 'typeorm'; +import { getManager, Repository } from 'typeorm'; import { BuyGoodsUserDto } from './dto/buygoods-user.dto'; import { CreateUserDto } from './dto/create-user.dto'; import { UpdateUserDto } from './dto/update-user.dto'; @@ -26,6 +27,7 @@ export class UserService { private readonly goodsService: GoodsService, private readonly orderLogService: OrderlogService, private readonly cacheService: CacheService, + private readonly chickensService: ChickensService, ) { } /**查询用户信息,没有则初始化 */ @@ -34,7 +36,13 @@ export class UserService { await this.initToken(createUserDto); //查询用户信息 const userInfo = await this.findOne(createUserDto.userid); - if (userInfo) return SuccessResult(userInfo); + if (userInfo) { + //小鸡信息 + userInfo['chickens'] = {}; + const chick = await this.chickensService.chickensInfo(createUserDto.userid); + if (chick) userInfo['chickens'] = chick; + return SuccessResult(userInfo); + } //有信息则返回 const appUserInfo = await ExternalApiService.getAppUserInfo(createUserDto.userid, createUserDto.token); //用户信息异常处理 @@ -96,31 +104,36 @@ export class UserService { /** 用户购买商品逻辑 */ async buy(buyGoodsUserDto: BuyGoodsUserDto) { + const { userid, goodsid, goods_count } = buyGoodsUserDto; //查询APP用户信息 - const token = await this.cacheService.get(getUserTokenKey(buyGoodsUserDto.userid)); - const appUserInfo = await ExternalApiService.getAppUserInfo(buyGoodsUserDto.userid, token); + const token = await this.cacheService.get(getUserTokenKey(userid)); + const appUserInfo = await ExternalApiService.getAppUserInfo(userid, token); if (appUserInfo?.code != 200) return appUserInfo; const user = appUserInfo.data; //查询商品信息 - const goodsInfo: any = await this.goodsService.findOne(buyGoodsUserDto.goodsid, GoodsStatus.HIDE); + const goodsInfo: any = await this.goodsService.findOne(goodsid, [GoodsStatus.SELLING]); if (goodsInfo?.code != 200) return CustomResult(CODE.CODE_GOODS_INFO_ERR); const goods = goodsInfo.data; //判断用户金额是否满足条件 (商品价格*商品数量) - const payCoin = goods.goods_price * buyGoodsUserDto.goods_count;//应支付价格 + const payCoin = goods.goods_price * goods_count;//应支付价格 if (user.coin < payCoin) return CustomResult(CODE.CODE_APPUSER_COIN_NOT_ENOUGHT_ERR); //扣减加锁 锁几秒 - const locKeys = getUserShoppingKey(buyGoodsUserDto.userid, buyGoodsUserDto.goodsid); + const locKeys = getUserShoppingKey(userid, goodsid); const queryLock = await this.cacheService.get(locKeys); if (queryLock) return CustomResult(CODE.CODE_USER_PAY_LOCK_ERR); - await this.cacheService.set(locKeys, true, 5); + await this.cacheService.set(locKeys, true, 3); //用户金额扣减 - const subUserInfo = await ExternalApiService.subAppUserCoin(buyGoodsUserDto.userid, token, payCoin); + const subUserInfo = await ExternalApiService.subAppUserCoin(userid, token, payCoin); if (subUserInfo?.code != 200) return CustomResult(CODE.CODE_APPUSER_SUB_COIN_ERR); + // 购买饲料则要发放饲料 + if (goods.goods_type === GoodsTypes.FODDER) { + await this.insertFodder(userid, goods.goods_attribute * goods_count); + } //发放完成记录日志 const orderInfo: CreateOrderlogDto = { @@ -128,14 +141,31 @@ export class UserService { goods_id: goods.goods_id, goods_type: goods.goods_type, goods_price: goods.goods_price, + goods_desc: goods.goods_desc || '', + goods_url: goods.goods_image || '', goods_attribute: goods.goods_attribute } const orderRes = await this.orderLogService.create(orderInfo); logger.debug(`购买订单日志结果: ${JSON.stringify(orderRes)}`); if (orderRes.code != 200) return CustomResult(CODE.CODE_GOODS_DEAL_ERR); - return SuccessResult(orderRes.data); + //返回用户信息 + const userInfo = await this.userRepository.createQueryBuilder(prefix).select().where('userid=:userid', { userid: userid }).getRawOne(); + return SuccessResult(userInfo); } - + /** + * 饲料领取 + */ + private async insertFodder(userid: number, fodder: number) { + await getManager().transaction(async manager => { + const user = await manager.findOne(UserEntity, { userid: userid }); + if (user) { + user.update_time = new Date(); + user.fodder_balance = Number(user.fodder_balance) + fodder; + await manager.save(user); + logger.debug(`用户 ${userid} 购买了 ${fodder} g饲料`); + } + }) + } }