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
|
}
|