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
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
import { default as BaseController } from "../../baseController";
import { BaseStorage } from "../../../entity/basicInfo/base/baseStorage";
import { Post } from "egg-shell-decorators";
import { BasePosition } from "../../../entity/basicInfo/base/basePosition";
import { BaseDestination } from "../../../entity/basicInfo/workshop/baseDestination";
 
export default class Storagetroller extends BaseController {
  //#region getList 获得仓库列表
  /**
   * 获得仓库列表
   */
  @Post()
  public async getList() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
 
    try {
      let whereStr = "";
      var where: any = {};
 
      // 超级管理员不需要走仓库权限
      if (userInfo.isAdministrator || body.isAll) {
        whereStr = `userProduct_Id=:userProduct_Id`;
        where = {
          userProduct_Id: userInfo.userProduct_Id
        };
      } else if (userInfo.userType === "consignor") {
        // 前端货主登录权限
        whereStr = `userProduct_Id=:userProduct_Id`;
        where = {
          userProduct_Id: userInfo.userProduct_Id
        };
      } else {
        // whereStr = `userProduct_Id=:userProduct_Id
        // And Storage_Id in(SELECT Node_Id FROM dbo.Sys_RoleAuthData WHERE DataType_Id=2 AND User_Id=:user_Id And AuthValue=1)`;
        whereStr = await this.ctx.service.auth.getStorageAuth("string");
        where = {
          userProduct_Id: userInfo.userProduct_Id,
          user_Id: userInfo.user_Id
        };
      }
      // 关键词查询条件
      if (body.name) {
        let name = this.ctx.helper.sqlSecurity(body.name);
        if (whereStr) whereStr += ` AND `;
        whereStr += `(storageName LIKE '%' + :name + '%' OR storageCode LIKE '%' + :name + '%')`;
        where.name = name;
      }
      let fields = ["storage_Id", "storageCode", "storageName"];
      // 自定义查询字段
      if (body.searchFields) {
        fields = body.searchFields;
      }
      if (body.appendField === "*") {
        fields = [];
      }
      let builder = this.dbRead.createQueryBuilder(BaseStorage, "t").where(whereStr, where);
      if (fields.length) {
        builder.select(fields);
      }
      builder.take(200);
      let dataList = [];
      if (body.appendField === "*") {
        dataList = await builder.getMany();
      } else {
        dataList = await builder.getRawMany();
      }
 
      this.info.result = true;
      this.info.data = dataList;
 
      ctx.body = this.info;
    } catch (ex) {
      let msg = "异常错误信息:" + ex.message;
      this.info.result = false;
      this.info.msg = msg;
      ctx.body = this.info;
    }
  }
  //#endregion
 
  //#region getDestination 获得目的地名称
  /**
   * 获得目的地名称
   */
  @Post()
  public async getDestination() {
    let { ctx } = this;
    let body = ctx.request.body;
 
    try {
      let whereStr = "";
      var where: any = {};
 
      // 关键词查询条件
      if (body.query) {
        let query = this.ctx.helper.sqlSecurity(body.query);
        whereStr += `(destinationName LIKE '%' + :query + '%' OR destinationCode LIKE '%' + :query + '%')`;
        where.query = query;
      }
      let fields = ["destination_Id", "destinationCode", "destinationName"];
      // 自定义查询字段
      if (body.searchFields) {
        fields = body.searchFields;
      }
      if (body.appendField === "*") {
        fields = [];
      }
      let builder = this.dbRead.createQueryBuilder(BaseDestination, "t").where(whereStr, where);
      if (fields.length) {
        builder.select(fields);
      }
      builder.take(200);
      let dataList = [];
      if (body.appendField === "*") {
        dataList = await builder.getMany();
      } else {
        dataList = await builder.getRawMany();
      }
 
      this.info.result = true;
      this.info.data = dataList.map(item => {
        item.value = item.destination_Id;
        item.label = item.destinationName;
        return item;
      });
 
      ctx.body = this.info;
    } catch (ex) {
      let msg = "异常错误信息:" + ex.message;
      this.info.result = false;
      this.info.msg = msg;
      ctx.body = this.info;
    }
  }
  //#endregion
 
  //#region getPositionList 根据仓库ID和货位类型获取货位
  /**
   * 根据仓库ID和货位类型获取货位
   */
  @Post()
  public async getPositionList() {
    let { ctx } = this;
    let body = ctx.request.body;
 
    try {
      var dataList = await this.dbRead.find(BasePosition, {
        select: ["positionName"],
        where: {
          storage_Id: body.storage_Id,
          positionType: body.positionType
        }
      });
 
      this.info.result = true;
      this.info.data = dataList;
 
      ctx.body = this.info;
      return;
    } catch (ex) {
      let msg = "异常错误信息:" + ex.message;
      this.info.result = false;
      this.info.msg = msg;
      ctx.body = this.info;
      return;
    }
  }
  //#endregion
 
  //#region 新增仓库信息
  /**
   * 新增仓库信息
   */
  @Post()
  public async add() {
    let { ctx } = this;
    let body = ctx.body;
    let userInfo = await this.userInfo;
    if (!userInfo && !userInfo.userProduct_Id) {
      this.info.result = false;
      this.info.msg = "数据不存在";
      ctx.body = this.info;
      return;
    }
    let userProduct_Id = userInfo.userProduct_Id;
 
    if (!body.storageCode) {
      this.info.result = false;
      this.info.msg = "仓库编号不能为空";
      ctx.body = this.info;
      return;
    }
 
    if (!body.storageName) {
      this.info.result = false;
      this.info.msg = "仓库名称不能为空";
      ctx.body = this.info;
      return;
    }
 
    try {
      let dataInfo = await this.dbRead.findOne(BaseStorage, {
        where: {
          storageCode: body.storageCode,
          userProduct_Id: userProduct_Id
        }
      });
      if (dataInfo) {
        dataInfo = Object.assign(dataInfo, this.body);
        await this.dbWrite.save(dataInfo);
 
        this.info.result = true;
        this.info.msg = "仓库更新成功";
        ctx.body = this.info;
        return;
      }
 
      dataInfo = await this.dbRead.findOne(BaseStorage, {
        where: {
          storageCode: body.storageCode,
          userProduct_Id: userProduct_Id
        }
      });
      if (dataInfo) {
        this.info.result = false;
        this.info.msg = "仓库已存在,不能重复推送";
        ctx.body = this.info;
        return;
      }
 
      dataInfo = new BaseStorage();
      dataInfo = Object.assign(dataInfo, this.body);
      await this.setAccountInfo(dataInfo);
      await this.dbWrite.save(dataInfo);
 
      this.info.result = true;
      this.info.msg = "仓库信息保存成功";
    } catch (error) {
      this.info.result = false;
      this.info.data = error.message;
    }
    ctx.body = this.info;
  }
  //#endregion
}