BaseEquipment.js 15.8 KB
let action = null;
layui.config({
    base: "/js/",
    version: 1
}).use(['system'], function () {
    var form = layui.form,
        $ = layui.jquery,
        element = layui.element,
        table = layui.table,
        system = layui.system,
        sysU = new system.u(),
        areaName = "equipment",
        controllerName = "BaseEquipment",
        app = null,
        selectMaintainRuleOpt = {
            selectMaintainRule: {
                SelType: "FromUrl",
                SelFrom: `/${areaName}/BusEquipmentMaintainRuleHead/Load`,
                SelLabel: "equipmentRuleName",
                SelValue: "equipmentRuleCode",
                Where: {},
                Dom: [$("#modifyForm [name='equipmentMaintainRuleCode']")],
            },
            selectData: {

            },
        },
        selectFactoryOpt = {
            factoryCode: {
                SelType: "FromUrl",
                SelFrom: `/configure/Factory/Load`,
                SelLabel: "factoryName",
                SelValue: "factoryCode",
                Where: {},
                Dom: [$("[name='factoryCode']")],
               /* isFirstSelected: false*/
            },
            selectData: {

            },
        },
        sendData = {
            editMaintainRuleWhere: null,
            selectFactorySelectWhere: null
        };
    

    action = {
        addOptions: function () {
            var options = {
                fromId: "#modifyForm form",
                url: `/${areaName}/${controllerName}/Ins`,
                //sendDataWhere: null,
                //isAddWhereExtend: "arbitrarily",
                //mainTable: app.data.tableIns,
                submit: "submit(fromAdd)"
            }
            return options;
        },

        editOptions: function () {
            var options = {
                fromId: "#modifyForm form",
                url: `/${areaName}/${controllerName}/Upd`,
                submit: "submit(fromUpdate)"
            }
            return options;
        },

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

        exportOptions: function () {
            var options = {
                fromId: "listForm",
                url: `/${areaName}/${controllerName}/Export`,
                isDefault: false,
                sendDataWhere: {},
                excelCols: {
                    head: app.data.cols,
                    body: app.data.colsDesc
                }
            }
            return options;
        },

        queryOptions: function () {
            var options = {
                resetFrom: "form[lay-filter=listForm]",
                fromId: "listForm",
                urlExport: `/${areaName}/${controllerName}/Export`,
                urlQuery: `/${areaName}/${controllerName}/Load`,
                sendDataWhere: {},
                mainTable: app.data.tableIns
            }
            return options;
        },
        /*rowClickOptions: function () {
            var options = {
                //是否主子表
                isDefault: true,
                targetTableId: app.data.tableElem,
                tabfilter: "tableTabBody",
                tabId: 2,
                customFn: app.methods.initTableDesc
            }
            return options;
        },*/

        uploadOptions: function () {
            var options = {
                area: ["500px", "300px"],
                url: `/${areaName}/${controllerName}/Import`,
                fromFile: "#excelfile",
                content: $("#ImportData")
            }
            return options;
        },

        //内部不支持异步
        uploadBefore: function (checkStatus, parm) {
            if (checkStatus.data.length === 0 ) {
                layer.alert(`请选择一条数据后,在上传或者更新图片。当前选择的行数【${checkStatus.data.length}】条!`, { icon: sysU.config.iconoError, shadeClose: true, title: "错误信息" });
                return false;
            }
            form.val(parm.content[0].id, checkStatus.data[0]);
            return checkStatus.data[0] != undefined;
        },

        addBefore: function (callBack) {
            sendData.editMaintainRuleWhere = null;

            selectMaintainRuleOpt.selectMaintainRule.Dom[0].empty();
            selectFactoryOpt.factoryCode.Dom[0].empty();
            form.render("select");
            //callBack是回调函数,如果editBefore有ajax 放在成功之后
            if (callBack != null) callBack();
        },

        addSaveBefore: function (data, callBack) {
            typeof data.field.isEnable == "undefined" ? data.field["isEnable"] = false : data.field["isEnable"] = true;
            if (callBack != null) callBack();
        },
        editBefore: function (data, callBack) {
            sendData.editMaintainRuleWhere = data;
            selectMaintainRuleOpt.selectMaintainRule.Where["equipmentTypeCode"] = data.equipmentTypeCode;
            var fn = () => {
                callBack();
                form.val("modifyForm", data);
            }
            sysU.initSelect(selectMaintainRuleOpt, fn);

            selectFactoryOpt.factoryCode.Where["factorySelectWhere"] = data.projectKeys;
            sysU.initSelect(selectFactoryOpt, () => {
                form.val("modifyForm", data);
            });
        },
        editSaveBefore: function (data, callBack) {
            typeof data.field.isEnable == "undefined" ? data.field["isEnable"] = false : data.field["isEnable"] = true;
            if (callBack != null) callBack();
        },

        //sim卡绑定设备时勾选触发
        checkboxMethod: function (obj) {
            var page = "".GetUrlParam("page"),
                simNumber = "".GetUrlParam("simNumber");
            if (page == "BaseSim") {
                let checkData = action.checkTableALLRowData,
                    equipmentCode = "",
                    projectKeysList=[];
                equipmentCode = checkData.GetArrValue("equipmentCode");
                projectKeysList = checkData.uniqueFuncs("projectKeys");

                if (projectKeysList.length > 1) {
                    let projectNameList = projectKeysList.GetArrValue("projectName");
                    layer.alert(`SIM卡关联设备,只能关联同一个项目【${projectNameList}】下面的设备(设备页面2秒会自动刷新数据)!`, { icon: sysU.config.iconoError, shadeClose: true, title: "错误信息" });
                    setTimeout(() => {
                        action.actionSuccess("mainList")
                    }, 2000);
                    return false;
                }
                var sendData = {
                    simNumber: simNumber,
                    checkeds: obj.checked,
                    equipmentCode: equipmentCode,
                    projectKeys: projectKeysList[0].projectKeys
                };
                var ajaxConfig = {
                    data: sendData,
                    url: "/base/BaseSim/BindEquipment",
                    success: function (result) {
                        if (sysU.successBefore(result)) return false;
                        layer.msg(result.Message);
                        sysU.refreshTable(app, sysU, null, "main");
                    }
                };
                sysU.ajax(ajaxConfig);
            }
        },

        //所有动作成功之后
        actionSuccess: (flag) => {
            sysU.refreshTable(app, sysU, null, flag);
        },
        closeAfter: function (callBack) {
            if (callBack != null) callBack();
        },

        //设备类型加载保养规则
        equipmentTypeCodeFromEvent: function (data) {
            if (data.value == "") {
                selectMaintainRuleOpt.selectMaintainRule.Dom[0].empty();
                form.render("select");
                return false;
            }
            //对应到实体(筛选条件)中字段属性
            selectMaintainRuleOpt.selectMaintainRule.Where["equipmentTypeCode"] = data.value;
            sysU.initSelect(selectMaintainRuleOpt);
        },

        //所属项目加载项目厂房
        projectKeysFromEvent: function (data) {
            if (data.value == "") {
                selectFactoryOpt.factoryCode.Dom[0].empty();
                form.render("select");
                return false;
            }
            //对应到实体(筛选条件)中字段属性
            selectFactoryOpt.factoryCode.Where["factorySelectWhere"] = data.value;
            sysU.initSelect(selectFactoryOpt);
        },
        EqDescTableRowEvent: function (obj) {
            window.open(`/equipment/BaseEquipment/EquipmentVisual?eqCode=${obj.data.equipmentCode}&eqType=${obj.data.equipmentTypeCode}`,"_self");
        },
        EqeqmaintainTableRowEvent: function (obj) {
            window.open("/equipment/BaseEquipment/EquipmentVisual", "_self");
        },
        //关联设备链接 
        btnBindEquipment: function (sU, toolbarId, obj) {
            sU.getParentElemId("BaseSim").click();
        },
    }

    app = {
        data: {
            cols: [[
                { field: "id", width: 80, hide: true, title: "Id" },
                { field: "projectKeys", width: 150, title: "项目keys", hide: true, },
                { field: "projectName", width: 150, title: "项目" },
                { field: "factoryName", width: 150, title: "厂房" },

                { field: "equipmentCode", width: 120, title: "设备编码", hide: true },
                { field: "equipmentName", width: 220, title: "设备"  },
                { field: "simNumber", width: 150, title: "SIM卡号" },
                { field: "equipmentTypeCode", width: 150, title: "设备类型编码", hide: true },
                { field: "name", width: 120, title: "设备类型" },
                { field: "equipmentMaintainRuleCode", width: 150, title: "维护规则", hide: true },

                {
                    field: "status", width: 200, title: "ip地址(5min无数据)", templet: function (d) {
                        if (d.status == "" || d.status == null) return "";
                        var bg = "layui-badge-dot layui-bg-green",
                            statusText = "在线";
                        if (d.status == "Failure") {
                            statusText = "故障";
                            bg = "layui-badge-dot layui-bg-red";
                        }
                        if (d.eqOnLineUpdateTime !="0001-01-01 00:00:00") {
                            var dateDiff = new Date(d.eqOnLineNowTime) - new Date(d.eqOnLineUpdateTime);
                            var seconds = dateDiff / 1000;
                            if (seconds > 300) {
                                statusText = "离线";
                                bg = "layui-badge-dot layui-bg-gray";
                            }
                        }                       
                        return ` <span style=" display: inline-block;width: 84px;" >${d.ip}</span><span class="${bg}" style=" left: 7px;"></span> <span style="padding-left: 8px;">${statusText}</span`;
                    }
                },

            

                { field: "startTime", width: 150, title: "出厂时间" },
                {
                    field: "imgSrc", width: 150, title: "图片", event: 'fileEvent', templet: function (d) {
                        d.url = "d.imgSrc";
                        if (d.imgSrc == "" || d.imgSrc == null) return "";
                        return `<div class="div-flex"> <img src="/images/equipment/${d.imgSrc}" onerror="this.src='/images/equipment/404.jpg'"/>&nbsp;
                                      <a  data-href="/images/equipment/${d.imgSrc}" href="#"  style="color: cornflowerblue;text-decoration: underline">地址</a>
                                </div>`;
                    }
                },

                {
                    field: 'url', width: 180, title: '文件下载'
                },
                { field: "ip", width: 200, title: "IP", hide: true, },
                { field: "destinationArea", width: 150, title: "区域", hide: true,},
                { field: "remark", width: 150, title: "描述", hide: true, },
                { field: "isEnable", width: 100, title: "启用", templet: '#isSysEnableTemplet' },

                { field: "createTime", width: 150, title: "createTime" },
                { field: "createBy", width: 150, title: "createBy" },

                { field: "updateTime", width: 150, title: "updateTime", hide: true, },
                { field: "updateBy", width: 150, title: "updateBy", hide: true, },
            ]],
            colsDesc: null,
            tableIns: null,
            tableElem: "mainList",
            tableInsDesc: null,
            tableElemDesc: "mainListDesc",

            //下拉框配置
            selectOption: {
                selectEquipmentTypeCode: {
                    SelType: "FromUrl",
                    SelFrom: `/${areaName}/BaseEquipmentType/Load`,
                    //SelModel:"EquipmentTypeName",
                    SelLabel: "name",
                    SelValue: "code",
                    Dom: [$("[name='equipmentTypeCode']")],
                    isFirstSelected: false
                },
                projectKeys: {
                    SelType: "FromUrl",
                    SelFrom: `/configure/BaseProject/Load`,
                    SelLabel: "projectName",
                    SelValue: "keys",
                    Dom: [$("[name='projectKeys']")],
                    isFirstSelected: false
                },
                //返回的数据 用于后续操作
                selectData: {

                }
            },
        },
        methods: {
            initTable: function (opt) {
                var config = {};
                if (opt != undefined) $.extend(config, opt);
                //项目地图跳转过来的
                var flag = $(".layui-breadcrumb");
                if (flag.length >0) {
                    config["projectKeys"] = localStorage.getItem("projectMapKeys");
                    app.data.cols[0].push({ fixed: 'right', title: '操作', width: 150, toolbar: '#barDemo' });
                } else {
                    app.data.cols[0].unshift({ checkbox: true, fixed: true });
                }
                let options = {
                    elem: "#" + app.data.tableElem,
                    url: `/${areaName}/${controllerName}/Load`,
                    cols: sysU.columnRecord(app.data.tableElem, app.data.cols),
                    toolbar: '#toolbarTable',
                    where: config,
                    //height: "full-56",//如果是主明细页签,列表主体高度要设置,否则分页导航不直观展示
                    doneExtend: function (res, obj) {
                        //SIM卡关联设备的实时刷新选中
                        var pageFlag = "".GetUrlParam("page");
                        if (pageFlag == "BaseSim") {
                            var targetArrValue = "".GetUrlParam("simNumber");
                            var selectColKey = "".GetUrlParam("selectColKeyCustom");
                            sysU.SetTableCheck(targetArrValue, res, selectColKey, app.data.tableElem);
                        } 
                    }
                }

                if (flag.length > 0) {
                    options["height"] = "full-40"
                }
                app.data.tableIns = sysU.initTable(options);
            },


            initFrom() {
                sysU.initSelect(app.data.selectOption);

                //sysU.initSelecteByEnum(app.data.selectOptionEnum);
            }
        },
        registerEvent: function () {

        },
        init: function () {
            //var sendDataWhere = form.val("listForm")
            app.methods.initTable();
            app.methods.initFrom();
            app.registerEvent();
        }
    };
    app.init();
});