ArchiveSettingServerFunctions.cs 2.66 KB
using System;
using System.Collections.Generic;
using System.Linq;
using Sungero.Core;
using Sungero.CoreEntities;
using DirRX.Container.ArchiveSetting;
using Newtonsoft.Json.Linq;
using Sungero.Domain.SessionExtensions;
using Sungero.Domain.Shared;
using Sungero.Metadata;

namespace DirRX.Container.Server
{
  partial class ArchiveSettingFunctions
  {
    /// <summary>
    /// Получить настройки долговременного архива.
    /// </summary>
    /// <returns>Настройки.</returns>
    [Public]
    public static IArchiveSetting GetSettings()
    {
      return ArchiveSettings.GetAllCached().SingleOrDefault();
    }

    /// <summary>
    /// Получить данные записи справочника для синхронизации в ДА.
    /// </summary>
    /// <param name="entity">Запись синхронизируемого справочника.</param>
    /// <returns>Данные для синхронизации, строка в формате json.</returns>
    [Public]
    public virtual string GetSyncData(Sungero.Domain.Shared.IEntity entity)
    {      
      var syncEntity = _obj.SyncEntities.Where(x => x.Type.ToLower() == entity.Info.Name.ToLower()).FirstOrDefault();
            
      if (syncEntity != null)
      {
        var json = new JObject();
        
        if (!string.IsNullOrWhiteSpace(_obj.Name))
          json.Add("SystemName", _obj.Name);
        
        json.Add("SystemUid", _obj.SourceUid);
        json.Add("SourceName", syncEntity.Name);
        json.Add("SourceType", syncEntity.Type);
        json.Add("ArchiveName", syncEntity.LTAName);
        json.Add("ArchiveType", syncEntity.LTAType);
        
        if (!string.IsNullOrEmpty(syncEntity.LTAProperties))
        {
          var jsonProperties = new JArray();
          foreach (var propertyName in syncEntity.LTAProperties.Split(','))
          {
            var property = entity.GetType().GetProperties().Where(p => p.Name == (propertyName == "_sourceId" ? "Id" : propertyName)).LastOrDefault();
            if (property != null)
            {
              object propertyValue = property.GetValue(entity);
              var propertyStringValue = Sungero.Commons.PublicFunctions.Module.GetValueAsString(propertyValue);
              {
                var jsonProp = new JObject();
                jsonProp.Add("Name", propertyName);
                jsonProp.Add("Value", propertyStringValue);
                jsonProperties.Add(jsonProp);
              }
            }
          }
          json.Add("Properties", jsonProperties);
        }
        
        return json.ToString();
      }
      
      return string.Empty;
    }
    
  }
}