materialnew.js 15.3 KB

let action = null,
    app = null;
layui.config({
    base: "/js/",
    version: 22
}).use(['ztree', 'system'], function () {
    var form = layui.form,
        $ = layui.jquery,
        table = layui.table,
        system = layui.system,
        u = new system.u(),
        sendData = {
            isok: null,
            mtTypeName: null,
            mtTypeCode: null,
        },
        tempSysData = {
            btnEditRateTitle: "编辑物料BOM比例",
            nowBtnIdentity: "btnEdit",

            enumBtnEdit: "btnEdit",
            enumBtnEditRate: "btnEditRate",
            btnEditRateReturnData: null,
        },
        treeAllData= null,
        areaName = 'material',
        tableName = 'Material';

    action = {
        rowData: null,
        addOptions: function () {
            var options = {
                fromId: "#modifyForm form",
                url: `/${areaName}/${tableName}/Ins`,
                submit: "submit(fromAdd)"
            }
            return options;
        },

        editOptions: function () {
            //区分按钮是编辑还是编辑物料BOM比例
            var options = {
                url: `/${tableName}/Upd`,
                submit: "submit(fromUpdate)"
            }
            switch (tempSysData.nowBtnIdentity) {
                case tempSysData.enumBtnEdit:
                    options.fromId = "#modifyForm form";
                    options.content = $("#modifyForm");
                    options.title ="";
                    options.url = `/${areaName}/${tableName}/Upd`;
                    break;
                case tempSysData.enumBtnEditRate:
                    options.fromId = "#modifyRateForm form";
                    options.content = $("#modifyRateForm");
                    options.title = tempSysData.btnEditRateTitle;
                    options.url = `/${areaName}/${tableName}/EditRate`;
                    break;
            }
            return options;
        },

        deleteOptions: function () {
            var options = {
                url: `/${areaName}/${tableName}/DelByIds`,
                keyId: "keys"
            }
            return options;
        },

        exportOptions: function () {
            var options = {
                fromId: "listForm",
                url: `/${areaName}/${tableName}/Export`,
                sendDataWhere: {
                    mtTypeCode: sendData.mtTypeCode == null ? 0 : sendData.mtTypeCode
                },
                isDefault: false,
                excelCols: {
                    head: app.data.cols
                }
            }
            return options;
        },

        queryOptions: function () {
            var options = {
                resetFrom: "form[lay-filter=listForm]",
                fromId: "listForm",
                urlExport: `/${areaName}/${tableName}/Export`,
                urlQuery: `/${areaName}/${tableName}/Load`,
                mainTable: app.data.tableIns
            }
            return options;
        },

        uploadOptions: function () {
            var options = {
                url: `/${areaName}/${tableName}/Import`,
                fromFile: "#excelfile",
                content: $("#ImportData")
            }
            return options;
        },
        //-----------------------------------按钮事件--------------------------------------

        editBefore: function (data, callBack) {
            if (sendData.mtTypeName != null ) {
                data["mtTypeName"] = sendData.mtTypeName;
            } else {
                treeAllData = app.data.treeObj.getNodes()[0].children;
                var arr = treeAllData.GetArrValueRow("mtTypeCode", data.mtTypeCode)
                if (arr.length > 0) data["mtTypeName"] = arr[0].name
            }
            //区分按钮是编辑还是编辑物料BOM比例
            switch (tempSysData.nowBtnIdentity) {
                case tempSysData.enumBtnEdit:
                    form.val("modifyForm", data);
                    break;
                case tempSysData.enumBtnEditRate:
                    form.val("modifyRateForm", tempSysData.btnEditRateReturnData);
                    break;
            }
            //callBack是回调函数,如果editBefore有ajax 放在成功之后
            if (callBack != null) callBack();
        },
        editSaveBefore: function (data, callBack) {
            if (typeof data.field.isDelete == "undefined") data.field["isDelete"] = "0";
            if (callBack != null) callBack();
        },

        addBefore: function (callBack) {
            if (sendData.isok !== "true") {
                layer.alert("请选择不是根节点新增物料", { icon: u.config.iconoError, shade: 0.4, time: u.config.errorTime });
                return;
            }
            if (sendData.mtTypeCode == "cp") {
                layer.alert("请选择物料类别不是【成品】后,再新增物料信息!", { icon: u.config.iconoError, shade: 0.4, time: u.config.errorTime });
                return;
            }
            form.val("modifyForm", { mtTypeCode: sendData.mtTypeCode, mtTypeName: sendData.mtTypeName });
            //callBack是回调函数,如果editBefore有ajax 放在成功之后
            if (callBack != null) callBack();
        },
        addSaveBefore: function (data, callBack) {
            if (sendData.isok !== "true") {
                layer.alert("请选择物料类别不是根节点后,再新增物料信息!", { icon: u.config.iconoError, shade: 0.4, time: u.config.errorTime });
                return;
            }
            data.field.mtTypeCode = sendData.mtTypeCode;
            if (typeof data.field.isDelete == "undefined") data.field["isDelete"] = "0";
            if (callBack != null) callBack();
        },

        //编辑物料BOM比例 
        btnEditRate: function (sysU, toolbarId, obj) {
            var checkStatus = table.checkStatus(toolbarId),
                data = checkStatus.data;
            if (data.length !== 1) {
                layer.alert("请选择一条要编辑【使用比例】的物料",{ icon: u.config.iconoError, shade: 0.4, time: u.config.errorTime });
                return false;
            }
            tempSysData.btnEditRateTitle = "为物料【" + data[0].materialCode + "--" + data[0].materialName + "】设置使用比例";
            tempSysData.nowBtnIdentity = obj.event;

            var ajaxConfig = {
                url: `/${areaName}/${tableName}/GetMaterialRateByCode?materialKeys=` + data[0].keys,
                type: "get",
                success: function (result) {
                    if (u.successBefore(result)) return;
                    tempSysData.btnEditRateReturnData = result.Result[0];
                    $("a[lay-event=btnEdit]").click();
                }
            };
            u.ajax(ajaxConfig); 
        },

        //同步信科物料
        btnSyncMaterial: function (obj) {
            var ajaxConfig = {
                url: $("#materiaApilUrl").val(),
                type: "get",
                isDataFilter: false,//不特殊处理调用api返回的数据
                success: function (result) {
                    if (u.successBefore(result)) return false;
                    layer.msg(result.Message, { icon: 1, time: 3000 })
                    action.actionSuccess();
                }
            };
            u.ajax(ajaxConfig);
        },

        //产品明细物料绑定删除
        checkboxMethod: function (obj) {
            var page = "".GetUrlParam("page"),
                proHeadKeys = "".GetUrlParam("proHeadKeys");
            if (page == "ProductDesc") {
                var checkBoxData = {
                    materialCodes: null,
                    proHeadKeys: proHeadKeys,
                    checkeds: obj.checked,
                };
                if (obj.type == "all") {
                    checkBoxData.materialCodes =  u.getTableAllData(app.data.tableElem).GetArrValue("materialCode");
                } else {
                    checkBoxData.materialCodes = obj.data.materialCode;
                }
                if (checkBoxData.materialCodes == null || checkBoxData.materialCodes == "") {
                    layer.alert("checkboxMethod:物料编码标识【materialCode】为空,请重新关闭后再操作,反复出现请联系管理员", { icon: 2, shadeClose: true, title: "提示" });
                    return false;
                }
                var ajaxConfig = {
                    data: checkBoxData,
                    url: `/configure/ProductHeader/AssignMaterial`,
                    success: function (result) {
                        if (u.successBefore(result)) return false;
                        layer.msg(result.Message);
                    }
                };
                u.ajax(ajaxConfig);
            }

        },
        //所有动作成功之后
        actionSuccess: () => {
            app.methods.initTable({ mtTypeCode: sendData.mtTypeCode });
        },
        closeAfter: function (callBack) {
            tempSysData.nowBtnIdentity = tempSysData.enumBtnEdit;
            if (callBack != null) callBack();
        }
    }

    app = {
        data: {
            cols: [[
                { checkbox: true, fixed: true },
                { field: "id", width: 80, hide: true, title: "Id" },
                { field: "keys", width: 120, title: "keys", hide: true, },
                { field: "materialCode", width: 120, title: "物料编号" },
                { field: "materialName", width: 150, title: "物料名称" },
                { field: "mtTypeCode", width: 150, title: "物料类别", templet: function (d) { return GetDicLabel("mtTypeCode", "mtTypeCode", "mtTypeName", d.mtTypeCode) } },
                { field: "mtClassify", width: 150, title: "物料分类", templet: function (d) { return GetDicLabel("mtClassify", "DictValue", "DictLabel", d.mtClassify) } },
                
                { field: "specifications", width: 100, title: "规格" },
                { field: "materialType", width: 200, title: "物料类型" },
                { field: "types", width: 150, title: "材质" },
                { field: "diameter", width: 100, title: "直径" },
                { field: "thickness", width: 150, title: "壁厚" },
                { field: "unitCode", width: 150, title: "单位", templet: function (d) { return GetDicLabel("unitCode", "Code", "Name", d.unitCode) } },//
                { field: 'plmeid', width: 150, title: 'plmeid', hide: true },
                { field: 'otherCode', width: 120, title: '外部编码' },
                { field: 'isDelete', width: 120, title: '是否有效', templet: "#isSysDeleteTemplet" },

                { field: "createTime", width: 150, title: "创建时间" },
                { field: "createBy", width: 150, title: "创建用户" },
                //{ field: "updateTime", width: 150, title: "更新时间" },
                //{ field: "updateBy", width: 150, title: "更新用户" }
            ]],
            urlTable: `/${areaName}/${tableName}/Load`,
            tableIns: null,
            tableElem: "mainList",
            selectOption: {
                unitCode: {
                    SelType: "FromUrl",
                    SelFrom: "/HomeRedis/GetBaseInfoByKey?key=base_material_unit",
                    SelLabel: "Name",
                    SelValue: "Code",
                    Dom: [$("[name='unitCode']")]
                },
                mtClassify: {
                    SelType: "FromDict",
                    SelFrom: "mtClassify",
                    SelLabel: "DictLabel",
                    SelValue: "DictValue",
                    Dom: [$("[name='mtClassify']")]
                },
                mtTypeCode: {
                    SelType: "FromUrl",
                    SelFrom: "/HomeRedis/GetBaseInfoByKey?key=base_material_type",
                    SelLabel: "mtTypeName",
                    SelValue: "mtTypeCode",
                    Dom: [$("[name='mtTypeCode']")]
                },
                oprSequenceCode: {
                    SelType: "FromUrl",
                    SelFrom: "/configure/ProcessRoute/GetProcessDetail",
                    SelLabel: "oprSequenceName",
                    SelValue: "oprSequenceCode",
                    Dom: [$("[name='oprSequenceCode']")]
                },
                //返回的数据 用于后续操作
                selectData: {

                }
            },

            tree: $("#tree"),
            treeObj: null,
            setting: {
                view: { selectedMulti: false },
                async: {
                    enable: true,
                    url: `/${areaName}/${tableName}/GetTreeList`,
                    type: "get"
                },
                data: {
                    simpleData: {
                        enable: true,
                        idKey: "keys",
                        pIdKey: "parentId",
                        rootPId: ""
                    }
                },
                callback: {
                    onClick: function (event, treeId, treeNode) {
                        sendData.isok = treeNode.level!=0?"true":"false";
                        sendData.mtTypeCode = treeNode.mtTypeCode;
                        sendData.mtTypeName = treeNode.name;
                        app.methods.initTable({ mtTypeCode: sendData.mtTypeCode });
                    },
                    onAsyncSuccess: function (event, treeId, treeNode, msg) {
                        app.data.treeObj.expandAll(true);
                    }
                }
            }
        },
        methods: {
            initTable: function (options) {
                var config = {};
                if (options != undefined) $.extend(config, options);
                let opt = {
                    cols: u.columnRecord(app.data.tableElem, app.data.cols),
                    elem: "#" + app.data.tableElem,
                    url: `/${areaName}/${tableName}/Load`,
                    toolbar: '#toolbarTable',
                    where: config,
                    doneExtend: function (res, obj) {
                        //产品明细选中
                        var pageFlag = "".GetUrlParam("page");     
                        if (pageFlag == "ProductDesc") {
                            var targetArrValue = "".GetUrlParam("materialCode");
                            var selectColKey = "".GetUrlParam("selectColKeyCustom");
                            u.SetTableCheck(targetArrValue, res, selectColKey, app.data.tableElem);
                        }
                    }
                }
                var pageFlag = "".GetUrlParam("page");
                if (pageFlag == "ProductDesc") {
                    opt["height"] = "".GetUrlParam("height")-20;
                }
                app.data.tableIns = u.initTable(opt);
            },
            initTree: function () {
                setTimeout(() => {
                    app.data.treeObj = $.fn.zTree.init(app.data.tree, app.data.setting);
                    app.data.tree.height($("div.layui-table-view").height() - 20);
                }, 50);
            },
            initFrom: function () {
                u.initSelect(app.data.selectOption);
            }
        },
        registerEvent: function () {


        },
        init: function () {
            app.methods.initFrom();
            app.methods.initTree();
            app.methods.initTable();
            app.registerEvent();
        }
    };
    app.init();
})