Skip to content
basic.component.ts 8.38 KiB
Newer Older
wangqinghua's avatar
wangqinghua committed
import { Component, OnInit, ViewChild } from '@angular/core';
import { OverAllService } from "../overAll.service";
import { NzModalService } from "ng-zorro-antd";
import { NzMessageService } from "ng-zorro-antd";
import { Router } from "@angular/router";
import {
    FormBuilder,
    FormGroup,
    Validators
} from '@angular/forms';
import {BasicEditComponent} from "../../modal/basic-edit/basic-edit.component";
import {BasiCheckComponent} from "../../modal/basi-check/basi-check.component";
import {CreateGroupComponent} from "../../modal/create-group/create-group.component";
import { EmitService } from "../../shared/event/eventEmitter";

export interface TreeNodeInterface {
    host: any;
    name: string;
    age: number;
    level: number;
    expand: boolean;
    address: string;
    children?: TreeNodeInterface[];
}
@Component({
  selector: 'jhi-basic',
  templateUrl: './basic.component.html',
  styles: []
})
export class BasicComponent implements OnInit {
    //组件
    @ViewChild('basicEdit') basicEdit:BasicEditComponent;
    @ViewChild('smartCheck') smartCheck:BasiCheckComponent;
    @ViewChild('smartCreateGroup') smartCreateGroup:CreateGroupComponent;

    //表格信息
    loading = false;

    selectedValue;
    isCheck = false;
    allChecked = false;
    interfaceList :any[];
    hostId;
    deviceNo;     //设备监控数

    isKey = false;
    keyList:any[];
    keyType = '1';
  constructor(private fb: FormBuilder, private overAllSer: OverAllService,private router: Router,
              private modalService: NzModalService, private message: NzMessageService,
              private emitService:EmitService) {
  }

    dataSet:any[];
    expandDataCache = {};
    ckeckObj:object;

    collapse(array: TreeNodeInterface[], data: TreeNodeInterface, $event: boolean): void {
        if ($event === false) {
            if (data.children) {
                data.children.forEach(d => {
                    const target = array.find(a => a.host === d.host);
                    target.expand = false;
                    this.collapse(array, target, false);
                });
            } else {
                return;
            }
        }
    }

    convertTreeToList(root: object,groupId: string): TreeNodeInterface[] {
        const stack = [];
        const array = [];
        const hashMap = {};
        stack.push({ ...root, level: 0, expand: false });
        while (stack.length !== 0) {
            const node = stack.pop();

            this.visitNode(node, hashMap, array);
            if (node.list) {
                for (let i = node.list.length - 1; i >= 0; i--) {
                    stack.push({ ...node.list[ i ], level: node.level + 1, expand: false, parent: node,gId:groupId, checked:false });
                }
            }
        }

        return array;
    }

    visitNode(node: TreeNodeInterface, hashMap: object, array: TreeNodeInterface[]): void {
        if (!hashMap[ node.host ]) {
            hashMap[ node.host ] = true;
            array.push(node);
        }
    }

    submitForm(): void {
        // for (const i in this.validateForm.controls) {
        //     this.validateForm.controls[ i ].markAsDirty();
        //     this.validateForm.controls[ i ].updateValueAndValidity();
        // }
    }

    ngOnInit(): void {
        this.selectedValue = 'group';
        this.select();
        // this.findList();
        this.findSize();
    }

    //添加资源  ,name-分组名称,id-分组id
    showBasicEditModal(item){
        console.log(item);
        this.basicEdit.showModal(item.hostId,item.name,item.id);
    }

    //编辑资源 name--分组名称,gId-当前分组的id
    editBasicModal(item){
        console.log(item);
        this.basicEdit.editModal(item.hostid,item.name,item.gId);
    }

    //添加监测点
    showBasicCheckModal(hostid){
       this.smartCheck.showCheckModal(hostid);
    }

    //列表
    findList(){
        const data = {};
        this.overAllSer.find(data).subscribe(
            (res)=>{
                if(res.data){
                    this.dataSet = res.data;
                }else{
                    this.message.info(res.errMsg);
                }

            }
        )
    }

    //选择分组
    select(){
        this.loading = true;
        if(this.selectedValue == 'group'){
            this.overAllSer.findGroup().subscribe(
                (res)=>{
                    this.loading = false;
                    if( res.errCode == 10000 ){
                        this.dataSet = res.data;
                        // this.dataSet = this.dataSet.filter(d => d.list);     //过滤无list的
                        for( let i = 0;i <  this.dataSet.length;i++ ){
                          this.dataSet[i].host = i;
                          this.dataSet[i].checked = false;
                        }
                        console.log(this.dataSet);
                    this.toTree();
                    }else{
                        this.message.info(res.errMsg);
                    }
                }
            )
        }else if(this.selectedValue == 'type'){
            this.overAllSer.findType().subscribe(
                (res)=>{
                    this.loading = false;
                    if( res.errCode == 10000 ){
                        this.dataSet = res.data;
                        // this.dataSet = this.dataSet.filter(d => d.list);     //过滤无list的
                        for( let i = 0;i <  this.dataSet.length;i++ ){
                            this.dataSet[i].host = i;
                            this.dataSet[i].checked = false;
                        }
                        console.log(this.dataSet);
                        this.toTree();
                    }else{
                        this.message.info(res.errMsg);
                    }
                }
            )
        }else if( this.selectedValue == 'all' ){
            this.findList();
        }
    }

    toTree(){
        this.dataSet.forEach(item => {
            this.expandDataCache[ item.host ] = this.convertTreeToList(item,item.id);
        });
    }

    //全选

    checkAll(e){

    }

    //删除资源
    showDeleteConfirm(data){
        this.modalService.confirm({
            nzTitle     : '删除',
            nzContent   : '<b style="color: red;">确认删除该资源吗?</b>',
            nzOkText    : '确定',
            nzOkType    : 'danger',
            nzOnOk      : ()=>{
                this.overAllSer.deleteHostGet(data.hostid).subscribe(
                    (res)=>{
                        if( res.errCode == 10000 ){
                            this.message.info('删除成功');
                            this.select();
                        }else{
                            this.message.info(res.errMsg);
                        }
                    }
                )
            },
            nzCancelText: '取消',
            nzOnCancel  : () => console.log('Cancel')
        });
    }

    //添加分组
    showGroupModal(){
        this.smartCreateGroup.showModal();
    }

    //删除分组
    showDeleteGroupConfirm(item){
        this.modalService.confirm({
            nzTitle     : '删除',
            nzContent   : '<b style="color: red;">确认删除该分组吗?</b>',
            nzOkText    : '确定',
            nzOkType    : 'danger',
            nzOnOk      : ()=>{

                const data = { params :[]};
                data.params.push(item.id);
                this.overAllSer.deleteGroup(data).subscribe(
                    (res)=>{
                        if( res.errCode == 10000 ){
                            this.message.info('删除成功');
                            this.select();
                        }else{
                            this.message.info(res.errMsg);
                        }
                    }
                )
            },
            nzCancelText: '取消',
            nzOnCancel  : () => console.log('Cancel')
        });
    }

    handleOk(e){
        this.select();
    }

    goDetail(item){
        const data = {
            'groupids':[item.groupid],
            'hostExtend':{
                'superiorHostid':null
            }
        };
        this.overAllSer.findDetail(data).subscribe(
            (res)=>{
                console.log(res);
            }
        )
    }

    //监控设备数
    findSize(){
        this.overAllSer.findSize().subscribe(
            (res)=>{
                this.deviceNo = res.data.size;
            }
        )
    }

}