zs 1 سال پیش
والد
کامیت
0491573afa

+ 1 - 0
package.json

@@ -22,6 +22,7 @@
     "@typegoose/typegoose": "^11.5.0",
     "amqp-connection-manager": "^4.1.14",
     "amqplib": "^0.10.3",
+    "decimal.js": "^10.4.3",
     "exceljs": "^4.3.0",
     "free-midway-component": "^1.0.35",
     "midway-schedule": "^2.15.0",

+ 2 - 1
src/controller/order.controller.ts

@@ -31,7 +31,8 @@ export class OrderController extends BaseController {
   @Validate()
   @ApiResponse({ type: CVO_order })
   async create(@Body() data: CDTO_order) {
-    const dbData = await this.service.create(data);
+    const arr = await this.service.createBefore(data);
+    const dbData = await this.service.create(arr);
     await this.service.createAfter(dbData);
     const result = new CVO_order(dbData);
     return result;

+ 5 - 1
src/entity/order.entity.ts

@@ -17,8 +17,12 @@ export class Order extends BaseModel {
     remark: '字典表:order_type',
   })
   type: string;
-  @prop({ required: false, index: false, zh: '金额' })
+  @prop({ required: false, index: false, zh: '金额' })
   money: number;
+  @prop({ required: false, index: false, zh: '优惠金额' })
+  coupon_money: number;
+  @prop({ required: false, index: false, zh: '总金额' })
+  total_money: number;
   @prop({ required: false, index: false, zh: '日期' })
   date: string;
   @prop({ required: false, index: false, zh: '数量' })

+ 16 - 6
src/interface/order.interface.ts

@@ -22,8 +22,12 @@ export class FVO_order {
   'source_name': string = undefined;
   @ApiProperty({ description: '类型' })
   'type': string = undefined;
-  @ApiProperty({ description: '金额' })
+  @ApiProperty({ description: '金额' })
   'money': number = undefined;
+  @ApiProperty({ description: '优惠金额' })
+  'coupon_money': number = undefined;
+  @ApiProperty({ description: '总金额' })
+  'total_money': number = undefined;
   @ApiProperty({ description: '日期' })
   'date': string = undefined;
   @ApiProperty({ description: '数量' })
@@ -45,14 +49,14 @@ export class FVO_order {
 export class QDTO_order extends SearchBase {
   constructor() {
     const like_prop = [];
-    const props = ['user', 'source', 'type', 'status'];
+    const props = ['user', 'source_name', 'type', 'status'];
     const mapping = [];
     super({ like_prop, props, mapping });
   }
   @ApiProperty({ description: '用户' })
   'user': string = undefined;
-  @ApiProperty({ description: '来源' })
-  'source': object = undefined;
+  @ApiProperty({ description: '来源名称' })
+  'source_name': string = undefined;
   @ApiProperty({ description: '类型' })
   'type': string = undefined;
   @ApiProperty({ description: '状态' })
@@ -71,7 +75,7 @@ export class CDTO_order {
   @Rule(RuleType['string']().empty(''))
   'user': string = undefined;
   @ApiProperty({ description: '来源' })
-  @Rule(RuleType['string']().empty(''))
+  @Rule(RuleType['object']().empty(''))
   'source': object = undefined;
   @ApiProperty({ description: '来源名称' })
   @Rule(RuleType['string']().empty(''))
@@ -79,9 +83,15 @@ export class CDTO_order {
   @ApiProperty({ description: '类型' })
   @Rule(RuleType['string']().empty(''))
   'type': string = undefined;
-  @ApiProperty({ description: '金额' })
+  @ApiProperty({ description: '金额' })
   @Rule(RuleType['number']().empty(''))
   'money': number = undefined;
+  @ApiProperty({ description: '优惠金额' })
+  @Rule(RuleType['number']().empty(''))
+  'coupon_money': number = undefined;
+  @ApiProperty({ description: '总金额' })
+  @Rule(RuleType['number']().empty(''))
+  'total_money': number = undefined;
   @ApiProperty({ description: '日期' })
   @Rule(RuleType['string']().empty(''))
   'date': string = undefined;

+ 40 - 1
src/service/order.service.ts

@@ -1,4 +1,4 @@
-import { Provide } from '@midwayjs/decorator';
+import { Provide, Inject } from '@midwayjs/decorator';
 import { InjectEntityModel } from '@midwayjs/typegoose';
 import { ReturnModelType } from '@typegoose/typegoose';
 import { BaseService } from 'free-midway-component';
@@ -7,6 +7,8 @@ import { UserCoupon } from '../entity/userCoupon.entity';
 import _ = require('lodash');
 import { Hotel } from '../entity/hotel.entity';
 import { Ticket } from '../entity/ticket.entity';
+import { Coupon } from '../entity/coupon.entity';
+import { CountUtilService } from '../service/util/count.util';
 type modelType = ReturnModelType<typeof Order>;
 @Provide()
 export class OrderService extends BaseService<modelType> {
@@ -16,12 +18,49 @@ export class OrderService extends BaseService<modelType> {
   @InjectEntityModel(UserCoupon)
   uModel: ReturnModelType<typeof UserCoupon>;
 
+  @InjectEntityModel(Coupon)
+  cModel: ReturnModelType<typeof Coupon>;
+
   @InjectEntityModel(Hotel)
   hModel: ReturnModelType<typeof Hotel>;
 
   @InjectEntityModel(Ticket)
   tModel: ReturnModelType<typeof Ticket>;
 
+  @Inject()
+  countUtil: CountUtilService;
+
+  // 创建总订单前
+  async createBefore(data) {
+    const { discount } = data;
+    // 计算有优惠券的优惠金额和总金额
+    if (discount) {
+      const UserCoupon = await this.uModel.findById(discount);
+      if (UserCoupon) {
+        const coupon = await this.cModel.findById(_.get(UserCoupon, 'coupon'));
+        const { discount_type, discount_config, get_limit } = coupon;
+        // 满减
+        if (discount_type === 'min') {
+          // 无限制
+          if (get_limit === 'nolimit') {
+            data.coupon_money = _.get(discount_config, 'min');
+            data.total_money = await this.countUtil.minus(
+              data.money,
+              data.coupon_money
+            );
+          }
+        } else if (discount_type === 'discount') {
+          // 折扣
+        }
+      }
+    } else {
+      // 没有优惠券的优惠金额和总金额
+      data.coupon_money = 0;
+      data.total_money = data.money;
+    }
+    return data;
+  }
+
   // 创建总订单后
   async createAfter(data) {
     const { source, type, identity, discount } = data;

+ 49 - 0
src/service/util/count.util.ts

@@ -0,0 +1,49 @@
+import { Provide } from '@midwayjs/decorator';
+import Decimal from 'decimal.js'; // 具体文件中引入
+import _ = require('lodash');
+@Provide()
+export class CountUtilService {
+  turnDecimal(n) {
+    if (_.isObject(n)) {
+      n = JSON.parse(JSON.stringify(n));
+      if (_.isObject(n)) n = _.get(n, '$numberDecimal');
+    }
+    return new Decimal(n);
+  }
+  // 加法
+  plus(n1 = 0, n2 = 0) {
+    const number1 = this.turnDecimal(n1);
+    const number2 = this.turnDecimal(n2);
+    const result = number1.add(number2).toFixed(2, Decimal.ROUND_DOWN);
+    return this.toNumber(result);
+  }
+  // 减法
+  minus(n1 = 0, n2 = 0) {
+    const number1 = this.turnDecimal(n1);
+    const number2 = this.turnDecimal(n2);
+    const result = number1.minus(number2).toFixed(2, Decimal.ROUND_DOWN);
+    return this.toNumber(result);
+  }
+  // 乘法
+  multiply(n1 = 0, n2 = 0) {
+    const number1 = this.turnDecimal(n1);
+    const number2 = this.turnDecimal(n2);
+    const result = number1.mul(number2).toFixed(2, Decimal.ROUND_DOWN);
+    return this.toNumber(result);
+  }
+  // 除法
+  divide(n1 = 0, n2 = 0) {
+    const number1 = this.turnDecimal(n1);
+    const number2 = this.turnDecimal(n2);
+    const result = number1.div(number2).toFixed(2, Decimal.ROUND_DOWN);
+    return this.toNumber(result);
+  }
+
+  toNumber(num) {
+    if (_.isObject(num)) {
+      num = JSON.parse(JSON.stringify(num));
+      if (_.isObject(num)) num = _.get(num, '$numberDecimal');
+    }
+    return new Decimal(num).toNumber();
+  }
+}