index.jsx 17.7 KB
Newer Older
krysent's avatar
krysent committed
1
import React, { useState, useRef, useReducer } from "react";
krysent's avatar
krysent committed
2
import { Button, Tooltip, Row, Divider, Drawer, Form, message } from "antd";
krysent's avatar
krysent committed
3 4 5 6 7 8
import AutoTable from "@/components/AutoTable";
import getPrem from "@/utils/getPrem"; //权限判断fn
import { useRequest } from "umi";
import defaultFields from "./fields";
import { doFetch } from "@/utils/doFetch";
import Details from "@/components/Details";
krysent's avatar
krysent committed
9 10 11
import { materielDetail } from "@/utils/detailTotalCard";
import InitForm from "@/components/InitForm";
import moment from "moment";
krysent's avatar
krysent committed
12
import TreeRender from "@/components/TreeRender";
krysent's avatar
krysent committed
13
import { useEffect } from "react";
krysent's avatar
krysent committed
14
import { storeselectionBoxAll } from "@/services/system.js";
krysent's avatar
krysent committed
15 16

const initState = {
krysent's avatar
krysent committed
17 18 19 20 21 22 23 24 25 26 27 28
    vs: false,
    fields: {},
    iftype: {},
    curitem: {},
    detail: {
      dataSource: {},
      totalCard: [],
    },
    visible: false,
    specificFileds: {},
    commonFields: {},
    defaultCommonFields: [],
krysent's avatar
krysent committed
29
  },
krysent's avatar
krysent committed
30 31 32 33 34 35 36
  otherBasic = {
    materieCode: "物料编号",
    materieName: "物料名称",
    materieTypeName: "物料类型",
    productionUnitName: "生产单位(主)",
    processLineName: "工艺路线",
  };
krysent's avatar
krysent committed
37 38 39 40 41 42 43 44 45
function reducer(state, action) {
  let { type } = action,
    newState = {};
  switch (type) {
    case "add":
      newState = {
        ...state,
        vs: true,
        iftype: {
krysent's avatar
krysent committed
46
          title: "新增仓位",
krysent's avatar
krysent committed
47 48 49
          val: type,
        },
        fields: { ...action.fields },
krysent's avatar
krysent committed
50 51
        commonFields: action.commonFields,
        specificFileds: action.specificFileds,
krysent's avatar
krysent committed
52 53 54 55 56 57 58
      };
      break;
    case "edit":
      newState = {
        ...state,
        vs: true,
        iftype: {
krysent's avatar
krysent committed
59
          title: "编辑库位",
krysent's avatar
krysent committed
60 61 62
          val: type,
        },
        fields: { ...action.fields },
krysent's avatar
krysent committed
63 64 65 66
        curitem: action.curitem,
        commonFields: action.commonFields,
        specificFileds: action.specificFileds,
        defaultCommonFields: action.defaultCommonFields,
krysent's avatar
krysent committed
67 68
      };
      break;
krysent's avatar
krysent committed
69 70 71 72 73 74 75
    case "bom":
      newState = {
        ...state,
        vs: true,
        iftype: {
          title: "编辑物料BOM",
          val: type,
krysent's avatar
krysent committed
76
        },
krysent's avatar
krysent committed
77 78 79 80 81 82
        fields: { ...action.fields },
        curitem: action.curitem,
        markKey: action.markKey,
      };
      break;
    case "see":
krysent's avatar
krysent committed
83 84
      newState = {
        ...state,
krysent's avatar
krysent committed
85
        detail: {
krysent's avatar
krysent committed
86
          dataSource: action.dataSource,
krysent's avatar
krysent committed
87
          totalCard: action.totalCard,
krysent's avatar
krysent committed
88 89 90 91
        },
        visible: true,
      };
      break;
krysent's avatar
krysent committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    case "changeSpecificFileds":
      newState = {
        ...state,
        specificFileds: action.specificFileds,
      };
      break;
    case "changeFields":
      newState = {
        ...state,
        fields: action.fields,
      };
      break;
    case "changeCommonFields":
      newState = {
        ...state,
        commonFields: action.commonFields,
      };
      break;
    case "changeDefaultCommonFields":
      newState = {
        ...state,
        defaultCommonFields: action.defaultCommonFields,
      };
      break;
krysent's avatar
krysent committed
116 117 118 119 120
    case "close":
      newState = {
        vs: false,
        fields: {},
        iftype: {},
krysent's avatar
krysent committed
121 122
        curitem: {},
        detail: {
krysent's avatar
krysent committed
123 124 125 126
          dataSource: {},
          totalCard: [],
        },
        visible: false,
krysent's avatar
krysent committed
127
        specificFileds: {},
krysent's avatar
krysent committed
128 129 130
      };
      break;
  }
krysent's avatar
krysent committed
131

krysent's avatar
krysent committed
132 133 134
  return newState;
}

krysent's avatar
krysent committed
135
const Materiel = (props) => {
krysent's avatar
krysent committed
136
  let actionRef = useRef(),
krysent's avatar
krysent committed
137 138 139 140 141
    ChildRef = null,
    [formRef] = Form.useForm(),
    [formRefs] = Form.useForm();
  const [materieTypeId, setmaterieTypeId] = useState();
  const [defaultSpecificFileds, setdefaultSpecificFileds] = useState();
krysent's avatar
krysent committed
142 143 144 145
  function reload() {
    actionRef?.current?.reload();
    ChildRef?.onRefresh();
  }
krysent's avatar
krysent committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
  const { run, loading } = useRequest(doFetch, {
      manual: true,
      formatResult: (res) => res,
      onSuccess: (result, params) => {
        if (result.code == "0000") {
          reload();
          dispatch({ type: "close" });
        }
      },
    }),
    [state, dispatch] = useReducer(reducer, initState),
    {
      vs,
      fields,
      iftype,
      curitem,
      detail,
      visible,
      specificFileds,
      commonFields,
      defaultCommonFields,
      markKey,
    } = state,
krysent's avatar
krysent committed
169 170
    columns = [
      {
krysent's avatar
krysent committed
171 172 173
        title: "库位编码",
        dataIndex: "storePositionCode",
        key: "storePositionCode",
krysent's avatar
krysent committed
174 175
      },
      {
krysent's avatar
krysent committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
        title: "库位名称",
        dataIndex: "storePositionName",
        key: "storePositionName",
        // render: (_, row) => {
        //   return (
        //     <Tooltip title={row.materieName}>
        //       <a
        //         className="table-cell"
        //         onClick={() => {
        //           doFetch({
        //             url: "/ngic-workmanship/pmMaterie/queryDetailById",
        //             params: { id: row.id },
        //           }).then((res) => {
        //             if (res.code == "0000") {
        //               let dataSource = res?.data?.data ?? {},
        //                 specials = res?.data?.data?.materieCharList ?? [];
        //               let newData = formatFieds(specials, "special")?.data;

        //               let newSpecialData = newData.map((it) => {
        //                 dataSource[it.fieldsKey] = it.fieldRealValue;
        //                 return {
        //                   title: it.fieldName,
        //                   key: it.fieldsKey,
        //                 };
        //               });
        //               let itemCol = [
        //                 {
        //                   cardTitle: "特殊属性",
        //                   itemData: newSpecialData,
        //                 },
        //               ];
        //               dataSource.newqualityGuaranteePeriod =
        //                 dataSource.qualityGuaranteePeriod == 0 ||
        //                 dataSource.qualityGuaranteePeriod
        //                   ? dataSource.qualityGuaranteePeriod +
        //                     dataSource.qualityGuaranteePeriodUnitName
        //                   : null;
        //               let totalCard = materielDetail.concat(itemCol);
        //               dispatch({ type: "see", dataSource, totalCard });
        //             }
        //           });
        //         }}
        //       >
        //         {row.materieName}
        //       </a>
        //     </Tooltip>
        //   );
        // },
krysent's avatar
krysent committed
224 225
      },
      {
krysent's avatar
krysent committed
226 227 228
        title: "所属仓库",
        dataIndex: "storeName",
        key: "storeName",
krysent's avatar
krysent committed
229 230 231 232
        // valueType: "select",
        // options: {
        //   database: storeselectionBoxAll,
        // },
krysent's avatar
krysent committed
233 234
      },
      {
krysent's avatar
krysent committed
235 236 237
        title: "所属库区",
        dataIndex: "storeAreaName",
        key: "storeAreaName",
krysent's avatar
krysent committed
238 239 240 241 242 243 244 245 246 247 248
        // options: {
        //   database: () =>
        //     doFetch({
        //       url: "/ngic-base-business/sysDic/queryMaterieControlSelect",
        //     }),
        //   params: {},
        // },
        // valueType: "select",
        // formItemProps: {
        //   name: "materieControl",
        // },
krysent's avatar
krysent committed
249 250
      },
      {
krysent's avatar
krysent committed
251 252 253 254
        title: "排",
        dataIndex: "row",
        key: "row",
        valueType: "digit",
krysent's avatar
krysent committed
255
        search: false,
krysent's avatar
krysent committed
256 257
      },
      {
krysent's avatar
krysent committed
258 259 260 261
        title: "列",
        dataIndex: "line",
        key: "line",
        valueType: "digit",
krysent's avatar
krysent committed
262
        search: false,
krysent's avatar
krysent committed
263 264
      },
      {
krysent's avatar
krysent committed
265 266 267 268
        title: "层",
        dataIndex: "level",
        key: "level",
        valueType: "digit",
krysent's avatar
krysent committed
269
        search: false,
krysent's avatar
krysent committed
270 271 272 273 274
      },
      {
        title: "状态",
        dataIndex: "status",
        key: "status",
krysent's avatar
krysent committed
275
        search:false,
krysent's avatar
krysent committed
276
        render: (_, row) => <span>{row?.status == "1" ? "启用" : "禁用"}</span>,
krysent's avatar
krysent committed
277
      },
krysent's avatar
krysent committed
278 279 280 281
      {
        title: "更新人",
        dataIndex: "updateUserName",
        key: "updateUserName",
krysent's avatar
krysent committed
282
        search: false,
krysent's avatar
krysent committed
283 284 285 286 287 288
      },

      {
        title: "更新时间",
        dataIndex: "updateTime",
        key: "updateTime",
krysent's avatar
krysent committed
289
        search: false,
krysent's avatar
krysent committed
290 291 292 293 294
      },
      {
        title: "是否为移库仓位",
        dataIndex: "isMovePosition",
        key: "isMovePosition",
krysent's avatar
krysent committed
295 296 297 298 299 300 301 302 303 304 305 306
        valueType: "select",
        search: false,
        options: [
          {
            value: "1",
            label: "是",
          },
          {
            value: "2",
            label: "否",
          },
        ],
krysent's avatar
krysent committed
307 308 309
        render: (_, row) => {
          return row?.isMovePosition ? "是" : "否";
        },
krysent's avatar
krysent committed
310 311
      },

krysent's avatar
krysent committed
312 313
      {
        title: "操作",
krysent's avatar
krysent committed
314 315
        dataIndex: "option_dataindex",
        key: "option_dataindex",
krysent's avatar
krysent committed
316
        valueType: "option",
krysent's avatar
krysent committed
317
        width: 160,
krysent's avatar
krysent committed
318 319
        render: (text, row, _, action) => extraAction(text, row, _, action),
      },
krysent's avatar
krysent committed
320 321 322 323 324 325 326
    ];
  useEffect(() => {
    doFetch({
      url: "/ngic-workmanship/pmMaterieChar/queryCommonList",
      params: {},
    }).then((res) => {
      setdefaultSpecificFileds(res?.data?.dataList);
krysent's avatar
krysent committed
327
    });
krysent's avatar
krysent committed
328 329
  }, []);

krysent's avatar
krysent committed
330 331
  function extraAction(text, record, _, action) {
    return [
krysent's avatar
krysent committed
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 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
      getPrem("sysDepartment_save", action, "修改", () => {
        doFetch({
          url: "/ngic-auth/sysStorePosition/queryById",
          params: { id: record.id },
        }).then((res) => {
          if (res.code == "0000") {
            let data = res?.data?.data ?? {};
            console.log(data);
            let newArr =
              data?.materieProductUnitList?.map((it) => {
                return {
                  productionUnit: it.productionUnit,
                  productionUnitType: it.productionUnitType,
                  id: it.id,
                };
              }) ?? [];

            for (let i in defaultFields) {
              defaultFields[i].value = data[i];
              // if (i == "unitList") {
              //   defaultFields[i].value = [
              //     {
              //       id: moment().valueOf(),
              //       productionUnit: data?.productionUnit,
              //       productionUnitType: data?.productionUnitType,
              //     },
              //     ...newArr,
              //   ];
              // }
            }
            console.log(defaultFields);
            let all = async () => {
              let common = await doFetch({
                url: "/ngic-workmanship/pmMaterieChar/queryCommonList",
                params: { id: record.id },
              });

              return {
                commonData: common?.data?.dataList,
              };
            };
            all().then((res) => {
              let commonDataFields = formatFieds(
                  res?.commonData,
                  "common"
                )?.obj,
                newCommonData = formatFieds(res?.commonData, "common")?.data;
              console.log(defaultFields);
              dispatch({
                type: "edit",
                fields: defaultFields,
                curitem: record,
                commonFields: commonDataFields,
                specificFileds: { ...commonDataFields },
                defaultCommonFields: newCommonData,
              });
              setdefaultSpecificFileds([...newCommonData]);
            });
          }
krysent's avatar
krysent committed
391 392
        });
      }),
krysent's avatar
krysent committed
393 394
      getPrem("sysDepartment_deleteById", action, "删除", null, {
        title: "确认删除该库位?",
krysent's avatar
krysent committed
395 396
        onConfirm: () => {
          run({
krysent's avatar
krysent committed
397
            url: "/ngic-auth/sysStorePosition/deleteById",
krysent's avatar
krysent committed
398 399 400 401 402 403
            params: { id: record.id },
          });
        },
      }),
    ];
  }
krysent's avatar
krysent committed
404
  let saveData = () => {
krysent's avatar
krysent committed
405
    //新增&修改
krysent's avatar
krysent committed
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
    let difrid = iftype.val != "add" ? { id: curitem.id } : {},
      params,
      url;
    if (iftype.val != "bom") {
      formRef
        .validateFields()
        .then((firstValues) => {
          formRefs
            .validateFields()
            .then((secondFields) => {
              let secondFieldsKeys = Object.keys(secondFields);
              let materieProductUnitList =
                firstValues?.materieProductUnitList?.map((it) => {
                  return {
                    productionUnit: it.productionUnit,
                    conversionRate: it.conversionRate,
                  };
                });
krysent's avatar
krysent committed
424 425 426 427 428
              for (let i in firstValues) {
                if (firstValues[i] === undefined) {
                  firstValues[i] = null;
                }
              }
krysent's avatar
krysent committed
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
              params = {
                ...firstValues,
                ...difrid,
                materieProductUnitList,
              };
              url = "/ngic-auth/sysStorePosition/save";
              run({ url, params });
            })
            .catch((error) => {});
        })
        .catch((error) => {});
    } else {
      url = "/ngic-workmanship/pmMaterie/saveMaterieBom";
      let firstValues = formRef?.getFieldsValue();
      let materieBomList = firstValues?.materieBomList?.map((it, i) => {
        return {
          materieId: it.id,
          materieName: it.materieName,
          materieCode: it.materieCode,
          materieType: it.materieType,
          numberUnits: it.numberUnits,
          workingProduceId: it.workingProduceId,
          workingFeedingId: it.workingFeedingId,
          rowKey: String(i),
          productionUnitName: it.productionUnitName,
        };
      });
      params = {
        materieBomList,
        ...difrid,
      };
      run({ url, params });
    }
krysent's avatar
krysent committed
462 463 464
  };
  let extrarender = [
    <Button
krysent's avatar
krysent committed
465
      disabled={!getPrem("sysDepartment_save", "ifs")}
krysent's avatar
krysent committed
466
      type="primary"
krysent's avatar
krysent committed
467 468 469 470 471 472 473 474 475 476 477 478
      onClick={() => {
        doFetch({
          url: "/ngic-workmanship/pmMaterieChar/queryCommonList",
          params: {},
        }).then((res) => {
          if (res.code == "0000") {
            let data = res?.data?.dataList,
              otherFields = formatFieds(data, "common")?.obj,
              newData = formatFieds(data, "common")?.data;
            for (let i in defaultFields) {
              defaultFields[i].value = null;
              defaultFields[i].disabled = false;
krysent's avatar
krysent committed
479 480 481
              if (i == 'status') {
                defaultFields[i].value = 1;
              }
krysent's avatar
krysent committed
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
              if (
                i == "serialNumberRuleId" ||
                i == "labelTemplateId" ||
                i == "processLineId"
              ) {
                defaultFields[i].required = false;
              }
              // if (i == "issueType") {
              //   defaultFields[i].value = 1;
              // }
            }
            dispatch({
              type: "add",
              fields: defaultFields,
              commonFields: otherFields,
              specificFileds: otherFields,
            });
            dispatch({
              type: "changeDefaultCommonFields",
              defaultCommonFields: newData,
            });
krysent's avatar
krysent committed
503 504 505 506 507 508 509
          }
        });
      }}
    >
      新增
    </Button>,
  ];
krysent's avatar
krysent committed
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
  function formatFieds(data, type) {
    let obj = {};
    for (let i = 0; i < data.length; i++) {
      let item = data[i],
        fieldsKey = type + "field" + i;
      item["fieldsKey"] = fieldsKey;
      if (item.fieldChar == 1) {
        obj[fieldsKey] = {
          value: item?.fieldRealValue ?? "",
          title: item.fieldName,
          name: [fieldsKey],
          required: false,
          type: "input",
        };
      } else {
        obj[fieldsKey] = {
          value:
            item?.fieldRealValue == 0 || item?.fieldRealValue
              ? item?.fieldRealValue
              : item.fieldChar == 4
              ? []
              : "",
          title: item.fieldName,
          name: [fieldsKey],
          required: false,
          type: item.fieldChar == 3 ? "radio" : "select",
          options: item.valueList,
          multiple: item.fieldChar == 4 ? true : false,
        };
      }
krysent's avatar
krysent committed
540
    }
krysent's avatar
krysent committed
541 542 543 544 545
    return {
      obj,
      data,
    };
  }
krysent's avatar
krysent committed
546 547 548
  useEffect(() => {
    console.log("123", materieTypeId);
  }, [materieTypeId]);
krysent's avatar
krysent committed
549 550
  return (
    <div>
krysent's avatar
krysent committed
551 552 553 554 555 556 557 558
      <AutoTable
        pagetitle={props.route.name} //页面标题
        pageextra={extrarender} //页面操作 新增or批量删除
        columns={columns}
        path="/ngic-auth/sysStorePosition/queryList"
        actionRef={actionRef}
        onRef={(node) => (ChildRef = node)}
        childposition="left"
krysent's avatar
krysent committed
559
        extraparams={{ ...materieTypeId }}
krysent's avatar
krysent committed
560
        x={600}
krysent's avatar
krysent committed
561
      >
krysent's avatar
krysent committed
562 563 564 565 566 567 568 569 570
        <div
          style={{
            width: 260,
            flexShrink: 0,
            marginTop: -4,
            borderRight: "1px solid #f0f0f0",
            paddingRight: 12,
            marginRight: 12,
          }}
krysent's avatar
krysent committed
571
        >
krysent's avatar
krysent committed
572 573
          <TreeRender
            url="/ngic-auth/sysFactory/queryFactoryStoreTree"
krysent's avatar
krysent committed
574 575
            // deleteurl="/ngic-auth/sysFactory/deleteById"
            // saveurl="/ngic-auth/sysFactory/save"
krysent's avatar
krysent committed
576 577 578 579 580 581 582 583 584 585 586 587 588 589
            onselected={(vals, e, alldata) => {
              console.log(e?.node);
              const valType = e?.node?.level;
              let obj = {};
              if (valType == 1) {
                // 工厂
                obj.factoryId = vals[0] ?? "";
              } else if (valType == 2) {
                obj.storeId = vals[0] ?? "";
              } else if (valType == 3) {
                obj.storeAreaId = vals[0] ?? "";
              }
              console.log(obj);
              setmaterieTypeId(obj);
krysent's avatar
krysent committed
590 591 592 593 594
            }}
          />
        </div>
      </AutoTable>
      <Drawer
krysent's avatar
krysent committed
595 596
        title={iftype.title}
        visible={vs}
krysent's avatar
krysent committed
597
        onClose={() => dispatch({ type: "close" })}
krysent's avatar
krysent committed
598 599
        footer={false}
        destroyOnClose={true}
krysent's avatar
krysent committed
600 601 602
        getContainer={false}
        style={{ position: "absolute" }}
        width={"100%"}
krysent's avatar
krysent committed
603 604
      >
        <InitForm
krysent's avatar
krysent committed
605
          formRef={formRef}
krysent's avatar
krysent committed
606
          fields={fields}
krysent's avatar
krysent committed
607 608 609
          onChange={(changedValues, allValues) => {}}
          actions={() => {
            return null;
krysent's avatar
krysent committed
610 611
          }}
        ></InitForm>
krysent's avatar
krysent committed
612 613 614 615 616 617 618 619 620 621
        <Button
          style={{ width: "100%" }}
          type="primary"
          size="large"
          loading={loading || !vs}
          onClick={() => saveData()}
        >
          提交
        </Button>
      </Drawer>
krysent's avatar
krysent committed
622
      <Details
krysent's avatar
krysent committed
623
        title="物料详情"
krysent's avatar
krysent committed
624 625
        visible={visible}
        onClose={() => dispatch({ type: "close" })}
krysent's avatar
krysent committed
626
        footer={false}
krysent's avatar
krysent committed
627
        destroyOnClose={true}
krysent's avatar
krysent committed
628 629
        width={"100%"}
        {...detail}
krysent's avatar
krysent committed
630 631 632 633
      ></Details>
    </div>
  );
};
krysent's avatar
krysent committed
634
export default Materiel;