EmployeeServerFunctions.cs 21.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Sungero.Company.Employee;
using Sungero.Core;
using Sungero.CoreEntities;
using Sungero.Domain.Shared;
using ElasticsearchTypes = Sungero.Commons.PublicConstants.Module.ElasticsearchType;

namespace Sungero.Company.Server
{
  partial class EmployeeFunctions
  {
    /// <summary>
    /// Создать асинхронное событие обновления имени сотрудника из персоны.
    /// </summary>
    /// <param name="personId">ИД персоны.</param>
    [Public]
    public static void CreateUpdateEmployeeNameAsyncHandler(int personId)
    {
      var asyncUpdateEmployeeName = Sungero.Company.AsyncHandlers.UpdateEmployeeName.Create();
      asyncUpdateEmployeeName.PersonId = personId;
      asyncUpdateEmployeeName.ExecuteAsync();
    }
    
    /// <summary>
    /// Получить количество активных сотрудников.
    /// </summary>
    /// <returns>Количество.</returns>
    public static int GetEmployeesCount()
    {
      return Employees.GetAll().Where(e => e.Status.Equals(Sungero.CoreEntities.DatabookEntry.Status.Active)).Count();
    }
    
    /// <summary>
    /// Получить сотрудника по имени.
    /// </summary>
    /// <param name="name">Имя.</param>
    /// <returns>Сотрудник.</returns>
    [Public, Remote(IsPure = true)]
    public static Company.IEmployee GetEmployeeByName(string name)
    {
      var employees = GetEmployeesByName(name);
      return employees.Count == 1 ? employees.First() : null;
    }
    
    /// <summary>
    /// Получить сотрудников по имени.
    /// </summary>
    /// <param name="name">Имя.</param>
    /// <returns>Список сотрудников.</returns>
    /// <remarks>Используется на слое.</remarks>
    [Public, Remote(IsPure = true)]
    public static List<Company.IEmployee> GetEmployeesByName(string name)
    {
      #region Список форматов ФИО
      
      // Реализован парсинг следующих форматов ФИО:
      // Иванов
      // Иванов  Иван
      // Иванов  Иван  Иванович
      // Иван  Иванов
      // Иван  Иванович  Иванов
      // Иванов  И.  И.
      // Иванов  И.
      // Иванов  И.И.
      // Иванов  И  И
      // Иванов  И
      // Иванов  ИИ
      // И. И. Иванов
      // И. Иванов
      // И.И. Иванов
      // И И  Иванов
      // И Иванов
      // ИИ Иванов
      #endregion
      
      if (name == null)
        return null;
      
      var oldChar = "ё";
      var newChar = "е";
      name = name.ToLower().Replace(oldChar, newChar);
      var activeEmployees = Company.Employees.GetAll(e => e.Status == Company.Employee.Status.Active);
      
      var employees = activeEmployees.Where(e => e.Name.ToLower().Replace(oldChar, newChar) == name).ToList();
      
      if (!employees.Any())
        employees = activeEmployees.Where(e => e.Person.LastName.ToLower().Replace(oldChar, newChar) == name).ToList();
      
      if (!employees.Any())
      {
        // Полные ФИО, могут быть без отчества.
        var fullNameRegex = System.Text.RegularExpressions.Regex.Match(name, @"^(\S+)(?<!\.)\s*(\S+)(?<!\.)\s*(\S*)(?<!\.)$");
        if (fullNameRegex.Success)
        {
          var lastName = fullNameRegex.Groups[1].Value;
          var firstName = fullNameRegex.Groups[2].Value;
          employees = activeEmployees.Where(e => e.Person.LastName.ToLower().Replace(oldChar, newChar) == lastName &&
                                            e.Person.FirstName.ToLower().Replace(oldChar, newChar) == firstName).ToList();
          var middleName = fullNameRegex.Groups[3].Value;
          if (middleName != string.Empty)
            employees = employees.Where(e => e.Person.MiddleName == null ||
                                        e.Person.MiddleName != null && e.Person.MiddleName.ToLower().Replace(oldChar, newChar) == middleName).ToList();
          
          firstName = fullNameRegex.Groups[1].Value;
          lastName = fullNameRegex.Groups[3].Value;
          middleName = string.Empty;
          if (lastName == string.Empty)
            lastName = fullNameRegex.Groups[2].Value;
          else
            middleName = fullNameRegex.Groups[2].Value;
          
          var revertedEmployees = activeEmployees.Where(e => e.Person.LastName.ToLower().Replace(oldChar, newChar) == lastName &&
                                                        e.Person.FirstName.ToLower().Replace(oldChar, newChar) == firstName).ToList();
          
          if (middleName != string.Empty)
            revertedEmployees = revertedEmployees.Where(e => e.Person.MiddleName == null ||
                                                        e.Person.MiddleName != null && e.Person.MiddleName.ToLower().Replace(oldChar, newChar) == middleName).ToList();

          employees.AddRange(revertedEmployees);
        }
      }
      
      if (!employees.Any())
      {
        // Сокращённое ФИО (Иванов И. И.), могут быть без отчества.
        var fullNameRegex = System.Text.RegularExpressions.Regex.Match(name, @"^(\S+)\s*(\S)\.?\s*(\S?)(?<!\.)\.?$");
        if (fullNameRegex.Success)
        {
          var lastName = fullNameRegex.Groups[1].Value;
          var firstName = fullNameRegex.Groups[2].Value;
          employees = activeEmployees.Where(e => e.Person.LastName.ToLower().Replace(oldChar, newChar) == lastName).ToList();
          employees = employees.Where(e => e.Person.FirstName.ToLower().Replace(oldChar, newChar)[0] == firstName[0]).ToList();
          
          var middleName = fullNameRegex.Groups[3].Value;
          if (middleName != string.Empty)
            employees = employees.Where(e => e.Person.MiddleName == null ||
                                        e.Person.MiddleName != null && e.Person.MiddleName.ToLower().Replace(oldChar, newChar)[0] == middleName[0]).ToList();
        }
      }
      
      if (!employees.Any())
      {
        // Сокращённое ФИО (И. И. Иванов), могут быть без отчества.
        var fullNameRegex = System.Text.RegularExpressions.Regex.Match(name, @"^(\S)\.?\s*(\S?)(?<!\.)\.?\s+(\S+)$");
        if (fullNameRegex.Success)
        {
          var firstName = fullNameRegex.Groups[1].Value;
          var lastName = fullNameRegex.Groups[3].Value;
          var middleName = string.Empty;
          if (lastName == string.Empty)
            lastName = fullNameRegex.Groups[2].Value;
          else
            middleName = fullNameRegex.Groups[2].Value;
          
          employees = activeEmployees.Where(e => e.Person.LastName.ToLower().Replace(oldChar, newChar) == lastName).ToList();
          employees = employees.Where(e => e.Person.FirstName.ToLower().Replace(oldChar, newChar)[0] == firstName[0]).ToList();
          
          if (middleName != string.Empty)
            employees = employees.Where(e => e.Person.MiddleName == null ||
                                        e.Person.MiddleName != null && e.Person.MiddleName.ToLower().Replace(oldChar, newChar)[0] == middleName[0]).ToList();
        }
      }
      
      return employees;
    }
    
    /// <summary>
    /// Получить сотрудников по ИНН.
    /// </summary>
    /// <param name="tin">ИНН.</param>
    /// <returns>Список сотрудников.</returns>
    [Public, Remote(IsPure = true)]
    public static List<IEmployee> GetEmployeesByTIN(string tin)
    {
      var activeEmployees = Company.Employees.GetAll(e => e.Status == Company.Employee.Status.Active);
      return activeEmployees.Where(x => x.Person != null && Equals(x.Person.TIN, tin)).ToList();
    }
    
    /// <summary>
    /// Получить сотрудников по СНИЛС.
    /// </summary>
    /// <param name="inila">СНИЛС.</param>
    /// <returns>Список сотрудников.</returns>
    [Public, Remote(IsPure = true)]
    public static List<IEmployee> GetEmployeesByINILA(string inila)
    {
      // Получить сотрудников с заполненным СНИЛС.
      var activeEmployeesWithInila = Company.Employees
        .GetAll(x => x.Status == Company.Employee.Status.Active &&
                x.Person != null &&
                x.Person.INILA != string.Empty &&
                x.Person.INILA != null);
      
      var result = new List<IEmployee>();
      var clearedInila = RemoveInilaSpecialSymbols(inila);
      foreach (var employee in activeEmployeesWithInila)
      {
        var clearedEmployeeInila = RemoveInilaSpecialSymbols(employee.Person.INILA);
        if (clearedEmployeeInila == clearedInila)
          result.Add(employee);
      }
      
      return result;
    }
    
    /// <summary>
    /// Получить нумерованный список сотрудников.
    /// </summary>
    /// <param name="employees">Список сотрудников.</param>
    /// <param name="withJobTitle">Признак отображения должности сотрудников.</param>
    /// <returns>Строка с нумерованным списком сотрудников.</returns>
    [Public, Remote(IsPure = true)]
    public static string GetEmployeesNumberedList(List<IEmployee> employees, bool withJobTitle)
    {
      if (!employees.Any())
        return null;
      
      employees = employees
        .GroupBy(g => g)
        .Select(s => s.Key)
        .ToList<Company.IEmployee>();
      
      return Functions.Employee.GetEmployeesNumberedList(employees, withJobTitle, true);
    }
    
    /// <summary>
    /// Получить нумерованный список сотрудников.
    /// </summary>
    /// <param name="employees">Список сотрудников.</param>
    /// <param name="withJobTitle">Признак отображения должности сотрудников.</param>
    /// <param name="platformLogic">Дублировать платформенную логику при формировании ФИО сотрудников.</param>
    /// <returns>Строка с нумерованным списком сотрудников.</returns>
    [Public, Remote(IsPure = true)]
    public static string GetEmployeesNumberedList(List<IEmployee> employees, bool withJobTitle, bool platformLogic)
    {
      if (!employees.Any())
        return null;
      
      var employeesNumberedList = new List<string>();
      foreach (var employee in employees)
      {
        var shortName = Functions.Employee.GetShortName(employee, platformLogic);
        var employeeNumberedName = string.Format("{0}. {1}", employees.IndexOf(employee) + 1, shortName);
        if (withJobTitle && employee.JobTitle != null && !string.IsNullOrWhiteSpace(employee.JobTitle.Name))
          employeeNumberedName = string.Format("{0} – {1}", employeeNumberedName, employee.JobTitle.Name);
        employeesNumberedList.Add(employeeNumberedName);
      }
      
      return string.Join("\r\n", employeesNumberedList);
    }
    
    /// <summary>
    /// Очистить СНИЛС от пробелов, дефисов и тире.
    /// </summary>
    /// <param name="inila">СНИЛС.</param>
    /// <returns>СНИЛС без пробелов, дефисов и тире.</returns>
    public static string RemoveInilaSpecialSymbols(string inila)
    {
      return inila.Replace(" ", string.Empty).Replace("-", string.Empty).Replace("—", string.Empty);
    }
    
    /// <summary>
    /// Сформировать всплывающую подсказку о сотруднике в виде модели всплывающего окна.
    /// </summary>
    /// <returns>Всплывающая подсказка о сотруднике в виде модели всплывающего окна.</returns>
    /// <remarks>Используется в подсказке о сотруднике.</remarks>
    public virtual Sungero.Core.IDigestModel GetEmployeePopup()
    {
      if (_obj.IsSystem == true)
        return null;
      
      var digest = Sungero.Core.UserDigest.Create(_obj);
      if (_obj.Department != null)
        digest.AddEntity(_obj.Department);
      
      if (_obj.JobTitle != null)
        digest.AddLabel(_obj.JobTitle.Name);
      
      if (!string.IsNullOrWhiteSpace(_obj.Phone))
        digest.AddLabel(string.Format("{0} {1}", Company.Employees.Resources.PopupPhoneDescription, _obj.Phone));
      
      if (_obj.Department != null)
      {
        var manager = _obj.Department.Manager;
        if (manager == null && _obj.Department.HeadOffice != null)
          manager = _obj.Department.HeadOffice.Manager;
        
        if (manager != null && !Equals(manager, _obj))
          digest.AddEntity(manager, Company.Employees.Resources.PopupManagerDescription);
      }
      
      return digest;
    }
    
    /// <summary>
    /// Сформировать всплывающую подсказку о сотруднике в виде текста.
    /// </summary>
    /// <returns>Всплывающая подсказка о сотруднике в виде текста.</returns>
    [Public]
    public virtual string GetEmployeePopupText()
    {
      // Используется в тестах подсказки о сотруднике.
      var employeePopup = this.GetEmployeePopup() as Sungero.Domain.Shared.UserDigestModel;
      if (employeePopup == null)
        return string.Empty;
      
      var popupText = new System.Text.StringBuilder();
      popupText.AppendLine(employeePopup.Header);
      
      foreach (var control in employeePopup.Controls)
      {
        var digestLabel = control as DigestLabel;
        if (digestLabel != null)
        {
          popupText.AppendLine(digestLabel.Text);
          continue;
        }
        
        var digestProperty = control as DigestNavigationProperty;
        if (digestProperty != null)
        {
          popupText.AppendLine(string.Concat(digestProperty.Text, digestProperty.Entity.ToString()));
          continue;
        }
        
        var digestLink = control as DigestLink;
        if (digestLink != null)
        {
          popupText.AppendLine(string.Concat(digestLink.Text, digestLink.Href));
        }
      }
      
      return popupText.ToString();
    }
    
    /// <summary>
    /// Проверить, что сотрудник может готовить проект резолюции.
    /// </summary>
    /// <returns>True, если сотрудник может готовить проект резолюции, иначе - False.</returns>
    /// <remarks>Сотрудник может готовить проект резолюции,
    /// как непосредственно являясь помощником или замещая помощника.
    /// Используется CoreEntities.Recipients.DirectSubstitutionRecipientIdsFor,
    /// которая внутри себя делает remote-запрос.</remarks>
    [Public, Remote(IsPure = true)]
    public virtual bool CanPrepareDraftResolution()
    {
      var assistants = Functions.Module.GetResolutionPreparers();
      var substitutes = CoreEntities.Recipients.DirectSubstitutionRecipientIdsFor(_obj);
      return assistants.Any(x => substitutes.Contains(x.Assistant.Id) || Equals(x.Assistant, _obj));
    }
    
    /// <summary>
    /// Вернуть всех сотрудников.
    /// </summary>
    /// <returns>Все сотрудники.</returns>
    [Public, Remote(IsPure = true)]
    public static IQueryable<IEmployee> GetEmployees()
    {
      return Employees.GetAll();
    }
    
    /// <summary>
    /// Получить признак настройки рассылки по умолчанию.
    /// </summary>
    /// <returns>True - если рассылка выключена, иначе рассылка включена.</returns>
    public virtual bool GetDisableMailNotificationParam()
    {
      var key = Docflow.PublicConstants.Module.DisableMailNotification;
      var command = string.Format(Queries.Module.SelectDisableMailNotificationParam, key);
      var commandResult = Docflow.PublicFunctions.Module.ExecuteScalarSQLCommand(command);
      var disableMailNotificationValue = string.Empty;
      if (!(commandResult is DBNull) && commandResult != null)
        disableMailNotificationValue = commandResult.ToString();
      
      bool result = false;
      bool.TryParse(disableMailNotificationValue, out result);
      return result;
    }

    /// <summary>
    /// Получить сотрудников по имени с использованием нечеткого поиска.
    /// </summary>
    /// <param name="name">Имя.</param>
    /// <param name="businesUnitId">ИД НОР.</param>
    /// <returns>Список сотрудников.</returns>
    [Public]
    public static List<Company.IEmployee> GetEmployeesByNameFuzzy(string name, int businesUnitId)
    {
      var businesUnitIds = new List<int>();
      if (businesUnitId > 0)
        businesUnitIds.Add(businesUnitId);
      
      return GetEmployeesByNameFuzzy(name, businesUnitIds);
    }

    /// <summary>
    /// Получить сотрудников по имени по списку наших организаций с использованием нечеткого поиска.
    /// </summary>
    /// <param name="name">Имя.</param>
    /// <param name="businesUnitIds">Список ИД.</param>
    /// <returns>Список сотрудников.</returns>
    [Public]
    public static List<Company.IEmployee> GetEmployeesByNameFuzzy(string name, List<int> businesUnitIds)
    {
      var employees = new List<IEmployee>();
      
      name = Sungero.Commons.PublicFunctions.Module.TrimSpecialSymbols(name);
      if (string.IsNullOrWhiteSpace(name))
        return employees;
      
      // Искать только активные записи сотрудников внутри указанной НОР.
      var filter = Commons.PublicFunctions.Module.GetTermQuery("Status", CoreEntities.DatabookEntry.Status.Active.Value);
      
      if (businesUnitIds.Count == 1)
        filter = string.Format("{0},{1}", filter, Commons.PublicFunctions.Module.GetTermQuery("BusinessUnitId", businesUnitIds.Single().ToString()));
      
      if (businesUnitIds.Count > 1)
        filter = string.Format("{0},{1}", filter, 
                               Commons.PublicFunctions.Module.GetTermsQuery("BusinessUnitId", businesUnitIds.Select(x => x.ToString()).ToList()));
      
      var employeesIds = new List<int>();
      var matchInitials = Regex.Match(name, Sungero.Parties.PublicConstants.Module.InitialsRegex, RegexOptions.IgnoreCase);
      if (!matchInitials.Success)
      {
        // Если инициалы не найдены, искать по полному ФИО.
        var splittedName = name.Split(' ');
        if (splittedName.Length > 1)
        {
          // Поиск ФИО по вхождению строк.
          var must = Commons.PublicFunctions.Module.GetMatchQuery("FullName", name, true);
          var query = Commons.PublicFunctions.Module.GetBoolQuery(must, string.Empty, filter);
          employeesIds = Commons.PublicFunctions.Module.ExecuteElasticsearchQuery(Employees.Info.Name, query);
          
          // Нечеткий поиск по ФИО.
          if (employeesIds.Count == 0)
          {
            must = Commons.PublicFunctions.Module.GetMatchFuzzyQuery("FullName", name, true);
            query = Commons.PublicFunctions.Module.GetBoolQuery(must, string.Empty, filter);
            employeesIds = Commons.PublicFunctions.Module.ExecuteElasticsearchQuery(Employees.Info.Name, query, Constants.Employee.ElasticsearchMinScore);
          }
        }
      }
      else
      {
        // Вырезать инициалы из исходной строки (оставить только фамилию).
        var lastName = Regex.Replace(name, Sungero.Parties.PublicConstants.Module.InitialsRegex, string.Empty);
        if (string.IsNullOrWhiteSpace(lastName))
          return employees;
        
        // Сформировать обязательную часть запроса по совпадению инициалов.
        var initialFirstName = matchInitials.Groups[1].Value;
        var initialPatronymic = matchInitials.Groups[2].Value;
        
        var initialsMust = Commons.PublicFunctions.Module.GetTermQuery("InitialFirstName", initialFirstName);
        if (!string.IsNullOrWhiteSpace(initialPatronymic))
          initialsMust = string.Format("{0},{1}", initialsMust,
                                       Commons.PublicFunctions.Module.GetTermQuery("InitialPatronymic", initialPatronymic));

        // Добавить к запросу условие по поиску фамилии.
        var must = string.Format("{0},{1}", initialsMust, Commons.PublicFunctions.Module.GetMatchQuery("LastName", lastName, true));
        var query = Commons.PublicFunctions.Module.GetBoolQuery(must, string.Empty, filter);
        employeesIds = Commons.PublicFunctions.Module.ExecuteElasticsearchQuery(Employees.Info.Name, query);
        if (employeesIds.Count == 0)
        {
          must = string.Format("{0},{1}", initialsMust, Commons.PublicFunctions.Module.GetMatchFuzzyQuery("LastName", lastName, true));
          query = Commons.PublicFunctions.Module.GetBoolQuery(must, string.Empty, filter);
          employeesIds = Commons.PublicFunctions.Module.ExecuteElasticsearchQuery(Employees.Info.Name, query, Constants.Employee.ElasticsearchMinScore);
        }
      }
      
      if (employeesIds.Any())
        employees = Employees.GetAll(l => employeesIds.Contains(l.Id)).ToList();
      
      return employees;
    }

  }
}