var QXGDSZ = []; //95598抢修-市指工单去重
var QXGDSZList = [];
var qxszClassList = [];

var eventId = null; //派单日志参数
var eqPsrName = null; //派单日志参数
var phone = "";
var tel = [];
let objData = {
    time: mac.moment().format("YYYY-MM-DD HH:mm:ss"),
    type: "95598抢修-市指",
    pending: 0,
    pendingList: "进入自动派单",
    audit: 0,
    processed: 0
};
mac.localHostAxjos({
    url: "http://localhost:13313/MonitorServices/setMonitorLog",
    method: "POST",
    data: JSON.stringify(objData)
});
mac.localHostAxjos({
    url: "http://localhost:13313/MonitorServices/getDisposeLog",
    method: "POST",
    data: JSON.stringify({ type: "95598抢修-市指" }), //必须是字符串格式
})
    .then((res) => {
        if (res.status == 200) {
            // 抢修工单
            let arr = res.data.reverse();
            obj.pendingList
                .filter((x) => arr.findIndex((y) => y.orderID == x.id) == -1)
                .forEach((item) => {
                    QXGDSZList.push(item);
                });
            if (QXGDSZList.length > 0) {
                objData.pendingList = '未派过单-进入自动派单'
                mac.localHostAxjos({
                    url: "http://localhost:13313/MonitorServices/setMonitorLog",
                    method: "POST",
                    data: JSON.stringify(objData)
                });
                mac.localHostAxjos({
                    url: "http://localhost:13313/MonitorServices/getClassList",
                    method: "POST",
                    data: JSON.stringify({ type: "95598抢修" }),
                })
                    .then((res) => {
                        if (res.status == 200) {
                            res.data = res.data.filter(item => item.type != '95598抢修')
                            if (res.data.length == !0) {
                                res.data.forEach((item) => {
                                    let arr = [];
                                    if (item.scope != "") {

                                        item.scope = item.scope.replace(/\s*/g, "");
                                        arr.push(item.scope.split("、"));
                                        item.scope = [].concat.apply([], arr);
                                    } else {
                                        item.scope = [];
                                    }
                                });
                            }

                            qxszClassList = res.data;
                            let callbackName = "callBacks_95598qiangxiusz";
                            window[callbackName] = function (
                                targeturl,
                                actionurl,
                                responseTxt
                            ) {
                                try {
                                    const resData = JSON.parse(
                                        decodeURI(responseTxt).replace(/'/g, '"').replace("}/", "}")
                                    );
                                    if (resData.code == 0) {
                                        setTimeout(() => {
                                            mac.audioPlay("95598抢修-市指工单自动派单成功").then(response => {
                                                if (response.status == 200) {
                                                    mac.localHostAxjos({
                                                        url: "http://localhost:13313/MonitorServices/setAudioPlayLog",
                                                        method: "POST",
                                                        data: JSON.stringify({ type: '95598抢修-市指自动派单成功', 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: '95598抢修-市指自动派单成功', 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: '95598抢修-市指自动派单成功', time: mac.moment().format("YYYY-MM-DD HH:mm:ss"), status: "异常" })
                                                })
                                            })
                                        }, 6000);
                                        let type = "95598抢修-市指";
                                        let orderID = eventId;
                                        let time = mac.moment().format("YYYY-MM-DD HH:mm:ss");
                                        let name = eqPsrName;
                                        let state = "成功";
                                        //  i国网
                                        const request = {
                                            phoneList: [].concat.apply([], tel),
                                            content: "【业数融合一平台】您有95598抢修-市指工单，请及时处理！", //短信内容
                                        };
                                        if (request.phoneList.length > 0) {
                                            mac.sendMessages(request).then(res4 => {
                                                if (res4.status == 200) {
                                                    mac.localHostAxjos({
                                                        url: "http://localhost:13313/MonitorServices/setSendMessageLog",
                                                        method: "POST",
                                                        data: JSON.stringify({ type: '95598抢修-市指自动派单', time: mac.moment().format("YYYY-MM-DD HH:mm:ss"), status: "成功" })
                                                    })
                                                } else {
                                                    mac.localHostAxjos({
                                                        url: "http://localhost:13313/MonitorServices/setSendMessageLog",
                                                        method: "POST",
                                                        data: JSON.stringify({ type: '95598抢修-市指自动派单', time: mac.moment().format("YYYY-MM-DD HH:mm:ss"), status: "失败" })
                                                    })
                                                }
                                            }).catch(err => {
                                                mac.localHostAxjos({
                                                    url: "http://localhost:13313/MonitorServices/setSendMessageLog",
                                                    method: "POST",
                                                    data: JSON.stringify({ type: '95598抢修-市指自动派单', time: mac.moment().format("YYYY-MM-DD HH:mm:ss"), status: "异常" })
                                                })
                                            })
                                        }
                                        // 呼叫接口
                                        setTimeout(() => {
                                            const params = {
                                                taskName: "95598抢修-市指",
                                                phone: [].concat.apply([], tel),
                                                content: "您有95598抢修-市指工单，请尽快接单处理",
                                                name: "95598抢修-市指",
                                            };
                                            if (params.phone.length > 0) {
                                                mac.callOutLogin(params);
                                            }
                                        }, 1000);
                                        mac.localHostAxjos({
                                            url: "http://localhost:13313/MonitorServices/setDisposeLog",
                                            method: "POST",
                                            data: JSON.stringify({
                                                type,
                                                orderID,
                                                name,
                                                time,
                                                state,
                                            }), //必须是字符串格式
                                        }).then(re => {
                                            if (re.status == 200) {
                                                mac.exeTQueue();
                                            }
                                        })
                                    } else {
                                        setTimeout(() => {
                                            mac.audioPlay("95598抢修-市指工单自动派单失败，请及时处理！").then(response => {
                                                if (response.status == 200) {
                                                    mac.localHostAxjos({
                                                        url: "http://localhost:13313/MonitorServices/setAudioPlayLog",
                                                        method: "POST",
                                                        data: JSON.stringify({ type: '95598抢修-市指自动派单失败', 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: '95598抢修-市指自动派单失败', 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: '95598抢修-市指自动派单失败', time: mac.moment().format("YYYY-MM-DD HH:mm:ss"), status: "异常" })
                                                })
                                            })
                                        }, 6000);
                                        let type = "95598抢修-市指";
                                        let orderID = eventId;
                                        let time = mac.moment().format("YYYY-MM-DD HH:mm:ss");
                                        let name = eqPsrName;
                                        let state = "失败";
                                        mac.localHostAxjos({
                                            url: "http://localhost:13313/MonitorServices/setDisposeLog",
                                            method: "POST",
                                            data: JSON.stringify({
                                                type,
                                                orderID,
                                                name,
                                                time,
                                                state,
                                            }), //必须是字符串格式
                                        }).then(re => {
                                            if (re.status == 200) {
                                                mac.exeTQueue();
                                            }
                                        })
                                    }
                                } catch (x) {
                                    setTimeout(() => {
                                        mac.audioPlay("95598抢修-市指工单自动派单异常，请及时处理！").then(response => {
                                            if (response.status == 200) {
                                                mac.localHostAxjos({
                                                    url: "http://localhost:13313/MonitorServices/setAudioPlayLog",
                                                    method: "POST",
                                                    data: JSON.stringify({ type: '95598抢修-市指自动派单异常', 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: '95598抢修-市指自动派单异常', 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: '95598抢修-市指自动派单异常', time: mac.moment().format("YYYY-MM-DD HH:mm:ss"), status: "异常" })
                                            })
                                        })
                                    }, 6000);
                                    let type = "95598抢修-市指";
                                    let orderID = eventId;
                                    let time = mac.moment().format("YYYY-MM-DD HH:mm:ss");
                                    let name = eqPsrName;
                                    let state = "异常";
                                    mac.localHostAxjos({
                                        url: "http://localhost:13313/MonitorServices/setDisposeLog",
                                        method: "POST",
                                        data: JSON.stringify({ type, orderID, name, time, state }), //必须是字符串格式
                                    }).then(re => {
                                        if (re.status == 200) {
                                            mac.exeTQueue();
                                        }
                                    })
                                }
                            };
                            if (QXGDSZList.length > 0) {
                                let ppstatus = false;
                                eventId = QXGDSZList[0].id;
                                eqPsrName = QXGDSZList[0].gzdd;
                                // 区县级派单参数
                                var requestParam = {
                                    processBusinessKey: QXGDSZList[0].id,
                                    starter: "",
                                    variables: {
                                        cldw: QXGDSZList[0].cityCode, //处理单位
                                        cldwmc: QXGDSZList[0].cityName, //处理单位名称
                                        qxbz: "", //班组id
                                        qxdy: "", //抢修队员
                                        qxfzr: "", //抢修负责人编号
                                        qxfzrName: "", //抢修负责人名称
                                    },
                                };
                                let a = []
                                qxszClassList.forEach((item) => {
                                    item.scope.forEach((x) => {
                                        if (QXGDSZList[0].gzdd.indexOf(x) !== -1) {
                                            requestParam.variables.qxbz = item.orgId;
                                            requestParam.starter = item.pCode;
                                            requestParam.variables.qxfzr = item.pCode;
                                            requestParam.variables.qxfzrName = item.pName;
                                            if (item.wName !== null) {
                                                // phone = item.wName.replace(/、/g, ",");
                                                a.push(item.wName.split("、"));
                                            } else {
                                                // phone = "";
                                                tel = [];
                                            }
                                            ppstatus = true;
                                        }
                                    });
                                });
                                tel = Array.from(new Set(a))
                                if (ppstatus) {
                                    objData.pendingList = '进入自动派单-匹配成功'
                                    mac.localHostAxjos({
                                        url: "http://localhost:13313/MonitorServices/setMonitorLog",
                                        method: "POST",
                                        data: JSON.stringify(objData)
                                    });
                                    sgBrowerserJsAjax2(
                                        "window." + callbackName,
                                        // "http://21.77.244.194:18890/#/sczhEgis",
                                        "http://21.77.244.194:18890/#/webview/1543953229739896",
                                        `http://21.77.244.194:18890/qxgl/repairOrder/initProcess?lockBussinessId=${QXGDSZList[0].id}`,
                                        "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:*`,
                                        JSON.stringify(requestParam)
                                    );
                                } else {
                                    objData.pendingList = '进入自动派单-未匹配'
                                    mac.localHostAxjos({
                                        url: "http://localhost:13313/MonitorServices/setMonitorLog",
                                        method: "POST",
                                        data: JSON.stringify(objData),
                                    });
                                    setTimeout(() => {
                                        mac.audioPlay("95598抢修-市指工单自动派单未匹配到班组，请尽快手动处理！").then(response => {
                                            if (response.status == 200) {
                                                mac.localHostAxjos({
                                                    url: "http://localhost:13313/MonitorServices/setAudioPlayLog",
                                                    method: "POST",
                                                    data: JSON.stringify({ type: '95598抢修-市指自动派单未匹配', 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: '95598抢修-市指自动派单未匹配', 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: '95598抢修-市指自动派单未匹配', time: mac.moment().format("YYYY-MM-DD HH:mm:ss"), status: "异常" })
                                            })
                                        })
                                    }, 6000);
                                    let type = "95598抢修-市指";
                                    let orderID = eventId;
                                    let time = mac.moment().format("YYYY-MM-DD HH:mm:ss");
                                    let name = eqPsrName;
                                    let state = "未匹配";
                                    mac.localHostAxjos({
                                        url: "http://localhost:13313/MonitorServices/setDisposeLog",
                                        method: "POST",
                                        data: JSON.stringify({ type, orderID, name, time, state }), //必须是字符串格式
                                    }).then(re => {
                                        if (re.status == 200) {
                                            mac.exeTQueue();
                                        }
                                    })
                                }
                            }
                        }
                    });
            } else {
                mac.exeTQueue();
            }
        }
    });