ModifiedDiffModal.tsx 4.54 KB
import { postServiceOrderModifiedDiff } from '@/services';
import { enumValueToLabel, getAliYunOSSFileNameFromUrl } from '@/utils';
import { Button, Modal, Space, Table, TableProps } from 'antd';
import Base64 from 'base-64';
import { useEffect, useState } from 'react';
import {
  PRODUCT_BELONG_DEPARTMENT_OPTIONS,
  SHIPPING_WAREHOUSE_OPTIONS,
} from '../constant';
import '../table.less';

export default ({ setVisible, subOrders, onClose }) => {
  let ids = subOrders?.map((item: any) => {
    return item.id;
  });

  const [diffDatas, setDiffDatas] = useState([]);

  async function loadData() {
    let res = await postServiceOrderModifiedDiff({
      data: {
        subOrderIds: ids,
      },
    });
    let datas = res?.data;
    setDiffDatas(datas);
  }

  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;
    }
    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)}
        className="max-w-[300px] whitespace-no-wrap overflow-hidden overflow-ellipsis"
      >
        <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',
    },
    {
      title: '修改前字段值',
      dataIndex: 'oldValue',
      key: 'oldValue',
      render(value, record) {
        return cellRender(value, record);
      },
    },
    {
      title: '修改后(当前)字段值',
      dataIndex: 'newValue',
      key: 'newValue',
      render(value, record) {
        return cellRender(value, record);
      },
    },
  ];

  return (
    <>
      <Modal
        width={700}
        open
        title="信息对比"
        okText="返回"
        cancelText={false}
        onOk={() => {
          setVisible(false);
          onClose();
        }}
        destroyOnClose={true}
      >
        {diffDatas?.map((item: any, index) => {
          //转换为表格数据
          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 (
            <Table
              className="myTable"
              size="small"
              pagination={false}
              key={index}
              columns={columns}
              dataSource={tableData}
            />
          );
        })}
      </Modal>
    </>
  );
};