private void SyncTasklistAfterResponse(string tasklistId, ResultCode resultCode, RestResponse response) { string result = response.Content; List<Dictionary<string, string>> responseArray = (List<Dictionary<string, string>>)result.ToJSONObject(); string newTasklistId = null; for (int i = 0; i < responseArray.Count; i++) { Dictionary<string, string> dict = responseArray[i]; string oldId = dict["OldId"]; string newId = dict["NewId"]; this._console.log(string.Format("任务列表旧值ID:{0}, 变为新值ID:{1}", oldId, newId)); this._tasklistRepository.UpdateTasklistByNewId(oldId, newId); if (IsolatedStorageSettings.ApplicationSettings.Contains(Constant.USERNAME_KEY)) { string username = (string)IsolatedStorageSettings.ApplicationSettings[Constant.USERNAME_KEY]; if (!string.IsNullOrEmpty(username)) { this._taskRepository.UpdateTasklistIdByNewId(oldId, newId); this._taskIdxRepository.UpdateTasklistIdByNewId(oldId, newId); this._changeLogRepository.UpdateTasklistIdByNewId(oldId, newId); newTasklistId = newId; } } } if (string.IsNullOrEmpty(tasklistId)) { this._tasklistService.GetTasklists((response1, userState1) => { if (response.StatusCode == HttpStatusCode.OK) { result = response1.Content; Dictionary<string, string> tasklistsDict = (Dictionary<string, string>)result.ToJSONObject(); //删除当前账户所有列表 this._tasklistRepository.DeleteAll(); List<Tasklist> tasklists = new List<Tasklist>(); foreach (string key in tasklistsDict.Keys) { string value = tasklistsDict[key]; Tasklist tasklist = new Tasklist(); tasklist.TasklistId = key; tasklist.Name = value; tasklist.ListType = "personal"; tasklist.Editable = true; if (IsolatedStorageSettings.ApplicationSettings.Contains(Constant.USERNAME_KEY)) { string username = (string)IsolatedStorageSettings.ApplicationSettings[Constant.USERNAME_KEY]; tasklist.AccountId = username; } else { tasklist.AccountId = ""; } tasklists.Add(tasklist); } Tasklist defaultTasklist = new Tasklist(); defaultTasklist.TasklistId = "0"; defaultTasklist.Name = "默认列表"; defaultTasklist.ListType = "personal"; defaultTasklist.Editable = true; if (IsolatedStorageSettings.ApplicationSettings.Contains(Constant.USERNAME_KEY)) { string username = (string)IsolatedStorageSettings.ApplicationSettings[Constant.USERNAME_KEY]; defaultTasklist.AccountId = username; } else { defaultTasklist.AccountId = ""; } tasklists.Add(defaultTasklist); this._tasklistRepository.AddTasklists(tasklists); if (tasklists.Count == 0) { resultCode.Status = true; } else { foreach (var tasklist in tasklists) { //HACK:Fetch模式不支持同步变更 if (tasklist.TasklistId.Equals("github") || tasklist.TasklistId.Equals("ifree") || tasklist.TasklistId.Equals("wf")) { Dictionary<string, object> userState = new Dictionary<string, object>(); userState.Add("tasklistId", tasklist.TasklistId); this._taskService.GetTasks(tasklist.TasklistId , (response2, userState2) => { if (response2.StatusCode == HttpStatusCode.OK) { Dictionary<string, object> dict = (Dictionary<string, object>)userState2; this.GetTasksAfterResponse(dict["tasklistId"].ToString(), resultCode, response2); } else { this.DispatchCommandResult(response2); } } , exception => { this.DispatchCommandResult(exception); } , userState); } else { Dictionary<string, object> userState = new Dictionary<string, object>(); userState.Add("tasklistId", tasklist.TasklistId); this._taskService.SyncTasks(tasklist.TasklistId , (response2, userState2) => { if (response2.StatusCode == HttpStatusCode.OK) { Dictionary<string, object> dict = (Dictionary<string, object>)userState2; this.SyncTasksAfterResponse(dict["tasklistId"].ToString(), resultCode, response2); } else { this.DispatchCommandResult(response2); } } , exception => { this.DispatchCommandResult(exception); } , userState); } resultCode.Status = true; } } } else { this.DispatchCommandResult(response1); } } , exception => { this.DispatchCommandResult(exception); } , new Dictionary<string, object>()); } }
private string SyncTasklistAfterResponse(string tasklistId, Tasklist tasklist, ResultCode resultCode, RestResponse response) { string result = response.Content; this._tasklistRepository.AdjustWithNewId(tasklist.TasklistId, result); this._console.log(string.Format("任务列表旧值ID:{0} 变为新值ID:{1}", tasklist.TasklistId, result)); string newTasklistId = null; if (IsolatedStorageSettings.ApplicationSettings.Contains(Constant.USERNAME_KEY)) { string username = (string)IsolatedStorageSettings.ApplicationSettings[Constant.USERNAME_KEY]; if (!string.IsNullOrEmpty(username)) { this._taskRepository.UpdateTasklistIdByNewId(tasklist.TasklistId, result); this._taskIdxRepository.UpdateTasklistIdByNewId(tasklist.TasklistId, result); this._changeLogRepository.UpdateTasklistIdByNewId(tasklist.TasklistId, result); newTasklistId = result; } } return newTasklistId; }
private void GetTasklistsAfterResponse(string tasklistId, ResultCode resultCode, RestResponse response) { string result = response.Content; Dictionary<string, string> tasklistDict = JsonConvert.DeserializeObject<Dictionary<string, string>>(result); //删除当前账户的所有任务列表 this._tasklistRepository.DeleteAll(); List<Tasklist> tasklists = new List<Tasklist>(); foreach (var key in tasklistDict.Keys) { string value = tasklistDict[key]; Tasklist tasklist = new Tasklist(); tasklist.TasklistId = key; tasklist.Name = value; tasklist.ListType = "personal"; tasklist.Editable = true; if (IsolatedStorageSettings.ApplicationSettings.Contains(Constant.USERNAME_KEY)) { tasklist.AccountId = (string)IsolatedStorageSettings.ApplicationSettings[Constant.USERNAME_KEY]; } else { tasklist.AccountId = ""; } tasklists.Add(tasklist); } Tasklist defaultTasklist = new Tasklist(); defaultTasklist.TasklistId = "0"; defaultTasklist.Name = "默认列表"; defaultTasklist.ListType = "personal"; defaultTasklist.Editable = true; if (IsolatedStorageSettings.ApplicationSettings.Contains(Constant.USERNAME_KEY)) { defaultTasklist.AccountId = (string)IsolatedStorageSettings.ApplicationSettings[Constant.USERNAME_KEY]; } else { defaultTasklist.AccountId = ""; } tasklists.Add(defaultTasklist); this._tasklistRepository.AddTasklists(tasklists); if (tasklists.Count == 0) { resultCode.Status = true; this.DispatchCommandResult(resultCode); } else { foreach(Tasklist tempTasklist in tasklists) { //HACK:Fetch模式不支持同步变更 if(tempTasklist.TasklistId.Equals("github") || tempTasklist.TasklistId.Equals("ifree") || tempTasklist.TasklistId.Equals("wf")) { Dictionary<string, object> userState = new Dictionary<string, object>(); userState.Add("tasklistId", tempTasklist.TasklistId); this._taskService.GetTasks(tempTasklist.TasklistId , (response1, userState1) => { if (response1.StatusCode == HttpStatusCode.OK) { Dictionary<string, object> dict = (Dictionary<string, object>)userState1; this.GetTasksAfterResponse(dict["tasklistId"].ToString(), resultCode, response1); } else { this.DispatchCommandResult(response1); } } , exception => { this.DispatchCommandResult(exception); } , userState); } else { this._console.log("tasklistId:" + tempTasklist.TasklistId); Dictionary<string, object> userState = new Dictionary<string, object>(); userState.Add("tasklistId", tempTasklist.TasklistId); this._taskService.SyncTasks(tempTasklist.TasklistId , (response1, userState1) => { if (response1.StatusCode == HttpStatusCode.OK) { Dictionary<string, object> dict = (Dictionary<string, object>)userState1; this.SyncTasksAfterResponse(dict["tasklistId"].ToString(), resultCode, response1); } else { this.DispatchCommandResult(response1); } } , exception => { this.DispatchCommandResult(exception); } , userState); } } resultCode.Status = true; this.DispatchCommandResult(resultCode); } }
/// <summary> /// 获取 /// </summary> /// <param name="options"条件></param> public void get(string options) { ResultCode resultCode = new ResultCode(); SignKeyOptions keyOptions = JsonHelper.Deserialize<SignKeyOptions>(options); if (keyOptions.Key.Equals(Constant.GETNETWORKSTATUS)) { #region 是否连接网络 bool isOnline = Tools.IsOnline(); resultCode.Status = true; resultCode.Data = isOnline; this.DispatchCommandResult(resultCode); #endregion } else if (keyOptions.Key.Equals(Constant.GETCURRENTUSER)) { #region 获取当前用户名 string username = ""; //if (IsolatedStorageSettings.ApplicationSettings.Contains(Constant.USERNAME_KEY)) //{ // username = IsolatedStorageSettings.ApplicationSettings[Constant.USERNAME_KEY].ToString(); //} var userInfo = new UserInfo(); userInfo.Username = username; resultCode.Data = userInfo; resultCode.Status = true; this.DispatchCommandResult(resultCode); #endregion } else if (keyOptions.Key.Equals(Constant.GETTASKLISTS)) { #region 获取任务列表 List<Tasklist> tasklists = this._tasklistRepository.GetAllTasklist(); bool isDefaultTasklistExist = false; foreach (var tasklist in tasklists) { if (tasklist.TasklistId.Equals("0")) { isDefaultTasklistExist = true; break; } } if (!isDefaultTasklistExist) { Tasklist defaultTasklist = new Tasklist(); defaultTasklist.TasklistId = "0"; defaultTasklist.Name = "默认列表"; defaultTasklist.ListType = "personal"; defaultTasklist.Editable = true; if (IsolatedStorageSettings.ApplicationSettings.Contains(Constant.USERNAME_KEY)) { defaultTasklist.AccountId = IsolatedStorageSettings.ApplicationSettings[Constant.USERNAME_KEY].ToString(); } else { defaultTasklist.AccountId = ""; } this._tasklistRepository.AddTasklist(defaultTasklist); tasklists.Add(defaultTasklist); } Dictionary<string, string> dict = new Dictionary<string, string>(); foreach (var tasklist in tasklists) { dict.Add(tasklist.TasklistId, tasklist.Name); } resultCode.Data = dict; resultCode.Status = true; this.DispatchCommandResult(resultCode); #endregion } else if (keyOptions.Key.Equals(Constant.GETTASKSBYPRIORITY)) { #region 获取任务 var getTasksByPriorityOptions = JsonHelper.Deserialize<GetTasksByPriorityOptions>(options); string tasklistId = getTasksByPriorityOptions.TasklistId; Tasklist tasklist = this._tasklistRepository.GetTasklistById(tasklistId); Dictionary<string, object> dict = new Dictionary<string, object>(); dict.Add("editable", tasklist.Editable); List<TaskIdx> taskIdxs = this._taskIdxRepository.GetAllTaskIdx(tasklistId); List<Dictionary<string, object>> task_array = new List<Dictionary<string, object>>(); List<Dictionary<string, object>> taskIdx_array = new List<Dictionary<string, object>>(); foreach (var taskIdx in taskIdxs) { JArray taskIdsDict = (JArray)taskIdx.Indexes.ToJSONObject(); for (int i = 0; i < taskIdsDict.Count; i++) { string taskId = taskIdsDict[i].Value<string>(); Task task = this._taskRepository.GetTaskById(taskId); if (task != null) { Dictionary<string, object> taskDict = new Dictionary<string, object>(); taskDict.Add("id", task.TaskId); taskDict.Add("subject", task.Subject); taskDict.Add("body", task.Body); taskDict.Add("isCompleted", task.Status == 1 ? "true" : "false"); taskDict.Add("dueTime", !task.DueDate.HasValue ? "" : task.DueDate.Value.ToString("yyyy-MM-dd")); taskDict.Add("priority", task.Priority); task_array.Add(taskDict); } } Dictionary<string, object> taskIdx_dict = new Dictionary<string, object>(); taskIdx_dict.Add("by", "priority"); taskIdx_dict.Add("key", taskIdx.Key); taskIdx_dict.Add("name", taskIdx.Name); taskIdx_dict.Add("indexs", taskIdsDict); taskIdx_array.Add(taskIdx_dict); } dict.Add("tasks", task_array); dict.Add("sorts", taskIdx_array); resultCode.Status = true; resultCode.Data = dict; this.DispatchCommandResult(resultCode); #endregion } }
/// <summary> /// 保存 /// </summary> /// <param name="options">条件</param> public void save(string options) { ResultCode resultCode = new ResultCode(); SignKeyOptions keyOptions = JsonHelper.Deserialize<SignKeyOptions>(options); if (keyOptions.Key.Equals(Constant.CREATETASKLIST)) { #region 创建任务列表 CreateTasklistOptions createTasklistOptions = JsonHelper.Deserialize<CreateTasklistOptions>(options); string tasklistId = createTasklistOptions.Id; string tasklistName = createTasklistOptions.Name; string tasklistType = createTasklistOptions.Type; Tasklist tasklist = new Tasklist(); tasklist.TasklistId = tasklistId; tasklist.Name = tasklistName; tasklist.ListType = tasklistType; tasklist.Editable = true; if (IsolatedStorageSettings.ApplicationSettings.Contains(Constant.USERNAME_KEY)) { tasklist.AccountId = IsolatedStorageSettings.ApplicationSettings[Constant.USERNAME_KEY].ToString(); } else { tasklist.AccountId = ""; } this._tasklistRepository.AddTasklist(tasklist); resultCode.Status = true; this.DispatchCommandResult(resultCode); #endregion } else if (keyOptions.Key.Equals(Constant.CREATETASK)) { #region 创建任务 JObject updateTaskOptions = (JObject)options.ToJSONObject(); string tasklistId = updateTaskOptions["tasklistId"].Value<string>(); JToken taskDict = (JToken)updateTaskOptions["task"]; JArray changesArray = (JArray)updateTaskOptions["changes"]; DateTime currentDate = DateTime.Now; string subject = taskDict["subject"].Value<string>(); string body = taskDict["body"].Value<string>(); string isCompleted = taskDict["isCompleted"].Value<string>(); string priority = taskDict["priority"].Value<string>(); string id = taskDict["id"].Value<string>(); string dueTime = taskDict["dueTime"].Value<string>(); Task t = new Task(); t.TaskId = id; t.Subject = subject; t.LastUpdateDate = currentDate; t.Body = body; t.IsPublic = true; if (isCompleted.ToLower().Equals("true")) { t.Status = 1; } else { t.Status = 0; } t.Priority = priority; if (!string.IsNullOrEmpty(dueTime)) { t.DueDate = Convert.ToDateTime(dueTime); } t.Editable = true; t.TasklistId = tasklistId; if (IsolatedStorageSettings.ApplicationSettings.Contains(Constant.USERNAME_KEY)) { t.AccountId = IsolatedStorageSettings.ApplicationSettings[Constant.USERNAME_KEY].ToString(); } else { t.AccountId = ""; } this._taskRepository.AddTask(t); this._taskIdxRepository.AddTaskIdx(id, priority, tasklistId); for (int i = 0; i < changesArray.Count; i++) { JToken dict = (JToken)changesArray[i]; string name = dict["Name"].Value<string>(); string value = dict["Value"].Value<string>(); string taskId = dict["ID"].Value<string>(); string type = dict["Type"].Value<string>(); this._changeLogRepository.AddChangeLog(type, taskId, name, value, tasklistId); } resultCode.Status = true; this.DispatchCommandResult(resultCode); #endregion } else if (keyOptions.Key.Equals(Constant.UPDATETASK)) { #region 更新任务 JObject updateTaskOptions = (JObject)options.ToJSONObject(); string tasklistId = updateTaskOptions["tasklistId"].Value<string>(); JToken taskDict = (JToken)updateTaskOptions["task"]; JArray changesArray = (JArray)updateTaskOptions["changes"]; DateTime currentDate = DateTime.Now; string subject = taskDict["subject"].Value<string>(); string body = taskDict["body"].Value<string>(); string isCompleted = taskDict["isCompleted"].Value<string>(); string priority = taskDict["priority"].Value<string>(); string id = taskDict["id"].Value<string>(); string dueTime = taskDict["dueTime"].Value<string>(); Task t = this._taskRepository.GetTaskById(id); string oldPriority = priority; if (t != null) { oldPriority = t.Priority; } t.Subject = subject; t.LastUpdateDate = currentDate; t.Body = body; t.IsPublic = true; if (isCompleted.ToLower().Equals("true")) { t.Status = 1; } else { t.Status = 0; } t.Priority = priority; if (!string.IsNullOrEmpty(dueTime)) { t.DueDate = Convert.ToDateTime(dueTime); } t.Editable = true; t.TasklistId = tasklistId; this._taskRepository.UpdateTask(t); if (!oldPriority.Equals(priority)) { this._taskIdxRepository.UpdateTaskIdx(id, priority, tasklistId); } for (int i = 0; i < changesArray.Count; i++) { JToken dict = (JToken)changesArray[i]; string name = dict["Name"].Value<string>(); string value = dict["Value"].Value<string>(); string taskId = dict["ID"].Value<string>(); string type = dict["Type"].Value<string>(); this._changeLogRepository.AddChangeLog(type, taskId, name, value, tasklistId); } resultCode.Status = true; this.DispatchCommandResult(resultCode); #endregion } else if (keyOptions.Key.Equals(Constant.DELETETASK)) { #region 删除任务 DeleteTaskOptions deleteTaskOptions = JsonHelper.Deserialize<DeleteTaskOptions>(options); string tasklistId = deleteTaskOptions.TasklistId; string taskId = deleteTaskOptions.TaskId; this._changeLogRepository.AddChangeLog("1", taskId, "", "", tasklistId); this._taskIdxRepository.DeleteTaskIndexesByTaskId(taskId, tasklistId); Task task = this._taskRepository.GetTaskById(taskId); if (task != null) { this._taskRepository.DeleteTask(task); } resultCode.Status = true; this.DispatchCommandResult(resultCode); #endregion } }
public void AddTasklist(Tasklist tasklist) { this._context.Tasklists.InsertOnSubmit(tasklist); this._context.SubmitChanges(); }