let callbackName = "callBack_hubiaoshidianjiayuguan";
let callbackName1 = "callBack_hubiaoshidianjiayuguan01";
let list = []
let alist = []
let alist1 = []
let idList = []
let pendingList = []
window[callbackName] = function (targeturl, actionurl, responseTxt) {
    try {

        const res = JSON.parse(responseTxt
            .replace(/%7B/g, '{')
            .replace(/%27/g, "'")
            .replace(/%7D/g, "}")
            .replace(/'/g, '"')
            .replace("}/", "}")
        );
        console.log(res);
        if (res.msg && res.msg == "success") {
            list = res.page.list;
        //    list =[{
        //     "eventId": "5e8dcb5c-7add-4812-98e7-46ae3714ebc7",
        //     "outageType": "2",
        //     "outageTypeName": "单户停电",
        //     "ifOutage": "停电",
        //     "ifOutageName": null,
        //     "eventStatus": "处理中",
        //     "offTime": "2026-01-08 19:02:33",
        //     "onTime": null,
        //     "consNo": "6261250673482",
        //     "consType": "0",
        //     "consTypeName": "非重要用户",
        //     "consName": "樊微",
        //     "consAddr": "甘肃省嘉峪关市峪泉镇安远沟村机场路安远沟村六组",
        //     "phoneNum": "13014182867",
        //     "eqPsrId": "7036a473218a54727b73001fcd0173703307ad1ea8",
        //     "eqPsrName": "安远沟六组养殖区变",
        //     "eqPsrType": "0110",
        //     "eqPsrTypeName": "柱上变压器",
        //     "feederPsrId": "27DKX-6541",
        //     "feederPsrName": "110kV雄关变电站126雄开一回线",
        //     "startStationId": "6EFB88B2-07C8-43FB-A288-569A21BDBD93-02444",
        //     "startStationName": "110kV雄关变电站",
        //     "cityId": "8a5470c55ccb982b015d8840bfcc2a93",
        //     "cityName": "嘉峪关",
        //     "maintenanceOrgId": "8a5470c55ccb982b015d884190fb2a94",
        //     "maintenanceOrgName": null,
        //     "maintenanceGroupId": "72d5b4c09a034c60aeca73e0fbba5dd6",
        //     "maintenanceGroupName": "配网无人机巡检班（含运维业务）",
        //     "orderId": "ZDFW20260108210159684446849",
        //     "orderNo": "ZDFW20260108210159684446849",
        //     "orderStatus": "02",
        //     "sourceMarking": "1",
        //     "sourceMarkingName": "用采上送",
        //     "sourceReply": null,
        //     "sourceReplyName": null,
        //     "batchNumber": "5e8dcb5c-7add-4812-98e7-46ae3714ebc7",
        //     "createTime": "2026-01-08 19:12:33",
        //     "upTime": "2026-01-08 19:12:33",
        //     "qxdbh": null,
        //     "powerTypeOne": null,
        //     "powerTypeTwo": null,
        //     "showFlag": "1",
        //     "orgshortName": null,
        //     "id": null
        // }]
            list.forEach(item => {
                // 户号
                idList.push(item.consNo)
            })
        } else {
            //执行 下一队列方法
            _this.processQueue();
        }
    } catch (e) {
        let audioText = ""
        audioFC(audioText)
    }
    window[callbackName1] = function (targeturl, actionurl, responseTxt) {
        try {
            const res = JSON.parse(responseTxt
                .replace(/%7B/g, '{')
                .replace(/%27/g, "'")
                .replace(/%7D/g, "}")
                .replace(/'/g, '"')
                .replace("}/", "}")
            );
            if (res.msg == "success") {
                res.page.list.forEach((item) => {
                    if (item.gdztmc == "待审核") {
                        alist.push(item);
                    }
                    if (item.gdztmc == "已归档") {
                        alist1.push(item);
                    }
                });
            }

        } catch (e) {
            console.log(e);
        }
        let obj = {
            time: mac.moment().format("YYYY-MM-DD HH:mm:ss"), //监测时间
            type: "户表失电-嘉峪关", //监测工单类型
            pending: list.length, //待处理数字
            pendingList: JSON.stringify(idList), //待处理列表
            audit: alist.length, //待审核
            processed: alist1.length, //已处理
        };
        mac.localHostAxjos({
            url: "http://localhost:13313/MonitorServices/getMonitorLog",
            method: "POST",
            data: JSON.stringify({ type: "户表失电-嘉峪关" }), //必须是字符串格式
        }).then((res2) => {
            if (res2.status == 200) {
                let index = 0
                if (res2.data.length < 1 && list.length > 0 && _this.queueObj.voiceRemind == '1') {
                    let audioText = "您有户表失电工单，请及时处理"
                    audioFC(audioText)
                }
                if (res2.data.length > 0) {
                    let logList = []
                    if (res2.data[0].pendingList && res2.data[0].pendingList?.indexOf('解析异常') == -1) {
                        logList = JSON.parse(res2.data[0].pendingList)
                    }
                    if (!Array.isArray(logList)) {
                        logList = JSON.parse(logList)
                    }
                    try {
                        idList.forEach(x => {
                            index = logList.indexOf(x)
                            if (index == -1) {
                                throw new Error("停止循环")
                            }
                        })
                    } catch (e) {
                        console.log("跳出循环");
                    }
                }
                if (index == -1 && _this.queueObj.voiceRemind == '1') {
                    let audioText = "您有户表失电工单，请及时处理"
                    audioFC(audioText)
                }
                mac.localHostAxjos({
                    url: "http://localhost:13313/MonitorServices/setMonitorData",
                    method: "POST",
                    data: JSON.stringify(obj)
                })
                mac.localHostAxjos({
                    url: "http://localhost:13313/MonitorServices/setMonitorLog",
                    method: "POST",
                    data: JSON.stringify(obj),
                })
            }
        });
        // 将派过的工单去重
        mac.localHostAxjos({
            url: "http://localhost:13313/MonitorServices/getDisposeLog",
            method: "POST",
            data: JSON.stringify({ type: "户表失电-嘉峪关" }), //必须是字符串格式
        }).then((res) => {
            if (res.status == 200) {
                let resList = []
                try {
                    res.data.forEach(item => {
                        if (item.orderID != '') {
                            let logList = JSON.parse(item.orderID)
                            if (!Array.isArray(logList)) {
                                logList = JSON.parse(logList)
                                if (!Array.isArray(logList)) {
                                    logList = JSON.parse(logList)
                                }
                            }
                            logList.forEach(i => {
                                resList.push(i.id)
                            })
                        }
                    })
                    resList = Array.from(new Set(resList))
                    list.forEach(y => {
                        if (resList.indexOf(y.eventId) == -1) {
                            pendingList.push(y)
                        }
                    })
                } catch (error) {
                    obj.pendingList = "解析异常" + error
                    mac.localHostAxjos({
                        url: "http://localhost:13313/MonitorServices/setMonitorLog",
                        method: "POST",
                        data: JSON.stringify(obj)
                    });
                }
                if (pendingList.length > 0) {
                    //执行 下一队列方法
                    _this.queueObj.pendingList = pendingList;
                    _this.autoTask();
                } else {
                    //执行 下一队列方法
                    _this.processQueue();
                }
            }
        })
    };
    let slsj = `${mac.moment().format("YYYY-MM-DD")}+00:00:00,${mac.moment().format("YYYY-MM-DD")}+23:59:59`;
    const requestParam = `page=1&createTime=${slsj}&orgNo=${_this.orgID}&limit=200`;
    // sgBrowerserJsAjax2(
    //     "window." + callbackName1,
    //     "http://21.77.244.194:18890/mainSystem/#/login",
    //     `http://21.77.244.194:18890/gdgl/active/service/order/list?` + requestParam,
    //     "GET",
    //     `Content-Type:application/json;userId:${_this.userID};Access-Control-Allow-Credentials:true;Access-Control-Allow-Methods: GET, POST, OPTIONS,PUT,DELETE,OPTION;Access-Control-Allow-Origin:*`,
    //     ""
    // );
    BrowserAction(
        'sgBrowerserJsAjax2',
        "window." + callbackName1,
        "http://21.77.244.194:18890/mainSystem/#/login",
        `http://21.77.244.194:18890/gdgl/active/service/order/list?` + requestParam,
        "GET",
        `Content-Type:application/json;userId:${_this.userID};Access-Control-Allow-Credentials:true;Access-Control-Allow-Methods: GET, POST, OPTIONS,PUT,DELETE,OPTION;Access-Control-Allow-Origin:*`,
        ""
    )
};
let slsj = `${mac.moment(new Date().setDate(new Date().getDate() - 2)).format("YYYY-MM-DD")}+00:00:00,${mac.moment().format("YYYY-MM-DD")}+23:59:59`;
let requestParam = `page=1&sortFiled=off_time&ifAsc=false&outageType=2&flag=0&eventStatus=1&offTime=${slsj}&orgNo=${_this.orgID}&limit=100`;
// sgBrowerserJsAjax2(
//     "window." + callbackName,
//     "http://21.77.244.194:18890/mainSystem/#/login",
//     `http://21.77.244.194:18890/outage/dhsd/dhsdList?` + requestParam,
//     "POST",
//     `Content-Type:application/json;userId:${_this.userID};Access-Control-Allow-Credentials:true;Access-Control-Allow-Methods: GET, POST, OPTIONS,PUT,DELETE,OPTION;Access-Control-Allow-Origin:*`,
//     ""
// );
BrowserAction(
    'sgBrowerserJsAjax2',
    "window." + callbackName,
    "http://21.77.244.194:18890/mainSystem/#/login",
    `http://21.77.244.194:18890/outage/dhsd/dhsdList?` + requestParam,
    "POST",
    `Content-Type:application/json;userId:${_this.userID};Access-Control-Allow-Credentials:true;Access-Control-Allow-Methods: GET, POST, OPTIONS,PUT,DELETE,OPTION;Access-Control-Allow-Origin:*`,
    ""
)
function audioFC(text) {
    mac.audioPlay(text).then(response => {
        if (response.status == 200) {
            mac.localHostAxjos({
                url: "http://localhost:13313/MonitorServices/setAudioPlayLog",
                method: "POST",
                data: JSON.stringify({ type: text, time: mac.moment().format("YYYY-MM-DD HH:mm:ss"), status: "成功" })
            })
        } else {
            mac.localHostAxjos({
                url: "http://localhost:13313/MonitorServices/setAudioPlayLog",
                method: "POST",
                data: JSON.stringify({ type: text, time: mac.moment().format("YYYY-MM-DD HH:mm:ss"), status: "失败" })
            })
        }
    }).catch(err => {
        mac.localHostAxjos({
            url: "http://localhost:13313/MonitorServices/setAudioPlayLog",
            method: "POST",
            data: JSON.stringify({ type: text, time: mac.moment().format("YYYY-MM-DD HH:mm:ss"), status: "异常" })
        })
    })
}
