You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cls-server/ui/src/app/page/price/price-default/price-default.component.ts

123 lines
4.0 KiB
TypeScript

import { Component, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule, ReactiveFormsModule, FormBuilder, FormGroup, Validators } from '@angular/forms';
import { NzFormModule } from 'ng-zorro-antd/form';
import { NzInputModule } from 'ng-zorro-antd/input';
import { NzButtonModule } from 'ng-zorro-antd/button';
import { NzMessageService } from 'ng-zorro-antd/message';
import { PriceService } from '../price.service';
import { PriceDefault } from '../../../core/models/price';
import { NzInputNumberModule } from 'ng-zorro-antd/input-number';
import { NzCardModule } from 'ng-zorro-antd/card';
@Component({
selector: 'app-price-default',
standalone: true,
imports: [
CommonModule,
FormsModule,
ReactiveFormsModule,
NzFormModule,
NzInputModule,
NzButtonModule,
NzInputNumberModule,
NzCardModule,
],
templateUrl: './price-default.component.html',
styleUrl: './price-default.component.scss'
})
export class PriceDefaultComponent implements OnInit {
priceForm: FormGroup;
loading = false;
constructor(
private fb: FormBuilder,
private priceService: PriceService,
private message: NzMessageService
) {
this.priceForm = this.fb.group({
amount: [null, [Validators.required, Validators.min(0)]],
firstMontDiscount: [null, [Validators.required, Validators.min(0), Validators.max(100)]],
oneMonthPrice: [null, [Validators.required, Validators.min(0)]],
threeMonthsPrice: [null, [Validators.required, Validators.min(0)]],
sixMonthsPrice: [null, [Validators.required, Validators.min(0)]],
oneYearPrice: [null, [Validators.required, Validators.min(0)]],
discount: [null, [Validators.required, Validators.min(0), Validators.max(100)]]
});
}
ngOnInit() {
this.loadDefaultPrice();
}
// 将分转换为元
private convertCentsToYuan(cents: number): number {
return cents / 100;
}
// 将元转换为分
private convertYuanToCents(yuan: number): number {
return Math.round(yuan * 100);
}
loadDefaultPrice() {
this.loading = true;
this.priceService.getDefaultPrice().subscribe({
next: (data) => {
// 将后端返回的分转换为元
const formData = {
...data,
amount: this.convertCentsToYuan(data.amount),
oneMonthPrice: this.convertCentsToYuan(data.oneMonthPrice),
threeMonthsPrice: this.convertCentsToYuan(data.threeMonthsPrice),
sixMonthsPrice: this.convertCentsToYuan(data.sixMonthsPrice),
oneYearPrice: this.convertCentsToYuan(data.oneYearPrice)
};
this.priceForm.patchValue(formData);
this.loading = false;
},
error: (error) => {
this.message.error('获取默认价格失败');
this.loading = false;
}
});
}
onSubmit() {
if (this.priceForm.valid) {
this.loading = true;
const formValue = this.priceForm.value;
// 将表单中的元转换为分
const priceData: PriceDefault = {
id: 1,
amount: this.convertYuanToCents(formValue.amount),
firstMontDiscount: formValue.firstMontDiscount,
oneMonthPrice: this.convertYuanToCents(formValue.oneMonthPrice),
threeMonthsPrice: this.convertYuanToCents(formValue.threeMonthsPrice),
sixMonthsPrice: this.convertYuanToCents(formValue.sixMonthsPrice),
oneYearPrice: this.convertYuanToCents(formValue.oneYearPrice),
discount: formValue.discount
};
this.priceService.updateDefaultPrice(priceData).subscribe({
next: () => {
this.message.success('更新默认价格成功');
this.loadDefaultPrice();
},
error: (error) => {
this.message.error('更新默认价格失败');
this.loading = false;
}
});
} else {
Object.values(this.priceForm.controls).forEach(control => {
if (control.invalid) {
control.markAsTouched();
control.updateValueAndValidity({ onlySelf: true });
}
});
}
}
}