From 50e5cdbaceee71f000341a434620d10a87b582f5 Mon Sep 17 00:00:00 2001
From: wangzhibo <wangzhibo@shsening.com>
Date: 星期六, 15 八月 2026 17:12:59 +0800
Subject: [PATCH] 修改 用户属性表中userId从number到string

---
 src/modules/order/service/pay.ts |  113 ++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 104 insertions(+), 9 deletions(-)

diff --git a/src/modules/order/service/pay.ts b/src/modules/order/service/pay.ts
index 9f5d9f4..3fc8a61 100644
--- a/src/modules/order/service/pay.ts
+++ b/src/modules/order/service/pay.ts
@@ -1,14 +1,16 @@
 import { Init, Inject, Provide } from '@midwayjs/core';
-import { BaseService, CoolCommException } from '@cool-midway/core';
+import { BaseService, CoolCommException, CoolTransaction } from '@cool-midway/core';
 import { PluginService } from '../../plugin/service/info';
 import { OrderInfoService } from './info';
 import { UserWxService } from '../../user/service/wx';
 import BigNumber from 'bignumber.js';
 import { OrderInfoEntity } from '../entity/info';
 import { InjectEntityModel } from '@midwayjs/typeorm';
-import { Repository } from 'typeorm';
+import { QueryRunner, Repository } from 'typeorm';
 import { Action, OrderQueue } from '../queue/order';
 import { BaseSysParamService } from '../../base/service/sys/param';
+import { UserInfoEntity } from '../../user/entity/info';
+import { RecycleTransactionEntity } from '../../shop/entity/transaction';
 
 /**
  * 鏀粯
@@ -17,6 +19,12 @@
 export class OrderPayService extends BaseService {
   @InjectEntityModel(OrderInfoEntity)
   orderInfoEntity: Repository<OrderInfoEntity>;
+
+  @InjectEntityModel(UserInfoEntity)
+  userInfoEntity: Repository<UserInfoEntity>;
+
+  @InjectEntityModel(RecycleTransactionEntity)
+  recycleTransactionEntity: Repository<RecycleTransactionEntity>;
 
   @Inject()
   pluginService: PluginService;
@@ -41,7 +49,7 @@
   /**
    * 鏀粯鎴愬姛
    * @param orderNum 璁㈠崟鍙�
-   * @param payType 鏀粯鏂瑰紡 0-寰呮敮浠� 1-寰俊 2-鏀粯瀹�
+   * @param payType 鏀粯鏂瑰紡 0-寰呮敮浠� 1-寰俊 2-鏀粯瀹� 3-纰崇Н鍒�
    */
   async paySuccess(orderNum: string, payType: number) {
     const order = await this.orderInfoService.getByOrderNum(orderNum);
@@ -49,7 +57,7 @@
       await this.orderInfoEntity.update(order.id, {
         payType,
         status: 1,
-        payTime: order.payTime,
+        payTime: new Date(),
       });
       // 鍙戦�佽嚜鍔ㄧ‘璁ゆ敹璐ч槦鍒�
       const orderConfirm = await this.baseSysParamService.dataByKey(
@@ -71,7 +79,7 @@
    * @param userId
    * @param orderId
    */
-  async wxMiniPay(orderId: number, userId: number) {
+  async wxMiniPay(orderId: number, userId: string) {
     await this.orderInfoEntity.update(orderId, { wxType: 0 });
     return await this.wxJSAPI(orderId, userId, 0);
   }
@@ -81,7 +89,7 @@
    * @param userId
    * @param orderId
    */
-  async wxMpPay(orderId: number, userId: number) {
+  async wxMpPay(orderId: number, userId: string) {
     await this.orderInfoEntity.update(orderId, { wxType: 1 });
     return await this.wxJSAPI(orderId, userId, 1);
   }
@@ -134,7 +142,7 @@
    * @param userId
    * @returns
    */
-  async wxAppPay(orderId: number, userId: number) {
+  async wxAppPay(orderId: number, userId: string) {
     const appid = await this.getAppidByType(2);
 
     const { config, instance } = await this.wxPayInstance(appid);
@@ -162,7 +170,7 @@
    * @param userId
    * @returns
    */
-  async getOrder(orderId: number, userId: number) {
+  async getOrder(orderId: number, userId: string) {
     const order: OrderInfoEntity = await this.orderInfoService.info(orderId);
     if (!order || order.status != 0 || order.userId != userId) {
       throw new CoolCommException('璁㈠崟涓嶅瓨鍦ㄦ垨涓嶆槸鍙互鏀粯鐨勭姸鎬�');
@@ -177,7 +185,7 @@
    * @param type 0-灏忕▼搴� 1-鍏紬鍙� 2-App
    * @returns
    */
-  async wxJSAPI(orderId: number, userId: number, type = 0) {
+  async wxJSAPI(orderId: number, userId: string, type = 0) {
     const order = await this.getOrder(orderId, userId);
     const openid = await this.userWxService.getOpenid(userId, type);
 
@@ -235,4 +243,91 @@
     }
     throw new CoolCommException(result.message);
   }
+
+  /**
+   * 璇诲彇纰崇Н鍒嗗厬鎹㈡瘮渚嬶紝榛樿 100
+   */
+  async getCarbonRatio() {
+    try {
+      const value = await this.baseSysParamService.dataByKey('carbonratio');
+      const ratio = Number(value);
+      if (ratio > 0) {
+        return ratio;
+      }
+    } catch (e) {}
+    return 100;
+  }
+
+  /**
+   * 纰崇Н鍒嗘敮浠橈細涓嶈皟鐢ㄥ閮ㄦ笭閬�
+   * 鎵e噺绉垎 = 璁㈠崟瀹炰粯閲戦 * carbonratio
+   */
+  @CoolTransaction()
+  async carbonPay(orderId: number, userId: string, queryRunner?: QueryRunner) {
+    if (!queryRunner) {
+      throw new CoolCommException('浜嬪姟鍚姩澶辫触');
+    }
+
+    const manager = queryRunner.manager;
+    const order = await manager.findOne(OrderInfoEntity, {
+      where: { id: orderId },
+    });
+    if (!order || order.status != 0 || order.userId != userId) {
+      throw new CoolCommException('璁㈠崟涓嶅瓨鍦ㄦ垨涓嶆槸鍙互鏀粯鐨勭姸鎬�');
+    }
+
+    const user = await manager.findOne(UserInfoEntity, {
+      where: { unionid: userId },
+    });
+    if (!user) {
+      throw new CoolCommException('鐢ㄦ埛涓嶅瓨鍦�');
+    }
+
+    const ratio = await this.getCarbonRatio();
+    const payAmount = new BigNumber(order.price).minus(order.discountPrice || 0);
+    const carbonCost = payAmount.multipliedBy(ratio).toNumber();
+
+    if (Number(user.carbonBalance || 0) < carbonCost) {
+      throw new CoolCommException('纰崇Н鍒嗕笉瓒�');
+    }
+
+    const dec = await manager
+      .createQueryBuilder()
+      .update(UserInfoEntity)
+      .set({ carbonBalance: () => `carbonBalance - ${carbonCost}` })
+      .where('id = :id AND carbonBalance >= :cost', {
+        id: userId,
+        cost: carbonCost,
+      })
+      .execute();
+    if (!dec.affected) {
+      throw new CoolCommException('纰崇Н鍒嗕笉瓒�');
+    }
+
+    await manager.update(OrderInfoEntity, order.id, {
+      payType: 3,
+      status: 1,
+      payTime: new Date(),
+    });
+
+    await manager.save(RecycleTransactionEntity, {
+      userId: user.unionid || String(userId),
+      departmentId: user.departmentId,
+      type: '鏀嚭',
+      amount: carbonCost,
+      sourceType: '鍟嗗煄',
+      remark: `鍟嗗煄璁㈠崟 ${order.orderNum} 纰崇Н鍒嗘敮浠榒,
+      orderId: order.orderNum,
+    });
+
+    const orderConfirm = await this.baseSysParamService.dataByKey('orderConfirm');
+    this.orderQueue.add(
+      { orderId: order.id, action: Action.CONFIRM },
+      {
+        delay: orderConfirm * 60 * 60 * 1000,
+      }
+    );
+
+    return true;
+  }
 }

--
Gitblit v1.9.1