333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
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
import BaseController from "../baseController";
import { Post, Prefix } from "egg-shell-decorators";
import { BaseStorage } from "../../entity/basicInfo/base/baseStorage";
import { SaleOrder } from "../../entity/outbound/sale/saleOrder";
import * as mssql from "mssql";
import { BaseConsignor } from "../../entity/basicInfo/consignor/baseConsignor";
import { SaleOrderList } from "../../entity/outbound/sale/saleOrderList";
import { BaseProductInfo } from "../../entity/basicInfo/base/baseProductInfo";
 
@Prefix("/jiaoma")
export default class JiaoMaController extends BaseController {
  //#region 寻源结果接口
  /**
   * 寻源结果接口
   */
  @Post()
  public async orderTrack() {
    let { ctx } = this;
    let body = ctx.body;
    let userInfo = await this.userInfo;
    let db = this.app.mongodb;
 
    try {
      // 记录数据到mongodb日志中
      let collection = db.collection("apiJiaoMaOrderTrack");
      let mongoData = JSON.parse(JSON.stringify(body));
      mongoData.createDate = new Date();
      mongoData.userProduct_Id = userInfo.userProduct_Id;
      let re = await collection.insert(mongoData);
      ctx.logger.info("apiJiaoMaOrderTrack将数据保存至MongoDB after:" + JSON.stringify(re));
    } catch {}
 
    try {
      let orderId = this.body.orderId;
      if (!orderId) {
        let erroColl = db.collection("apiLogs");
        let data = {
          type: "角马寻源",
          msg: `订单ID不存在`,
          createDate: new Date()
        };
        await erroColl.insert(data);
 
        this.info.result = false;
        this.info.msg = "推送失败";
        ctx.body = this.info;
        return;
      }
 
      // 获取仓库信息
      let storageInfo = await this.dbRead.findOne(BaseStorage, {
        storageCode: body.storeCode,
        userProduct_Id: userInfo.userProduct_Id
      });
      // 仓库不存在, 01 02  都是失败  只有00才是成功
      if (!storageInfo || this.body.result != "00") {
        let erroColl = db.collection("apiLogs");
        let data = {
          type: "角马寻源",
          msg: `仓库名称【${body.storeName}】不存在,采用默认森泽顺义C仓`,
          order_Id: orderId,
          createDate: new Date()
        };
        await erroColl.insert(data);
 
        // 默认主仓
        await this.dbWrite.update(SaleOrder, orderId, {
          storage_Id: 73,
          storageName: "森泽顺义C仓"
        });
 
        this.info.result = false;
        this.info.msg = data.msg;
        ctx.body = this.info;
        return;
      }
 
      // 获取货主信息
      let consignorInfo = await this.dbRead.findOne(BaseConsignor, {
        consignorCode: body.storeCode,
        userProduct_Id: userInfo.userProduct_Id
      });
      if (!consignorInfo) {
        let erroColl = db.collection("apiLogs");
        let data = {
          type: "角马寻源",
          msg: `寻源失败,货主名称【${body.storeName}】不存在`,
          order_Id: orderId,
          createDate: new Date()
        };
        await erroColl.insert(data);
 
        this.info.result = false;
        this.info.msg = data.msg;
        ctx.body = this.info;
        return;
      }
 
      // 更新仓库
      await this.dbWrite.update(SaleOrder, orderId, {
        storage_Id: storageInfo.storage_Id,
        storageName: storageInfo.storageName,
        consignor_Id: consignorInfo.consignor_Id,
        consignorCode: consignorInfo.consignorCode,
        consignorName: consignorInfo.consignorName
      });
 
      this.info.msg = "推送成功";
      this.info.result = true;
      let logColl = db.collection("apiLogs");
      let data = {
        type: "角马寻源",
        msg: "寻源成功,修改成功为:" + storageInfo.storageName,
        order_Id: orderId,
        createDate: new Date()
      };
      await logColl.insert(data);
    } catch (error) {
      this.info.result = false;
      this.info.data = error.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 订单状态回调接口
  /**
   * 订单状态回调接口
   */
  @Post()
  public async orderStatus() {
    let { ctx } = this;
    let body = ctx.body;
    let userInfo = await this.userInfo;
    let db = this.app.mongodb;
    let orderId = this.body.orderId;
 
    try {
      // 记录数据到mongodb日志中
      let db = this.app.mongodb;
      let logColl = db.collection("apiLogs");
      let collection = db.collection("apiJiaoMaOrderStatus");
      let mongoData = JSON.parse(JSON.stringify(body));
      mongoData.createDate = new Date();
      mongoData.userProduct_Id = userInfo.userProduct_Id;
      let re = await collection.insert(mongoData);
      ctx.logger.info("apiJiaoMaOrderStatus将数据保存至MongoDB after:" + JSON.stringify(re));
 
      if (body.status === "09") {
        // 09-拒收退货,执行自动退货
        let erroColl = db.collection("apiLogs");
        let data = {
          type: "角马寻源",
          order_Id: orderId,
          msg: `09-拒收退货,执行自动退货`,
          createDate: new Date()
        };
        await erroColl.insert(data);
 
        const connection: any = await this.dbWrite.connection;
        let request = new mssql.Request(connection.driver.master);
        request.input("order_Id", body.orderId);
        request.input("user_Id", userInfo.user_Id);
        request.output("outMsg", mssql.NVarChar(2000));
        let result = await request.execute("sp_Sale_Return_AutoShelve");
        let outMsg = result.output.outMsg;
 
        if (outMsg) {
          this.info.msg = outMsg;
          this.info.result = false;
        } else {
          this.info.msg = "退货成功";
          this.info.result = true;
        }
      } else if (body.status === "07") {
        // 07-门店拒单
        let erroColl = db.collection("apiLogs");
        let data: any = {};
 
        // 默认主仓
        await this.dbWrite.update(SaleOrder, orderId, {
          storage_Id: 73,
          storageName: "森泽顺义C仓"
        });
 
        // 修改货主
        let orderDetail = await this.dbRead.findOne(SaleOrderList, {
          order_Id: orderId
        });
        if (orderDetail) {
          let prodInfo = await this.dbRead.findOne(BaseProductInfo, orderDetail.product_Id);
          await this.dbWrite.update(SaleOrder, orderId, {
            consignor_Id: prodInfo.consignor_Id,
            consignorName: prodInfo.consignorName
          });
          data = {
            type: "角马寻源",
            order_Id: orderId,
            msg: `07-角马门店拒单,采用默认森泽顺义C仓,货主:${prodInfo.consignorName}`,
            createDate: new Date()
          };
        } else {
          await this.dbWrite.update(SaleOrder, orderId, {
            consignor_Id: null,
            consignorName: null
          });
          data = {
            type: "角马寻源",
            order_Id: orderId,
            msg: `07-角马门店拒单,采用默认森泽顺义C仓,货主清空`,
            createDate: new Date()
          };
        }
        await erroColl.insert(data);
 
        this.info.result = true;
        this.info.msg = data.msg;
      } else if (body.status === "02") {
        // 02-门店已接单,执行自动出库
        const connection: any = await this.dbWrite.connection;
        let request = new mssql.Request(connection.driver.master);
        request.input("order_Id", orderId);
        request.input("user_Id", userInfo.user_Id);
        request.output("outMsg", mssql.NVarChar(2000));
        let result = await request.execute("sp_Sale_Order_AutoOut");
        let outMsg = result.output.outMsg;
 
        if (outMsg) {
          this.info.msg = outMsg;
          this.info.result = false;
          let logColl = db.collection("apiLogs");
          let data = {
            type: "角马寻源",
            msg: "02-门店已接单,状态回传自动出库失败:" + outMsg,
            order_Id: orderId,
            createDate: new Date()
          };
          await logColl.insert(data);
        } else {
          this.info.msg = "角马订单自动出库成功";
          this.info.result = true;
          let logColl = db.collection("apiLogs");
          let data = {
            type: "角马寻源",
            msg: "02-门店已接单,角马订单自动出库成功",
            order_Id: orderId,
            createDate: new Date()
          };
          await logColl.insert(data);
        }
      } else {
        this.info.result = true;
        this.info.msg = "推送成功";
      }
      let data = {
        type: "orderStatus",
        order_Id: body.orderId,
        msg: `执行自动退货:${this.info.msg}, orderId=${body.orderId}`,
        createDate: new Date()
      };
      await logColl.insert(data);
    } catch (e) {
      let logColl = db.collection("apiLogs");
      let data = {
        type: "orderStatus",
        order_Id: body.orderId,
        msg: `角马状态回传失败, orderId=${body.orderId},error:${e.message}`,
        createDate: new Date()
      };
      await logColl.insert(data);
      this.info.result = false;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 查找库存
  /**
   * 查找库存
   */
  @Post()
  public async findStock() {
    let { ctx } = this;
    let body = ctx.body;
    let userInfo = await this.userInfo;
 
    let stnCode = this.body.stnCode;
    let productCodes = this.body.productCodes;
    if (!productCodes) {
      this.info.result = false;
      this.info.msg = "物料编号为空";
      ctx.body = this.info;
      return;
    }
    try {
      // 记录数据到mongodb日志中
      let db = this.app.mongodb;
      let collection = db.collection("apiJiaoMaFindStock");
      let mongoData = JSON.parse(JSON.stringify(body));
      mongoData.createDate = new Date();
      mongoData.userProduct_Id = userInfo.userProduct_Id;
      let re = await collection.insert(mongoData);
      ctx.logger.info("apiJiaoMaFindStock 将数据保存至MongoDB after:" + JSON.stringify(re));
    } catch {}
 
    try {
      let storageInfo = await this.dbRead.findOne(BaseStorage, {
        userProduct_Id: userInfo.userProduct_Id,
        storageCode: stnCode
      });
      if (!storageInfo) {
        this.info.result = false;
        this.info.data = {
          productStockList: [],
          stnCode: stnCode
        };
        this.info.msg = "门店仓库不存在";
        ctx.body = this.info;
        return;
      }
 
      let sql = `SELECT Product_Id AS productId, MAX(productName) AS productName, productCode, NULL AS productPic, SUM(productStorage) AS stock, 0 AS stockStatus
        FROM dbo.Base_ProductPosition WHERE productStorage>0
        AND UserProduct_Id=@0
        AND storage_Id=@2
        AND ProductCode IN(Select col from dbo.f_split(@1, ','))
        GROUP BY Product_Id, productCode`;
      let ppList = await this.dbRead.query(sql, [userInfo.userProduct_Id, productCodes, storageInfo.storage_Id]);
      this.info.result = true;
      this.info.state = "00";
      this.info.msg = "操作成功";
      this.info.data = {
        productStockList: ppList,
        stnCode: stnCode
      };
    } catch (error) {
      this.info.result = false;
      this.info.data = error.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 查找库存-分货主
  /**
   * 查找库存-分货主
   */
  @Post()
  public async findStock2() {
    let { ctx } = this;
    let body = ctx.body;
    let userInfo = await this.userInfo;
 
    let stnCode = this.body.stnCode;
    let productCodes = this.body.productCodes;
    if (!productCodes) {
      this.info.result = false;
      this.info.msg = "物料编号为空";
      ctx.body = this.info;
      return;
    }
    try {
      // 记录数据到mongodb日志中
      let db = this.app.mongodb;
      let collection = db.collection("apiJiaoMaFindStock");
      let mongoData = JSON.parse(JSON.stringify(body));
      mongoData.createDate = new Date();
      mongoData.userProduct_Id = userInfo.userProduct_Id;
      let re = await collection.insert(mongoData);
      ctx.logger.info("apiJiaoMaFindStock 将数据保存至MongoDB after:" + JSON.stringify(re));
    } catch {}
 
    try {
      let storageInfo = await this.dbRead.findOne(BaseStorage, {
        storageCode: stnCode,
        userProduct_Id: userInfo.userProduct_Id
      });
      if (!storageInfo) {
        this.info.result = false;
        this.info.data = {
          productStockList: [],
          stnCode: stnCode
        };
        this.info.msg = "门店仓库不存在";
        ctx.body = this.info;
        return;
      }
 
      let sql = `SELECT Product_Id AS productId, MAX(productName) AS productName, productCode, NULL AS productPic, SUM(productStorage) AS stock, 0 AS stockStatus, consignorCode
        FROM dbo.Base_ProductPosition WHERE productStorage>0
        AND UserProduct_Id=@0
        AND storage_Id=@2
        AND consignorCode=@3
        AND ProductCode IN(Select col from dbo.f_split(@1, ','))
        GROUP BY Product_Id, productCode, consignorCode`;
      let ppList = await this.dbRead.query(sql, [userInfo.userProduct_Id, productCodes, storageInfo.storage_Id, stnCode]);
      this.info.result = true;
      this.info.state = "00";
      this.info.msg = "操作成功";
      this.info.data = {
        productStockList: ppList,
        stnCode: stnCode
      };
    } catch (error) {
      this.info.result = false;
      this.info.data = error.message;
    }
    ctx.body = this.info;
  }
  //#endregion
}