index.jsx 7.91 KB
Newer Older
TZW's avatar
TZW committed
1 2 3 4
/* 编号规则
 * @Author: Li Hanlin
 * @Date: 2022-11-09 14:44:44
 * @Last Modified by: Li Hanlin
TZW's avatar
TZW committed
5
 * @Last Modified time: 2023-01-30 18:43:49
TZW's avatar
TZW committed
6 7 8 9 10 11 12 13 14 15
 */

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';
TZW's avatar
TZW committed
16
import AddRules from './AddRules';
TZW's avatar
TZW committed
17
import _ from 'lodash';
TZW's avatar
TZW committed
18 19

function Rules(props) {
TZW's avatar
TZW committed
20
  let actionRef = useRef(),
TZW's avatar
TZW committed
21 22 23 24 25 26 27
    formRef = useRef();
  const [drawer, setDrawer] = useState({
    visible: false,
  });

  const urlParams = {
    save: '/base/bmNumberRule/save',
TZW's avatar
TZW committed
28
    remove: '/base/bmNumberRule/deleteByNoTypeId',
TZW's avatar
TZW committed
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
    list: '/base/bmNumberRule/queryList',
    detail: '/base/bmNumberRule/queryByNoTypeId',
  };

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

TZW's avatar
TZW committed
55
  const edit = (text, row, i, action) => {
TZW's avatar
TZW committed
56 57 58 59 60 61 62 63
    return (
      <PremButton
        btn={{
          size: 'small',
          onClick: () => {
            setDrawer((s) => ({
              ...s,
              visible: true,
TZW's avatar
TZW committed
64 65
              detailpath: urlParams.detail,
              params: { noTypeId: row?.id },
TZW's avatar
TZW committed
66 67 68
              title: '编辑',
              val: 'edit',
              onFinish: async (vals) => {
TZW's avatar
TZW committed
69
                console.log(vals);
TZW's avatar
TZW committed
70 71 72 73 74 75 76
                vals.nrList.other = _.pick(vals.nrList.other, [
                  'formatType',
                  'increaseList',
                  'noRuleCode',
                  'sort',
                ]);
                vals.nrList.value = vals.nrList.value.map((it) => {
TZW's avatar
TZW committed
77 78 79 80 81 82 83 84
                  return _.pick(it, [
                    'formatType',
                    'id',
                    'noContent',
                    'noRuleCode',
                    'sort',
                    'paramList',
                  ]);
TZW's avatar
TZW committed
85 86 87 88 89 90 91 92 93 94
                });
                let params = { ...vals };
                params.nrList = [];
                params.nrList = params.nrList.concat(vals?.nrList.value);
                if (vals?.nrList.other.sort) {
                  vals.nrList.other.increaseList = Array.from(
                    Array(vals?.nrList.value.length + 1),
                    (_, index) => index + 1,
                  ).filter((it) => it !== vals?.nrList.other.sort + 1);
                  params.nrList.splice(vals?.nrList.other.sort, 0, vals?.nrList.other);
wuhao's avatar
wuhao committed
95
                  params.nrList.forEach?.((it, i) => {
TZW's avatar
TZW committed
96 97 98 99 100 101 102
                    it.sort = i + 1;
                  });
                } else {
                  message.warning('请选择顺序递增规则的位置!');
                  return;
                }
                params.id = row?.id;
TZW's avatar
TZW committed
103 104 105 106 107
                let res = await doFetch({
                  url: urlParams.save,
                  params,
                });
                if (res.code === '0000') {
TZW's avatar
TZW committed
108
                  message.success('编辑成功!');
TZW's avatar
TZW committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
                  setDrawer((s) => ({
                    ...s,
                    visible: false,
                  }));
                  actionRef.current.reload();
                }
              },
            }));
          },
        }}
      >
        编辑
      </PremButton>
    );
  };

  const remove = (text, row, _, action) => {
    return (
      <PremButton
        pop={{
          title: '是否删除该规则?',
          okText: '确认',
          cancelText: '取消',
          onConfirm: async () => {
TZW's avatar
TZW committed
133
            let res = await doFetch({ url: urlParams.remove, params: { noTypeId: row.id } });
TZW's avatar
TZW committed
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
            if (res.code === '0000') {
              message.success('删除成功!');
              setDrawer((s) => ({
                ...s,
                visible: false,
              }));
              actionRef.current.reload();
            }
          },
        }}
        btn={{
          size: 'small',
          type: 'danger',
        }}
      >
        删除
      </PremButton>
    );
  };

  const columns = useMemo(() => {
    let defcolumn = getcolumns(setDrawer);
    return defcolumn.concat({
      title: '操作',
      valueType: 'option',
      width: 150,
      render: (text, row, _, action) => [edit(text, row, _, action), remove(text, row, _, action)],
    });
  }, []);

  return (
左玲玲's avatar
左玲玲 committed
165
    <div>
TZW's avatar
TZW committed
166 167 168
      <AutoTable
        pagetitle={<h3 className="page-title">编号规则</h3>}
        columns={columns}
TZW's avatar
TZW committed
169
        pagination={false}
TZW's avatar
TZW committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
        path={urlParams.list}
        actionRef={actionRef}
        pageextra={'add'}
        resizeable={false}
        addconfig={{
          // access: 'sysDepartment_save',
          btn: {
            type: 'primary',
            disabled: false,
            onClick: () => {
              setDrawer((s) => ({
                ...s,
                visible: true,
                item: null,
                detailpath: null,
                title: '新增',
wuhao's avatar
wuhao committed
186
                val: 'add',
TZW's avatar
TZW committed
187
                onFinish: async (vals) => {
TZW's avatar
TZW committed
188
                  console.log(vals);
TZW's avatar
TZW committed
189
                  let params = { ...vals };
TZW's avatar
TZW committed
190 191 192
                  params.nrList = [];
                  params.nrList = params.nrList.concat(vals?.nrList.value);
                  if (vals?.nrList.other.sort) {
TZW's avatar
TZW committed
193 194 195 196
                    // vals.nrList.other.increaseList = Array.from(
                    //   Array(vals?.nrList.value.length + 1),
                    //   (_, index) => index + 1,
                    // ).filter((it) => it !== vals?.nrList.other.sort + 1);
TZW's avatar
TZW committed
197
                    params.nrList.splice(vals?.nrList.other.sort, 0, vals?.nrList.other);
TZW's avatar
TZW committed
198
                    let arr = [];
wuhao's avatar
wuhao committed
199
                    params.nrList.forEach?.((it, i) => {
TZW's avatar
TZW committed
200 201
                      it.sort = i + 1;
                    });
TZW's avatar
TZW committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
                    console.log(arr);
                    params.nrList.forEach((it) => {
                      if (it.noRuleCode == 'increasing_order') {
                        let increaseListArr = [];
                        it.increaseList.forEach((id) => {
                          increaseListArr = params.nrList
                            .filter((item) => item?.id == id)
                            .map((it) => {
                              return it?.sort;
                            });
                        });
                        arr = increaseListArr;
                      }
                    });
                    params.nrList.forEach((it) => {
                      if (it.noRuleCode == 'increasing_order') {
                        it.increaseList = arr;
                      }
                    });
TZW's avatar
TZW committed
221 222 223 224
                  } else {
                    message.warning('请选择顺序递增规则的位置!');
                    return;
                  }
TZW's avatar
TZW committed
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
                  let res = await doFetch({
                    url: urlParams.save,
                    params,
                  });
                  if (res.code === '0000') {
                    message.success('新增成功!');
                    setDrawer((s) => ({
                      ...s,
                      visible: false,
                    }));
                    actionRef.current.reload();
                  }
                },
              }));
            },
          },
        }}
      />

      <DrawerPro
        fields={columns}
        // detailpath={urlParams.detail}
        // params={{ id: drawer?.item?.id }}
        defaultFormValue={drawer?.item ?? {}}
        formRef={formRef}
        placement="right"
        onClose={() => {
          setDrawer((s) => ({
            ...s,
            visible: false,
          }));
        }}
        {...drawer}
wuhao's avatar
wuhao committed
258
      />
TZW's avatar
TZW committed
259 260 261 262 263
    </div>
  );
}

export default Rules;