wangzhibo
5 天以前 50e5cdbaceee71f000341a434620d10a87b582f5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { Init, Inject, Provide } from '@midwayjs/core';
import { BaseService, CoolCommException } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { MarketCouponInfoEntity } from '../../entity/coupon/info';
import { MarketCouponUserService } from './user';
import { OrderInfoEntity } from '../../../order/entity/info';
import * as moment from 'moment';
 
/**
 * 优惠券信息
 */
@Provide()
export class MarketCouponInfoService extends BaseService {
  @InjectEntityModel(MarketCouponInfoEntity)
  marketCouponInfoEntity: Repository<MarketCouponInfoEntity>;
 
  @Inject()
  marketCouponUserService: MarketCouponUserService;
 
  @Init()
  async init() {
    await super.init();
    this.setEntity(this.marketCouponInfoEntity);
  }
 
  /**
   * 检查优惠券状态
   * @param couponId
   */
  async check(couponId: number) {
    const couponInfo: MarketCouponInfoEntity = await super.info(couponId);
    if (!couponInfo) {
      throw new CoolCommException('优惠券不存在');
    }
    // 判断数量
    if (couponInfo.receivedNum >= couponInfo.num) {
      throw new CoolCommException('优惠券已领完');
    }
    // 判断时间
    if (
      moment().isBefore(couponInfo.startTime) ||
      moment().isAfter(couponInfo.endTime)
    ) {
      throw new CoolCommException('优惠券未开始或已结束');
    }
    // 判断状态
    if (couponInfo.status !== 1) {
      throw new CoolCommException('优惠券未启用');
    }
    return couponInfo;
  }
 
  /**
   * 领取优惠券
   * @param couponId
   * @param userId
   */
  async receive(couponId: number, userId: string) {
    // 检查优惠券
    await this.check(couponId);
    // 检查用户是否已领取
    if (await this.marketCouponUserService.checkExist(couponId, userId)) {
      throw new CoolCommException('已领取过该优惠券');
    }
    // 保存
    await this.marketCouponUserService.save(couponId, userId);
    // 增加领取数量
    await this.marketCouponInfoEntity.increment(
      { id: couponId },
      'receivedNum',
      1
    );
  }
 
  /**
   * 检查优惠券是否满足条件,满足的话使用并设置和返回订单优惠金额
   * @param couponId
   * @param userId
   * @param order
   */
  async checkAndUse(couponId: number, userId: string, order: OrderInfoEntity) {
    // 判断优惠券
    const couponInfo = await this.check(couponId);
    // 判断用户
    await this.marketCouponUserService.check(couponId, userId);
    // 判断条件
    if (couponInfo.type == 0) {
      // 满减
      if (order.price < couponInfo.condition.fullAmount) {
        throw new CoolCommException('未满足优惠券使用条件');
      }
      // 设置订单的优惠金额
      order.discountPrice = couponInfo.amount;
      // 设置优惠来源
      order.discountSource = {
        type: 0,
        objectId: couponId,
        info: couponInfo,
      };
      // 设置商品的优惠金额
      if (order.price > 0) {
        order.goodsList.forEach(goods => {
          // 优惠金额 = 商品价格 / 订单总价 * 优惠金额 保留两位小数
          goods.discountPrice = Number(
            ((goods.price / order.price) * couponInfo.amount).toFixed(2)
          );
        });
      }
      // 使用优惠券
      await this.marketCouponUserService.use(couponId, userId);
      return couponInfo.amount;
    }
    return 0;
  }
}