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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { Init, Provide } from '@midwayjs/core';
import { BaseService } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Equal, In, Repository } from 'typeorm';
import { GoodsSpecEntity } from '../entity/spec';
 
/**
 * 规格
 */
@Provide()
export class GoodsSpecService extends BaseService {
  @InjectEntityModel(GoodsSpecEntity)
  goodsSpecEntity: Repository<GoodsSpecEntity>;
 
  @Init()
  async init() {
    await super.init();
    this.setEntity(this.goodsSpecEntity);
  }
 
  /**
   * 保持规格
   * @param goodsId
   * @param specs
   */
  async save(goodsId: number, specs: GoodsSpecEntity[]) {
    // 先删除原来的规格
    await this.goodsSpecEntity.delete({ goodsId });
    // 保存新的规格
    await this.goodsSpecEntity.save(
      specs.map(item => {
        item.goodsId = goodsId;
        return item;
      })
    );
  }
 
  /**
   * 通过商品ID获取规格
   * @param goodsId
   */
  async removeByGoodsId(goodsIds: number[]) {
    await this.goodsSpecEntity.delete({ goodsId: In(goodsIds) });
  }
 
  /**
   * 通过商品ID获取规格
   * @param goodsId
   * @returns
   */
  async getByGoodsId(goodsId: number) {
    return await this.goodsSpecEntity.findBy({ goodsId: Equal(goodsId) });
  }
 
  /**
   * 更新库存
   * @param specId 规格ID
   * @param count 数量
   */
  async updateStock(specId: number, count: number) {
    await this.goodsSpecEntity.increment({ id: specId }, 'stock', count);
 
    // 更新后检查库存,如果小于0则设置为0
    const spec = await this.goodsSpecEntity.findOneBy({ id: Equal(specId) });
    if (spec && spec.stock < 0) {
      await this.goodsSpecEntity.update(spec.id, {
        stock: 0,
      });
    }
  }
}