| New file |
| | |
| | | import { Inject } from "@midwayjs/core"; |
| | | import { CoolController, BaseController } from "@cool-midway/core"; |
| | | import { RecycleAppointmentEntity } from "../../entity/recycleappointment"; |
| | | import { RecycleAppointmentService } from "../../service/recycleappointment"; |
| | | |
| | | /** |
| | | * 回收预约-应用端 |
| | | */ |
| | | @CoolController({ |
| | | api: ["add", "info", "page"], |
| | | entity: RecycleAppointmentEntity, |
| | | service: RecycleAppointmentService, |
| | | pageQueryOp: { |
| | | fieldEq: ["a.status", "a.campusUserId"], |
| | | addOrderBy: { |
| | | createTime: "DESC", |
| | | }, |
| | | where: async (ctx) => { |
| | | const { tab } = ctx.request.body; |
| | | if (tab === "done") { |
| | | return [["a.status = :st", { st: "已完成" }]]; |
| | | } |
| | | if (tab === "booked") { |
| | | return [["a.status != :st", { st: "已完成" }]]; |
| | | } |
| | | return []; |
| | | }, |
| | | }, |
| | | }) |
| | | export class AppRecycleAppointmentController extends BaseController { |
| | | @Inject() |
| | | recycleAppointmentService: RecycleAppointmentService; |
| | | } |
| | |
| | | entity: RecycleOrderEntity, |
| | | service: RecycleOrderService, |
| | | pageQueryOp: { |
| | | fieldEq: ["a.status"], |
| | | where: async (ctx) => { |
| | | return [ |
| | | ["a.campusUserId = :userId", { userId: ctx.user.id }] |
| | | ]; |
| | | } |
| | | fieldEq: ["a.status","a.campusUserId","a.orderType"], |
| | | addOrderBy:{createTime:'DESC'} |
| | | } |
| | | }) |
| | | export class AppRecycleOrderController extends BaseController { |
| | |
| | | import { CoolController, BaseController } from "@cool-midway/core"; |
| | | import { RecycleOrderItemEntity } from "../../entity/recycleorderitem"; |
| | | import { RecycleOrderItemService } from "../../service/recycleorderitem"; |
| | | import { RecycleItemEntity } from "../../entity/recycleitem"; |
| | | |
| | | /** |
| | | * 回收订单条目-应用端 |
| | |
| | | entity: RecycleOrderItemEntity, |
| | | service: RecycleOrderItemService, |
| | | pageQueryOp: { |
| | | select: ["a.*", "b.mainPic"], |
| | | fieldEq: ["a.orderId"], |
| | | join: [ |
| | | { |
| | | entity: RecycleItemEntity, |
| | | alias: "b", |
| | | condition: "a.itemId = b.id", |
| | | }, |
| | | ], |
| | | fieldEq: ["a.orderId"] |
| | | }, |
| | | }) |
| | | export class AppRecycleOrderitemController extends BaseController { |
| New file |
| | |
| | | import { Body, Inject, Post } from '@midwayjs/core'; |
| | | import { CoolController, BaseController } from '@cool-midway/core'; |
| | | import { RecycleTransactionEntity } from '../../entity/transaction'; |
| | | import { RecycleTransactionService } from '../../service/transaction'; |
| | | |
| | | /** |
| | | * 碳积分流水-应用端 |
| | | */ |
| | | @CoolController({ |
| | | api: ['page'], |
| | | entity: RecycleTransactionEntity, |
| | | service: RecycleTransactionService, |
| | | pageQueryOp: { |
| | | addOrderBy: { |
| | | createTime: 'DESC', |
| | | }, |
| | | }, |
| | | }) |
| | | export class AppRecycleTransactionController extends BaseController { |
| | | @Inject() |
| | | recycleTransactionService: RecycleTransactionService; |
| | | |
| | | @Post('/checkUser', { summary: '校验转赠接收人' }) |
| | | async checkUser(@Body('unionid') unionid: string) { |
| | | return this.ok(await this.recycleTransactionService.checkUser(unionid)); |
| | | } |
| | | |
| | | @Post('/transfer', { summary: '碳积分转赠' }) |
| | | async transfer( |
| | | @Body() |
| | | body: { |
| | | fromUnionid: string; |
| | | toUnionid: string; |
| | | amount: number; |
| | | } |
| | | ) { |
| | | return this.ok(await this.recycleTransactionService.transfer(body)); |
| | | } |
| | | } |
| | |
| | | }); |
| | | |
| | | if (order?.campusUserId) { |
| | | await UserInfoEntity.update( |
| | | await UserInfoEntity.getRepository().increment( |
| | | { unionid: order.campusUserId }, |
| | | { |
| | | carbonBalance: () => `carbonBalance + ${totalCarbonPoint}` |
| | | } |
| | | 'carbonBalance', |
| | | totalCarbonPoint |
| | | ); |
| | | } |
| | | |
| | |
| | | import { Provide } from '@midwayjs/core'; |
| | | import { BaseService } from '@cool-midway/core'; |
| | | import { BaseService, CoolCommException, CoolTransaction } from '@cool-midway/core'; |
| | | import { InjectEntityModel } from '@midwayjs/typeorm'; |
| | | import { Repository } from 'typeorm'; |
| | | import { QueryRunner, Repository } from 'typeorm'; |
| | | import { RecycleTransactionEntity } from '../entity/transaction'; |
| | | import { UserInfoEntity } from '../../user/entity/info'; |
| | | |
| | | /** |
| | | * 碳积分流转记录 |
| | |
| | | @InjectEntityModel(RecycleTransactionEntity) |
| | | recycleTransactionEntity: Repository<RecycleTransactionEntity>; |
| | | |
| | | @InjectEntityModel(UserInfoEntity) |
| | | userInfoEntity: Repository<UserInfoEntity>; |
| | | |
| | | /** |
| | | * App 端分页:转赠 tab 只看赠送/接受,收支 tab 看其余类型 |
| | | */ |
| | | async page(query: any) { |
| | | const { userId, tab, page = 1, size = 10 } = query || {}; |
| | | const find = this.recycleTransactionEntity.createQueryBuilder('a'); |
| | | |
| | | if (userId) { |
| | | find.andWhere('a.userId = :uid', { uid: String(userId) }); |
| | | } |
| | | |
| | | if (tab === 'io') { |
| | | find.andWhere('(a.sourceType != :t1 AND a.sourceType != :t2)', { |
| | | t1: '赠送', |
| | | t2: '接受', |
| | | }); |
| | | } else { |
| | | find.andWhere('(a.sourceType = :t1 OR a.sourceType = :t2)', { |
| | | t1: '赠送', |
| | | t2: '接受', |
| | | }); |
| | | } |
| | | |
| | | find.orderBy('a.createTime', 'DESC'); |
| | | find.addOrderBy('a.id', 'DESC'); |
| | | |
| | | console.log(find) |
| | | |
| | | return this.entityRenderPage(find, { ...query, page, size }, false); |
| | | } |
| | | |
| | | async checkUser(unionid: string) { |
| | | if (!unionid) { |
| | | throw new CoolCommException('请选择系统用户作为接受转赠对象'); |
| | | } |
| | | const user = await this.userInfoEntity.findOneBy({ unionid }); |
| | | if (!user) { |
| | | throw new CoolCommException('请选择系统用户作为接受转赠对象'); |
| | | } |
| | | return { |
| | | unionid: user.unionid, |
| | | nickName: user.nickName, |
| | | phone: user.phone, |
| | | }; |
| | | } |
| | | |
| | | @CoolTransaction() |
| | | async transfer( |
| | | param: { fromUnionid: string; toUnionid: string; amount: number }, |
| | | queryRunner?: QueryRunner |
| | | ) { |
| | | if (!queryRunner) { |
| | | throw new CoolCommException('事务启动失败'); |
| | | } |
| | | |
| | | const fromUnionid = String(param.fromUnionid || '').trim(); |
| | | const toUnionid = String(param.toUnionid || '').trim(); |
| | | const amount = Number(param.amount); |
| | | |
| | | if (!fromUnionid || !toUnionid) { |
| | | throw new CoolCommException('转赠参数不完整'); |
| | | } |
| | | if (fromUnionid === toUnionid) { |
| | | throw new CoolCommException('不能转赠给自己'); |
| | | } |
| | | if (!amount || amount <= 0) { |
| | | throw new CoolCommException('转赠金额必须大于0'); |
| | | } |
| | | |
| | | const manager = queryRunner.manager; |
| | | const fromUser = await manager.findOne(UserInfoEntity, { |
| | | where: { unionid: fromUnionid }, |
| | | }); |
| | | const toUser = await manager.findOne(UserInfoEntity, { |
| | | where: { unionid: toUnionid }, |
| | | }); |
| | | |
| | | if (!toUser) { |
| | | throw new CoolCommException('请选择系统用户作为接受转赠对象'); |
| | | } |
| | | if (!fromUser) { |
| | | throw new CoolCommException('当前用户不存在'); |
| | | } |
| | | if (Number(fromUser.carbonBalance || 0) < amount) { |
| | | throw new CoolCommException('碳积分不足'); |
| | | } |
| | | |
| | | const dec = await manager |
| | | .createQueryBuilder() |
| | | .update(UserInfoEntity) |
| | | .set({ carbonBalance: () => `carbonBalance - ${amount}` }) |
| | | .where('unionid = :unionid AND carbonBalance >= :amount', { |
| | | unionid: fromUnionid, |
| | | amount, |
| | | }) |
| | | .execute(); |
| | | |
| | | if (!dec.affected) { |
| | | throw new CoolCommException('碳积分不足'); |
| | | } |
| | | |
| | | await manager |
| | | .createQueryBuilder() |
| | | .update(UserInfoEntity) |
| | | .set({ carbonBalance: () => `carbonBalance + ${amount}` }) |
| | | .where('unionid = :unionid', { unionid: toUnionid }) |
| | | .execute(); |
| | | |
| | | const fromName = fromUser.nickName || fromUser.phone || fromUnionid; |
| | | const toName = toUser.nickName || toUser.phone || toUnionid; |
| | | |
| | | await manager.save(RecycleTransactionEntity, [ |
| | | { |
| | | userId: fromUnionid, |
| | | departmentId: fromUser.departmentId, |
| | | type: '支出', |
| | | amount, |
| | | sourceType: '赠送', |
| | | remark: `转赠给${toName}`, |
| | | }, |
| | | { |
| | | userId: toUnionid, |
| | | departmentId: toUser.departmentId, |
| | | type: '收入', |
| | | amount, |
| | | sourceType: '接受', |
| | | remark: `从${fromName}获赠`, |
| | | }, |
| | | ]); |
| | | |
| | | return true; |
| | | } |
| | | } |
| | |
| | | ) |
| | | ); |
| | | } |
| | | |
| | | @Post('/dashboard', { summary: '获取用户碳积分信息' }) |
| | | async dashboard(@Body() body) { |
| | | console.log(body) |
| | | const {unionid} = body; |
| | | return this.ok( |
| | | await this.userInfoService.dashboard( |
| | | unionid |
| | | ) |
| | | ); |
| | | } |
| | | } |
| | |
| | | import { UserInfoEntity } from '../entity/info'; |
| | | import { UserSmsService } from './sms'; |
| | | import { UserWxService } from './wx'; |
| | | import { RecycleOrderEntity } from '../../shop/entity/recycleorder'; |
| | | |
| | | |
| | | /** |
| | | * 用户信息 |
| | |
| | | export class UserInfoService extends BaseService { |
| | | @InjectEntityModel(UserInfoEntity) |
| | | userInfoEntity: Repository<UserInfoEntity>; |
| | | |
| | | @InjectEntityModel(RecycleOrderEntity) |
| | | recycleOrderEntity: Repository<RecycleOrderEntity>; |
| | | |
| | | @Inject() |
| | | pluginService: PluginService; |
| | |
| | | } |
| | | await this.userInfoEntity.update({ id: userId }, { phone }); |
| | | } |
| | | |
| | | /** |
| | | * 获取用户的碳积分信息 |
| | | * @param unionid |
| | | */ |
| | | async dashboard(unionid) { |
| | | const info = await this.userInfoEntity.findOneBy({ unionid: unionid }); |
| | | if (!info) { |
| | | throw new CoolCommException('查询无结果'); |
| | | } |
| | | |
| | | const count_ = await this.recycleOrderEntity.countBy({ |
| | | campusUserId: unionid |
| | | }); |
| | | |
| | | |
| | | return { carbonPoints: info.carbonBalance, recycleCount:count_, overPercent: 90 } |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | } |