import BaseController from "../baseController";
|
import { Post } from "egg-shell-decorators";
|
import { In } from "typeorm";
|
import { RequireVoucher } from "../../entity/outbound/require/requireVoucher";
|
import * as mssql from "mssql";
|
|
export default class VoucherController extends BaseController {
|
//#region 确认兑换
|
@Post()
|
public async exhange() {
|
let userInfo = await this.userInfo;
|
let ids = this.body.ids;
|
let storage_Id = this.body.storage_Id;
|
if (!Array.isArray(ids) || !ids.length) {
|
this.info.result = false;
|
this.info.msg = "数据不存在";
|
this.ctx.body = this.info;
|
return;
|
}
|
|
if (!storage_Id) {
|
this.info.result = false;
|
this.info.msg = "仓库不能为空";
|
this.ctx.body = this.info;
|
return;
|
}
|
|
try {
|
let dataList = await this.dbRead.find(RequireVoucher, {
|
voucher_Id: In(ids)
|
});
|
let msg = [];
|
for (let item of dataList) {
|
if (item.validDate.getTime() <= new Date().getTime()) {
|
this.info.result = false;
|
this.info.msg = "已过期无法兑换";
|
this.dbWrite.update(
|
RequireVoucher,
|
{
|
voucher_Id: item.voucher_Id
|
},
|
{
|
voucherStatus: "已过期"
|
}
|
);
|
this.ctx.body = this.info;
|
return;
|
}
|
const connection: any = await this.dbWrite.connection;
|
let request = new mssql.Request(connection.driver.master);
|
request.input("Voucher_Id", item.voucher_Id);
|
request.input("user_Id", userInfo.user_Id);
|
request.input("storage_Id", storage_Id);
|
request.input("exchangeUser", this.body.exchangeUser);
|
request.input("mobile", this.body.mobile);
|
request.output("outMsg", mssql.NVarChar(2000));
|
let result = await request.execute("sp_Require_Voucher_ToSaleOrder");
|
let outMsg = result.output.outMsg;
|
if (outMsg) {
|
this.info.result = false;
|
msg.push(item.voucherCode + "兑换失败:" + outMsg);
|
} else {
|
msg.push(item.voucherCode + "兑换成功");
|
this.info.result = true;
|
}
|
}
|
this.info.msg = msg.join(",");
|
} catch (error) {
|
this.info.result = false;
|
this.info.msg = "兑换失败," + error.message;
|
}
|
this.ctx.body = this.info;
|
}
|
}
|