store.js 9.16 KB
Newer Older
wuhao's avatar
wuhao committed
1
import React, { useState, useRef, useReducer } from "react";
wuhao's avatar
wuhao committed
2 3 4 5 6 7 8
import { Button, Tooltip, Row, Divider } from "antd";
import AutoTable from "@/components/AutoTable";
import getPrem from "@/utils/getPrem"; //权限判断fn
import { useRequest } from "umi";
import defaultFields from "./fieldstore";
import { doFetch } from "@/utils/doFetch";
import DrawInitForm from "@/components/DrawInitForm";
wuhao's avatar
wuhao committed
9
import { ProDescriptions } from '@ant-design/pro-components';
wuhao's avatar
wuhao committed
10 11 12 13 14
import JsBarcode from 'jsbarcode'
import { useReactToPrint } from 'react-to-print';
import PrintProvider, { Print, NoPrint } from 'react-easy-print';


wuhao's avatar
wuhao committed
15 16 17 18 19 20 21 22 23
const initState = {
    vs: false,
    fields: {},
    iftype: {},
    curitem: {},
    detail: {
        dataSource: {},
        totalCard: [],
    },
wuhao's avatar
wuhao committed
24
    vs: false,
wuhao's avatar
wuhao committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
};
function reducer(state, action) {
    let { type } = action,
        newState = {};
    switch (type) {
        case "add":
            newState = {
                ...state,
                vs: true,
                iftype: {
                    title: "新增库位",
                    val: type,
                },
                fields: { ...action.fields },
            };
            break;
wuhao's avatar
wuhao committed
41 42 43 44 45 46 47 48 49 50
        case "only":
            newState = {
                ...state,
                vs: true,
                iftype: {
                    title: "打印预览",
                    val: type,
                },
            };
            break;
wuhao's avatar
wuhao committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
        case "edit":
            newState = {
                ...state,
                vs: true,
                iftype: {
                    title: "编辑库位",
                    val: type,
                },
                fields: { ...action.fields },
                curitem: action.curitem,
            };
            break;
        case "see":
            newState = {
                ...state,
                curitem: action.curitem,
wuhao's avatar
wuhao committed
67
                vs: true,
wuhao's avatar
wuhao committed
68 69 70 71 72 73
            };
            break;
        case "close":
            newState = {
                ...state,
                curitem: {},
wuhao's avatar
wuhao committed
74
                vs: false,
wuhao's avatar
wuhao committed
75 76 77 78 79 80 81 82 83
            };
            break;
    }

    return newState;
}

const StoreApp = (props) => {
    let actionRef = useRef(),
wuhao's avatar
wuhao committed
84 85 86 87 88
        ChildRef = null,
        printRef = useRef();

    const [selectedRowKeys, setselectedRowKeys] = useState([]);

wuhao's avatar
wuhao committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    function reload() {
        actionRef?.current?.reload();
        ChildRef?.onRefresh();
    }
    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),
wuhao's avatar
wuhao committed
104
        { vs, fields, iftype, curitem } = state,
wuhao's avatar
wuhao committed
105 106 107 108 109 110 111 112 113 114 115 116
        columns = [
            {
                "title": "库位名称",
                "dataIndex": "storePositionName",
                "key": "storePositionName"
            },
            {
                "title": "所属库区",
                "dataIndex": "storeAreaName",
                "key": "storeAreaName",
                "valueType": "select",
                "options": {
左玲玲's avatar
左玲玲 committed
117
                    database: () => doFetch({ url: "/ngic-base-business/sysDic/queryStorePositionSelect" }),
wuhao's avatar
wuhao committed
118
                    params: {}
wuhao's avatar
wuhao committed
119 120 121 122 123
                }
            },
            {
                "title": "描述",
                "dataIndex": "description",
wuhao's avatar
wuhao committed
124 125
                "key": "description",
                "search": false
wuhao's avatar
wuhao committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
            },
            {
                title: "操作",
                dataIndex: "option_dataindex",
                key: "option_dataindex",
                valueType: "option",
                width: 135,
                render: (text, row, _, action) => extraAction(text, row, _, action),
            },
        ];

    function extraAction(text, record, _, action) {
        return [
            getPrem("sysDepartment_save", action, "修改", () => {
                for (let i in defaultFields) {
                    defaultFields[i].value = record[i];
                }
                dispatch({ type: "edit", fields: defaultFields, curitem: record });
            }),
            getPrem("sysDepartment_deleteById", action, "删除", null, {
                title: "确认删除该库位?",
                onConfirm: () => {
                    run({
wuhao's avatar
wuhao committed
149
                        url: "/ngic-auth/sysStorePosition/deleteById",
wuhao's avatar
wuhao committed
150 151 152 153 154 155 156 157 158 159
                        params: { id: record.id },
                    });
                },
            }),
        ];
    }

    let saveData = (values, fn) => {
        let newfields = JSON.parse(JSON.stringify(values));
        //新增&修改
wuhao's avatar
wuhao committed
160
        let difrid = iftype.val == "edit" ? { id: curitem.id, storeId: props?.curitem?.id } : { storeId: props?.curitem?.id };
wuhao's avatar
wuhao committed
161
        run({
wuhao's avatar
wuhao committed
162
            url: "/ngic-auth/sysStorePosition/save",
wuhao's avatar
wuhao committed
163 164 165 166
            params: { ...newfields, ...difrid },
        });
    };

wuhao's avatar
wuhao committed
167 168 169 170 171 172 173 174 175
    let print = selectedRowKeys.length > 0 ? [
        <Button
            disabled={!getPrem("sysDepartment_save", "ifs")}
            type="danger"
            style={{ marginLeft: 12 }}
            onClick={async () => {
                await dispatch({ type: "only" });
                setTimeout(async () => {
                    await Promise.all(selectedRowKeys.map(async (it) => {
wuhao's avatar
wuhao committed
176 177 178 179
                        await JsBarcode("#s" + it.id, it.id, {
                            width: 2,
                            height: 100,
                        });
wuhao's avatar
wuhao committed
180 181 182 183 184 185 186 187 188 189 190 191 192 193
                    }))
                }, 600)

            }}
        >
            打印
        </Button>
    ] : []

    const handlePrint = useReactToPrint({
        content: () => printRef.current,
    });


wuhao's avatar
wuhao committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207
    let extrarender = [
        <Button
            disabled={!getPrem("sysDepartment_save", "ifs")}
            type="primary"
            onClick={() => {
                for (let i in defaultFields) {
                    defaultFields[i].value = null;
                    defaultFields[i].disabled = false;
                }
                dispatch({ type: "add", fields: defaultFields });
            }}
        >
            新增
        </Button>,
wuhao's avatar
wuhao committed
208
        ...print
wuhao's avatar
wuhao committed
209 210 211 212
    ];

    return (
        <div>
wuhao's avatar
wuhao committed
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
            <ProDescriptions
                columns={[
                    {
                        "title": "仓库编号",
                        "dataIndex": "storeCode",
                        "key": "storeCode"
                    },
                    {
                        "title": "仓库名称",
                        "dataIndex": "storeName",
                        "key": "storeName"
                    },
                    {
                        "title": "仓库类型",
                        "dataIndex": "storeTypeName",
                        "key": "storeTypeName",
                    },
                    {
                        "title": "工厂名",
                        "dataIndex": "factoryName",
                        "key": "factoryName"
                    }]}
                dataSource={props.curitem}
                column={4}
            />
wuhao's avatar
wuhao committed
238 239 240 241 242 243 244
            <AutoTable
                pagetitle={"库位管理"} //页面标题
                pageextra={extrarender} //页面操作 新增or批量删除
                columns={columns}
                path="/ngic-auth/sysStorePosition/queryList"
                actionRef={actionRef}
                onRef={(node) => (ChildRef = node)}
wuhao's avatar
wuhao committed
245
                extraparams={{ storeId: props?.curitem?.id ?? "0" }}
wuhao's avatar
wuhao committed
246 247 248 249
                rowSelection={{
                    onChange: (selectedRowKeys, selectedRows) => {
                        setselectedRowKeys(selectedRows)
                    },
wuhao's avatar
wuhao committed
250

wuhao's avatar
wuhao committed
251 252
                }}
            ></AutoTable>
wuhao's avatar
wuhao committed
253
            <DrawInitForm
左玲玲's avatar
左玲玲 committed
254
                visible={false}
wuhao's avatar
wuhao committed
255
                title={iftype?.title}
wuhao's avatar
wuhao committed
256 257 258 259 260 261 262 263 264 265 266 267 268
                visible={vs}
                onClose={() => dispatch({ type: "close" })}
                footer={false}
                destroyOnClose={true}
                fields={fields}
                submitData={(values) => {
                    saveData(values);
                }}
                onChange={(changedValues, allValues) => {
                    //联动操作
                }}
                submitting={loading || !vs}
                width={"60%"}
wuhao's avatar
wuhao committed
269 270 271 272 273
                val={iftype.val}
                width={iftype.val == "only" ? 380 : 600}
                extra={iftype.val == "only" ? <Button
                    onClick={handlePrint}
                >打印</Button> : null}
wuhao's avatar
wuhao committed
274
            >
wuhao's avatar
wuhao committed
275 276 277
                <PrintProvider>
                    <div ref={printRef}>
                        {
左玲玲's avatar
左玲玲 committed
278 279
                            selectedRowKeys.map((it,i) => {
                                return <div key={i}>
wuhao's avatar
wuhao committed
280 281 282 283 284 285 286 287 288 289
                                    <NoPrint>
                                        <span>{it.storePositionName}</span>
                                    </NoPrint>
                                    <svg id={"s" + it.id} />
                                    <span className="page-break"></span>
                                </div>
                            })
                        }
                    </div>
                </PrintProvider>
wuhao's avatar
wuhao committed
290 291 292 293 294
            </DrawInitForm>
        </div>
    );
};
export default StoreApp;