This commit is contained in:
Danil Markov 2023-05-02 14:23:01 +04:00
commit 4771750051
8 changed files with 33 additions and 35 deletions

View File

@ -70,17 +70,17 @@ namespace LawFirmDatabaseImplement.Implements
using var transaction = context.Database.BeginTransaction();
try
{
var product = context.Documents.FirstOrDefault(rec =>
var document = context.Documents.FirstOrDefault(rec =>
rec.Id == model.Id);
if (product == null)
if (document == null)
{
return null;
}
product.Update(model);
document.Update(model);
context.SaveChanges();
product.UpdateBlanks(context, model);
document.UpdateBlanks(context, model);
transaction.Commit();
return product.GetViewModel;
return document.GetViewModel;
}
catch
{

View File

@ -13,19 +13,19 @@ namespace LawFirmDatabaseImplement.Models
public string DocumentName { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
private Dictionary<int, (IBlankModel, int)>? _productBlanks = null;
private Dictionary<int, (IBlankModel, int)>? _documentBlanks = null;
[NotMapped]
public Dictionary<int, (IBlankModel, int)> DocumentBlanks
{
get
{
if (_productBlanks == null)
if (_documentBlanks == null)
{
_productBlanks = Blanks
_documentBlanks = Blanks
.ToDictionary(recPC => recPC.BlankId, recPC =>
(recPC.Blank as IBlankModel, recPC.Count));
}
return _productBlanks;
return _documentBlanks;
}
}
[ForeignKey("DocumentId")]
@ -61,15 +61,15 @@ namespace LawFirmDatabaseImplement.Models
public void UpdateBlanks(LawFirmDatabase context,
DocumentBindingModel model)
{
var productBlanks = context.DocumentBlanks.Where(rec =>
var documentBlanks = context.DocumentBlanks.Where(rec =>
rec.DocumentId == model.Id).ToList();
if (productBlanks != null && productBlanks.Count > 0)
if (documentBlanks != null && documentBlanks.Count > 0)
{ // удалили те, которых нет в модели
context.DocumentBlanks.RemoveRange(productBlanks.Where(rec
context.DocumentBlanks.RemoveRange(documentBlanks.Where(rec
=> !model.DocumentBlanks.ContainsKey(rec.BlankId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateBlank in productBlanks)
foreach (var updateBlank in documentBlanks)
{
updateBlank.Count =
model.DocumentBlanks[updateBlank.BlankId].Item2;
@ -77,18 +77,18 @@ namespace LawFirmDatabaseImplement.Models
}
context.SaveChanges();
}
var product = context.Documents.First(x => x.Id == Id);
var document = context.Documents.First(x => x.Id == Id);
foreach (var pc in model.DocumentBlanks)
{
context.DocumentBlanks.Add(new DocumentBlank
{
Document = product,
Document = document,
Blank = context.Blanks.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_productBlanks = null;
_documentBlanks = null;
}
}

View File

@ -50,14 +50,14 @@ namespace LawFirmFileImplement.Implements
}
public DocumentViewModel? Update(DocumentBindingModel model)
{
var ship = source.Documents.FirstOrDefault(x => x.Id == model.Id);
if (ship == null)
var document = source.Documents.FirstOrDefault(x => x.Id == model.Id);
if (document == null)
{
return null;
}
ship.Update(model);
document.Update(model);
source.SaveDocuments();
return ship.GetViewModel;
return document.GetViewModel;
}
public DocumentViewModel? Delete(DocumentBindingModel model)
{

View File

@ -68,10 +68,10 @@ namespace LawFirmFileImplement.Implements
private OrderViewModel GetViewModel(Order order)
{
var viewModel = order.GetViewModel;
var ship = _source.Documents.FirstOrDefault(x => x.Id == order.DocumentId);
if (ship != null)
var document = _source.Documents.FirstOrDefault(x => x.Id == order.DocumentId);
if (document != null)
{
viewModel.DocumentName = ship.DocumentName;
viewModel.DocumentName = document.DocumentName;
}
return viewModel;
}

View File

@ -11,20 +11,20 @@ namespace LawFirmFileImplement.Models
public string DocumentName { get; private set; } = string.Empty;
public double Price { get; private set; }
public Dictionary<int, int> Blanks { get; private set; } = new();
private Dictionary<int, (IBlankModel, int)>? _productBlanks =
private Dictionary<int, (IBlankModel, int)>? _documentBlanks =
null;
public Dictionary<int, (IBlankModel, int)> DocumentBlanks
{
get
{
if (_productBlanks == null)
if (_documentBlanks == null)
{
var source = DataFileSingleton.GetInstance();
_productBlanks = Blanks.ToDictionary(x => x.Key, y =>
_documentBlanks = Blanks.ToDictionary(x => x.Key, y =>
((source.Blanks.FirstOrDefault(z => z.Id == y.Key) as IBlankModel)!,
y.Value));
}
return _productBlanks;
return _documentBlanks;
}
}
public static Document? Create(DocumentBindingModel model)
@ -70,7 +70,7 @@ namespace LawFirmFileImplement.Models
Price = model.Price;
Blanks = model.DocumentBlanks.ToDictionary(x => x.Key, x =>
x.Value.Item2);
_productBlanks = null;
_documentBlanks = null;
}
public DocumentViewModel GetViewModel => new()
{

View File

@ -69,12 +69,12 @@ namespace LawFirmView
try
{
int id = Convert.ToInt32(comboBoxDocument.SelectedValue);
var product = _logicD.ReadElement(new DocumentSearchModel
var document = _logicD.ReadElement(new DocumentSearchModel
{
Id = id
});
int count = Convert.ToInt32(textBoxCount.Text);
textBoxSum.Text = Math.Round(count * (product?.Price ?? 0), 2).ToString();
textBoxSum.Text = Math.Round(count * (document?.Price ?? 0), 2).ToString();
_logger.LogInformation("Расчет суммы заказа");
}
catch (Exception ex)

View File

@ -174,7 +174,7 @@ namespace LawFirmView
}
}
private void BlanksReportToolStripMenuItem_Click(object sender, EventArgs
private void DocumentsReportToolStripMenuItem_Click(object sender, EventArgs
e)
{
using var dialog = new SaveFileDialog { Filter = "docx|*.docx" };

View File

@ -31,11 +31,9 @@ namespace LawFirmView
dataGridView.Rows.Add(new object[] { elem.DocumentName, "", "" });
foreach (var listElem in elem.Blanks)
{
dataGridView.Rows.Add(new object[] { "",
listElem.Item1, listElem.Item2 });
dataGridView.Rows.Add(new object[] { "", listElem.Item1, listElem.Item2 });
}
dataGridView.Rows.Add(new object[] { "Итого", "",
elem.TotalCount });
dataGridView.Rows.Add(new object[] { "Итого", "", elem.TotalCount });
dataGridView.Rows.Add(Array.Empty<object>());
}
}