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
import BaseController from "../baseController";
import { Post } from "egg-shell-decorators";
import { SysExportInfo } from "../../entity/sys/import/sysExportInfo";
import { SysExportColumnInfo } from "../../entity/sys/import/sysExportColumnInfo";
import { SysImportInfo } from "../../entity/sys/import/sysImportInfo";
 
/**
 * 通用导入模块
 */
export default class ImportController extends BaseController {
  //#region GetExportInfo
  /// <summary>
  /// 获得导出字段信息
  /// </summary>
  /// <param name="this.body">请求参数</param>
  @Post()
  public async getExportInfo() {
    try {
      var dataInfo = await this.dbRead.findOne(SysExportInfo, this.body.exportInfo_Id);
      if (dataInfo != null) {
        this.info.data = dataInfo;
        this.info.result = true;
      } else {
        this.info.result = false;
        this.info.msg = "获取数据失败";
      }
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "获取数据失败," + ex.message;
    }
 
    this.ctx.body = this.info;
  }
  //#endregion
 
  //#region GetExportColList
  /// <summary>
  /// 获得导出字段信息
  /// </summary>
  /// <param name="this.body">请求参数</param>
  @Post()
  public async getExportColList() {
    try {
      var dataInfo = await this.dbRead.find(SysExportColumnInfo, {
        where: {
          exportInfo_Id: this.body.exportInfo_Id,
          enable: 1
        },
        order: {
          orderNo: "DESC",
          exportInfo_Id: "ASC"
        }
      });
      if (dataInfo != null) {
        this.info.data = dataInfo;
        this.info.result = true;
      } else {
        this.info.result = false;
        this.info.msg = "获取数据失败";
      }
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "获取数据失败," + ex.message;
    }
 
    this.ctx.body = this.info;
  }
  //#endregion
 
  //#region GetImportInfo
  /// <summary>
  /// 获得导出字段信息
  /// </summary>
  /// <param name="this.body">请求参数</param>
  @Post()
  public async getImportInfo() {
    try {
      var dataInfo = await this.dbRead.findOne(SysImportInfo, this.body.importInfo_Id);
      if (dataInfo != null) {
        this.info.data = dataInfo;
        this.info.result = true;
      } else {
        this.info.result = false;
        this.info.msg = "获取数据失败";
      }
    } catch (ex) {
      this.info.result = false;
      this.info.msg = "获取数据失败," + ex.message;
    }
 
    this.ctx.body = this.info;
  }
  //#endregion
 
  //#region startImport
  @Post()
  public async startImport() {
    setTimeout(async () => {
      await this.importWork();
    }, 0);
 
    this.info.result = true;
    this.info.statusCode = 0;
    this.ctx.body = this.info;
  }
 
  /**
   * 批量导入业务处理
   */
  private async importWork() {
    let { ctx } = this;
    let body = ctx.request.body;
    let redis = ctx.app.redis.clients.get("common"); // 将消息放入redis缓存
    let fileUrl = body.fileUrl;
    redis.expire(body.uploadKey, 5 * 60);
    if (!body.uploadKey) {
      redis.rpush(body.uploadKey, "上传key不存在");
      redis.expire(body.uploadKey, 5 * 60);
      return;
    }
    if (!fileUrl) {
      redis.rpush(body.uploadKey, "上传文件不存在");
      redis.expire(body.uploadKey, 5 * 60);
      return;
    }
 
    await this.ctx.service.sys.importCommon.start();
    await redis.rpush(this.body.uploadKey, "执行完成");
    await redis.rpush(this.body.uploadKey, "-1");
    redis.expire(this.body.uploadKey, 5 * 60);
  }
  //#endregion
}