From 3f249e399659dcd09d3fe58071b146e50e45c17f Mon Sep 17 00:00:00 2001
From: wangzhibo <wangzhibo@shsening.com>
Date: 星期五, 14 八月 2026 21:56:15 +0800
Subject: [PATCH] 为小程序端增加接口
---
src/modules/shop/service/transaction.ts | 142 ++++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 139 insertions(+), 3 deletions(-)
diff --git a/src/modules/shop/service/transaction.ts b/src/modules/shop/service/transaction.ts
index e8304cd..26639b5 100644
--- a/src/modules/shop/service/transaction.ts
+++ b/src/modules/shop/service/transaction.ts
@@ -1,8 +1,9 @@
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';
/**
* 纰崇Н鍒嗘祦杞褰�
@@ -12,4 +13,139 @@
@InjectEntityModel(RecycleTransactionEntity)
recycleTransactionEntity: Repository<RecycleTransactionEntity>;
-}
\ No newline at end of file
+ @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;
+ }
+}
--
Gitblit v1.9.1