Esempio n. 1
0
        internal void EmitModification(PropertyTemplate pt, object value)
        {
            instanceAge++;
            var now = DateTime.UtcNow;

            ages[pt.Index] = instanceAge;
            modificationDates[pt.Index] = now;

            if (pt.Storage == StorageMode.NonVolatile)
            {
                store.Modify(resource, pt.Name, value, ages[pt.Index], now);
            }
            else if (pt.Storage == StorageMode.Recordable)
            {
                store.Record(resource, pt.Name, value, ages[pt.Index], now);
            }

            ResourceModified?.Invoke(resource, pt.Name, value);
        }
Esempio n. 2
0
        public async Task SaveResources(IEnumerable <ExtraResource> saveItems)
        {
            var removedList  = new List <ExtraResource>();
            var modifiedList = new List <ExtraResource>();
            var addedList    = new List <ExtraResource>();
            var existIds     = new List <Guid>();

            foreach (var saveItem in saveItems)
            {
                var resource = _Resources.FirstOrDefault(r => r.Id == saveItem.Id);
                if (resource == null)
                {
                    // 추가 대상
                    addedList.Add(saveItem);
                }
                else
                {
                    existIds.Add(saveItem.Id);

                    // 수정 대상
                    if (resource.Name != saveItem.Name ||
                        resource.Type != saveItem.Type ||
                        resource.Price != saveItem.Price)
                    {
                        // 수정
                        resource.Name  = saveItem.Name;
                        resource.Type  = (ResourceType)saveItem.Type;
                        resource.Price = (double)saveItem.Price;

                        modifiedList.Add(resource);
                    }
                }
            }

            // 삭제 대상
            removedList.AddRange(_Resources.Where(r => existIds.Contains(r.Id) == false));

            // 추가
            foreach (var addItem in addedList)
            {
                addItem.Id = Guid.NewGuid();
                _Resources.Add(addItem);
            }

            // 삭제
            _Resources.RemoveAll(r => removedList.Contains(r));

            await _ResourceRepo.SaveResources(_Resources);

            if (addedList.Count > 0)
            {
                Debug.WriteLine($"추가: {string.Join(" ", addedList.Select(d => $"{d.Id}_{d.Name}"))}");
            }
            if (modifiedList.Count > 0)
            {
                Debug.WriteLine($"수정: {string.Join(" ", modifiedList.Select(d => $"{d.Id}_{d.Name}"))}");

                // modify to packing template
                if (_PackingTemplates != null && _PackingTemplates.Count > 0)
                {
                    foreach (var packingTemplate in _PackingTemplates)
                    {
                        foreach (var packResource in packingTemplate.Resources)
                        {
                            var modifiedResource = modifiedList.FirstOrDefault(d => d.Id.Equals(packResource.Resource.Id));
                            if (modifiedResource != null)
                            {
                                packResource.Resource.Name  = modifiedResource.Name;
                                packResource.Resource.Price = modifiedResource.Price;
                                packResource.Resource.Type  = modifiedResource.Type;
                            }
                        }
                    }

                    await _PackingTempalteRepo.Save(_PackingTemplates);
                }

                ResourceModified?.Invoke(this, modifiedList);
            }
            if (removedList.Count > 0)
            {
                Debug.WriteLine($"삭제: {string.Join(" ", removedList.Select(d => $"{d.Id}_{d.Name}"))}");

                // remove to packing template
                if (_PackingTemplates != null && _PackingTemplates.Count > 0)
                {
                    var removedResourceIds = removedList.Select(n => n.Id).ToList();
                    foreach (var packingTemplate in _PackingTemplates.ToList())
                    {
                        foreach (var packResource in packingTemplate.Resources.ToList())
                        {
                            if (removedResourceIds.Contains(packResource.Resource.Id))
                            {
                                packingTemplate.Resources.Remove(packResource);
                            }
                        }

                        if (packingTemplate.Resources.Count < 1)
                        {
                            _PackingTemplates.Remove(packingTemplate);
                        }
                    }

                    await _PackingTempalteRepo.Save(_PackingTemplates);
                }

                ResourceRemoved?.Invoke(this, removedList);
            }
        }