public void CreateHistory(CreateHistoryInput input) { var historyPath = StoreHistoryFile(input.HistoryUrl, input.FileType); var changesPath = StoreChangesFile(input.ChangesUrl, input.FileType); var history = _context.DocumentHistories.FirstOrDefault(a => a.DocumentInfoId == input.DocumentInfoId && a.Version == input.Version); if (history == null) { history = Mapper.Map <DocumentHistory>(input); history.Path = historyPath; history.ChangesUrl = changesPath; _context.DocumentHistories.Add(history); } else { Mapper.Map(input, history); history.Path = historyPath; history.ChangesUrl = changesPath; } _context.SaveChanges(); }
public bool SaveFile(SaveFileInput input) { try { var fileData = new JavaScriptSerializer().Deserialize <Dictionary <string, object> >(input.Data); int status = (int)fileData["status"]; //状态4处理手动保存后关闭文档的响应,让远程可以回调 if (status == 4) { var key = fileData["key"].ToString(); DocumentInfo documentInfo = _context.DocumentInfos.FirstOrDefault(a => a.Key == key); //处理远程回调 if (documentInfo.ExternalFlag) { ExternalCallback(documentInfo, input.ServerOrigin, false); } } else if (status == 2) { var key = fileData["key"].ToString(); var userList = (ArrayList)fileData["users"]; string userId = userList.Count > 0 ? userList[0].ToString() : null; var user = _context.Users.FirstOrDefault(a => a.UserID == userId); DocumentInfo documentInfo = _context.DocumentInfos.FirstOrDefault(a => a.Key == key); DocumentHistory documentHistory = _context.DocumentHistories.FirstOrDefault(a => a.DocumentInfoId == documentInfo.Id && a.Version == documentInfo.CurrentVersion); documentInfo.Key = Guid.NewGuid().ToString(); documentInfo.ModifyTime = DateTime.Now; documentInfo.ModifyUserId = user?.UserID; documentInfo.ModifyUserName = user?.UserName; //不使用history的key,因为可能修改文件后没有产生新版本 documentHistory.Key = documentInfo.Key; documentService.StoreDocumentFile(documentInfo, (string)fileData["url"]); //txt文件需要做转换处理 string convertPath = ConvertFile(documentInfo); if (!string.IsNullOrWhiteSpace(convertPath)) { documentService.StoreDocumentFile(documentInfo, convertPath); } _context.SaveChanges(); //处理远程回调 if (documentInfo.ExternalFlag) { ExternalCallback(documentInfo, input.ServerOrigin, true); } } else if (status == 6) { var key = fileData["key"].ToString(); var historyDic = (Dictionary <string, object>)fileData["history"]; var serverVersion = historyDic["serverVersion"].ToString(); var changes = new JavaScriptSerializer().Serialize(historyDic["changes"]); var changesUrl = fileData["changesurl"].ToString(); var userList = (ArrayList)fileData["users"]; string userId = userList.Count > 0 ? userList[0].ToString() : null; //更新文档信息 DocumentInfo documentInfo = _context.DocumentInfos.FirstOrDefault(a => a.Key == key); DocumentHistory documentHistory = _context.DocumentHistories.Where(a => a.DocumentInfoId == documentInfo.Id) .OrderByDescending(a => a.Version).FirstOrDefault(); bool newVersion = documentHistory.Key == documentInfo.Key; documentInfo.CurrentVersion = newVersion ? documentHistory.Version + 1 : documentHistory.Version; _context.SaveChanges(); //存储新文档 var user = _context.Users.FirstOrDefault(a => a.UserID == userId); string userName = user?.UserName; CreateHistoryInput createHistoryInput = new CreateHistoryInput { DocumentInfoId = documentInfo.Id, Version = documentInfo.CurrentVersion, PreviousVersion = documentInfo.CurrentVersion - 1, ServerVersion = serverVersion, Key = newVersion ? Guid.NewGuid().ToString() : documentHistory.Key, Changes = changes, UserId = userId, UserName = userName, AutoSave = false, CreateTime = DateTime.Now, FileType = documentInfo.FileType, HistoryUrl = (string)fileData["url"], ChangesUrl = changesUrl }; documentService.CreateHistory(createHistoryInput); } return(true); } catch { return(true); } }