示例#1
0
        public int Update(int id, FormDto form)
        {
            var updatedId = 0;
            var model     = _dbContext.PurchasingMemoDetailTextiles.FirstOrDefault(entity => entity.Id == id);

            if (model != null)
            {
                EntityExtension.FlagForUpdate(model, _identityService.Username, UserAgent);
                _dbContext.PurchasingMemoDetailTextiles.Update(model);

                var items            = _dbContext.PurchasingMemoDetailTextileItems.Where(entity => entity.PurchasingMemoDetailTextileId == model.Id).ToList();
                var details          = _dbContext.PurchasingMemoDetailTextileDetails.Where(entity => entity.PurchasingMemoDetailTextileId == model.Id).ToList();
                var unitReceiptNotes = _dbContext.PurchasingMemoDetailTextileUnitReceiptNotes.Where(entity => entity.PurchasingMemoDetailTextileId == model.Id).ToList();

                items = items.Select(element =>
                {
                    EntityExtension.FlagForDelete(element, _identityService.Username, UserAgent);
                    return(element);
                }).ToList();
                _dbContext.PurchasingMemoDetailTextileItems.UpdateRange(items);

                details = details.Select(element =>
                {
                    EntityExtension.FlagForDelete(element, _identityService.Username, UserAgent);
                    return(element);
                }).ToList();
                _dbContext.PurchasingMemoDetailTextileDetails.UpdateRange(details);

                unitReceiptNotes = unitReceiptNotes.Select(element =>
                {
                    EntityExtension.FlagForDelete(element, _identityService.Username, UserAgent);
                    return(element);
                }).ToList();
                _dbContext.PurchasingMemoDetailTextileUnitReceiptNotes.UpdateRange(unitReceiptNotes);

                _dbContext.SaveChanges();

                if (form.Type == PurchasingMemoType.Disposition)
                {
                    foreach (var item in form.Items)
                    {
                        var itemModel = new PurchasingMemoDetailTextileItemModel(item.Disposition.Id, item.Disposition.DocumentNo, item.Disposition.Date, model.Id);
                        EntityExtension.FlagForCreate(itemModel, _identityService.Username, UserAgent);
                        _dbContext.PurchasingMemoDetailTextileItems.Add(itemModel);
                        _dbContext.SaveChanges();

                        foreach (var detail in item.Disposition.Details)
                        {
                            var detailModel = new PurchasingMemoDetailTextileDetailModel(model.Id, itemModel.Id, detail.Expenditure.Id, detail.Expenditure.DocumentNo, detail.Expenditure.Date, detail.Supplier.Id, detail.Supplier.Code, detail.Supplier.Name, detail.Remark, detail.UnitPaymentOder.Id, detail.UnitPaymentOder.UnitPaymentOrderNo, detail.UnitPaymentOder.UnitPaymentOrderDate, detail.PaymentAmountCurrency, detail.PurchaseAmount, detail.PaymentAmount, detail.PaymentAmountCurrency);
                            EntityExtension.FlagForCreate(detailModel, _identityService.Username, UserAgent);
                            _dbContext.PurchasingMemoDetailTextileDetails.Add(detailModel);
                            _dbContext.SaveChanges();

                            foreach (var unitReceiptNote in detail.UnitReceiptNotes)
                            {
                                var unitReceiptNoteModel = new PurchasingMemoDetailTextileUnitReceiptNoteModel(model.Id, itemModel.Id, detailModel.Id, unitReceiptNote.Id, unitReceiptNote.UnitReceiptNoteNo, unitReceiptNote.UnitReceiptNoteDate);
                                EntityExtension.FlagForCreate(unitReceiptNoteModel, _identityService.Username, UserAgent);
                                _dbContext.PurchasingMemoDetailTextileUnitReceiptNotes.Add(unitReceiptNoteModel);
                                _dbContext.SaveChanges();
                            }
                        }
                    }
                }
                else
                {
                    foreach (var detail in form.Details)
                    {
                        var detailModel = new PurchasingMemoDetailTextileDetailModel(model.Id, 0, detail.Expenditure.Id, detail.Expenditure.DocumentNo, detail.Expenditure.Date, detail.Supplier.Id, detail.Supplier.Code, detail.Supplier.Name, detail.Remark, detail.UnitPaymentOder.Id, detail.UnitPaymentOder.UnitPaymentOrderNo, detail.UnitPaymentOder.UnitPaymentOrderDate, detail.PaymentAmountCurrency, detail.PurchaseAmount, detail.PaymentAmount, detail.PaymentAmountCurrency);
                        EntityExtension.FlagForCreate(detailModel, _identityService.Username, UserAgent);
                        _dbContext.PurchasingMemoDetailTextileDetails.Add(detailModel);
                        _dbContext.SaveChanges();

                        foreach (var unitReceiptNote in detail.UnitReceiptNotes)
                        {
                            var unitReceiptNoteModel = new PurchasingMemoDetailTextileUnitReceiptNoteModel(model.Id, 0, detailModel.Id, unitReceiptNote.Id, unitReceiptNote.UnitReceiptNoteNo, unitReceiptNote.UnitReceiptNoteDate);
                            EntityExtension.FlagForCreate(unitReceiptNoteModel, _identityService.Username, UserAgent);
                            _dbContext.PurchasingMemoDetailTextileUnitReceiptNotes.Add(unitReceiptNoteModel);
                            _dbContext.SaveChanges();
                        }
                    }
                }

                updatedId = model.Id;
            }

            return(updatedId);
        }