index.jsx 7.88 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-11 19:37:17
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 20 21 22 23 24 25 26 27

function Rules(props) {
  const actionRef = useRef(),
    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 77 78 79 80 81 82 83 84 85 86 87 88
                vals.nrList.other = _.pick(vals.nrList.other, [
                  'formatType',
                  'increaseList',
                  'noRuleCode',
                  'sort',
                ]);
                vals.nrList.value = vals.nrList.value.map((it) => {
                  return _.pick(it, ['id', 'noContent', 'noRuleCode', 'sort', 'paramList']);
                });
                console.log('1214', vals);
                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
89
                  params.nrList.forEach?.((it, i) => {
TZW's avatar
TZW committed
90 91 92 93 94 95 96 97
                    it.sort = i + 1;
                  });
                } else {
                  message.warning('请选择顺序递增规则的位置!');
                  return;
                }
                params.id = row?.id;
                console.log('11111', params);
TZW's avatar
TZW committed
98 99 100 101 102
                let res = await doFetch({
                  url: urlParams.save,
                  params,
                });
                if (res.code === '0000') {
TZW's avatar
TZW committed
103
                  message.success('编辑成功!');
TZW's avatar
TZW committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
                  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
128
            let res = await doFetch({ url: urlParams.remove, params: { noTypeId: row.id } });
TZW's avatar
TZW committed
129 130 131 132 133 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
            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 (
    <div style={{ position: 'relative' }}>
      <AutoTable
        pagetitle={<h3 className="page-title">编号规则</h3>}
        columns={columns}
TZW's avatar
TZW committed
164
        pagination={false}
TZW's avatar
TZW committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
        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
181
                val: 'add',
TZW's avatar
TZW committed
182
                onFinish: async (vals) => {
TZW's avatar
TZW committed
183
                  console.log('新增:', vals);
TZW's avatar
TZW committed
184
                  let params = { ...vals };
TZW's avatar
TZW committed
185
                  //console.log('vals:', vals);
TZW's avatar
TZW committed
186 187 188 189 190 191 192 193 194 195 196
                  // for (let i in vals) {
                  //   if (i == 'nrList') {
                  //     let reshow = {
                  //       value: vals[i].filter((it) => it.noRuleCode != 'increasing_order'),
                  //       other: vals[i].filter((it) => it.noRuleCode == 'increasing_order')[0] || {},
                  //     };
                  //     reshow.other.increaseList = reshow.other.increaseList.maps((it) => {
                  //       return reshow.value.filter((item) => item.sort == it)[0].id;
                  //     });
                  //     reshow.other.sort =
                  //       reshow.other.sort == 1 ? reshow.other.sort : reshow.other.sort - 1;
TZW's avatar
TZW committed
197
                  //     //console.log('reshow', reshow);
TZW's avatar
TZW committed
198 199 200 201 202 203 204 205 206 207
                  //   }
                  // }
                  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
208
                    params.nrList.forEach?.((it, i) => {
TZW's avatar
TZW committed
209 210 211 212 213 214
                      it.sort = i + 1;
                    });
                  } else {
                    message.warning('请选择顺序递增规则的位置!');
                    return;
                  }
TZW's avatar
TZW committed
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
                  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
248
      />
TZW's avatar
TZW committed
249 250 251 252 253
    </div>
  );
}

export default Rules;