schangxiang@126.com
2025-09-09 3d8966ba2c81e7e0365c8b123e861d18ee4f94f5
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
import BaseService from "../baseService";
import { BaseProductInfo } from "../../entity/basicInfo/base/baseProductInfo";
import { BasePosition } from "../../entity/basicInfo/base/basePosition";
import { Not } from "typeorm";
 
/**
 * 物料信息
 */
export default class ProductInfoService extends BaseService {
  /**
   * 添加保存前事件
   */
  public async onAddSaveBefore(t: BaseProductInfo) {
    let userInfo = await this.userInfo;
    this.info.result = true;
 
    //#region 版本权限控制
    if (!this.ctx.service.common.isSaaSAuth("SKU数量")) {
      this.info.result = false;
      this.info.msg = "当前版本不支持该操作";
    } else {
      var num = await this.ctx.service.common.getAuthNum("SKU数量");
      if (num) {
        var currentCount = await this.dbRead.count(BaseProductInfo, {
          userProduct_Id: userInfo.userProduct_Id
        });
        if (currentCount >= num) {
          this.info.result = false;
          this.info.msg = "超出当前版本数量限制,请升级为更高版本!";
          return this.info;
        }
      }
    }
    //#endregion
 
    //自动编码
    var sku_autoProductCode = await this.ctx.service.common.getConfigBool("sku_autoProductCode");
    if (sku_autoProductCode && !t.productCode === null) {
      var productCode = await this.ctx.service.common.getCodeRegular(254);
      t.productCode = productCode;
      if (!t.productModel) {
        t.productModel = t.productCode;
      }
    }
    //物料条码默认同物料编号
    var sku_barcodeSyncProductCode = await this.ctx.service.common.getConfigBool("sku_barcodeSyncProductCode");
    if (sku_barcodeSyncProductCode) {
      t.productModel = t.productCode;
    }
 
    //判断货位是否存在
    if (t.positionName) {
      let count = await this.dbRead.count(BasePosition, {
        positionName: t.positionName
      });
      if (!count) {
        this.info.msg = t.positionName + "货位号不存在,无法添加!";
        this.info.result = false;
        return this.info;
      }
    }
 
    //是否支持一码多品
    let sku_barcodeToMultiProduct = await this.ctx.service.common.getConfigBool("sku_barcodeToMultiProduct");
    //判断条码是否重复
    if (!sku_barcodeToMultiProduct) {
      let where: any = {
        userProduct_Id: t.userProduct_Id,
        productModel: t.productModel
      };
      if (t.product_Id > 0) {
        where = {
          userProduct_Id: t.userProduct_Id,
          productModel: t.productModel,
          product_Id: Not(t.product_Id)
        };
      }
      // 编辑时排除自己
      if (t.product_Id) {
        where["product_Id"] = Not(t.product_Id);
      }
      let count = await this.dbRead.count(BaseProductInfo, where);
      if (count) {
        this.info.msg = t.productModel + "当前模式是一品一码,【" + t.productModel + "】条码已存在不允许重复添加,无法添加!";
        this.info.result = false;
      }
    }
 
    return this.info;
  }
}