103 lines
2.7 KiB
C#
Raw Normal View History

2024-08-10 18:43:15 +04:00
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using ServiceStationContracts.BindingModels;
2024-04-30 19:55:37 +03:00
using ServiceStationContracts.ViewModels;
using ServiceStationDataModels.Models;
2024-04-30 19:55:37 +03:00
using System;
using System.Collections.Generic;
2024-08-10 18:43:15 +04:00
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
2024-04-30 19:55:37 +03:00
using System.Linq;
using System.Text;
namespace ServiceStationsDataBaseImplement.Models
{
2024-08-10 18:43:15 +04:00
public class Work : IWorkModel {
2024-04-30 19:55:37 +03:00
2024-08-10 18:43:15 +04:00
public int Id { get; set; }
2024-04-30 19:55:37 +03:00
2024-08-10 18:43:15 +04:00
[Required]
public DateTime Date { get; set; } = DateTime.Now;
2024-04-30 19:55:37 +03:00
2024-08-10 18:43:15 +04:00
[Required]
public double Price { get; set; }
2024-04-30 19:55:37 +03:00
2024-08-10 18:43:15 +04:00
[ForeignKey("ExecutorId")]
public int ExecutorId { get; set; }
2024-04-30 19:55:37 +03:00
2024-08-10 18:43:15 +04:00
[ForeignKey("TaskId")]
public int TaskId { get; set; }
2024-04-30 19:55:37 +03:00
2024-08-22 20:40:32 +04:00
public virtual TaskByWork? _task { get; set; }
2024-04-30 19:55:37 +03:00
2024-08-10 18:43:15 +04:00
[ForeignKey("WorkId")]
public virtual List<WorkClient> WorkClients { get; set; } = new();
2024-04-30 19:55:37 +03:00
2024-08-10 18:43:15 +04:00
public Dictionary<int, IClientModel>? _clientList = null;
[NotMapped]
public Dictionary<int, IClientModel> ClientList {
get {
if (_clientList == null) {
2024-08-22 20:40:32 +04:00
_clientList = WorkClients.ToDictionary(recWC => recWC.ClientId, recWC => recWC._client as IClientModel);
2024-08-10 18:43:15 +04:00
}
return _clientList;
}
}
public static Work? Create(Database context, WorkBindingModel? model) {
if (model == null) {
return null;
}
return new Work() {
Id = model.Id,
Date = model.Date,
Price = model.Price,
ExecutorId = model.ExecutorId,
TaskId = model.TaskId,
WorkClients = model.ClientList.Select(x => new WorkClient {
2024-08-22 20:40:32 +04:00
_client = context.Clients.First(y => y.Id == x.Key),
PointCount = 0
2024-08-10 18:43:15 +04:00
}).ToList()
};
}
public void Update(WorkBindingModel? model) {
if (model == null) {
return;
}
Price = model.Price;
}
public WorkViewModel GetViewModel => new() {
Id = Id,
Date = Date,
Price = Price,
ExecutorId = ExecutorId,
TaskId = TaskId,
2024-08-22 20:40:32 +04:00
ClientList = ClientList,
TaskName = _task?.Name ?? string.Empty
2024-08-10 18:43:15 +04:00
};
public void UpdateClients(Database context, WorkBindingModel model) {
var worckClients = context.WorksClients.Where(rec => rec.WorkId == model.Id).ToList();
if (worckClients != null && worckClients.Count > 0) {
// Нужно удалить те записи, которых нет в модели
context.WorksClients.RemoveRange(worckClients.Where(rec => !model.ClientList.ContainsKey(rec.ClientId)));
context.SaveChanges();
}
// получаем работу, добавляем новые записи
var work = context.Works.First(x => x.Id == Id);
foreach (var wc in model.ClientList) {
context.WorksClients.Add(new WorkClient {
_work = work,
_client = context.Clients.First(x => x.Id == wc.Key)
});
context.SaveChanges();
}
_clientList = null;
}
}
2024-04-30 19:55:37 +03:00
}