///// <summary> ///// 开启一个或多个任务 ///// </summary> ///// <param name="userId">用户ID</param> ///// <param name="jobCode">任务编码</param> //public void OpenJob(string userId, string[] jobCode) //{ // lock (_jobStateLock) // { // Dictionary<string, Dictionary<string, JobState>> jobStateDict = // _cache.GetCache<Dictionary<string, Dictionary<string, JobState>>>(""); // //已有任务 // Dictionary<string, JobState> historyJobStatedict = new Dictionary<string, JobState>(); // //新加任务 // Dictionary<string, JobState> newJobStatedict = new Dictionary<string, JobState>(); // _jobStateDict.TryGetValue(userId, out historyJobStatedict); // if (historyJobStatedict == null || historyJobStatedict.Count == 0) // { // historyJobStatedict = new Dictionary<string, JobState>(); // //添加任务状态到字典 // foreach (string code in jobCode) // { // historyJobStatedict.Add(code, JobState.Open); // } // newJobStatedict = historyJobStatedict; // _jobStateDict.TryAdd(userId, historyJobStatedict); // } // else // { // //添加未标识的任务 // foreach (string code in jobCode) // { // //如果不包含该任务,则直接添加 // if (!historyJobStatedict.ContainsKey(code)) // { // historyJobStatedict.Add(code, JobState.Open); // newJobStatedict.Add(code, JobState.Open); // } // else // { // //如果任务是关闭状态,则开启它 // if (historyJobStatedict[code] == JobState.Closed) // { // historyJobStatedict[code] = JobState.Open; // newJobStatedict.Add(code, JobState.Open); // } // } // } // _jobStateDict[userId] = historyJobStatedict; // } // //TODO 调用开启、关闭任务方法 // this.OpenOrCloseJob(userId, newJobStatedict); // } //} ///// <summary> ///// 关闭一个或多个任务 ///// </summary> ///// <param name="userId">用户ID</param> ///// <param name="jobCode">任务编码</param> //public void CloseJob(string userId, string[] jobCode) //{ // lock (_jobStateLock) // { // //已有任务 // Dictionary<string, JobState> historyJobStatedict = new Dictionary<string, JobState>(); // if (_jobStateDict.Count > 0 && _jobStateDict != null) // { // _jobStateDict.TryGetValue(userId, out historyJobStatedict); // if (historyJobStatedict != null && historyJobStatedict.Count > 0) // { // foreach (string code in jobCode) // { // if (historyJobStatedict.ContainsKey(code)) // { // if (historyJobStatedict[code] == JobState.Open) // { // historyJobStatedict[code] = JobState.Closed; // } // } // else // { // historyJobStatedict.Add(code, JobState.Closed); // } // } // } // } // _jobStateDict[userId] = historyJobStatedict; // this.OpenOrCloseJob(userId, historyJobStatedict); // } //} ///// <summary> ///// 重置一个或多个任务 ///// </summary> ///// <param name="userId">用户ID</param> ///// <param name="jobCode">任务编码</param> //public void ResetJob(string userId, string[] jobCode) //{ // throw new NotImplementedException(); //} ///// <summary> ///// 开启或者关闭任务 ///// </summary> ///// <param name="userId"></param> ///// <param name="newJobStatedict"></param> //private void OpenOrCloseJob(string userId, Dictionary<string, JobState> newJobStatedict) //{ // foreach (KeyValuePair<string, JobState> pair in newJobStatedict) // { // string jobClde = pair.Key; // switch (pair.Value) // { // case JobState.Open: // //TODO 开启任务 // //向客户端广播消息 // Clients.Client(userId).BroadcastJobOpened(jobClde); // break; // case JobState.Closed: // //TODO 关闭任务 // //向客户端广播消息 // Clients.Client(userId).BroadcastJobClosed(jobClde); // break; // } // } //} #endregion #region 任务控制 /// <summary> /// 开启一个或多个任务 /// </summary> /// <param name="userId">用户ID</param> /// <param name="connId">用户ID-连接ID</param> /// <param name="jobCode">任务编码</param> public void OpenJob(string userId, Dictionary <string, string> connId, string[] jobCode) { lock (_jobStateLock) { #region 1-获取用户列表(放在客户端连接时操作) ////1-获取用户列表 List <string> jobUserList = _cache.GetCache <List <string> >("__JobUserCacheKey"); if (jobUserList != null && jobUserList.Count != 0) { //判断当前用户是否在集合里面 if (!jobUserList.Contains(userId)) { jobUserList.AddRange(new List <string> { userId }); } _cache.WriteCache(jobUserList, "__JobUserCacheKey"); } else { jobUserList = new List <string> { userId }; _cache.WriteCache(jobUserList, "__JobUserCacheKey"); } #endregion #region 2-获取此用户已经在使用的任务列表 //2-获取此用户已经在使用的任务列表 List <string> usedJobList = _cache.GetCache <List <string> >(userId + "_UsedJobListCacheKey"); if (usedJobList == null || usedJobList.Count == 0) { usedJobList = new List <string>(); //如果还没使用任何任务,则直接全部加入 usedJobList.AddRange(jobCode); //加入缓存 _cache.WriteCache(usedJobList, userId + "_UsedJobListCacheKey"); } else { //检测是否重复开启 List <string> temp = jobCode.ToList(); //筛选出差集 //List<string> except = usedJobList.Except(temp).ToList(); //取并集并去重 List <string> union = usedJobList.Union(temp).Distinct().ToList(); if (union.Count > 0) { usedJobList = union; //加入缓存 _cache.WriteCache(usedJobList, userId + "_UsedJobListCacheKey"); } } #endregion #region 3-检测每个任务是否开启 //防止重复开启任务 Dictionary <string, JobState> doJob = new Dictionary <string, JobState>(); //3-检测每个任务是否开启 Dictionary <string, JobState> jobStateDict = _cache.GetCache <Dictionary <string, JobState> >("__JobStateCacheKey"); if (jobStateDict == null || jobStateDict.Count == 0) { jobStateDict = new Dictionary <string, JobState>(); //检测状态 foreach (string code in jobCode) { jobStateDict.Add(code, JobState.Open); doJob.Add(code, JobState.Open); } } else { foreach (string code in jobCode) { if (!jobStateDict.ContainsKey(code)) { jobStateDict.Add(code, JobState.Open); doJob.Add(code, JobState.Open); } else { if (jobStateDict[code] == JobState.Closed) { jobStateDict[code] = JobState.Open; doJob.Add(code, JobState.Open); } } } } _cache.WriteCache(jobStateDict, "__JobStateCacheKey"); #endregion #region 4-开启任务并推送消息 ////4-开启任务并推送消息 ////准备广播消息的用户ID //List<string> connIds = new List<string>(); //foreach (string id in userList) //{ // connIds.Add(connId[id]); //} this.OpenJob(userId, doJob); #endregion #region 弃用代码 ////已有任务 //Dictionary<string, JobState> historyJobStatedict = new Dictionary<string, JobState>(); ////新加任务 //Dictionary<string, JobState> newJobStatedict = new Dictionary<string, JobState>(); //_jobStateDict.TryGetValue(userId, out historyJobStatedict); //if (historyJobStatedict == null || historyJobStatedict.Count == 0) //{ // historyJobStatedict = new Dictionary<string, JobState>(); // //添加任务状态到字典 // foreach (string code in jobCode) // { // historyJobStatedict.Add(code, JobState.Open); // } // newJobStatedict = historyJobStatedict; // _jobStateDict.TryAdd(userId, historyJobStatedict); //} //else //{ // //添加未标识的任务 // foreach (string code in jobCode) // { // //如果不包含该任务,则直接添加 // if (!historyJobStatedict.ContainsKey(code)) // { // historyJobStatedict.Add(code, JobState.Open); // newJobStatedict.Add(code, JobState.Open); // } // else // { // //如果任务是关闭状态,则开启它 // if (historyJobStatedict[code] == JobState.Closed) // { // historyJobStatedict[code] = JobState.Open; // newJobStatedict.Add(code, JobState.Open); // } // } // } // _jobStateDict[userId] = historyJobStatedict; //} #endregion } }