333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
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
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;
  }
}