index.jsx 6.88 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:22
TZW's avatar
TZW committed
6
 */
wuhao's avatar
wuhao committed
7 8 9 10 11 12
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';
TZW's avatar
TZW committed
13 14
import { doFetch } from '@/utils/doFetch';
import { message } from 'antd';
wuhao's avatar
wuhao committed
15

TZW's avatar
TZW committed
16
function Type(props) {
wuhao's avatar
wuhao committed
17 18
  const actionRef = useRef(),
    formRef = useRef();
TZW's avatar
TZW committed
19
  const [drawer, setDrawer] = useState({
wuhao's avatar
wuhao committed
20 21 22
    visible: false,
  });

TZW's avatar
TZW committed
23 24 25 26 27 28 29
  const urlParams = {
    save: '/auth/sysDepartment/save',
    remove: '/auth/sysDepartment/deleteById',
    list: '/auth/sysDepartment/query/tree',
    detail: '/asset/equipmentType/queryById',
  };

wuhao's avatar
wuhao committed
30 31 32 33 34 35 36
  const detail = (text, row, _, action) => {
    return (
      <PremButton
        btn={{
          size: 'small',
          type: 'link',
          onClick: () => {
TZW's avatar
TZW committed
37
            setDrawer((s) => ({
wuhao's avatar
wuhao committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
              ...s,
              visible: true,
              item: row,
              title: '详情',
              val: 'detail',
              title: row.userName + '的详细信息',
            }));
          },
        }}
      >
        详情
      </PremButton>
    );
  };

  const edit = (text, row, _, action) => {
    return (
      <PremButton
        btn={{
          size: 'small',
          onClick: () => {
TZW's avatar
TZW committed
59
            setDrawer((s) => ({
wuhao's avatar
wuhao committed
60 61
              ...s,
              visible: true,
TZW's avatar
TZW committed
62 63 64 65 66 67
              // detailpath: urlParams.detail,
              // params: { id: row.key },
              item: {
                ...row,
                departmentName: row.title,
              },
wuhao's avatar
wuhao committed
68 69
              title: '编辑',
              val: 'edit',
TZW's avatar
TZW committed
70
              onFinish: async (vals) => {
TZW's avatar
TZW committed
71
                //console.log(vals);
TZW's avatar
TZW committed
72 73 74 75 76 77
                let params = {
                  ...vals,
                  id: row.key,
                  parentId: row.parentKey,
                  departmentType: row.departmentType,
                };
TZW's avatar
TZW committed
78
                //console.log(params);
TZW's avatar
TZW committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92
                delete params.title;
                let res = await doFetch({
                  url: urlParams.save,
                  params,
                });
                if (res.code === '0000') {
                  message.success('编辑成功!');
                  setDrawer((s) => ({
                    ...s,
                    visible: false,
                  }));
                  actionRef.current.reload();
                }
              },
wuhao's avatar
wuhao committed
93 94 95 96 97 98 99 100 101
            }));
          },
        }}
      >
        编辑
      </PremButton>
    );
  };

TZW's avatar
TZW committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
  const addChild = (text, row, _, action) => {
    return (
      <PremButton
        btn={{
          size: 'small',
          onClick: () => {
            setDrawer((s) => ({
              ...s,
              visible: true,
              detailpath: null,

              item: {
                parentKey: row.title,
                departmentTypeName: '部门类型',
              },
TZW's avatar
TZW committed
117
              title: '新增组织',
TZW's avatar
TZW committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
              val: 'addChild',
              onFinish: async (vals) => {
                let params = {
                  ...vals,
                  parentId: row.key,
                  equipmentTypeName: vals.title,
                  departmentType: '1',
                };
                delete params.title;
                delete params.parentKey;
                let res = await doFetch({
                  url: urlParams.save,
                  params,
                });
                if (res.code === '0000') {
TZW's avatar
TZW committed
133
                  message.success('新增组织成功!');
TZW's avatar
TZW committed
134 135 136 137 138 139 140 141 142 143 144
                  setDrawer((s) => ({
                    ...s,
                    visible: false,
                  }));
                  actionRef.current.reload();
                }
              },
            }));
          },
        }}
      >
TZW's avatar
TZW committed
145
        新增组织
TZW's avatar
TZW committed
146 147 148 149
      </PremButton>
    );
  };

wuhao's avatar
wuhao committed
150 151 152 153
  const remove = (text, row, _, action) => {
    return (
      <PremButton
        pop={{
TZW's avatar
TZW committed
154
          title: '是否删除该设备类型?',
wuhao's avatar
wuhao committed
155 156
          okText: '确认',
          cancelText: '取消',
TZW's avatar
TZW committed
157 158 159 160 161 162 163 164 165 166
          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();
            }
wuhao's avatar
wuhao committed
167 168 169 170 171 172 173 174 175 176 177 178 179
          },
        }}
        btn={{
          size: 'small',
          type: 'danger',
        }}
      >
        删除
      </PremButton>
    );
  };

  const columns = useMemo(() => {
TZW's avatar
TZW committed
180 181 182 183 184 185
    let defcolumn = getcolumns(setDrawer);
    if (drawer?.val == 'add' || drawer?.val == 'edit') {
      defcolumn[3].hideInForm = true;
    } else {
      defcolumn[3].hideInForm = false;
    }
wuhao's avatar
wuhao committed
186 187 188 189 190
    return defcolumn.concat({
      title: '操作',
      valueType: 'option',
      width: 150,
      render: (text, row, _, action) => [
TZW's avatar
TZW committed
191
        addChild(text, row, _, action),
wuhao's avatar
wuhao committed
192 193 194 195
        edit(text, row, _, action),
        remove(text, row, _, action),
      ],
    });
TZW's avatar
TZW committed
196
  }, [drawer.val]);
wuhao's avatar
wuhao committed
197 198 199 200

  return (
    <div style={{ position: 'relative' }}>
      <AutoTable
TZW's avatar
TZW committed
201
        pagetitle={<h3 className="page-title">组织管理</h3>}
wuhao's avatar
wuhao committed
202
        columns={columns}
TZW's avatar
TZW committed
203
        path={urlParams.list}
wuhao's avatar
wuhao committed
204
        actionRef={actionRef}
TZW's avatar
TZW committed
205
        rowKey={'key'}
wuhao's avatar
wuhao committed
206
        pageextra={'add'}
TZW's avatar
TZW committed
207
        resizeable={false}
wuhao's avatar
wuhao committed
208 209 210 211
        addconfig={{
          // access: 'sysDepartment_save',
          btn: {
            disabled: false,
TZW's avatar
TZW committed
212
            type: 'primary',
wuhao's avatar
wuhao committed
213
            onClick: () => {
TZW's avatar
TZW committed
214
              setDrawer((s) => ({
wuhao's avatar
wuhao committed
215 216
                ...s,
                visible: true,
TZW's avatar
TZW committed
217 218 219
                item: {
                  departmentTypeName: '公司类型',
                },
wuhao's avatar
wuhao committed
220
                title: '新增',
TZW's avatar
TZW committed
221
                detailpath: null,
wuhao's avatar
wuhao committed
222
                val: 'add',
TZW's avatar
TZW committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
                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();
                  }
                },
wuhao's avatar
wuhao committed
241 242 243 244
              }));
            },
          },
        }}
TZW's avatar
TZW committed
245
        pagination={false}
wuhao's avatar
wuhao committed
246 247 248 249
      />

      <DrawerPro
        fields={columns}
TZW's avatar
TZW committed
250
        defaultFormValue={drawer?.item ?? {}}
wuhao's avatar
wuhao committed
251 252 253
        formRef={formRef}
        placement="right"
        onClose={() => {
TZW's avatar
TZW committed
254
          setDrawer((s) => ({
wuhao's avatar
wuhao committed
255 256 257 258 259 260 261 262 263 264
            ...s,
            visible: false,
          }));
        }}
        {...drawer}
      />
    </div>
  );
}

TZW's avatar
TZW committed
265
export default Type;