Skip to content
event.component.ts 5.5 KiB
Newer Older
wangqinghua's avatar
wangqinghua committed
import {Component, EventEmitter, OnInit, Output, ViewChild} from '@angular/core';
wangqinghua's avatar
wangqinghua committed
import {FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms';
import {WorkService} from '../../work.service';
import {NzMessageService, UploadFile} from 'ng-zorro-antd';
import {SelectPersonComponent} from '../../../modal/select-person/select-person.component';

@Component({
    selector: 'smart-event',
    templateUrl: './event.component.html',
    styles: []
})
export class EventComponent implements OnInit {
wangqinghua's avatar
wangqinghua committed
    @ViewChild('smartSelectPerson') smartSelectPerson: SelectPersonComponent;
    @Output() done = new EventEmitter<any>();
    isVisiable = false;
wangqinghua's avatar
wangqinghua committed
    title;
    eventId;
    validateForm: FormGroup;

    fileList: UploadFile[] = [];
    operatorsList = [];    //处理人列表
wangqinghua's avatar
wangqinghua committed
    eventTypeList = [];   //事件分类列表
wangqinghua's avatar
wangqinghua committed
    constructor(private workSer: WorkService, private message: NzMessageService,
                private fb: FormBuilder) {
    }

wangqinghua's avatar
wangqinghua committed
    ngOnInit() {
        this.initForm();
    }
wangqinghua's avatar
wangqinghua committed

    initForm() {
wangqinghua's avatar
wangqinghua committed
        this.fileList = [];
        this.operatorsList = [];
wangqinghua's avatar
wangqinghua committed
        this.validateForm = this.fb.group(
            {
wangqinghua's avatar
wangqinghua committed
                title: [null, [Validators.required,Validators.maxLength(20)]],
                description: [null,Validators.maxLength(300)],
                type: [null, [Validators.required]],
                requester: [null, [Validators.required]],
                requesterPhone: [null, [Validators.required]],
wangqinghua's avatar
wangqinghua committed
                operators: [null],
wangqinghua's avatar
wangqinghua committed
            }
        );
    }

wangqinghua's avatar
wangqinghua committed
    getEventType() {
wangqinghua's avatar
wangqinghua committed
        const data = {
wangqinghua's avatar
wangqinghua committed
            type: 0
wangqinghua's avatar
wangqinghua committed
        };
        this.workSer.findByType(data).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.eventTypeList = res.data;
                }
            }
        );
    }

wangqinghua's avatar
wangqinghua committed
    beforeUpload = (file: UploadFile): boolean => {
        const isLt5M = file.size / 1024 < 5000;
        if (!isLt5M) {
            this.message.error('图标必须小于5M!');
        } else {
            this.fileList.push(file);
        }
        return false;
    };

    showAddModal(title) {
wangqinghua's avatar
wangqinghua committed
        this.isVisiable = true;
wangqinghua's avatar
wangqinghua committed
        this.title = title;
wangqinghua's avatar
wangqinghua committed
        this.getEventType();
wangqinghua's avatar
wangqinghua committed
    }

    showEditModal(title, id) {
wangqinghua's avatar
wangqinghua committed
        this.isVisiable = true;
wangqinghua's avatar
wangqinghua committed
        this.title = title;
        this.eventId = id;
wangqinghua's avatar
wangqinghua committed
        this.getEventType();
wangqinghua's avatar
wangqinghua committed
        this.workSer.findByNo(this.eventId).subscribe(
wangqinghua's avatar
wangqinghua committed
            (res) => {
                this.validateForm.patchValue(res.data);
wangqinghua's avatar
wangqinghua committed
            }
wangqinghua's avatar
wangqinghua committed
        );
wangqinghua's avatar
wangqinghua committed
    }

    handEditleOk() {
        if (this.title == '新增事件') {
            this.create();
        }
        if (this.title == '编辑事件') {
            this.update();
        }
    }

    //新增
    create() {
        let formData = new FormData();
        this.fileList.forEach((file: any) => {
            formData.append('file', file);
        });
wangqinghua's avatar
wangqinghua committed
        if (this.operatorsList.length == 0) {
            this.message.error('请选择人员');
wangqinghua's avatar
wangqinghua committed
            return false;
        }
wangqinghua's avatar
wangqinghua committed
        this.validateForm.patchValue({
            operators: this.operatorsList
        });
wangqinghua's avatar
wangqinghua committed
        for (let i in this.validateForm.controls) {
            this.validateForm.controls[i].markAsDirty();
            this.validateForm.controls[i].updateValueAndValidity();
        }
wangqinghua's avatar
wangqinghua committed
        if (this.validateForm.invalid) {
wangqinghua's avatar
wangqinghua committed
            return false;
        }
wangqinghua's avatar
wangqinghua committed
        formData.append('json', JSON.stringify(this.validateForm.value));
        this.workSer.create(formData).subscribe(
            (res) => {
                if (res.errCode == 10000) {
wangqinghua's avatar
wangqinghua committed
                    this.isVisiable = false;
                    this.initForm();
                    this.done.emit();
wangqinghua's avatar
wangqinghua committed
                    this.message.success('新增事件成功');
                } else {
                    this.message.error(res.errMsg);
                }
            }
        );
    }

    //编辑
    update() {
        let formData = new FormData();
        this.fileList.forEach((file: any) => {
            formData.append('file', file);
        });
wangqinghua's avatar
wangqinghua committed
        this.validateForm.patchValue({
            operators: this.operatorsList
        });
wangqinghua's avatar
wangqinghua committed

wangqinghua's avatar
wangqinghua committed
        if (this.operatorsList.length == 0) {
            this.message.error('请选择人员');
wangqinghua's avatar
wangqinghua committed
            return false;
        }
wangqinghua's avatar
wangqinghua committed
        for (let i in this.validateForm.controls) {
            this.validateForm.controls[i].markAsDirty();
            this.validateForm.controls[i].updateValueAndValidity();
        }
wangqinghua's avatar
wangqinghua committed
        if (this.validateForm.invalid) {
wangqinghua's avatar
wangqinghua committed
            return false;
        }

wangqinghua's avatar
wangqinghua committed
        formData.append('json', JSON.stringify(this.validateForm.value));
        formData.append('id', this.eventId);
        this.workSer.update(formData).subscribe(
            (res) => {
                if (res.errCode == 10000) {
wangqinghua's avatar
wangqinghua committed
                    this.isVisiable = false;
                    this.initForm();
                    this.done.emit();
wangqinghua's avatar
wangqinghua committed
                    this.message.success('修改事件成功');
                } else {
                    this.message.error(res.errMsg);
                }
            }
        );
    }

    handleEditCancel() {
wangqinghua's avatar
wangqinghua committed
        this.isVisiable = false;
        this.initForm();
wangqinghua's avatar
wangqinghua committed
    }

    //选择处理人
wangqinghua's avatar
wangqinghua committed
    selectPerson() {
        this.smartSelectPerson.showModal('选择处理人', null);
wangqinghua's avatar
wangqinghua committed
    }

wangqinghua's avatar
wangqinghua committed
    deletePerson(i) {
        this.operatorsList.splice(i, 1);
wangqinghua's avatar
wangqinghua committed
    }

    getUser(e) {
        const arr = [];
        e.forEach(res => {
            const data = {
                username: res.name,
                userId: res.id
            };
            arr.push(data);
        });
        this.operatorsList = arr;
    }
}