index.jsx 6.81 KB
Newer Older
TZW's avatar
TZW committed
1 2 3 4
/* 数据字典
 * @Author: Li Hanlin
 * @Date: 2022-11-24 11:16:02
 * @Last Modified by: Li Hanlin
TZW's avatar
TZW committed
5
 * @Last Modified time: 2022-12-05 16:20:55
TZW's avatar
TZW committed
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
 */
import * as React from 'react';
import { useState, useMemo, useRef } from 'react';
import DrawerPro from '@/components/DrawerPro';
import AutoTable from '@/components/AutoTable';
import PremButton from '@/components/PremButton';
import getcolumns from './columns';
import { doFetch } from '@/utils/doFetch';
import { message } from 'antd';

function Type(props) {
  const actionRef = useRef(),
    formRef = useRef();
  const [drawer, setDrawer] = useState({
    visible: false,
  });

  const urlParams = {
    save: '/base/sysEtcData/save',
    remove: '/base/sysEtcData/delete',
    list: '/base/sysData/query/tree',
    detail: '/base/sysEtcData/detail',
  };

  const detail = (text, row, _, action) => {
    return (
      <PremButton
        btn={{
          size: 'small',
          type: 'link',
          onClick: () => {
            setDrawer((s) => ({
              ...s,
              visible: true,
              item: row,
              title: '详情',
              val: 'detail',
              title: row.userName + '的详细信息',
            }));
          },
        }}
      >
        详情
      </PremButton>
    );
  };

  const edit = (text, row, _, action) => {
    return (
      <PremButton
        btn={{
          size: 'small',
TZW's avatar
TZW committed
58
          disabled: row?.dataType == 1 ? true : false,
TZW's avatar
TZW committed
59
          onClick: () => {
TZW's avatar
TZW committed
60
            //console.log(row);
TZW's avatar
TZW committed
61 62 63 64 65
            setDrawer((s) => ({
              ...s,
              visible: true,
              item: {
                ...row,
TZW's avatar
TZW committed
66 67
                dataType: row?.dataTypeName,
                dataName: row?.title,
TZW's avatar
TZW committed
68 69 70 71
              },
              title: '编辑',
              val: 'edit',
              onFinish: async (vals) => {
TZW's avatar
TZW committed
72
                //console.log(vals);
TZW's avatar
TZW committed
73 74 75 76 77
                let params = {
                  ...vals,
                  id: row.key,
                  parentId: row.parentKey,
                };
TZW's avatar
TZW committed
78
                //console.log(params);
TZW's avatar
TZW committed
79
                delete params.title;
TZW's avatar
TZW committed
80
                delete params.dataType;
TZW's avatar
TZW committed
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
                let res = await doFetch({
                  url: urlParams.save,
                  params,
                });
                if (res.code === '0000') {
                  message.success('编辑成功!');
                  setDrawer((s) => ({
                    ...s,
                    visible: false,
                  }));
                  actionRef.current.reload();
                }
              },
            }));
          },
        }}
      >
        编辑
      </PremButton>
    );
  };

  const addChild = (text, row, _, action) => {
    return (
      <PremButton
        btn={{
TZW's avatar
TZW committed
107
          // disabled: row?.dataType == 1 ? true : false,
TZW's avatar
TZW committed
108 109 110 111 112 113 114
          size: 'small',
          onClick: () => {
            setDrawer((s) => ({
              ...s,
              visible: true,
              detailpath: null,
              item: {
TZW's avatar
TZW committed
115
                dataType: '自定义',
TZW's avatar
TZW committed
116
              },
TZW's avatar
TZW committed
117
              title: '新增数据',
TZW's avatar
TZW committed
118 119
              val: 'addChild',
              onFinish: async (vals) => {
TZW's avatar
TZW committed
120
                //console.log(vals);
TZW's avatar
TZW committed
121 122 123 124
                let params = {
                  ...vals,
                  parentId: row.key,
                };
TZW's avatar
TZW committed
125
                delete params.dataType;
TZW's avatar
TZW committed
126 127 128 129 130
                let res = await doFetch({
                  url: urlParams.save,
                  params,
                });
                if (res.code === '0000') {
TZW's avatar
TZW committed
131
                  message.success('新增数据成功!');
TZW's avatar
TZW committed
132 133 134 135 136 137 138 139 140 141 142
                  setDrawer((s) => ({
                    ...s,
                    visible: false,
                  }));
                  actionRef.current.reload();
                }
              },
            }));
          },
        }}
      >
TZW's avatar
TZW committed
143
        新增数据
TZW's avatar
TZW committed
144 145 146 147 148 149 150 151
      </PremButton>
    );
  };

  const remove = (text, row, _, action) => {
    return (
      <PremButton
        pop={{
TZW's avatar
TZW committed
152 153
          title: '是否删除该字典项?',
          disabled: row?.dataType == 1 ? true : false,
TZW's avatar
TZW committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
          okText: '确认',
          cancelText: '取消',
          onConfirm: async () => {
            let res = await doFetch({ url: urlParams.remove, params: { id: row.key } });
            if (res.code === '0000') {
              message.success('删除成功!');
              setDrawer((s) => ({
                ...s,
                visible: false,
              }));
              actionRef.current.reload();
            }
          },
        }}
        btn={{
          size: 'small',
TZW's avatar
TZW committed
170
          disabled: row?.dataType == 1 ? true : false,
TZW's avatar
TZW committed
171 172 173 174 175 176 177 178 179 180
          type: 'danger',
        }}
      >
        删除
      </PremButton>
    );
  };

  const columns = useMemo(() => {
    let defcolumn = getcolumns(setDrawer);
TZW's avatar
TZW committed
181

TZW's avatar
TZW committed
182 183 184 185
    return defcolumn.concat({
      title: '操作',
      valueType: 'option',
      width: 150,
TZW's avatar
TZW committed
186 187 188 189 190 191 192
      render: (text, row, _, action) => {
        if (row?.parentKey == '0') {
          return [addChild(text, row, _, action)];
        } else {
          return [edit(text, row, _, action), remove(text, row, _, action)];
        }
      },
TZW's avatar
TZW committed
193 194 195 196 197 198 199 200 201 202
    });
  }, [drawer.val]);

  return (
    <div style={{ position: 'relative' }}>
      <AutoTable
        pagetitle={<h3 className="page-title">数据字典</h3>}
        columns={columns}
        path={urlParams.list}
        actionRef={actionRef}
TZW's avatar
TZW committed
203
        rowKey={'key'}
TZW's avatar
TZW committed
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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
        resizeable={false}
        addconfig={{
          // access: 'sysDepartment_save',
          btn: {
            disabled: false,
            type: 'primary',
            onClick: () => {
              setDrawer((s) => ({
                ...s,
                visible: true,
                item: {
                  departmentTypeName: '公司类型',
                },
                title: '新增',
                detailpath: null,
                val: 'add',
                onFinish: async (vals) => {
                  let params = {
                    ...vals,
                    departmentType: 0,
                  };
                  let res = await doFetch({
                    url: urlParams.save,
                    params,
                  });
                  if (res.code === '0000') {
                    message.success('新增成功!');
                    setDrawer((s) => ({
                      ...s,
                      visible: false,
                    }));
                    actionRef.current.reload();
                  }
                },
              }));
            },
          },
        }}
        pagination={false}
      />

      <DrawerPro
        fields={columns}
        defaultFormValue={drawer?.item ?? {}}
        formRef={formRef}
        placement="right"
        onClose={() => {
          setDrawer((s) => ({
            ...s,
            visible: false,
          }));
        }}
        {...drawer}
      />
    </div>
  );
}

export default Type;