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
import { default as BaseController } from "../../baseController";
import { Post } from "egg-shell-decorators";
import { BaseProvider } from "../../../entity/basicInfo/base/baseProvider";
import { isNumber } from "util";
 
export default class ProviderController extends BaseController {
  //#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 === "provider") {
        // 前端货主登录权限
        whereStr = `userProduct_Id=:userProduct_Id`;
        where = {
          userProduct_Id: userInfo.userProduct_Id
        };
      } else {
        whereStr = `userProduct_Id=:userProduct_Id 
        And provider_Id in(SELECT Node_Id FROM dbo.Sys_RoleAuthData WHERE DataType_Id=1 AND User_Id=:user_Id And AuthValue=1)`;
        where = {
          userProduct_Id: userInfo.userProduct_Id,
          user_Id: userInfo.user_Id
        };
      }
      // 关键词查询条件
      if (body.name) {
        let name = this.ctx.helper.sqlSecurity(body.name);
        whereStr += ` AND (providerShortName LIKE '%' + :name + '%' OR providerCode LIKE '%' + :name + '%')`;
        where.name = name;
      }
      let fields = ["provider_Id", "providerShortName"];
      // 自定义查询字段
      if (body.searchFields) {
        fields = body.searchFields;
      }
      if (body.appendField === "*") {
        fields = [];
      }
      let builder = this.dbRead.createQueryBuilder(BaseProvider, "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 (e) {
      this.info.result = false;
      this.info.msg = e.message;
    }
    ctx.body = this.info;
  }
 
  //#region 获取供应商
  /**
   * 获取供应商
   */
  @Post()
  public async getLists() {
    let { ctx } = this;
 
    try {
      let dataList = await this.dbRead.find(BaseProvider, {
        select: ["provider_Id", "providerName"]
      });
 
      this.info.result = true;
      this.info.data = dataList;
    } catch (error) {
      this.info.result = false;
      this.info.data = error.message;
    }
    ctx.body = this.info;
  }
  //#endregion
 
  //#endregion
  //#region  获取供应商
  /**
   * _获取供应商
   */
  // @Post()
  // public async getList() {
  //   let { ctx } = this;
  //   let userInfo = await ctx.helper.userInfo();
  //   try {
  //     var where = await await ctx.service.auth.getProviderAuth("string");
  //     // var dataList = await this.dbRead.find(BaseProvider, {
  //     //   select: ["provider_Id", "providerCode", "providerShortName"],
  //     //   where: { userProduct_Id: userInfo.userProduct_Id, enable: 1 }
  //     // });
  //     let dataList = await this.dbRead
  //       .createQueryBuilder(BaseProvider, "t")
  //       .where(`userProduct_Id=:userProduct_Id and enable=1 and ` + where, {
  //         userProduct_Id: userInfo.userProduct_Id
  //       })
  //       .getMany();
  //     this.info.result = true;
  //     this.info.data = dataList;
  //   } catch (e) {
  //     this.info.result = false;
  //     this.info.msg = e.message;
  //   }
  //   ctx.body = this.info;
  // }
  //#endregion
  //#region  获取供应商
 
  //#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.providerCode) {
      this.info.result = false;
      this.info.msg = "供应商编号不能为空";
      ctx.body = this.info;
      return;
    }
 
    if (!body.providerShortName) {
      this.info.result = false;
      this.info.msg = "供应商简称不能为空";
      ctx.body = this.info;
      return;
    }
 
    if (!body.providerName) {
      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(BaseProvider, {
        where: {
          providerCode: body.providerCode,
          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(BaseProvider, {
        where: {
          providerShortName: body.providerShortName,
          userProduct_Id: userProduct_Id
        }
      });
      if (dataInfo) {
        this.info.result = false;
        this.info.msg = "供应商已存在,不能重复推送";
        ctx.body = this.info;
        return;
      }
 
      dataInfo = new BaseProvider();
      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
}