Blame view

src/pages/Order/components/ModifiedDiffModal.tsx 6.78 KB
zhongnanhuang authored
1
2
import { postServiceOrderModifiedDiff } from '@/services';
import { enumValueToLabel, getAliYunOSSFileNameFromUrl } from '@/utils';
3
4
import { getReceivingCompanyOptions } from '@/utils/order';
import { Button, Divider, Modal, Space, Table, TableProps } from 'antd';
zhongnanhuang authored
5
6
7
import Base64 from 'base-64';
import { useEffect, useState } from 'react';
import {
8
  PAYEE_OPTIONS,
zhongnanhuang authored
9
10
11
12
13
  PRODUCT_BELONG_DEPARTMENT_OPTIONS,
  SHIPPING_WAREHOUSE_OPTIONS,
} from '../constant';
import '../table.less';
14
15
export default ({ setVisible, subOrders, mainOrder, onClose }) => {
  let subIds = subOrders?.map((item: any) => {
zhongnanhuang authored
16
17
18
    return item.id;
  });
19
20
21
22
  let mainId = mainOrder?.id;

  const [subOrderDiffs, setSubOrderDiffs] = useState([]);
  const [mainOrderDiffs, setMainOrderDiffs] = useState([]);
zhongnanhuang authored
23
24
25
26

  async function loadData() {
    let res = await postServiceOrderModifiedDiff({
      data: {
27
28
        subOrderIds: subIds,
        mainOrderId: mainId,
zhongnanhuang authored
29
30
      },
    });
31
32
    setSubOrderDiffs(res?.data?.subOrderDiffs);
    setMainOrderDiffs(res?.data?.mainOrderDiffs);
zhongnanhuang authored
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  }

  useEffect(() => {
    loadData();
  }, []);

  function toChineseName(key: any, text: any) {
    let newText = text;
    if (key === '所属事业部') {
      newText = enumValueToLabel(text, PRODUCT_BELONG_DEPARTMENT_OPTIONS);
    }
    if (key === '发货仓库') {
      newText = enumValueToLabel(text, SHIPPING_WAREHOUSE_OPTIONS);
    }
    if (key === '单价' || key === '合计') {
      newText = '¥' + newText;
    }
50
51
52
53
54
55
    if (key === '开票收款单位') {
      newText = enumValueToLabel(
        text,
        getReceivingCompanyOptions(PAYEE_OPTIONS),
      );
    }
zhongnanhuang authored
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
    return newText;
  }

  function cellRender(value: any, record: any) {
    if (record.fieldName === '附件') {
      return (
        <Space className="max-w-[300px]" wrap>
          {value?.map((item: any, index: any) => {
            let fileName = getAliYunOSSFileNameFromUrl(item);
            return (
              <Button
                className="p-0 pr-2"
                key={index}
                danger={record.isDiff}
                type="link"
                onClick={() => {
                  window.open(
                    '/previewApi/onlinePreview?url=' +
                      encodeURIComponent(Base64.encode(item)),
                  );
                }}
              >
                {fileName}
              </Button>
            );
          })}
        </Space>
      );
    }
    return (
      <div
        title={toChineseName(record.fieldName, value)}
88
        className="max-w-[250px] whitespace-no-wrap overflow-hidden overflow-ellipsis"
zhongnanhuang authored
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
      >
        <span className={record.isDiff ? 'text-[red]' : ''}>
          {toChineseName(record.fieldName, value)}
        </span>
      </div>
    );
  }

  interface DataType {
    fieldName: string;
    oldValue: string;
    newValue: string;
    isDiff: boolean;
  }

  const columns: TableProps<DataType>['columns'] = [
    {
      title: '字段名',
      dataIndex: 'fieldName',
      key: 'fieldName',
109
110
111
112
113
114
115
116
117
118
      render(value) {
        return (
          <div
            title={value}
            className="max-w-[80px] whitespace-no-wrap overflow-hidden overflow-ellipsis"
          >
            {value}
          </div>
        );
      },
zhongnanhuang authored
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
    },
    {
      title: '修改前字段值',
      dataIndex: 'oldValue',
      key: 'oldValue',
      render(value, record) {
        return cellRender(value, record);
      },
    },
    {
      title: '修改后(当前)字段值',
      dataIndex: 'newValue',
      key: 'newValue',
      render(value, record) {
        return cellRender(value, record);
      },
    },
  ];
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
  function loadSubOrderDiffTable(item: any, index: any) {
    //转换为表格数据
    let oldDatas = item[0];
    let curDatas = item[1];
    let diffFiledNames = oldDatas?.diffFieldsName;

    let tableData = [];
    let visibleFields = [
      ['productName', '商品名称'],
      ['productCode', '商品编码'],
      ['parameters', '商品参数'],
      ['quantity', '数量'],
      ['productPrice', '单价'],
      ['unit', '单位'],
      ['subOrderPayment', '合计'],
      ['productBelongBusiness', '所属事业部'],
      ['shippingWarehouse', '发货仓库'],
      ['notes', '备注'],
      ['listAnnex', '附件'],
    ];
    for (let field of visibleFields) {
      let filedKey = field[0];
      let filedName = field[1];
      tableData.push({
        fieldName: filedName,
        oldValue: oldDatas[filedKey],
        newValue: curDatas[filedKey],
        isDiff: diffFiledNames?.includes(filedKey),
      });
    }
    return (
      <>
        <Divider orientation="left">商品{index + 1}:</Divider>
        <Table
          className="myTable"
          size="small"
          pagination={false}
          key={index}
          columns={columns}
          dataSource={tableData}
        />
      </>
    );
  }

  function loadMainOrderDiffTable(item: any, index: any) {
    if (!item || item.length <= 0) {
      return;
    }
    //转换为表格数据
    let oldDatas = item[0];
    let curDatas = item[1];
    let diffFiledNames = oldDatas?.diffFieldsName;

    let tableData = [];
    let visibleFields = [
      ['salesCode', '销售代号'],
      ['customerName', '收货人姓名'],
      ['customerContactNumber', '收货人联系手机号'],
      ['customerShippingAddress', '收货人地址信息'],
      ['institutionContactName', '单位联系人'],
      ['institution', '单位'],
      ['totalPayment', '支付总金额'],
      ['notes', '备注'],
      ['bank', '开户银行'],
      ['bankAccountNumber', '银行账号'],
      ['invoiceIdentificationNumber', '开票识别号'],
      ['receivingCompany', '开票收款单位'],
    ];
    for (let field of visibleFields) {
      let filedKey = field[0];
      let filedName = field[1];
      tableData.push({
        fieldName: filedName,
        oldValue: oldDatas[filedKey],
        newValue: curDatas[filedKey],
        isDiff: diffFiledNames?.includes(filedKey),
      });
    }
    return (
      <Table
        className="myTable"
        size="small"
        pagination={false}
        key={index}
        columns={columns}
        dataSource={tableData}
      />
    );
  }
zhongnanhuang authored
229
230
231
232
233
234
235
236
237
238
239
240
  return (
    <>
      <Modal
        width={700}
        open
        title="信息对比"
        okText="返回"
        cancelText={false}
        onOk={() => {
          setVisible(false);
          onClose();
        }}
241
242
243
244
245
246
        onCancel={() => {
          setVisible(false);
        }}
        cancelButtonProps={{
          hidden: true,
        }}
zhongnanhuang authored
247
248
        destroyOnClose={true}
      >
249
250
        <Divider>主订单信息:</Divider>
        {loadMainOrderDiffTable(mainOrderDiffs, 0)}
zhongnanhuang authored
251
252
253
254
        <Divider>子订单信息:</Divider>
        {subOrderDiffs?.map((item: any, index) => {
          return loadSubOrderDiffTable(item, index);
zhongnanhuang authored
255
256
257
258
259
        })}
      </Modal>
    </>
  );
};