public static WorkTask ToEntity(this NewWorkTaskModel model)
        {
            model.Valid(); //验证Task参数是否有效

            var entity = new WorkTask()
            {
                Outline     = model.Outline,
                Type        = (int)model.Type,
                Status      = (int)WorkTaskStatus.Created,
                Desc        = model.Desc,
                MeetingId   = model.MeetingId,
                ProjectId   = model.ProjectId,
                Sponsor     = model.Sponsor,
                Supervisor  = model.Supervisor,
                Executor    = model.Executor,
                Source      = model.Source.HasValue ? (int)model.Source.Value : (int)WorkTaskSource.Person,
                Priority    = model.Priority.HasValue ? (int)model.Priority.Value : (int)WorkTaskPriority.Normal,
                Urgency     = model.Urgency.HasValue ? (int)model.Urgency.Value : (int)WorkTaskUrgency.Low,
                Importance  = model.Importance.HasValue ? (int)model.Importance.Value : (int)WorkTaskImportance.Low,
                EndTime     = model.EndTime,
                StartTime   = model.StartTime,
                CreatedTime = DateTime.Now
            };

            return(entity);
        }
        public ApiResponse <bool> EditWorkTask(int taskId, NewWorkTaskModel task)
        {
            task.Valid(); //验证Task参数是否有效

            var response = new ApiResponse <bool>()
            {
                Result = WorkTaskService.Edit(taskId, task, this.Member)
            };

            return(response);
        }
        public ApiResponse <int> AddWorkTask(NewWorkTaskModel task)
        {
            task.Valid(); //验证Task参数是否有效

            var response = new ApiResponse <int>()
            {
                Result = WorkTaskService.Add(task, this.Member)
            };

            return(response);
        }
        /// <summary>
        /// 修改工作任务
        /// </summary>
        /// <param name="taskId"></param>
        /// <param name="task"></param>
        /// <param name="currentUser"></param>
        /// <returns></returns>
        public bool Edit(int taskId, NewWorkTaskModel task, UserModel currentUser)
        {
            using (var dbContext = new MissionskyOAEntities())
            {
                var entity = dbContext.WorkTasks.FirstOrDefault(it => it.Id == taskId);
                if (entity == null)
                {
                    Log.Error(string.Format("任务无效, 任务ID: {0}", taskId));
                    throw new InvalidOperationException("任务无效");
                }

                int oldSupervisor = entity.Supervisor == null ? task.Sponsor : entity.Supervisor.Value; //原来的监督人
                int newSupervisor = task.Supervisor == null ? task.Sponsor : task.Supervisor.Value;     //新的监督人
                int oldExecutor   = entity.Executor == null ? task.Sponsor : entity.Executor.Value;     //原来的执行人
                int newExecutor   = task.Executor == null ? task.Sponsor : task.Executor.Value;         //新的执行人

                entity.Outline    = task.Outline;
                entity.Type       = (int)task.Type;
                entity.Desc       = task.Desc;
                entity.MeetingId  = task.MeetingId;
                entity.ProjectId  = task.ProjectId;
                entity.Source     = task.Source == null ? entity.Source : (int)task.Source;
                entity.Priority   = task.Priority == null ? entity.Priority : (int)task.Priority;
                entity.Urgency    = task.Urgency == null ? entity.Urgency : (int)task.Urgency;
                entity.Importance = task.Importance == null ? entity.Importance : (int)task.Importance;
                entity.EndTime    = task.EndTime;
                entity.StartTime  = task.StartTime;
                if (oldSupervisor != newSupervisor)
                {
                    entity.Supervisor = task.Supervisor;
                }

                if (oldExecutor != newExecutor)
                {
                    entity.Executor = task.Executor;
                }

                //添加推送消息
                AddNotification(dbContext, entity.ToModel(), currentUser);

                dbContext.SaveChanges();

                return(true);
            }
        }
        /// <summary>
        /// 创建新工作任务
        /// </summary>
        /// <param name="task"></param>
        /// <param name="currentUser"></param>
        /// <returns>返回新的Task id</returns>
        public int Add(NewWorkTaskModel task, UserModel currentUser)
        {
            using (var dbContext = new MissionskyOAEntities())
            {
                //创建工作任务
                var entity = task.ToEntity();
                dbContext.WorkTasks.Add(entity);

                //添加操作记录
                AddHistory(dbContext, currentUser.Id, entity.Id, WorkTaskStatus.Created, string.Format("{0}创建了一个工作任务: {1}。", currentUser.EnglishName, task.Outline));

                //添加消息推送
                AddNotification(dbContext, task.ToModel(), currentUser);

                dbContext.SaveChanges();

                return(entity.Id);
            }
        }
        public static WorkTaskModel ToModel(this NewWorkTaskModel model)
        {
            model.Valid(); //验证Task参数是否有效

            var task = new WorkTaskModel()
            {
                Outline     = model.Outline,
                Type        = model.Type,
                Status      = WorkTaskStatus.Started,
                Desc        = model.Desc,
                MeetingId   = model.MeetingId,
                ProjectId   = model.ProjectId,
                Sponsor     = model.Sponsor,
                Supervisor  = model.Supervisor,
                Executor    = model.Executor,
                Source      = model.Source ?? WorkTaskSource.Person,
                Priority    = model.Priority ?? WorkTaskPriority.Normal,
                EndTime     = model.EndTime.Value,
                StartTime   = model.StartTime.Value,
                CreatedTime = DateTime.Now
            };

            return(task);
        }
        /// <summary>
        /// 验证新的工作任务是否有效
        /// </summary>
        /// <param name="task">新的工作任务</param>
        public static void Valid(this NewWorkTaskModel task)
        {
            if (task == null)
            {
                Log.Error("任务无效");
                throw new InvalidOperationException("任务无效");
            }

            if (string.IsNullOrEmpty(task.Outline))
            {
                Log.Error("任务具体要求无效");
                throw new InvalidOperationException("任务具体要求无效");
            }

            if (task.EndTime.HasValue && task.EndTime.Value <= DateTime.Now)
            {
                Log.Error("截止日期无效");
                throw new InvalidOperationException("截止日期无效");
            }

            if (task.StartTime.HasValue && task.StartTime.Value < DateTime.Now)
            {
                Log.Error("开始日期无效");
                throw new InvalidOperationException("开始日期无效");
            }

            if (task.EndTime.HasValue && task.StartTime.HasValue && task.EndTime.Value <= task.StartTime.Value)
            {
                Log.Error("截止日期必须大于开始日期");
                throw new InvalidOperationException("截止日期必须大于开始日期");
            }

            if (task.Sponsor <= 0)
            {
                Log.Error("任务发起人无效");
                throw new InvalidOperationException("任务发起人无效");
            }

            //默认当前登录用户为监督人
            if (!task.Supervisor.HasValue || task.Supervisor.Value <= 0)
            {
                task.Supervisor = task.Sponsor;
            }
            if (task.EndTime <= DateTime.Now)
            {
                Log.Error("截止时间必须大于当前时间");
                throw new InvalidOperationException("截止时间必须大于当前时间");
            }

            //默认当前登录用户为执行人
            if (!task.Executor.HasValue || task.Executor.Value <= 0)
            {
                task.Executor = task.Sponsor;
            }

            //为空则默认通讯录个人
            if (!task.Source.HasValue || task.Source.Value == WorkTaskSource.Invalid)
            {
                task.Source = WorkTaskSource.Person;
            }

            //为空则默认一般
            if (!task.Priority.HasValue || task.Priority.Value == WorkTaskPriority.Invalid)
            {
                task.Priority = WorkTaskPriority.Normal;
            }
        }