186 lines
6.6 KiB
C#
186 lines
6.6 KiB
C#
|
using ConstructionFirmDataModels.Models;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.ComponentModel.DataAnnotations.Schema;
|
|||
|
using System.ComponentModel.DataAnnotations;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using Subd_4.BindingModels;
|
|||
|
using Subd_4.ViewModels;
|
|||
|
|
|||
|
namespace ConstructionFirmDatabaseImplement.Models
|
|||
|
{
|
|||
|
public class Project : IProjectModel
|
|||
|
{
|
|||
|
public int Id { get; set; }
|
|||
|
|
|||
|
[Required]
|
|||
|
public string ClientName { get; set; } = string.Empty;
|
|||
|
|
|||
|
[Required]
|
|||
|
public string ObjectType { get; set; } = string.Empty;
|
|||
|
|
|||
|
[Required]
|
|||
|
public string LocationP { get; set; } = string.Empty;
|
|||
|
|
|||
|
[Required]
|
|||
|
public int Budget { get; set; }
|
|||
|
|
|||
|
[Required]
|
|||
|
public DateTime DeadLine { get; set; }
|
|||
|
|
|||
|
[Required]
|
|||
|
public ConstructionFirmDataModels.Enum.TaskStatus Status { get; set; }
|
|||
|
|
|||
|
[Required]
|
|||
|
public double FullPrice { get; set; }
|
|||
|
|
|||
|
public int CliendId { get; set; }
|
|||
|
|
|||
|
public int EmployeeId { get; set; }
|
|||
|
|
|||
|
private Dictionary<int, (IConstructionMaterialModel, int)> _constructionMaterialProject = null;
|
|||
|
|
|||
|
[NotMapped]
|
|||
|
public Dictionary<int, (IConstructionMaterialModel, int)> ConstructionMaterialProjects
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
if (_constructionMaterialProject == null)
|
|||
|
{
|
|||
|
_constructionMaterialProject = MaterialProjects.ToDictionary(recTM => recTM.ConstructionMaterialId, recTM => (recTM.ConstructionMaterial as IConstructionMaterialModel, recTM.Count));
|
|||
|
}
|
|||
|
return _constructionMaterialProject;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private Dictionary<int, (ITeamModel, int)> _teamProject { get; set; } = null;
|
|||
|
|
|||
|
[NotMapped]
|
|||
|
public Dictionary<int, (ITeamModel, int)> TeamProject
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
if (_teamProject == null)
|
|||
|
{
|
|||
|
_teamProject = ProjectTeams.ToDictionary(recTM => recTM.TeamId, recTM => (recTM.Team as ITeamModel, recTM.CountTeam));
|
|||
|
}
|
|||
|
return _teamProject;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
[ForeignKey("ProjectId")]
|
|||
|
public virtual List<ConstructionMaterialProject> MaterialProjects { get; set; } = new();
|
|||
|
|
|||
|
[ForeignKey("ProjectId")]
|
|||
|
public virtual List<ProjectTeam> ProjectTeams { get; set; } = new();
|
|||
|
|
|||
|
public static Project Create(ConstructionFirmDatabase context, ProjectBindingModel model)
|
|||
|
{
|
|||
|
return new Project()
|
|||
|
{
|
|||
|
Id = model.Id,
|
|||
|
ClientName = model.ClientName,
|
|||
|
ObjectType = model.ObjectType,
|
|||
|
LocationP = model.LocationP,
|
|||
|
Budget = model.Budget,
|
|||
|
DeadLine = model.DeadLine,
|
|||
|
Status = model.Status,
|
|||
|
CliendId = model.CliendId,
|
|||
|
EmployeeId = model.EmployeeId,
|
|||
|
MaterialProjects = model.ConstructionMaterialProjects.Select(x => new ConstructionMaterialProject
|
|||
|
{
|
|||
|
ConstructionMaterial = context.ConstructionMaterials.First(y => y.Id == x.Key),
|
|||
|
Count = x.Value.Item2
|
|||
|
}).ToList(),
|
|||
|
ProjectTeams = model.TeamProject.Select(x => new ProjectTeam
|
|||
|
{
|
|||
|
Team = context.Teams.First(y => y.Id == x.Key),
|
|||
|
CountTeam = x.Value.Item2
|
|||
|
}).ToList(),
|
|||
|
};
|
|||
|
}
|
|||
|
|
|||
|
public void Update(ProjectBindingModel model)
|
|||
|
{
|
|||
|
Status = model.Status;
|
|||
|
}
|
|||
|
|
|||
|
public ProjectViewModel GetViewModel => new()
|
|||
|
{
|
|||
|
Id = Id,
|
|||
|
ClientName = ClientName,
|
|||
|
ObjectType = ObjectType,
|
|||
|
LocationP = LocationP,
|
|||
|
Budget = Budget,
|
|||
|
DeadLine = DeadLine,
|
|||
|
Status = Status,
|
|||
|
CliendId = CliendId,
|
|||
|
EmployeeId = EmployeeId,
|
|||
|
ConstructionMaterialProjects = ConstructionMaterialProjects,
|
|||
|
TeamProject = TeamProject
|
|||
|
};
|
|||
|
|
|||
|
public void UpdateConstructionMaterials(ConstructionFirmDatabase context, ProjectBindingModel model)
|
|||
|
{
|
|||
|
var taskMenus = context.ConstructionMaterialProjects.Where(rec => rec.ProjectId == model.Id).ToList();
|
|||
|
if (taskMenus != null && taskMenus.Count > 0)
|
|||
|
{
|
|||
|
context.ConstructionMaterialProjects.RemoveRange(taskMenus.Where(rec => !model.ConstructionMaterialProjects.ContainsKey(rec.ConstructionMaterialId)));
|
|||
|
context.SaveChanges();
|
|||
|
|
|||
|
foreach (var updateMenu in taskMenus)
|
|||
|
{
|
|||
|
updateMenu.Count = model.ConstructionMaterialProjects[updateMenu.ConstructionMaterialId].Item2;
|
|||
|
model.ConstructionMaterialProjects.Remove(updateMenu.ConstructionMaterialId);
|
|||
|
}
|
|||
|
context.SaveChanges();
|
|||
|
}
|
|||
|
|
|||
|
var task = context.Projects.First(x => x.Id == Id);
|
|||
|
foreach (var rc in model.ConstructionMaterialProjects)
|
|||
|
{
|
|||
|
context.ConstructionMaterialProjects.Add(new ConstructionMaterialProject
|
|||
|
{
|
|||
|
Project = task,
|
|||
|
ConstructionMaterial = context.ConstructionMaterials.First(x => x.Id == rc.Key),
|
|||
|
Count = rc.Value.Item2
|
|||
|
});
|
|||
|
context.SaveChanges();
|
|||
|
}
|
|||
|
_constructionMaterialProject = null;
|
|||
|
}
|
|||
|
|
|||
|
public void UpdateTeams(ConstructionFirmDatabase context, ProjectBindingModel model)
|
|||
|
{
|
|||
|
var taskTeams = context.ProjectTeams.Where(rec => rec.ProjectId == model.Id).ToList();
|
|||
|
if (taskTeams != null && taskTeams.Count > 0)
|
|||
|
{
|
|||
|
context.ProjectTeams.RemoveRange(taskTeams.Where(rec => !model.TeamProject.ContainsKey(rec.TeamId)));
|
|||
|
context.SaveChanges();
|
|||
|
|
|||
|
foreach (var updateTeam in taskTeams)
|
|||
|
{
|
|||
|
updateTeam.CountTeam = model.TeamProject[updateTeam.TeamId].Item2;
|
|||
|
model.TeamProject.Remove(updateTeam.TeamId);
|
|||
|
}
|
|||
|
context.SaveChanges();
|
|||
|
}
|
|||
|
|
|||
|
var task = context.Projects.First(x => x.Id == Id);
|
|||
|
foreach (var rc in model.TeamProject)
|
|||
|
{
|
|||
|
context.ProjectTeams.Add(new ProjectTeam
|
|||
|
{
|
|||
|
Project = task,
|
|||
|
Team = context.Teams.First(x => x.Id == rc.Key),
|
|||
|
CountTeam = rc.Value.Item2
|
|||
|
});
|
|||
|
context.SaveChanges();
|
|||
|
}
|
|||
|
_constructionMaterialProject = null;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|