using Admin.Core.IService; using Admin.Core.Model; using Aucma.Core.OldBoxFoam.Models; using Aucma.Core.OldBoxFoam.Views; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Messaging; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; namespace Aucma.Core.OldBoxFoam.ViewModels { /** * 发泡计划 * * */ public partial class FoamPlanPageViewModel : ObservableObject { protected readonly IBoxFoamPlanServices? _boxFoamPlanServices; public FoamPlanPageViewModel() { DeleteCommand = new RelayCommand(obj => DeleteCommandExecute(obj)); _boxFoamPlanServices = App.ServiceProvider.GetService(); WeakReferenceMessenger.Default.Register(this, Recive); InitData(); } public RelayCommand DeleteCommand { get; set; } private void DeleteCommandExecute(int id) { Console.WriteLine(id); } public async void InitData() { int count = 0; List list =await _boxFoamPlanServices.QueryAsync(); if (list == null) return; foreach (BoxFoamPlan item in list) { BoxFoamPlanModel model = new BoxFoamPlanModel(); model.No = ++count; model.MaterialCode = item.MaterialCode; model.MaterialName = item.MaterialName; model.PlanAmount = item.PlanAmount; model.ObjId = item.ObjId; ListItems.Add(model); ListItems1.Add(model); } } #region MyRegion private ObservableCollection _id = new ObservableCollection(); public ObservableCollection Id { get => _id; set => SetProperty(ref _id, value); } private ObservableCollection _materialCode = new ObservableCollection(); public ObservableCollection MaterialCode { get => _materialCode; set => SetProperty(ref _materialCode, value); } private ObservableCollection _materialName = new ObservableCollection(); public ObservableCollection MaterialName { get => _materialName; set => SetProperty(ref _materialName, value); } private ObservableCollection _planAmount = new ObservableCollection(); public ObservableCollection PlanAmount { get => _planAmount; set => SetProperty(ref _planAmount, value); } #endregion [RelayCommand] public void AddPlan(string objId) { SplitPlanView plan = new SplitPlanView(objId); plan.Show(); } [RelayCommand] public async void ClearPlan(string objId) { int id=int.Parse(objId); var obj=await _boxFoamPlanServices.FirstAsync(d=>d.ObjId== id); obj.MaterialCode = ""; obj.MaterialName = ""; obj.PlanAmount = 0; var result= await _boxFoamPlanServices.UpdateAsync(obj); if (result) { ClearData(); MessageBox.Show("清除计划成功!", "系统提醒"); } else { MessageBox.Show("清除计划失败!", "系统提醒"); } } #region 初始化datagrid private ObservableCollection listItems = new ObservableCollection(); public ObservableCollection ListItems { get => listItems; set => SetProperty(ref listItems, value); } #endregion #region 初始化datagrid private ObservableCollection listItems1 = new ObservableCollection(); public ObservableCollection ListItems1 { get => listItems1; set => SetProperty(ref listItems1, value); } #endregion private void Recive(object recipient, string message) { if (message == "RefreshTask") { ClearData(); } } public void ClearData() { Id.Clear(); MaterialCode.Clear(); MaterialName.Clear(); PlanAmount.Clear(); InitData(); } } }