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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
import { Init, Inject, Provide } from '@midwayjs/core';
import { BaseService, CoolCommException } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Equal, In, Repository } from 'typeorm';
import { OrderInfoEntity } from '../entity/info';
import { OrderGoodsService } from './goods';
import { OrderGoodsEntity } from '../entity/goods';
import * as moment from 'moment';
import { UserAddressService } from '../../user/service/address';
import { Action, OrderQueue } from '../queue/order';
import { OrderPayService } from './pay';
import { PluginService } from '../../plugin/service/info';
import { GoodsSpecEntity } from '../../goods/entity/spec';
import { MarketCouponInfoService } from '../../market/service/coupon/info';
import { MarketCouponUserEntity } from '../../market/entity/coupon/user';
import { BaseSysParamService } from '../../base/service/sys/param';
import BigNumber from 'bignumber.js';
 
/**
 * 订单信息
 */
@Provide()
export class OrderInfoService extends BaseService {
  @InjectEntityModel(OrderInfoEntity)
  orderInfoEntity: Repository<OrderInfoEntity>;
 
  @InjectEntityModel(GoodsSpecEntity)
  goodsSpecEntity: Repository<GoodsSpecEntity>;
 
  @InjectEntityModel(MarketCouponUserEntity)
  marketCouponUserEntity: Repository<MarketCouponUserEntity>;
 
  @Inject()
  orderGoodsService: OrderGoodsService;
 
  @Inject()
  userAddressService: UserAddressService;
 
  @Inject()
  orderPayService: OrderPayService;
 
  @Inject()
  orderQueue: OrderQueue;
 
  @Inject()
  pluginService: PluginService;
 
  @Inject()
  marketCouponInfoService: MarketCouponInfoService;
 
  @Inject()
  baseSysParamService: BaseSysParamService;
 
  @Init()
  async init() {
    await super.init();
    this.setEntity(this.orderInfoEntity);
  }
 
  /**
   * 修改前
   * @param data
   * @param type
   */
  async modifyBefore(data: any, type: 'add' | 'update' | 'delete') {
    if (type == 'add' || type == 'update') {
      delete data.refundStatus;
      delete data.refundApplyTime;
    }
  }
 
  /**
   * 分页查询
   * @param query
   * @param option
   * @param connectionName
   */
  async page(query: any, option: any, connectionName?: any) {
    const result = await super.page(query, option, connectionName);
    // 筛选条件的非分页sql
    const countSql = await super.getOptionFind(query, option);
 
    const countResult = await this.nativeQuery(
      `SELECT COUNT(a.id) as totalCount, SUM(a.price) as totalPrice FROM ${
        countSql.split('FROM')[1].split('ORDER BY')[0]
      }`
    );
 
    result['subData'] = {
      totalCount: countResult[0].totalCount,
      totalPrice: countResult[0].totalPrice,
    };
 
    const goodsList = await this.orderGoodsService.getByOrderIds(
      result.list.map(e => e.id)
    );
    for (const item of result.list) {
      item.goodsList = goodsList.filter(e => e.orderId == item.id);
    }
    return result;
  }
 
  /**
   * 根据订单号获取订单
   * @param orderNum
   * @returns
   */
  async getByOrderNum(orderNum: string) {
    return this.orderInfoEntity.findOneBy({
      orderNum: Equal(orderNum),
    });
  }
 
  /**
   * 修改订单状态
   * @param id
   * @param status
   */
  async changeStatus(id: number, status: number) {
    await this.orderInfoEntity.update({ id }, { status });
  }
 
  /**
   * 关闭订单
   * @param orderId
   * @param remark
   */
  async close(orderId: number, remark: string) {
    const order = await this.info(orderId);
    if (!order || !remark)
      throw new CoolCommException('订单不存在或备注不能为空');
    if (order.status != 0) {
      throw new CoolCommException('订单状态不允许关闭');
    }
 
    // 退回优惠券
    if (order.discountSource && order.discountPrice > 0) {
      if (order.discountSource.type == 0) {
        this.marketCouponUserEntity.update(
          {
            id: order.discountSource.objectId,
          },
          {
            status: 0,
          }
        );
      }
    }
 
    await this.orderInfoEntity.update(
      { id: orderId },
      { status: 7, closeRemark: remark }
    );
 
    // 释放库存
    await this.orderGoodsService.updateStock(order.goodsList, 'add');
  }
 
  /**
   * 订单详情
   * @param id
   * @param infoIgnoreProperty
   */
  async info(id: any, infoIgnoreProperty?: string[]) {
    const info = await super.info(id, infoIgnoreProperty);
    if (!info) {
      throw new CoolCommException('订单不存在');
    }
    if (info) {
      // 获取商品
      info.goodsList = await this.orderGoodsService.getByOrderId(info.id);
    }
    return info;
  }
 
  /**
   * 创建订单
   * @param data
   */
  async create(data: {
    userId: string;
    goodsList: OrderGoodsEntity[];
    addressId: number;
    remark: string;
    title: string;
    couponId?: number;
    payType?: number;
  }) {
    const address = await this.userAddressService.info(data.addressId);
    const payType = Number(data.payType) || 0;
    const order = {
      userId: data.userId,
      address,
      remark: data.remark,
      title: data.title,
      goodsList: data.goodsList,
      payType,
    } as OrderInfoEntity;
    // 检查库存
    await this.checkStock(data.goodsList);
    order.price = await this.orderGoodsService.getTotalPrice(data.goodsList);
    order.goodsList = data.goodsList;
 
    // 使用优惠券
    if (data.couponId) {
      await this.marketCouponInfoService.checkAndUse(
        data.couponId,
        data.userId,
        order
      );
    }
    await this.orderInfoEntity.insert(order);
 
    // 生成订单号
    const orderNum = await this.generateOrderNum(order.id);
 
    await this.orderInfoEntity.update(order.id, {
      orderNum,
      payType,
    });
 
    // 保存订单商品
    await this.orderGoodsService.save(order.id, data.goodsList);
 
    // 更新商品库存
    await this.orderGoodsService.updateStock(data.goodsList);
 
    const orderTimeout = await this.baseSysParamService.dataByKey(
      'orderTimeout'
    );
    // 发送订单创建消息
    this.orderQueue.add(
      { orderId: order.id, action: Action.TIMEOUT },
      {
        // 超时关闭订单
        delay: orderTimeout * 60 * 1000,
      }
    );
    return order;
  }
 
  /**
   * 生成订单号
   * @param orderId
   */
  async generateOrderNum(orderId: number, label = 'U') {
    const orderNum =
      moment().format('YYYYMMDDHHmmss') +
      Math.floor(Math.random() * 10000).toString() +
      orderId.toString();
    return label + orderNum;
  }
 
  /**
   * 退款
   * @param userId
   * @param orderId
   * @param goodsId
   * @param reason
   */
  async refund(userId: number, orderId: number, reason: string) {
    const order = await this.info(orderId);
    if (order && order.userId != userId) {
      throw new CoolCommException('非法操作');
    }
    if (![1, 2].includes(order.status)) {
      throw new CoolCommException('订单状态不允许退款');
    }
 
    await this.orderInfoEntity.update(
      { id: Equal(orderId) },
      {
        status: 5,
        refund: {
          amount: new BigNumber(order.price)
            .minus(order.discountPrice)
            .toNumber(),
          status: 0,
          applyTime: new Date(),
          reason,
          orderNum: 'R' + order.orderNum.slice(1),
        },
      }
    );
  }
 
  /**
   * 退款处理
   * @param orderId
   * @param action 0-拒绝 1-同意
   * @param refuseReason 0-拒绝 1-同意
   * @param amount
   */
  async refundHandle(
    orderId: number,
    action: number,
    refuseReason: string,
    amount: number
  ) {
    const order = await this.info(orderId);
    if (order.status != 5 || !order.refund) {
      throw new CoolCommException('订单状态不允许退款处理');
    }
    // 拒绝退款
    if (action == 0) {
      await this.orderInfoEntity.update(
        { id: Equal(orderId) },
        {
          status: 4,
          refund: {
            ...order.refund,
            status: 2,
            refuseReason: refuseReason,
          },
        }
      );
    }
    // 同意退款
    if (action == 1) {
      if (amount > order.price) {
        throw new CoolCommException('退款金额不能大于订单金额');
      }
      // 执行退款操作
      const result = await this.orderPayService.wxRefund(order, amount);
 
      if (result) {
        await this.orderInfoEntity.update(
          { id: Equal(orderId) },
          {
            status: 6,
            refund: {
              ...order.refund,
              status: 1,
              realAmount: amount,
              time: new Date(),
            },
          }
        );
      }
    }
  }
 
  /**
   * 确认收货
   * @param userId
   * @param orderId
   */
  async confirm(orderId: number, userId: number) {
    const order = await this.info(orderId);
    if (order && order.userId != userId) {
      throw new CoolCommException('非法操作');
    }
    if (![2].includes(order.status)) {
      throw new CoolCommException('订单状态不允许退款');
    }
    await this.orderInfoEntity.update(
      { id: Equal(orderId) },
      {
        status: 3,
      }
    );
  }
 
  /**
   * 自动确认收货
   * @param orderId
   * @returns
   */
  async autoConfirm(orderId: number) {
    const info = await this.orderInfoEntity.findOneBy({ id: Equal(orderId) });
    if (info.status != 2) {
      return;
    }
    await this.orderInfoEntity.update({ id: Equal(orderId) }, { status: 3 });
  }
 
  /**
   * 物流信息
   * @param orderId
   */
  async logistics(orderId: number, userId?: string) {
    const order: OrderInfoEntity = await this.info(orderId);
    if (userId && order.userId != userId) {
      throw new CoolCommException('非法操作');
    }
    const no = order.logistics?.num;
 
    if (!no) {
      return null;
    }
 
    const instance: any = await this.pluginService.getInstance('wuliu');
    return await instance.query(no);
  }
 
  /**
   * 发货
   * @param orderId
   * @param logistics
   */
  async deliver(
    orderId: number,
    logistics: {
      // 物流公司
      company: string;
      // 物流单号
      num: string;
    }
  ) {
    const order: OrderInfoEntity = await this.info(orderId);
    if (order.status != 1) {
      throw new CoolCommException('订单状态不允许发货');
    }
    await this.orderInfoEntity.update(orderId, {
      status: 2,
      logistics,
    });
  }
 
  /**
   * 检查库存
   * @param goodsList
   */
  async checkStock(goodsList: OrderGoodsEntity[]) {
    const specs = goodsList.map(item => item.spec);
    const specList = await this.goodsSpecEntity.findBy({
      id: In(specs.map(e => e.id)),
    });
    for (const spec of specList) {
      const goods = goodsList.find(e => e.spec.id == spec.id);
      // 设置商品价格
      goods.price = spec.price;
      if (goods.count > spec.stock) {
        throw new CoolCommException(`商品[${goods.goodsInfo.title}],库存不足`);
      }
    }
  }
 
  /**
   * 用户订单数量
   * @param userId
   */
  async userCount(userId: number) {
    const statusLabels = [
      '待付款',
      '待发货',
      '待收货',
      '待评价',
      '交易完成',
      '退款中',
      '已退款',
      '已关闭',
    ];
    // 生成查询字符串
    const selectQueries = statusLabels.map(
      (label, index) =>
        `SUM(CASE WHEN status = ${index} THEN 1 ELSE 0 END) AS '${label}'`
    );
    const list = await this.orderInfoEntity
      .createQueryBuilder('a')
      .select(selectQueries)
      .where('a.userId = :userId', { userId })
      .getRawMany();
    return list[0];
  }
}