index.jsx 5.03 KB
Newer Older
krysent's avatar
krysent committed
1 2 3 4 5 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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
import React, { useEffect, useRef, useState } from 'react';
import { Card, Modal, Button, Divider, message } from 'antd';
import { connect, useRequest } from "umi";
import AutoTable from '@/components/AutoTable';
import getPrem from '@/utils/getPrem';//权限判断fn
import InitForm from '@/components/InitForm';
import defaultFields from './fields'
import { doFetch } from '@/utils/doFetch';
import { bmNoType } from '@/services/system';

let Rule = (props) => {
  const actionRef = useRef();
  const [vs, cvs] = useState(false),//表单显/隐
    [fields, cf] = useState(defaultFields),
    [iftype, ciftype] = useState({});

  const { run, loading } = useRequest(doFetch, {
    manual: true,
    formatResult: (res) => res,
    onSuccess: (result, params) => {
      if (result.code == "0000") {
        actionRef.current.reload();
        cvs(false);
      }
    }
  })

  const columns = [
    {
      "title": "单号类型",
      "dataIndex": "noTypeName",
      "key": "noTypeId",
      "options": {
        "database": bmNoType,
        "params": {}
      },
      valueType: "select"
    },
    {
      "title": "预览",
      "dataIndex": "ruleNames",
      "key": "ruleNames",
      "search": false
    },
    {
      title: '操作',
      valueType: 'option',
      width: 155,
      render: (text, row, s, action) => extraAction(text, row, s, action)
    },
  ];

  function extraAction(text, record, s, action) {
    return [
      getPrem("bmNumberRule_save", action, "修改", () => {
        doFetch({ url: "/ngic-base-business/bmNumberRule/queryByNoTypeId", params: { noTypeId: record.id } }).then(res => {
          let row = res?.data?.data;
          ciftype({
            val: "edit",
            id: record.id,
            title: "修改编号规则"
          })
          cf((fields) => {
            for (let i in fields) {
              fields[i].value = row[i];
              fields.noTypeId.options.database = bmNoType;
              fields.noTypeId.disabled = true;
              if (i == "nrList") {
                let reshow = {
                  value: row[i].filter(it => it.noRuleCode != "increasing_order"),
                  other: row[i].filter(it => it.noRuleCode == "increasing_order")[0] || {}
                }
                reshow.other.increaseList = reshow.other.increaseList.map(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;

                fields[i].value = reshow
              }
            }
            return { ...fields }
          });
          cvs(true);
        })

      }),
      getPrem('bmNumberRule_deleteByNoTypeId', action, "删除", null, {
        title: "确认删除该编号规则?",
        onConfirm: () => {
          run({ url: "/ngic-base-business/bmNumberRule/deleteByNoTypeId", params: { noTypeId: record.id } });
        }
      })
    ]
  }

  let saveData = (values, fn) => {
    let newvalues = JSON.parse(JSON.stringify(values));
    let nrList = newvalues.nrList;
    let submitnolist = nrList.value,
      other = nrList.other;

    if (other.sort) {
      submitnolist.splice(other.sort, 0, other);
    } else {
      submitnolist.push(other);
    }


    submitnolist = submitnolist.map((it, i) => {
      return {
        ...it,
        sort: i + 1
      }
    })


    submitnolist = submitnolist.map((it, i) => {
      if (it.noRuleCode == "increasing_order") {
        it.increaseList = it.increaseList.map(item => {
          return submitnolist.filter(itemz => itemz.id == item)[0].sort
        })
      }
      return {
        ...it
      }
    })


    let newfields = {
      ...newvalues,
      nrList: submitnolist
    }

    let difrid = iftype.val == "edit" ? { id: iftype.id } : {};
    run({ url: "/ngic-base-business/bmNumberRule/save", params: { ...newfields, ...difrid } });

  }


  let extra = (<Button disabled={!getPrem("bmNumberRule_save", "ifs")} type="primary" onClick={() => {
    cvs(true);
    cf(fields => {
      for (let i in fields) {
        fields[i].value = null;
        fields[i].disabled = false;

      }
      return { ...fields }
    });
    ciftype({
      val: "add",
      title: "新增编号规则"
    })
  }}>新增</Button>)

  return (
    <div>
      <AutoTable
        columns={columns}
        actionRef={actionRef}
        pagetitle={props.route.name}
        pageextra={extra}
        path="/ngic-base-business/bmNumberRule/queryList"
        pagination="false"
      ></AutoTable>


      <Modal
        maskClosable={false}
        title={iftype.title}
        visible={vs}
        onCancel={() => cvs(false)}
        footer={false}
        destroyOnClose={true}
        width={1200}
      >
        <InitForm
          fields={fields}
          submitData={(values, fn) => {
            saveData(values, fn)
          }}
          onChange={(changedValues, allValues) => {
          }}
          submitting={
            loading || !vs
          }
        >
        </InitForm>
      </Modal>
    </div>
  )
}

export default Rule;