hai-oil-web/src/app/pages/discount/discount-edit/discount-edit.component.ts

147 lines
5.2 KiB

import { Component } from '@angular/core';
import {environment} from "../../../../environments/environment";
import {FormGroup, FormsModule, NonNullableFormBuilder, ReactiveFormsModule, Validators} from "@angular/forms";
import {DictionaryPipe} from "../../../pipes/common/dictionary.pipe";
import {MerService} from "../../../services/merchant/mer.service";
import {MerChainBrandService} from "../../../services/merchant/mer-chain-brand.service";
import {RoleService} from "../../../services/role/role.service";
import {NzMessageService} from "ng-zorro-antd/message";
import {ActivatedRoute} from "@angular/router";
import {HttpClient} from "@angular/common/http";
import {NzUploadFile, NzUploadModule} from "ng-zorro-antd/upload";
import {Observable, Observer} from "rxjs";
import {NgForOf, NgIf} from "@angular/common";
import {NzButtonComponent} from "ng-zorro-antd/button";
import {NzColDirective} from "ng-zorro-antd/grid";
import {NzFlexDirective} from "ng-zorro-antd/flex";
import {
NzFormControlComponent,
NzFormDirective,
NzFormItemComponent,
NzFormLabelComponent,
NzFormModule
} from "ng-zorro-antd/form";
import {NzInputDirective, NzInputGroupComponent} from "ng-zorro-antd/input";
import {NzOptionComponent, NzSelectComponent} from "ng-zorro-antd/select";
import {NzModalModule} from "ng-zorro-antd/modal";
import {NzIconDirective} from "ng-zorro-antd/icon";
import {NzInputNumberComponent, NzInputNumberGroupComponent} from "ng-zorro-antd/input-number";
import {NzCascaderModule} from "ng-zorro-antd/cascader";
import {NzTabsModule} from "ng-zorro-antd/tabs";
import {DiscountService} from "../../../services/discount/discount.service";
import {NzCheckboxModule} from "ng-zorro-antd/checkbox";
import {NzDatePickerModule} from "ng-zorro-antd/date-picker";
import {NzSwitchModule} from "ng-zorro-antd/switch";
import {NzTooltipDirective} from "ng-zorro-antd/tooltip";
@Component({
selector: 'app-discount-edit',
standalone: true,
imports: [
FormsModule,
NgForOf,
NzButtonComponent,
NzColDirective,
NzFlexDirective,
NzFormControlComponent,
NzFormDirective,
ReactiveFormsModule,
NzFormItemComponent,
NzFormLabelComponent,
NzInputDirective,
NzSelectComponent,
NzOptionComponent,
NzModalModule,
NzFormModule,
NzUploadModule,
NzIconDirective,
NgIf,
NzInputGroupComponent,
NzInputNumberComponent,
NzCascaderModule,
NzTabsModule,
NzCheckboxModule,
NzDatePickerModule,
NzInputNumberGroupComponent,
NzSwitchModule,
NzTooltipDirective
],
templateUrl: './discount-edit.component.html',
styleUrl: './discount-edit.component.less'
})
export class DiscountEditComponent {
form: FormGroup;
// 优惠券类型
discountTypeArray = new DictionaryPipe().getDictionaryList('DISCOUNT_TYPE');
// 优惠券使用范围
discountUseScopeArray = new DictionaryPipe().getDictionaryList('DISCOUNT_USE_SCOPE');
constructor(private fb: NonNullableFormBuilder,
private merService: MerService,
private discountService: DiscountService,
private message: NzMessageService,
private activatedRoute: ActivatedRoute) {
// 初始化表单
this.form = this.fb.group({
discountNo: [null],
discountName: [null, [Validators.required]],
discountType: [null, [Validators.required]],
//overlayDiscount: [null, [Validators.required]],
discountPrice: [null, [Validators.required]],
discountCondition: [null],
useScope: [null],
receiveExpirationDate: [null, [Validators.required]],
endTime: [null, [Validators.required]],
});
this.activatedRoute.queryParams.subscribe(queryParams => {
// 接收到路由参数
if (queryParams['discountNo'] != null) {
// 查询详情
this.discountService.queryDetail({discountNo: queryParams['discountNo']}, (data: any) => {
if (data['return_code'] == '000000') {
const discountData = data['return_data'];
discountData['discountType'] = String(discountData['discountType']);
discountData['useScope'] = String(discountData['useScope']).split(',');
this.form.patchValue(discountData);
}
});
}
});
}
/**
* 提交表单
*/
submitForm() {
if (this.form.controls['useScope'].value == null || this.form.controls['useScope'].value.length == 0) {
this.message.create('warning', '请选择使用范围');
return;
}
if (this.form.valid) {
const param = this.form.value;
param['useScope'] = this.form.controls['useScope'].value.join(',');
param['endTime'] = new Date(this.form.controls['endTime'].value).getTime();
this.discountService.editDiscount(param, (data: any) => {
if (data['return_code'] == '000000') {
this.message.create('success', '操作成功');
if (param['discountNo'] == null) {
// 清空表单
this.form.reset();
}
} else {
this.message.create('error', data['return_msg']);
}
});
} else {
Object.values(this.form.controls).forEach(control => {
if (control.invalid) {
control.markAsDirty();
control.updateValueAndValidity({ onlySelf: true });
}
});
}
}
}