private WorkSchedule FetchWorkSchedule(short year, short month)
 {
     return(Kernel.TryGetValue(Standards.FormatYearMonth(year, month), out WorkSchedule workSchedule)
         ? workSchedule
         : WorkSchedule.New(Database,
                            NameValue.Set <WorkSchedule>(p => p.Manager, Manager).
                            Set(p => p.Year, year).
                            Set(p => p.Month, month).
                            Set(p => p.Workers, new long[0])));
 }
        /// <summary>
        /// 接收消息
        /// </summary>
        /// <param name="content">消息内容</param>
        /// <param name="token">StreamSequenceToken</param>
        protected override Task OnReceiving(string content, StreamSequenceToken token)
        {
            DateTime deadline = GetDeadline();

            if (Kernel.TryGetValue(Standards.FormatYearMonth((short)deadline.Year, (short)deadline.Month), out WorkSchedule workSchedule))
            {
                foreach (long receiver in workSchedule.Workers)
                {
                    SendEventForRefreshProjectWorkloads(receiver, content, token);
                }
            }
            return(Task.CompletedTask);
        }
        async Task IWorkScheduleGrain.PutWorkSchedule(WorkSchedule source)
        {
            bool unlimited = await User.Identity.IsInRole(ProjectRoles.经营管理);

            if (!(unlimited || new DateTime(source.Year, source.Month, 1).AddDays(DateTime.Now.Day - 1) < GetDeadline()))
            {
                throw new ValidationException("不允许修改已归档的工作档期!");
            }
            if (!(unlimited || User.Identity.Id == Manager))
            {
                throw new SecurityException("管好自己的工作档期就行啦!");
            }

            List <long> receivers = new List <long>();
            DateTime    key       = Standards.FormatYearMonth(source.Year, source.Month);

            if (Kernel.TryGetValue(key, out WorkSchedule workSchedule))
            {
                receivers.AddRange(workSchedule.Workers);
                Database.Execute(dbTransaction =>
                {
                    workSchedule.UpdateSelf(dbTransaction, source);
                    ResetWorkers(dbTransaction, workSchedule);
                });
                foreach (long item in source.Workers)
                {
                    if (!receivers.Contains(item))
                    {
                        receivers.Add(item);
                    }
                }
            }
            else
            {
                Database.Execute(dbTransaction =>
                {
                    source.InsertSelf(dbTransaction);
                    ResetWorkers(dbTransaction, source);
                    Kernel[key] = source;
                });
                receivers.AddRange(source.Workers);
            }

            //播报
            foreach (long item in receivers)
            {
                await SendEventForRefreshProjectWorkloads(item, Manager.ToString());
            }
        }
        public async Task Put()
        {
            ProjectWorkload projectWorkload = await Request.ReadBodyAsync <ProjectWorkload>();

            await ClusterClient.Default.GetGrain <IProjectWorkloadGrain>(projectWorkload.Worker, Standards.FormatYearMonth(projectWorkload.Year, projectWorkload.Month).ToString(CultureInfo.InvariantCulture)).PutProjectWorkload(projectWorkload);
        }
        public IDictionary <long, IList <ProjectWorkload> > GetAll(string workers, short year, short month)
        {
            SynchronizedDictionary <long, IList <ProjectWorkload> > result = new SynchronizedDictionary <long, IList <ProjectWorkload> >();
            List <Task> tasks = new List <Task>();

            foreach (string s in workers.Split(',', StringSplitOptions.RemoveEmptyEntries))
            {
                long worker = Int64.Parse(s);
                tasks.Add(Task.Run(async() =>
                {
                    result.Add(worker, await ClusterClient.Default.GetGrain <IProjectWorkloadGrain>(worker, Standards.FormatYearMonth(year, month).ToString(CultureInfo.InvariantCulture)).GetProjectWorkloads());
                }));
            }

            Task.WaitAll(tasks.ToArray());
            return(result);
        }