order.ts 5.0 KB
Newer Older
Z
zengqiao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
import { observable, action } from 'mobx';
import { IBaseOrder, IOrderInfo, IStatusMap, IConfigInfo, IApprovalOrder, IBrokerMetadata, IBatchApproval, IBatchApprovalData, IBatchData } from 'types/base-type';
import { getOrderTypeList, getApplyOrderList, getApprovalOrderList, getOrderDetail, cancelOrder, approvalOrder, getBrokerMetadata, getBrokerBasicInfo, batchApprovalOrders } from 'lib/api';
import { setCookie, getCookie } from 'lib/utils';

class Order {
  @observable
  public applyUnique: number = null;

  @observable
  public unique: number = null;

  @observable
  public apply: number = null;

  @observable
  public approval: number = null;

  @observable
  public loading: boolean = false;

Z
v2.1 fe  
zengqiao 已提交
22 23 24
  @observable
  public selectedRows: any[] = [];

Z
zengqiao 已提交
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
  @observable
  public orderList: IBaseOrder[] = [];

  @observable
  public applyList: IBaseOrder[] = [];

  @observable
  public approvalList: IBaseOrder[] = [];

  @observable
  public orderTypeMap: IStatusMap = {};

  @observable
  public orderInfo: IOrderInfo = {
    applicant: {},
    detail: {},
    approverList: [],
  } as IOrderInfo;

  @observable
  public brokerMetadata: IBrokerMetadata[] = [];

  @observable
  public brokerBasicInfo: IBrokerMetadata[] = [];

  @observable
  public existence: number[] = [];

  @observable
  public batchApprovalList: IBatchData[] = [];

  @action.bound
  public setLoading(value: boolean) {
    this.loading = value;
  }

Z
v2.1 fe  
zengqiao 已提交
61 62 63 64 65 66 67 68 69
  @action.bound
  public setSelectedRows(rows?: any[]) {
    if (rows) {
      this.selectedRows = rows;
    } else {
      this.selectedRows = [];
    }
  }

Z
zengqiao 已提交
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
  @action.bound
  public setOrderList(data: IBaseOrder[]) {
    this.orderList = data;
    this.setLoading(false);
  }

  @action.bound
  public setApplyList(data: IBaseOrder[]) {
    this.applyList = data;
    if (this.applyUnique === 0) {
      setCookie([{ key: 'apply', value: `${data.length}`, time: 1 }]);
      this.apply = Number(getCookie('apply'));
      if ( data && data.length ) {
        this.apply = data.length;
      }
    }
    this.setLoading(false);
  }

  @action.bound
  public setApprovalList(data: IBaseOrder[]) {
    this.approvalList = data;
    if (this.unique === 0) {
      setCookie([{ key: 'approval', value: `${data.length}`, time: 1 }]);
      this.approval = Number(getCookie('approval'));
      if ( data && data.length ) {
        this.approval = data.length;
      }
    }
    this.setLoading(false);
  }

  @action.bound
  public setOrderDetail(data: IOrderInfo) {
    this.orderInfo = data || {
      applicant: {},
      detail: {},
      approverList: [],
    } as IOrderInfo;
    this.setLoading(false);
  }

  @action.bound
  public setOrderTypeList(data: IConfigInfo[]) {
    data.map(item => {
      this.orderTypeMap[item.type] = item.message;
    });
  }

  @action.bound
  public setBrokerMetadata(data: IBrokerMetadata[]) {
    this.brokerMetadata = data ? data.map((item, index) => {
      item.key = index;
      return {
        ...item,
        value: item.brokerId,
        label: item.host,
        text: `${item.host} (BrokerID:${item.brokerId}`,
      };
    }) : [];
    return this.brokerMetadata;
  }

  @action.bound
  public setBrokerBasicInfo(data: IBrokerMetadata[]) {
    const existList = data.filter(ele => ele.logicClusterId);
    this.existence = existList.map(ele => {
      return ele.brokerId;
    });
    this.brokerBasicInfo = data ? data.map((item, index) => {
      item.key = index;
      return {
        ...item,
        value: item.brokerId,
        lable: item.host,
      };
    }) : [];
  }

  @action.bound
  public setBatchApprovalOrders(data: IBatchApprovalData[]) {
    const failList = data.filter(ele => ele.result.code !== 0);
    const successList = data.filter(ele => ele.result.code === 0);
    const approvalData = failList.concat(successList);
    return this.batchApprovalList = approvalData.map(ele => {
      return {
        id: ele.id,
        code: ele.result.code,
        message: ele.result.message,
      };
    });
  }

  public getOrderTypeList() {
    getOrderTypeList().then(this.setOrderTypeList);
  }

  public getApplyOrderList(status: number) {
    this.applyUnique = status;
    this.setLoading(true);
    getApplyOrderList(status).then(this.setApplyList);
  }

  public getApprovalList(status: number) {
    this.unique = status;
    this.setLoading(true);
    getApprovalOrderList(status).then(this.setApprovalList);
  }

  public getOrderDetail(orderId: number) {
    this.setLoading(true);
    getOrderDetail(orderId).then(this.setOrderDetail);
  }

  public getBrokerMetadata(clusterId: number) {
    return getBrokerMetadata(clusterId).then(this.setBrokerMetadata);
  }

  public getBrokerBasicInfo(clusterId: number) {
    getBrokerBasicInfo(clusterId).then(this.setBrokerBasicInfo);
  }

  public approvalOrder(value: IApprovalOrder) {
    return approvalOrder(value);
  }

  public cancelOrder(id: number) {
    return cancelOrder(id);
  }

  public batchApprovalOrders(params: IBatchApproval) {
    return batchApprovalOrders(params).then(this.setBatchApprovalOrders);
  }
}

export const order = new Order();