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
//#region import
import { default as BaseController } from "../baseController";
import * as sql from "mssql";
import { Post } from "egg-shell-decorators";
import { SaleOuter } from "../../entity/outbound/sale/saleOuter";
//#endregion
 
/**
 * 发货校验
 */
export default class OrderSendController extends BaseController {
  //#region   校验快递单号
  @Post()
  public async checkExpressCode() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
 
    let expressCode = body.expressCode;
    let scanType = body.scanType;
    let isNoCheckExpressCorp = body.isNoCheckExpressCorp;
    let where = "";
    let whereParam: any = [];
 
    try {
      if (isNoCheckExpressCorp) {
        //不校验快递公司
        //#region
        if (scanType == "快递单号") {
          //快递单号
          where = " (t.expressCode=@0) AND userProduct_Id=@1";
        } else if (scanType == "出库单号") {
          //出库单号
          where = " (t.orderCode=@0 OR L.orderCode=@0 OR t.storeOrderCode=@0) AND userProduct_Id=@1";
        } else if (scanType == "波次号") {
          //波次号
          where = " (t.orderPrintCode=@0) AND userProduct_Id=@1";
        } else {
          where = " (t.expressCode=@0 OR t.orderCode=@0 OR L.orderCode=@0 OR t.storeOrderCode=@0) AND userProduct_Id=@1";
        }
        //#endregion
      } else {
        //#region
        if (scanType == "快递单号") {
          //快递单号
          where = `(t.expressCode=@0) AND userProduct_Id=@1`;
        } else if (scanType == "出库单号") {
          //出库单号
          where = `
                        (
                            t.orderCode=@0 
                            OR L.orderCode=@0 OR t.storeOrderCode=@0
                        ) AND userProduct_Id=@1`;
        } else if (scanType == "波次号") {
          //波次号
          where = `
                        (
                            t.orderPrintCode=@0
                        ) AND userProduct_Id=@1`;
        } else {
          where = `
                        (
                            t.expressCode=@0 OR t.orderCode=@0 
                            OR L.orderCode=@0 OR t.storeOrderCode=@0
                        ) AND userProduct_Id=@1`;
        }
        //#endregion
      }
      let sql = "";
      sql =
        `
            SELECT top 1 t.expressCorp_Id, t.IsSend, t.outer_Id, 
                CASE WHEN L.order_Id IS NULL THEN t.order_Id ELSE L.order_Id END AS order_Id,
                CASE WHEN L.orderCode IS NULL THEN t.orderCode ELSE L.orderCode END AS orderCode,
                CASE WHEN L.orderCode IS NULL THEN 0 ELSE 1 END AS isBatchOuter    --波次出库
            FROM Sale_Outer t INNER JOIN dbo.Sale_OuterList L ON t.outer_Id=L.outer_Id
            Where ISNULL(t.IsSend,0)=0    --只查询未发运的单子
          AND ` + where;
 
      whereParam = [expressCode, userInfo.userProduct_Id];
      let dt = await this.dbRead.query(sql, whereParam);
      if (dt.length > 0) {
        this.info.result = true;
        this.info.data = {
          order_Id: dt[0]["order_Id"],
          outer_Id: dt[0]["outer_Id"],
          orderCode: dt[0]["orderCode"],
          isBatchOuter: dt[0]["isBatchOuter"]
        };
      } else {
        this.info.result = false;
        this.info.msg = "快递单号不存在!";
      }
    } catch (error) {
      this.info.msg = "错误:" + error.message;
      this.info.result = false;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 订单校验
  @Post()
  public async orderSave() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
 
    try {
      let saleOuter = await this.dbRead.findOne(SaleOuter, body.outer_Id);
 
      const connection: any = await this.dbWrite.connection;
      let request = new sql.Request(connection.driver.master);
      request.input("isNoCheckExpressCorp", body.isNoCheckExpressCorp);
      request.input("expressCode", body.expressCode);
      request.input("expressCorp_Id", body.expressCorp_Id);
      request.input("weight", body.weight);
      request.input("user_Id", userInfo.user_Id);
      request.input("outer_Id", body.outer_Id);
      request.output("outMsg", sql.NVarChar(2000));
      let result = await request.execute("sp_Sale_SendScan");
      let outMsg = result.output.outMsg;
 
      if (outMsg) {
        this.info.msg = outMsg;
        this.info.result = false;
        ctx.body = this.info;
        return;
      } else {
        this.info.msg = "打包单号" + saleOuter.outerCode + "发货成功。";
        this.info.result = true;
        this.info.data = {
          orderCode: saleOuter.outerCode,
          expressCode: saleOuter.expressCode,
          shippingName: saleOuter.shippingName,
          shippingAddress: saleOuter.shippingAddress,
          weight: body.weight || 0
        };
      }
    } catch (error) {
      this.info.msg = "错误:" + error.message;
      this.info.result = false;
    }
 
    ctx.body = this.info;
  }
  //#endregion
}