/// <summary>
        /// cancel and remove the notify
        /// </summary>
        /// <param name="notify_id"></param>
        public async Task SetNotifyCancelAndRemoveAsync(int notify_id)
        {
            using (var scope = _scopefactory.CreateScope())
            {
                var ctx    = scope.ServiceProvider.GetService <ApplicationDbContext>();
                var record = await ctx.Set <TnTaskNotify>()
                             .FirstOrDefaultAsync(x => x.Id == notify_id)
                ;

                record.State    = TaskNotifyState.Remove;
                record.Complete = DateTime.Now;

                var evt = new TnUserNotify();
                evt.UserId = record.UserId;
                ctx.Add(evt);

                await ctx.SaveChangesAsync();

                //notify user event
                var t1 = SendEvt_UserChanged(record.UserId);

                //notify cancel event
                var t2 = SendEvt_NotifyCancle(notify_id);
            }
        }
        /// <summary>
        /// change the state of notify
        /// </summary>
        /// <param name="notify_id"></param>
        /// <param name="state"></param>
        public async Task <bool> SetNotifyRuningAsync(int notify_id)
        {
            using (var scope = _scopefactory.CreateScope())
            {
                var ctx = scope.ServiceProvider.GetService <ApplicationDbContext>();

                var record = await ctx.Set <TnTaskNotify>()
                             .FirstOrDefaultAsync(x => x.Id == notify_id);

                if (record == null)
                {
                    throw new Exception($"notify {notify_id} not exits");
                }

                if (record.State != TaskNotifyState.Append)
                {
                    return(false);
                }

                record.State = TaskNotifyState.Run;

                var evt = new TnUserNotify();
                evt.UserId = record.UserId;
                ctx.Add(evt);

                await ctx.SaveChangesAsync();

                //raise event
                var t = SendEvt_UserChanged(evt.UserId);
                return(true);
            }
        }
        /// <summary>
        /// notify complete
        /// </summary>
        /// <param name="notify_id"></param>
        /// <param name="state"></param>
        public async Task <bool> SetNotifyCompleteAsync(int notify_id, TaskNotifyResult result)
        {
            using (var scope = _scopefactory.CreateScope())
            {
                var ctx    = scope.ServiceProvider.GetService <ApplicationDbContext>();
                var record = await ctx.Set <TnTaskNotify>()
                             .FirstOrDefaultAsync(x => x.Id == notify_id)
                ;

                if (record.State != TaskNotifyState.Run)
                {
                    return(false);
                }

                record.Result   = result.ToJsonText();
                record.State    = TaskNotifyState.Finish;
                record.Complete = DateTime.Now;

                var evt = new TnUserNotify();
                evt.UserId = record.UserId;
                ctx.Add(evt);

                await ctx.SaveChangesAsync();

                //notify event
                var t = SendEvt_UserChanged(record.UserId);

                //notify complete event
                var t2 = SendEvt_NotifyCancle(notify_id);

                return(true);
            }
        }
        /// <summary>
        /// add new notify
        /// </summary>
        /// <param name="user_id"></param>
        /// <param name="title"></param>
        /// <returns>the notify' id</returns>
        public async Task <int> AddNotifyAsync(int user_id, string title)
        {
            using (var scope = _scopefactory.CreateScope())
            {
                var ctx    = scope.ServiceProvider.GetService <ApplicationDbContext>();
                var record = new TnTaskNotify();
                record.UserId = user_id;
                record.Title  = title;
                //record.Content = content;
                record.State = TaskNotifyState.Append;
                ctx.Add(record);

                var evt = new TnUserNotify();
                evt.UserId = user_id;
                ctx.Add(evt);

                await ctx.SaveChangesAsync();

                //add cancel token
                lock (_WaitCancels)
                {
                    _WaitCancels.Add(record.Id, new CancellationTokenSource());
                }

                //notify event
                var t = SendEvt_UserChanged(user_id);

                return(record.Id);
            }
        }
        /// <summary>
        /// change title of notify
        /// </summary>
        /// <param name="notify_id"></param>
        public async Task SetNotifyTitleAsync(int notify_id, string title)
        {
            using (var scope = _scopefactory.CreateScope())
            {
                var ctx    = scope.ServiceProvider.GetService <ApplicationDbContext>();
                var record = await ctx.Set <TnTaskNotify>()
                             .FirstOrDefaultAsync(x => x.Id == notify_id)
                ;

                record.Title = title;

                var evt = new TnUserNotify();
                evt.UserId = record.UserId;
                ctx.Add(evt);

                await ctx.SaveChangesAsync();

                //notify user event
                var t1 = SendEvt_UserChanged(record.UserId);
            }
        }