wangzhibo
6 天以前 3f249e399659dcd09d3fe58071b146e50e45c17f
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
import { CoolController, BaseController } from '@cool-midway/core';
import { MarketCouponUserEntity } from '../../../entity/coupon/user';
import { MarketCouponUserService } from '../../../service/coupon/user';
import { MarketCouponInfoService } from '../../../service/coupon/info';
import { Inject, Post, Body } from '@midwayjs/core';
import { MarketCouponInfoEntity } from '../../../entity/coupon/info';
 
/**
 * 优惠券用户
 */
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: MarketCouponUserEntity,
  service: MarketCouponUserService,
  pageQueryOp: {
    keyWordLikeFields: ['a.title'],
    fieldEq: ['a.status'],
    select: ['b.*', 'a.status as useStatus'],
    where: ctx => {
      const userId = ctx.user.id;
      return [
        // 只返回当前用户的优惠券
        ['a.userId =:userId', { userId }],
      ];
    },
    join: [
      {
        entity: MarketCouponInfoEntity,
        alias: 'b',
        condition: 'a.couponId = b.id',
      },
    ],
  },
})
export class AppMarketCouponUserController extends BaseController {
  @Inject()
  marketCouponInfoService: MarketCouponInfoService;
 
  @Inject()
  ctx;
 
  @Post('/receive', { summary: '领取优惠券' })
  async receive(@Body('couponId') couponId: number) {
    return this.ok(
      await this.marketCouponInfoService.receive(couponId, this.ctx.user.id)
    );
  }
}