VerificationActTaskServerFunctions.cs 7.49 KB
using System;
using System.Collections.Generic;
using System.Linq;
using Sungero.Core;
using Sungero.CoreEntities;
using DirRX.Storage.VerificationActTask;

namespace DirRX.Storage.Server
{
  partial class VerificationActTaskFunctions
  {
    public Sungero.Core.StateView GetStateView(Sungero.Docflow.IOfficialDocument document)
    {
      if (_obj.DocumentGroup.DestructionActs.Any(d => Equals(document, d)))
        return this.GetStateView();
      else
        return StateView.Create();
    }
    
    [Remote(IsPure = true)]
    public Sungero.Core.StateView GetStateView()
    {
      var stateView = StateView.Create();
      
      // Блок информации о задаче.
      var taskBlock = this.AddTaskBlock(stateView);
      
      // Получить все задания по задаче.
      var taskAssignments = VerificationActAssignments.GetAll(a => Equals(a.Task, _obj)).OrderBy(a => a.Created).ToList();
      
      // Статус задачи.
      var status = _obj.Info.Properties.Status.GetLocalizedValue(_obj.Status);
      
      var lastAssignment = taskAssignments.OrderByDescending(a => a.Created).FirstOrDefault();
      
      if (!string.IsNullOrWhiteSpace(status))
        Sungero.Docflow.PublicFunctions.Module.AddInfoToRightContent(taskBlock, status);
      
      // Блоки информации о заданиях.
      foreach (var assignment in taskAssignments)
      {
        var assignmentBlock = this.GetAssignmentBlock(assignment);
        
        taskBlock.AddChildBlock(assignmentBlock);
      }
      
      return stateView;
    }
    
    /// <summary>
    /// Добавить блок задачи на верификацию.
    /// </summary>
    /// <param name="stateView">Схема представления.</param>
    /// <returns>Новый блок.</returns>
    public Sungero.Core.StateBlock AddTaskBlock(Sungero.Core.StateView stateView)
    {
      // Создать блок задачи.
      var taskBlock = stateView.AddBlock();
      
      // Добавить ссылку на задачу и иконку.
      taskBlock.Entity = _obj;
      taskBlock.AssignIcon(StateBlockIconType.OfEntity, StateBlockIconSize.Large);
      
      // Определить схлопнутость.
      taskBlock.IsExpanded = _obj.Status == Sungero.Workflow.Task.Status.InProcess;
      taskBlock.AddLabel(_obj.Info.LocalizedName, Sungero.Docflow.PublicFunctions.Module.CreateHeaderStyle());
      
      return taskBlock;
    }
    
    /// <summary>
    /// Добавить блок задания на верификацию.
    /// </summary>
    /// <param name="assignment">Задание.</param>
    /// <returns>Новый блок.</returns>
    public Sungero.Core.StateBlock GetAssignmentBlock(Sungero.Workflow.IAssignment assignment)
    {
      // Стили.
      var performerDeadlineStyle = Sungero.Docflow.PublicFunctions.Module.CreatePerformerDeadlineStyle();
      var boldStyle = Sungero.Docflow.PublicFunctions.Module.CreateStyle(true, false);
      var grayStyle = Sungero.Docflow.PublicFunctions.Module.CreateStyle(false, true);
      var separatorStyle = Sungero.Docflow.PublicFunctions.Module.CreateSeparatorStyle();
      var noteStyle = Sungero.Docflow.PublicFunctions.Module.CreateNoteStyle();
      
      var block = StateView.Create().AddBlock();
      block.Entity = assignment;
      
      // Иконка.
      this.SetIcon(block, VerificationActAssignments.As(assignment));
      
      // Заголовок.
      block.AddLabel(VerificationActAssignments.Info.LocalizedName, boldStyle);
      block.AddLineBreak();
      
      // Кому.
      var assigneeShortName = Sungero.Company.PublicFunctions.Employee.GetShortName(Sungero.Company.Employees.As(assignment.Performer), false);
      var performerInfo = string.Format("{0}: {1}", Sungero.Docflow.OfficialDocuments.Resources.StateViewTo, assigneeShortName);
      block.AddLabel(performerInfo, performerDeadlineStyle);
      
      // Срок.
      if (assignment.Deadline.HasValue)
      {
        var deadlineLabel = Sungero.Docflow.PublicFunctions.Module.ToShortDateShortTime(assignment.Deadline.Value.ToUserTime());
        block.AddLabel(string.Format("{0}: {1}", Sungero.Docflow.OfficialDocuments.Resources.StateViewDeadline, deadlineLabel), performerDeadlineStyle);
      }
      
      // Текст задания.
      var comment = Sungero.Docflow.PublicFunctions.Module.GetAssignmentUserComment(Sungero.Workflow.Assignments.As(assignment));
      if (!string.IsNullOrWhiteSpace(comment))
      {
        // Разделитель.
        block.AddLineBreak();
        block.AddLabel(Sungero.Docflow.PublicFunctions.Module.GetSeparatorText(), separatorStyle);
        block.AddLineBreak();
        block.AddEmptyLine(Sungero.Docflow.PublicFunctions.Module.GetEmptyLineMargin());
        
        block.AddLabel(comment, noteStyle);
      }
      
      // Статус.
      var status = Sungero.Workflow.AssignmentBases.Info.Properties.Status.GetLocalizedValue(assignment.Status);
      
      // Для непрочитанных заданий указать это.
      if (assignment.IsRead == false)
        status = Sungero.Docflow.ApprovalTasks.Resources.StateViewUnRead.ToString();
      
      // Для исполненных заданий указать результаты, с которым они исполнены, кроме "Проверено".
      if (assignment.Status == Sungero.Workflow.AssignmentBase.Status.Completed
          && assignment.Result != Storage.VerificationActAssignment.Result.Complete)
        status = Storage.VerificationActAssignments.Info.Properties.Result.GetLocalizedValue(assignment.Result);
      
      if (!string.IsNullOrWhiteSpace(status))
        Sungero.Docflow.PublicFunctions.Module.AddInfoToRightContent(block, status);
      
      // Задержка исполнения.
      if (assignment.Deadline.HasValue &&
          assignment.Status == Sungero.Workflow.AssignmentBase.Status.InProcess)
        Sungero.Docflow.PublicFunctions.OfficialDocument.AddDeadlineHeaderToRight(block, assignment.Deadline.Value, assignment.Performer);
      
      return block;
    }
    
    /// <summary>
    /// Установить иконку.
    /// </summary>
    /// <param name="block">Блок, для которого требуется установить иконку.</param>
    /// <param name="assignment">Задание, от которого построен блок.</param>
    private void SetIcon(StateBlock block, IVerificationActAssignment assignment)
    {
      var iconSize = StateBlockIconSize.Large;
      
      // Иконка по умолчанию.
      block.AssignIcon(StateBlockIconType.OfEntity, iconSize);

      // Прекращено, остановлено по ошибке.
      if (assignment.Status == Sungero.Workflow.AssignmentBase.Status.Aborted ||
          assignment.Status == Sungero.Workflow.AssignmentBase.Status.Suspended)
      {
        block.AssignIcon(StateBlockIconType.Abort, iconSize);
        return;
      }
      
      if (assignment.Result == null)
        return;
      
      // Проверено.
      if (assignment.Result == Storage.VerificationActAssignment.Result.Complete)
      {
        block.AssignIcon(StateBlockIconType.Completed, iconSize);
        return;
      }
      
      // Переадресовано.
      if (assignment.Result == Storage.VerificationActAssignment.Result.Forward)
      {
        block.AssignIcon(Sungero.Docflow.FreeApprovalTasks.Resources.Forward, iconSize);
      }
    }
    
  }
}