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
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
import BaseService from "../baseService";
import { StorageCheck } from "../../entity/storage/check/storageCheck";
import { StorageCheckList } from "../../entity/storage/check/storageCheckList";
 
export default class CheckService extends BaseService {
  //#region 盘点扫描获取明细
  public async getCheckInfoList(checkCode: string) {
    let { ctx } = this;
    let userInfo = await ctx.helper.userInfo();
    this.info.result = false;
    if (!checkCode) {
      this.info.msg = "出库单号不能为空!";
      return this.info;
    }
    // 查询盘点单主表
    let dataInfo = await this.dbRead
      .createQueryBuilder(StorageCheck, "t")
      .where("checkCode=:checkCode and userProduct_Id=:userProduct_Id", {
        checkCode: checkCode,
        userProduct_Id: userInfo.userProduct_Id
      })
      .getOne();
    if (!dataInfo) {
      this.info.msg = "未找到盘点数据!";
      this.info.result = false;
      return this.info;
    }
    if (dataInfo.statusText !== "新建") {
      this.info.msg = "盘点单不是新建状态,不能盘点!";
      this.info.result = false;
      return this.info;
    }
    // 获取盘点明细数据
    let checkDetailList = await this.dbRead
      .createQueryBuilder(StorageCheckList, "t")
      .where("check_Id=:check_Id and checkQuantity is null", {
        check_Id: dataInfo.check_Id
      })
      .getMany();
    if (checkDetailList.length == 0) {
      this.info.msg = "盘点单号下没有可以盘点的明细!";
      this.info.result = false;
      return this.info;
    }
    let info = {
      checkDetailList: checkDetailList,
      storageCheckInfo: {
        check_Id: dataInfo.check_Id,
        checkCode: dataInfo.checkCode
      }
    };
    this.info.result = true;
    this.info.msg = null;
    this.info.data = info;
    return this.info;
  }
  //#endregion
 
  //#region 盘点扫描提交
  public async saveCheckByCheckList(checkCode: string, dataArray: Array<any>) {
    let { ctx } = this;
    let userInfo = await ctx.helper.userInfo();
    this.info.result = false;
    if (!checkCode) {
      this.info.result = false;
      this.info.msg = "盘点单号不能为空!";
      return this.info;
    }
    // 查询盘点单主表
    let dataInfo = await this.dbRead
      .createQueryBuilder(StorageCheck, "t")
      .where("checkCode=:checkCode And userProduct_Id=:userProduct_Id", {
        checkCode: checkCode,
        userProduct_Id: userInfo.userProduct_Id
      })
      .getOne();
    if (!dataInfo) {
      this.info.msg = "未找到盘点数据!";
      this.info.result = false;
      return this.info;
    }
    if (dataInfo.statusText !== "新建") {
      this.info.result = false;
      this.info.msg = "盘点单[" + dataInfo.checkCode + "]不是新建状态的,不能再修改!";
      return this.info;
    }
    try {
      //#region 盘点货位 判断货位是否存在
      let positionArray: Array<any> = [];
      for (var model of dataArray) {
        if (!model.productCode) {
          this.info.msg = "物料编号为空,请检查数据!";
          this.info.result = false;
          return this.info;
        }
        if (!model.positionName) {
          this.info.msg = "物料[" + model.productCode + "]的货位是空的!";
          this.info.result = false;
          return this.info;
        }
        positionArray.push(model.positionName);
      }
      let positionStr = positionArray.join(",");
      let sql = `SELECT Col AS PositionName FROM Func_SplitStrToTable(@0)
          WHERE Col NOT IN(
            SELECT PositionName FROM dbo.Base_Position WHERE storage_Id=@1
          )`;
      let dataPosition = await this.dbRead.query(sql, [positionStr, dataInfo.storage_Id]);
      if (dataPosition.length) {
        let _msg = dataPosition.map(item => item.positionName).join(",");
        this.info.result = false;
        this.info.msg = "货位[" + _msg + "]不存在";
        return this.info;
      }
      //#endregion
 
      //#region 验证物料在某货位的拣货数量是否正确
      for (var modelOne of dataArray) {
        let sqlOne = `SELECT COUNT(*) FROM dbo.Storage_CheckList WHERE Check_Id=@0 AND ProductModel=@1 AND PositionName=@2`;
        let _count = await this.dbRead.query(sqlOne, [dataInfo.check_Id, modelOne.productCode, modelOne.positionName]);
        if (_count == 0) {
          this.info.result = false;
          this.info.msg =
            "盘点单[" +
            dataInfo.checkCode +
            "]中不存在物料[" +
            modelOne.productCode +
            "]货位[" +
            modelOne.positionName +
            "]的明细数据,请盘点其他物料或者货位数据!";
          return this.info;
        }
      }
      //#endregion
 
      //#region 更新最后操作时间
      let sqlTwo = `UPDATE dbo.Sys_User SET PDAOperateDate=GETDATE() WHERE User_Id=@0`;
      await this.dbWrite.query(sqlTwo, [userInfo.user_Id]);
      //#endregion
 
      //#region 插入到盘点记录明细
      // 得到当前单据的所有可用上架明细
      let modelList = await this.dbRead.find(StorageCheckList, {
        check_Id: dataInfo.check_Id
      });
      // 插入到明细记录
      // var deleteSql = null;
      // var insertSql = null;
      for (var scanItem of dataArray) {
        // 把原来盘点的这个物料货位的数据删掉
        let deleteSql = `DELETE dbo.Storage_CheckListDetail WHERE Check_Id=@0 AND checkList_Id=@1`;
        await this.dbWrite.query(deleteSql, [dataInfo.check_Id, scanItem.checkList_Id]);
      }
      for (var scanItem of dataArray) {
        // 从可用上架明细中 ,得到当前物料编号的数据
        let currentModel = modelList.find(item => {
          let result =
            item.productCode === scanItem.productCode && item.positionName === scanItem.positionName && item.checkList_Id === scanItem.checkList_Id;
          return result;
        });
        if (currentModel) {
          let insertSql = `INSERT INTO Storage_CheckListDetail
            (CheckList_Id, Check_Id, Product_Id, ProductCode, ProductName, ProductModel, ProductSpec, PositionName,ScanQuantity, CreateID, Creator, CreateDate) 
            SELECT CheckList_Id, Check_Id, Product_Id, ProductCode, ProductName, ProductModel, ProductSpec, PositionName, @0 AS ScanQuantity, 
            @1 AS CreateID,'PDA' AS  Creator, GETDATE() AS CreateDate 
            FROM dbo.Storage_CheckList WHERE CheckList_Id=@2`;
          await this.dbWrite.query(insertSql, [scanItem.finishedQuantity, userInfo.user_Id, currentModel.checkList_Id]);
        }
      }
      //#endregion
 
      //#region 更新盘点单明细和主表数据
      let sqlend = `--盘点数量
        UPDATE dbo.Storage_CheckList SET CheckQuantity=(
          SELECT SUM(ScanQuantity) FROM dbo.Storage_CheckListDetail WHERE CheckList_Id=Storage_CheckList.CheckList_Id
        ),ProfitQuantity=0,ProfitMoney=0,LossQuantity=0,LossMoney=0 WHERE Check_Id=@0;
        --盘盈数量
        UPDATE dbo.Storage_CheckList SET ProfitQuantity=CheckQuantity-ProductStorage,LossQuantity=0 WHERE CheckQuantity-ProductStorage>0 AND Check_Id=@0;
        --盘亏数量
        UPDATE dbo.Storage_CheckList SET LossQuantity=ProductStorage-CheckQuantity,ProfitQuantity=0 WHERE ProductStorage-CheckQuantity>0 AND Check_Id=@0;
        --盘盈金额,盘亏金额
        UPDATE dbo.Storage_CheckList SET ProfitMoney=PurchaseMoney*ProfitQuantity,LossMoney=PurchaseMoney*LossQuantity WHERE Check_Id=@0;
        --主表
        UPDATE dbo.Storage_Check SET 
        TotalCheckQuantity=(SELECT SUM(CheckQuantity) FROM dbo.Storage_CheckList WHERE Check_Id=dbo.Storage_Check.Check_Id),
        TotalProfitQuantity=(SELECT SUM(ProfitQuantity) FROM dbo.Storage_CheckList WHERE Check_Id=dbo.Storage_Check.Check_Id),
        TotalLossQuantity=(SELECT SUM(LossQuantity) FROM dbo.Storage_CheckList WHERE Check_Id=dbo.Storage_Check.Check_Id),
        TotalProfitMoney=(SELECT SUM(ProfitQuantity) FROM dbo.Storage_CheckList WHERE Check_Id=dbo.Storage_Check.Check_Id),
        TotalLossMoney=(SELECT SUM(LossMoney) FROM dbo.Storage_CheckList WHERE Check_Id=dbo.Storage_Check.Check_Id)
        WHERE dbo.Storage_Check.Check_Id=@0`;
      await this.dbWrite.query(sqlend, [dataInfo.check_Id]);
      //#endregion
      this.info.result = true;
      this.info.msg = "盘点记录保存成功!";
      return this.info;
    } catch (error) {
      this.info.result = false;
      this.info.msg = error.message;
      return this.info;
    }
  }
  //#endregion
}