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
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
import { default as BaseController } from "../../baseController";
import { BaseConsignor } from "../../../entity/basicInfo/consignor/baseConsignor";
import { Post } from "egg-shell-decorators";
import { isNumber } from "util";
import * as XLSX from "xlsx";
import * as path from "path";
import { BaseCity } from "../../../entity/basicInfo/base/baseCity";
 
export default class ConsignorController extends BaseController {
  //#region 根据货主ID获取货主信息
  /**
   * 根据货主ID获取货主信息
   */
  @Post()
  public async getById() {
    let { ctx } = this;
    let body = ctx.body;
    let userInfo = await ctx.helper.userInfo();
    let userProduct_Id = userInfo.userProduct_Id;
    if (!body.consignor_Id) {
      this.info.result = false;
      this.info.msg = "数据不存在";
      ctx.body = this.info;
      return;
    }
 
    try {
      let dataInfo = await this.dbRead.findOne(BaseConsignor, {
        select: ["consignor_Id", "consignorCode", "consignorName", "mobile", "address", "creditLine", "premiumRate", "consignorType"],
        where: {
          consignor_Id: body.consignor_Id,
          userProduct_Id: userProduct_Id
        },
        order: {
          consignor_Id: "ASC"
        }
      });
 
      this.info.result = true;
      this.info.data = dataInfo;
    } catch (error) {
      this.info.result = false;
      this.info.data = error.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 获得货主列表
  /**
   * 获得货主列表
   */
  @Post()
  public async getList() {
    let { ctx } = this;
    let userInfo = await ctx.helper.userInfo();
    let body = ctx.request.body;
 
    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 consignor_Id in(SELECT Node_Id FROM dbo.Sys_RoleAuthData WHERE DataType_Id=1 AND User_Id=:user_Id And AuthValue=1)`;
        whereStr = await this.ctx.service.auth.getConsignorAuth("string");
        where = {
          userProduct_Id: userInfo.userProduct_Id,
          user_Id: userInfo.user_Id
        };
      }
      // 关键词查询条件
      if (body.name) {
        let name = this.ctx.helper.sqlSecurity(body.name);
        whereStr += ` AND (consignorName LIKE '%' + :name + '%' OR consignorCode LIKE '%' + :name + '%')`;
        where.name = name;
      }
      let fields = ["consignor_Id", "consignorCode", "consignorName"];
      // 自定义查询字段
      if (body.searchFields) {
        fields = body.searchFields;
      }
      if (body.appendField === "*") {
        fields = [];
      }
      let builder = this.dbRead.createQueryBuilder(BaseConsignor, "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 (error) {
      this.info.result = false;
      this.info.data = error.message;
    }
    ctx.body = this.info;
  }
  //#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.consignorCode) {
      this.info.result = false;
      this.info.msg = "货主编号不能为空";
      ctx.body = this.info;
      return;
    }
 
    if (!body.consignorName) {
      this.info.result = false;
      this.info.msg = "货主名称不能为空";
      ctx.body = this.info;
      return;
    }
    if (!isNumber(body.enable)) {
      body.enable = parseInt(body.enable);
    }
 
    try {
      let dataInfo = await this.dbRead.findOne(BaseConsignor, {
        where: {
          consignorName: body.consignorName,
          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 = new BaseConsignor();
      dataInfo = Object.assign(dataInfo, this.body);
      await this.setAccountInfo(dataInfo);
      dataInfo.enable = 1;
      await this.dbWrite.save(dataInfo);
 
      this.info.result = true;
      this.info.data = {
        consignor_Id: dataInfo.consignor_Id,
        consignorCode: dataInfo.consignorCode,
        consignorName: dataInfo.consignorName
      };
      this.info.msg = "货主信息保存成功";
    } catch (error) {
      this.info.result = false;
      this.info.data = error.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#region 更新货主信息
  /**
   * 更新货主信息
   */
  @Post()
  public async update() {
    let { ctx } = this;
    let body = ctx.body;
    let userInfo = await ctx.helper.userInfo();
    let userProduct_Id = userInfo.userProduct_Id;
    if (!body.consignorCode) {
      this.info.result = false;
      this.info.msg = "货主编号不能为空";
      ctx.body = this.info;
      return;
    }
 
    try {
      let dataInfo = await this.dbRead.findOne(BaseConsignor, {
        where: {
          consignorCode: body.consignorCode,
          userProduct_Id: userProduct_Id
        },
        order: {
          consignor_Id: "ASC"
        }
      });
      if (!dataInfo) {
        this.info.result = false;
        this.info.msg = "账号不存在";
        ctx.body = this.info;
        return;
      }
 
      dataInfo = Object.assign(dataInfo, this.body);
      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
 
  //#region 客户端修改密码
  @Post()
  public async modifyPwd() {
    try {
      //校验数据
      if (!this.body.oldPwd) {
        this.info.result = false;
        this.info.msg = "请输入旧密码!";
        this.ctx.body = this.info;
        return;
      }
      if (!this.body.userPwd) {
        this.info.result = false;
        this.info.msg = "请输入密码!";
        this.ctx.body = this.info;
        return;
      }
      if (!this.body.repeatPassWord) {
        this.info.result = false;
        this.info.msg = "请确认密码!";
        this.ctx.body = this.info;
        return;
      }
      if (this.body.repeatPassWord != this.body.userPwd) {
        this.info.result = false;
        this.info.msg = "新密码与确认密码不一致!";
        this.ctx.body = this.info;
        return;
      }
 
      var userInfo = await this.dbRead.findOne(BaseConsignor, {
        consignor_Id: this.body.consignor_Id,
        password: this.ctx.helper.md5EncodingSalt(this.body.oldPwd)
      });
      if (!userInfo) {
        this.info.result = false;
        this.info.msg = "旧密码不正确!";
        this.ctx.body = this.info;
        return;
      }
      //修改密码
      let result = await this.dbWrite.save(BaseConsignor, {
        consignor_Id: this.body.consignor_Id,
        password: this.ctx.helper.md5EncodingSalt(this.body.userPwd)
      });
      if (result) {
        this.info.result = true;
        this.info.msg = "修改成功!";
      } else {
        this.info.result = false;
        this.info.msg = "修改失败!";
      }
    } catch {
      this.info.result = false;
      this.info.msg = "修改失败!";
    }
    this.ctx.body = this.info;
  }
  //#endregion
 
  //#region 货主导入Excel
  @Post()
  public async importExcel() {
    setTimeout(async () => {
      await this.importExcelWork();
    }, 0);
 
    this.info.result = true;
    this.info.msg = "开始上传...";
    this.ctx.body = this.info;
  }
  //#endregion
 
  //#region importExcelWork
  private async importExcelWork() {
    let { ctx } = this;
    let userInfo = await this.userInfo;
    let body = ctx.request.body;
    let redis = ctx.app.redis.clients.get("common"); // 将消息放入redis缓存
    let fileUrl = body.fileUrl;
    redis.expire(body.key, 5 * 60);
    if (!fileUrl) {
      this.setMsg("上传文件不存在", "red");
      return;
    }
    if (!body.key) {
      this.setMsg("上传key不存在", "red");
      return;
    }
 
    try {
      let rootPath = path.resolve(); // 获得根目录
      let filePath = rootPath + path.sep + fileUrl.replace(/\//gi, path.sep); // 上传文件路径
      var workbook = XLSX.readFile(filePath); //整个 excel 文档
      var sheetNames = workbook.SheetNames; //获取所有工作薄名
      var sheet1 = workbook.Sheets[sheetNames[0]]; //根据工作薄名获取工作薄
      let dataList = XLSX.utils.sheet_to_json(sheet1); // 获得当前sheet表单数据转为json格式
 
      //#region 验证数据正确性
      this.info.result = true;
      if (!dataList.length) {
        this.setMsg("没有可导入的数据", "red");
        return;
      }
      //#endregion
 
      let i = 1,
        successCount = 0,
        updateCount = 0;
      let dataRows = []; // 数据集合
      for (let row of dataList) {
        i++;
        let data = {
          consignorName: row["货主名称"],
          consignorType: row["货主类型"],
          linker: row["联系人"],
          address: row["联系地址"],
          mobile: row["手机号"],
          email: row["电子邮箱"],
          province_Id: 0,
          provinceName: row["省"],
          city_Id: 0,
          cityName: row["市"],
          region_Id: 0,
          regionName: row["区"],
          lng: row["经度"],
          lat: row["纬度"]
        };
 
        //#region  // 校验数据
        if (!data.consignorName) {
          this.setMsg(`第${i}行、货主名称为空`, "red");
          updateCount++;
        }
        if (!data.provinceName) {
          this.setMsg(`第${i}行、省为空`, "red");
          updateCount++;
        }
        if (!data.cityName) {
          this.setMsg(`第${i}行、市为空`, "red");
          updateCount++;
        }
        if (!data.regionName) {
          this.setMsg(`第${i}行、区为空`, "red");
          updateCount++;
        }
        // 查询省市区
        let pInfo = await this.dbRead.findOne(BaseCity, {
          cityName: data.provinceName,
          parentId: 0
        });
        if (pInfo) {
          // 市
          let cpInfo = await this.dbRead.findOne(BaseCity, {
            cityName: data.cityName,
            parentId: pInfo.city_Id
          });
          // 区
          let rpInfo = await this.dbRead.findOne(BaseCity, {
            cityName: data.regionName,
            parentId: cpInfo ? cpInfo.city_Id : 0
          });
 
          // 赋值省市区ID
          data.province_Id = pInfo.city_Id;
          data.city_Id = cpInfo.city_Id;
          data.region_Id = rpInfo ? rpInfo.city_Id : 0;
        }
        //#endregion
 
        dataRows.push(data);
      }
 
      if (this.isMsgError) {
        this.setMsg(`导入数据有错误,请处理好重新导入`, "red");
        this.setMsg("-1");
        return;
      }
      i = 1;
      successCount = 0;
      updateCount = 0;
      let rows = []; // 准备更新或者新增的数据
      for (let data of dataRows) {
        i++;
        // 查询货主是否存在
        let conInfo = await this.dbRead.findOne(BaseConsignor, {
          consignorName: "" + data.consignorName,
          userProduct_Id: userInfo.userProduct_Id
        });
        conInfo = Object.assign(conInfo, data);
        await this.setAccountInfo(conInfo, "consignor_Id");
 
        if (conInfo) {
          this.setMsg(`第${i}行、${data.consignorName}已存在,正在更新...`);
          updateCount++;
          rows.push(conInfo);
        } else {
          this.setMsg(`第${i}行、${data.consignorName}正在导入...`);
          let consignorCode = await this.ctx.service.common.getCodeRegular(501);
          data.consignorCode = consignorCode;
          successCount++;
          rows.push(conInfo);
        }
        if (i % 100 === 0) {
          await this.dbWrite.save(BaseConsignor, rows);
          rows = [];
        }
      }
      if (rows.length > 0) {
        await this.dbWrite.save(BaseConsignor, rows);
        rows = [];
      }
      this.setMsg(`导入成功,新增${successCount}条,更新${updateCount}条`, "blue");
    } catch (ex) {
      this.setMsg("出现异常:" + ex.message, "red");
      this.info.result = false;
    }
    this.setMsg("-1");
  }
  //#endregion
}