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