index.jsx 10.1 KB
Newer Older
wuhao's avatar
wuhao committed
1 2 3 4 5 6 7 8 9 10 11 12 13
import React, { useState, useRef, useReducer } from "react";
import {
  Modal,
  Button,
  Drawer,
  Tooltip,
  Popconfirm,
  message,
  Switch,
} from "antd";
import AutoTable from "@/components/AutoTable";
import getPrem from "@/utils/getPrem"; //权限判断fn
import InitForm from "@/components/InitForm";
wuhao's avatar
wuhao committed
14 15
import { useRequest } from "umi";
import defaultFields from "./fields";
wuhao's avatar
wuhao committed
16
import { doFetch } from "@/utils/doFetch";
wuhao's avatar
wuhao committed
17
import { deviceprovideDetail, deleteProvideCheck } from "@/services/device";
wuhao's avatar
wuhao committed
18
import { usercusDetails } from "@/utils/detailTotalCard";
wuhao's avatar
wuhao committed
19
import Details from "@/components/Details";
wuhao's avatar
wuhao committed
20 21
import { useAsyncEffect } from "ahooks";
import tempfields from "@/utils/tempfields";
wuhao's avatar
wuhao committed
22

wuhao's avatar
wuhao committed
23
const initState = {
wuhao's avatar
wuhao committed
24 25 26 27 28 29 30 31 32
  vs: false,
  fields: {},
  iftype: {},
  curitem: {},
  details: {
    dataSource: {},
    totalCard: [],
  },
  visible: false,
wuhao's avatar
wuhao committed
33 34
};
function reducer(state, action) {
wuhao's avatar
wuhao committed
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
  let { type } = action,
    newState = {};
  switch (type) {
    case "add":
      newState = {
        ...state,
        vs: true,
        iftype: {
          title: "新增供应商",
          val: type,
        },
        fields: { ...action.fields },
      };
      break;
    case "edit":
      newState = {
        ...state,
        vs: true,
        iftype: {
          title: "编辑供应商",
          val: type,
        },
        fields: { ...action.fields },
        curitem: action.curitem,
      };
      break;
    case "cdetails":
      let n = 1,
        dataSource = action.dataSource;
      let special = action?.dataSource?.supplierCharList?.map?.((it) => {
        n++;
        let key = n.toString();
        dataSource[key] = it?.fieldRealValue;
        return {
          title: it?.fieldName,
          key: key,
        };
      });
      let extraspecial = [
        {
          cardTitle: "特殊属性",
          itemData: special,
        },
      ];
wuhao's avatar
wuhao committed
79

wuhao's avatar
wuhao committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
      newState = {
        ...state,
        details: {
          dataSource: action.dataSource,
          totalCard: [...usercusDetails, ...extraspecial],
        },
        visible: true,
      };
      break;
    case "close":
      newState = {
        vs: false,
        fields: {},
        iftype: {},
        curitem: {},
        details: {
          dataSource: {},
          totalCard: [],
        },
        visible: false,
      };
      break;
  }
  return newState;
wuhao's avatar
wuhao committed
104 105 106
}

const Deviceprovide = (props) => {
wuhao's avatar
wuhao committed
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
  let actionRef = useRef(),
    ChildRef = null;
  function reload() {
    actionRef?.current?.reload();
    ChildRef?.onRefresh();
  }
  const [state, dispatch] = useReducer(reducer, initState),
    { vs, fields, iftype, details, visible } = state,
    columns = [
      {
        title: "供应商编号",
        dataIndex: "supplierNo",
        key: "supplierNo",
      },
      {
        title: "供应商名称",
        dataIndex: "supplierName",
        key: "supplierName",
        render: (_, row) => {
          return (
            <div
              style={{
                padding: "3px 0",
                overflow: "hidden",
                whiteSpace: "nowrap",
                textOverflow: "ellipsis",
              }}
            >
              <Tooltip title={row.supplierName}>
                <a
                  onClick={() => {
                    deviceprovideDetail({ id: row.id }).then((res) => {
                      let data = res?.data?.data || {};
                      dispatch({ type: "cdetails", dataSource: data });
                    });
                  }}
                >
                  {row.supplierName}
                </a>
              </Tooltip>
            </div>
          );
        },
      },
      {
        title: "联系电话",
        dataIndex: "telephone",
        key: "telephone",
      },
      {
        title: "邮箱",
        dataIndex: "email",
        key: "email",
      },
      {
        title: "地址",
        dataIndex: "address",
        key: "address",
        search: false,
      },
      {
        title: "备注",
        dataIndex: "remark",
        key: "remark",
        search: false,
      },
      {
        title: "评分",
        dataIndex: "score",
        key: "score",
        render: (_, row) => {
          return <div>{row.score}</div>;
        },
      },
      {
        title: "状态",
        dataIndex: "status",
        key: "status",
        valueType: "select",
        options: [
          {
            label: "启用",
            value: 1,
          },
          {
            label: "关闭",
            value: 0,
          },
wuhao's avatar
wuhao committed
195
        ],
wuhao's avatar
wuhao committed
196 197 198 199 200 201 202 203
        render: (text, row, _, action) => {
          return (
            <Popconfirm
              title="是否开启或关闭供应商?"
              onConfirm={() => {
                run({
                  url: "/ngic-auth/sysSupplier/update/status",
                  params: { id: row.id, status: row.status == 1 ? 0 : 1 },
wuhao's avatar
wuhao committed
204
                });
wuhao's avatar
wuhao committed
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
              }}
              onCancel={() => {}}
              okText="确定"
              cancelText="取消"
              disabled={
                !getPrem("equipmentSupplier_updatestatus", "ifs") ||
                row.sourceType == 1
              }
            >
              <Switch
                checked={row.status == 1 ? true : false}
                checkedChildren="开启"
                unCheckedChildren="关闭"
                defaultChecked={false}
              />
            </Popconfirm>
          );
        },
      },
      {
        title: "操作",
        valueType: "option",
        width: 150,
        render: (text, row, _, action) => extraAction(text, row, _, action),
      },
    ],
    { run, loading } = useRequest(doFetch, {
      manual: true,
      formatResult: (res) => res,
      onSuccess: (result, params) => {
        if (result.code == "0000") {
          message.success("操作成功!");
          reload();
          dispatch({ type: "close" });
wuhao's avatar
wuhao committed
239
        }
wuhao's avatar
wuhao committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
      },
    });
  const [extrafields, setextrafields] = useState({});
  const [subdata, setsubdata] = useState([]);
  const [curitem, setcuritem] = useState({});
  function extraAction(text, record, _, action) {
    return [
      getPrem("equipmentSupplier_save", action, "修改", async () => {
        await setcuritem({
          ...record,
          key: "edit",
        });
      }),
      record.sourceType != 1 &&
        getPrem("equipmentSupplier_deleteById", action, "删除", null, {
          title: "确认删除该供应商?",
          onConfirm: () => {
            run({
              url: "/ngic-auth/sysSupplier/deleteById",
              params: { id: record.id },
            });
          },
        }),
    ];
  }
  let saveData = (values, fn) => {
    let newfields = JSON.parse(JSON.stringify(values));
    //新增&修改
    let difrid = iftype.val == "edit" ? { id: curitem.id } : {};
    let supplierUserList = newfields?.supplierUserList?.map((it) => {
      delete it.id;
      return it;
    });
    let supplierCharList = subdata.map((it, i) => {
      let key = "field" + i.toString();
      it.fieldRealValue = values[key];
      delete newfields[key];
      return it;
    });
    let params = {
      ...newfields,
      supplierUserList,
      supplierCharList,
wuhao's avatar
wuhao committed
283
    };
wuhao's avatar
wuhao committed
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
    run({
      url: "/ngic-auth/sysSupplier/save",
      params: { ...params, ...difrid },
    });
  };
  let extrarender = [
    <Button
      disabled={!getPrem("equipmentSupplier_save", "ifs")}
      type="primary"
      onClick={async () => {
        let result = await tempfields(
          "/ngic-auth/sysSupplierChar/queryCommonList",
          {}
        );
        for (let i in defaultFields) {
          defaultFields[i].value = null;
          defaultFields[i].disabled = false;
          if (i == "supplierUserList") {
            defaultFields[i].value = [];
          }
          if (i == "status") {
            defaultFields[i].value = 1;
          }
        }
        for (let i in extrafields) {
          extrafields[i].value = null;
          if (extrafields[i]["multiple"] == true) {
            extrafields[i].value = [];
          }
wuhao's avatar
wuhao committed
313
        }
wuhao's avatar
wuhao committed
314 315 316 317 318 319 320 321 322
        dispatch({
          type: "add",
          fields: { ...defaultFields, ...result?.fields },
        });
      }}
    >
      新增
    </Button>,
  ];
wuhao's avatar
wuhao committed
323

wuhao's avatar
wuhao committed
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
  useAsyncEffect(async () => {
    let result = await tempfields(
      "/ngic-auth/sysSupplierChar/queryCommonList",
      { id: curitem.id }
    );
    await setextrafields(result?.fields);
    await setsubdata(result?.data);
    if (curitem.key == "edit") {
      doFetch({
        url: "/ngic-auth/sysSupplier/query/detail",
        params: { id: curitem.id },
      }).then((res) => {
        if (res.code == "0000") {
          let data = res?.data?.data || {};
          for (let i in defaultFields) {
            defaultFields[i].value = data[i];
            if (i == "status" && curitem.sourceType == 1) {
              defaultFields[i].disabled = true;
            } else {
              defaultFields[i].disabled = false;
            }
          }
          dispatch({
            type: "edit",
            fields: { ...defaultFields, ...result?.fields },
          });
        }
      });
    }
  }, [curitem]);
wuhao's avatar
wuhao committed
354

wuhao's avatar
wuhao committed
355 356 357 358 359 360 361 362 363 364
  return (
    <div>
      <AutoTable
        pagetitle={props.route.name} //页面标题
        pageextra={extrarender} //页面操作 新增or批量删除
        columns={columns}
        path="/ngic-auth/sysSupplier/queryList"
        actionRef={actionRef}
        onRef={(node) => (ChildRef = node)}
      ></AutoTable>
wuhao's avatar
wuhao committed
365

wuhao's avatar
wuhao committed
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
      <Modal
        maskClosable={false}
        title={iftype.title}
        visible={vs}
        onCancel={() => dispatch({ type: "close" })}
        footer={false}
        destroyOnClose={true}
        width={1000}
      >
        <InitForm
          fields={fields}
          submitData={(values) => {
            saveData(values);
          }}
          onChange={(changedValues, allValues) => {
            //联动操作
          }}
          submitting={loading || !vs}
        ></InitForm>
      </Modal>
      <Details
        title="供应商详情"
        closable={true}
        visible={visible}
        onClose={() => dispatch({ type: "close" })}
        destroyOnClose={true}
        afterVisibleChange={(v) => {}}
        width="100%"
        className="drawerDetails"
        {...details}
      ></Details>
wuhao's avatar
wuhao committed
397
    </div>
wuhao's avatar
wuhao committed
398 399 400
  );
};
export default Deviceprovide;