Skip to content
assets.component.ts 3.94 KiB
Newer Older
wangqinghua's avatar
wangqinghua committed
import {Component, EventEmitter, OnInit, Output} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
wangqinghua's avatar
wangqinghua committed
import {WorkService} from '../../work.service';
wangqinghua's avatar
wangqinghua committed
import {NzMessageService} from 'ng-zorro-antd';
wangqinghua's avatar
wangqinghua committed
import {DatePipe} from '@angular/common';
wangqinghua's avatar
wangqinghua committed

@Component({
    selector: 'smart-assets',
    templateUrl: './assets.component.html',
    styles: []
})
export class AssetsComponent implements OnInit {
wangqinghua's avatar
wangqinghua committed
    @Output() done = new EventEmitter<any>();
wangqinghua's avatar
wangqinghua committed

    isVisible = false;
    title;
wangqinghua's avatar
wangqinghua committed
    parentId;
wangqinghua's avatar
wangqinghua committed
    validateForm:FormGroup;

    parentTypeList;
    typeList;

wangqinghua's avatar
wangqinghua committed
    invertoryId     //资产Id

    constructor(private workSer:WorkService,private fb:FormBuilder,private datePipe:DatePipe,
wangqinghua's avatar
wangqinghua committed
                private message:NzMessageService) {
wangqinghua's avatar
wangqinghua committed
    }

    ngOnInit() {
        this.initForm();
    }

    initForm(){
        this.validateForm = this.fb.group({
            serialnoA:[null],
            invertoryname:[null],
wangqinghua's avatar
wangqinghua committed
            name:[null,[Validators.required]],
wangqinghua's avatar
wangqinghua committed
            hostid:[null],
            person:[null],
            ip:[null],
            equipmentTypeid:[null],
            inventorycount:[null],
            secondlevelTypeid:[null],
            stock:[null],
            usedcount:[null],
            lendcount:[null],
            mac:[null],
            repaircount:[null],
            maintenanceExpiration:[null],
            scrapcount:[null],
            storageLocation:[null],
            groupid:[null],
wangqinghua's avatar
wangqinghua committed
            inventoryTypeid:[null,[Validators.required]],
wangqinghua's avatar
wangqinghua committed
        });
    }

    //父级分类
    getParentType(){
        this.workSer.findByParentType().subscribe(
            (res)=>{
                const data = res.data;
                this.parentTypeList = data;
            }
        )
    }
    //通过父级分类查询子级分类
    getTypeByParent(parentid){
        this.workSer.findByParentidCount(parentid).subscribe(
            (res)=>{
                const data = res.data;
                this.typeList = data;
            }
        )
    }

    showAddModal(title) {
        this.title = title;
        this.isVisible = true;
        this.getParentType();
    }

wangqinghua's avatar
wangqinghua committed
    showEditModal(title,id) {
wangqinghua's avatar
wangqinghua committed
        this.title = title;
wangqinghua's avatar
wangqinghua committed
        this.invertoryId = id;
wangqinghua's avatar
wangqinghua committed
        this.isVisible = true;
        this.getParentType();
wangqinghua's avatar
wangqinghua committed
        this.workSer.selectByPrimaryKey(this.invertoryId).subscribe(
            (res)=>{
                this.validateForm.patchValue( res.data );
            }
        )
wangqinghua's avatar
wangqinghua committed
    }

    handEditleOk() {
        for(let i in this.validateForm.controls){
            this.validateForm.controls[i].markAsDirty();
            this.validateForm.controls[i].updateValueAndValidity();
        }
        if(this.validateForm.invalid){
            return false;
        }
wangqinghua's avatar
wangqinghua committed
        if(this.title == "添加资产"){
wangqinghua's avatar
wangqinghua committed
            this.create();
        }
wangqinghua's avatar
wangqinghua committed
        if(this.title == "编辑资产"){
wangqinghua's avatar
wangqinghua committed
            this.update();
        }
    }

    create(){
        this.workSer.createInventory(this.validateForm.value).subscribe(
            (res)=>{
wangqinghua's avatar
wangqinghua committed
                if(res.errCode == 10000){
                    this.message.success("添加资产成功");
                    this.isVisible = false;
                    this.done.emit();
                }else{
                    this.message.error(res.errMsg);
                }
wangqinghua's avatar
wangqinghua committed
            }
        )
    }

    update(){
wangqinghua's avatar
wangqinghua committed
        this.validateForm.value.id = this.invertoryId;
        this.validateForm.value.maintenanceExpiration = this.datePipe.transform(this.validateForm.value.maintenanceExpiration,"yyyy-MM-dd HH:mm:ss");
        this.workSer.updateInventory(this.validateForm.value).subscribe(
            (res)=>{
                if(res.errCode == 10000){
                    this.message.success("编辑资产成功");
                    this.isVisible = false;
                    this.done.emit();
                }else{
                    this.message.error(res.errMsg);
                }
            }
        )
wangqinghua's avatar
wangqinghua committed
    }

    handleEditCancel() {
        this.isVisible = false;
    }

}