/**
@author:fugy
@date:2019.1.10
@info:alarm-api
 */
import httputils from '@/api/httputils'

const api = {
    //获取对象类型
    getObjectType() {
        return new Promise((resolve, reject) => {
            httputils.getJson(
                '/server/dataplatform/dict/query/equipment_all',
                null,
                res => {
                    if (res.result === 'success') {
                        let dataArr = res.data
                        objTypeArr = []
                        let sysArr = []
                        let devArr = []
                        dataArr.forEach(item => {
                            objTypeArr.push(item)
                            let sysObj = {
                                label: item.name,
                                value: item.code,
                                type: item.type
                            }
                            let devObj = {
                                label: item.name,
                                value: item.code,
                                type: item.type
                            }
                            if (item.content) {
                                sysObj.children = []
                                devObj.children = []
                                item.content.forEach(ele => {
                                    objTypeArr.push(ele)
                                    let sysObj1 = {
                                        label: ele.name,
                                        value: ele.code,
                                        type: ele.type
                                    }
                                    let devObj1 = {
                                        label: ele.name,
                                        value: ele.code,
                                        type: ele.type
                                    }
                                    if (ele.content) {
                                        devObj1.children = []
                                        ele.content.forEach(strip => {
                                            objTypeArr.push(strip)
                                            let devObj2 = {
                                                label: strip.name,
                                                value: strip.code,
                                                type: strip.type
                                            }
                                            devObj1.children.push(devObj2)
                                        })
                                    } else {
                                        devObj1 = {
                                            label: ele.name,
                                            value: ele.code,
                                            type: ele.type,
                                            disabled: true
                                        }
                                    }
                                    sysObj.children.push(sysObj1)
                                    devObj.children.push(devObj1)
                                })
                            }
                            sysArr.push(sysObj)
                            devArr.push(devObj)
                        })
                        let resObj = {
                            sysArr,
                            devArr
                        }
                        objTypeDevArr = devArr
                        resolve(resObj)
                    } else {
                        reject('fail')
                    }
                },
                fail => {
                    reject(fail)
                },
                err => {
                    reject(err)
                }
            )
        })
    },
    // 获取对象类的改造成一个整的数组
    getObjTypeNameFromCode(objType, category, spaceNameObj) {
        if (objType == 'building') {
            return { name: '建筑' }
        } else if (objType == 'floor') {
            return { name: '楼层' }
        } else if (objType == 'space') {
            let name
            if (category) {
                name = spaceNameObj[category]
            } else {
                name = '空间'
            }

            return { name: name }
        } else {
            let arr = objTypeArr.filter((ele, index) => {
                return ele.code == category
            })
            if (arr.length) {
                return arr[0]
            } else {
                return { name: '---' }
            }
        }
    },
    // 获取对象类中的空间级联
    getObjTypeSpace() {
        return new Promise((resolve, reject) => {
            httputils.getJson('/server/dataplatform/dict/query/space', {}, res => {
                if (res.result == "success") {
                    resolve(res.data)
                } else {
                    resolve([])
                }
            })
        })
    },
    // 获取固定和项目报警的list数据
    getListData(params) {
        return new Promise((resolve, reject) => {
            httputils.postJson(
                '/server/alarm/config/queryConfig',
                params,
                res => {
                    resolve(res)
                },
                fail => {
                    reject(fail)
                },
                err => {
                    reject(err)
                }
            )
        })
    },
    // 通过根据Id批量获取各物理实体
    getApplyObjInfo(projectId, data) {
        return new Promise((resolve, reject) => {
            httputils.postJson('/server/dataplatform/object/batchquery?projectId=' + projectId, data, res => {
                if (res.Result === "success") {
                    resolve(res.Content || []);
                }
            }, fail => {
                reject(fail);
            }, err => {
                reject(err);
            })
        })
    },
  //查询报警分类
  alarmClassList(params) {
    return new Promise(resolve => {
      httputils.postJson('/server/alarm/config/category/query', params, resp => {
        if (resp.result == 'success' && resp.count) {
          resolve(resp.content)
        } else {
          resolve([])
        }
      })
    })
  },
  //创建报警分类
  createAlarmClass(params) {
    return new Promise(resolve => {
      httputils.postJson('/server/alarm/config/category/create', params, resp => {
        resolve(resp)
      })
    })
  },
  /***************断数报警*************************************************** */
  //获取断数报警通知人员
  getNumUserNotice(params) {
    return new Promise(resolve => {
        httputils.postJson('/server/alarm/notifyuser/queryBrokenAlarmNotifyUser', params, resp => {
            if(resp.result == 'success' && resp.count) {
                resolve(resp.content)
            } else {
                resolve([])
            }
        })
    })
  },
  //更新断数报警的通知方式
  updateNumUserNotice(params) {
    return new Promise(resolve => {
        httputils.postJson('/server/alarm/brokenalarmnotifyuser/update', params, resp => {
            resolve(resp)
        })
    })
  }
}
export default api
