/// <summary> /// 把[Data对象]转换为[BaseData对象] /// </summary> /// <param name="_data">要转换的Data对象</param> /// <returns>转换后的BaseData对象</returns> public static RecordBaseData DataToBaseData(RecordData _data) { if (_data != null) { RecordBaseData _baseData = new RecordBaseData(); _baseData.Id = _data.Id; _baseData.BugId = _data.BugId; _baseData.ReplyId = _data.ReplyId; _baseData.Content = _data.Content; _baseData.Time = new List <int>() { _data.Time.Year, _data.Time.Month, _data.Time.Day, _data.Time.Hour, _data.Time.Minute, _data.Time.Second }; _baseData.Images = ObservableCollectionTool.ObservableCollectionToList(_data.Images); _baseData.IsDelete = _data.IsDelete; return(_baseData); } else { return(null); } }
/// <summary> /// 保存所有记录 /// </summary> /// <param name="_modeType">项目模式的类型</param> public void SaveRecords(ModeType _modeType) { /* 保存 */ try { //获取所有的Record ObservableCollection <RecordData> _recordDatas = RecordDatas; //判断项目的模式 switch (_modeType) { //如果项目是[默认模式]:把所有的Record,保存到/Record/Records.json文件中 case ModeType.Default: //把RecordData转换为RecordBaseData List <RecordBaseData> _recordBaseDatas = new List <RecordBaseData>(); for (int i = 0; i < _recordDatas.Count; i++) { RecordBaseData _recordBaseData = RecordBaseData.DataToBaseData(_recordDatas[i]); if (_recordBaseData != null) { _recordBaseDatas.Add(_recordBaseData); } } //把RecordBaseData转换为json string _recordsJsonText = JsonMapper.ToJson(_recordBaseDatas); //Record文件的路径(文件夹+文件名+后缀) string _recordsFilePath = RecordFolderPath + "/Records" + AppManager.Systems.ProjectSystem.OtherFileSuffix; //把json文件保存到[Records.json]文件里 File.WriteAllText(_recordsFilePath, _recordsJsonText, Encoding.Default); break; //如果项目是[协同合作模式]:把每一个Record,分别保存到/Record/Record - 20200119080230555.json文件中 case ModeType.Collaboration: for (int i = 0; i < _recordDatas.Count; i++) { SaveRecord(ModeType.Collaboration, _recordDatas[i].Id); } break; } } /* 如果有错误,就输出错误 */ catch (Exception e) { //输出错误 AppManager.Uis.ErrorUi.UiControl.TipContent = e.ToString(); AppManager.Uis.ErrorUi.OpenOrClose(true); } }
/// <summary> /// 保存1个记录 /// </summary> /// <param name="_modeType">项目模式的类型</param> /// <param name="_recordId">要保存的Record的编号</param> public void SaveRecord(ModeType _modeType, long _recordId) { /* 保存 */ try { //判断项目的模式 switch (_modeType) { //如果项目是[默认模式]:把所有的Record,保存到/Record/Records.json文件中 case ModeType.Default: SaveRecords(_modeType); break; //如果项目是[协同合作模式]:把每一个Record,分别保存到/Record/Record - 20200119080230555.json文件中 case ModeType.Collaboration: //通过Id取到Record RecordData _recordData = GetRecordData(_recordId); if (_recordData != null) { //把RecordData转换为RecordBaseData RecordBaseData _recordBaseData = RecordBaseData.DataToBaseData(_recordData); //把RecordBaseData转换为json string _recordJsonText = JsonMapper.ToJson(_recordBaseData); //Record文件的路径(文件夹+文件名+后缀) string _recordFilePath = RecordFolderPath + "/Record - " + _recordBaseData.Id + AppManager.Systems.ProjectSystem.OtherFileSuffix; //把json文件保存到[Record - RecordId.json]文件里 File.WriteAllText(_recordFilePath, _recordJsonText, Encoding.Default); } break; } } /* 如果有错误,就输出错误 */ catch (Exception e) { //输出错误 AppManager.Uis.ErrorUi.UiControl.TipContent = e.ToString(); AppManager.Uis.ErrorUi.OpenOrClose(true); } }
/// <summary> /// 把[BaseData对象]转换为[Data对象] /// </summary> /// <param name="_baseData">要转换的BaseData对象</param> /// <returns>转换后的Data对象</returns> public static RecordData BaseDataToData(RecordBaseData _baseData) { if (_baseData != null) { RecordData _data = new RecordData(); _data.Id = _baseData.Id; _data.BugId = _baseData.BugId; _data.ReplyId = _baseData.ReplyId; _data.Content = _baseData.Content; _data.Time = new DateTime(_baseData.Time[0], _baseData.Time[1], _baseData.Time[2], _baseData.Time[3], _baseData.Time[4], _baseData.Time[5]); _data.Images = ObservableCollectionTool.ListToObservableCollection(_baseData.Images); _data.IsDelete = _baseData.IsDelete; return(_data); } else { return(null); } }
/// <summary> /// 备份[Record] ///(备份[Record/Records.json]文件) /// </summary> public void BackupRecord() { /* 格式:Backup/Record/年月日时分秒.json */ try { /*创建[Backup/Record]文件夹(_projectFolderPath/Backup/Record/)*/ DirectoryInfo _recordBackupDirectoryInfo = new DirectoryInfo(RecordBackupFolderPath); //如果没有文件夹,就创建文件夹 if (_recordBackupDirectoryInfo.Exists == false) { _recordBackupDirectoryInfo.Create(); } /*获取【Record】文件的个数(如果文件超过10个,就删除最早创建的那个文件)*/ FileInfo[] _recordBackupFileInfos = _recordBackupDirectoryInfo.GetFiles(); if (_recordBackupFileInfos != null && _recordBackupFileInfos.Length > 10) { //找到Record备份文件中,最早创建的那个文件 FileInfo _firstFileInfo = FindFirstBackupFile(_recordBackupFileInfos); //删除最早的1个文件 if (_firstFileInfo != null) { File.Delete(_firstFileInfo.FullName); } } /* 获取要备份的文件的路径 */ //当前的时间(年月日时分秒) string _nowDateTimeString = DateTimeTool.DateTimeToString(DateTime.UtcNow, TimeFormatType.YearMonthDayHourMinuteSecondMillisecond); //文件的备份路径 string _recordFilePath = RecordBackupFolderPath + "/" + _nowDateTimeString + AppManager.Systems.ProjectSystem.OtherFileSuffix; /* 进行备份 */ //取到所有的Record数据 ObservableCollection <RecordData> _recordDatas = AppManager.Systems.RecordSystem.RecordDatas; //把RecordData转换为RecordBaseData List <RecordBaseData> _recordBaseDatas = new List <RecordBaseData>(); for (int i = 0; i < _recordDatas.Count; i++) { RecordBaseData _recordBaseData = RecordBaseData.DataToBaseData(_recordDatas[i]); if (_recordBaseData != null) { _recordBaseDatas.Add(_recordBaseData); } } //把RecordBaseData转换为json string _recordsJsonText = JsonMapper.ToJson(_recordBaseDatas); //把json文件保存到[Records.json]文件里 File.WriteAllText(_recordFilePath, _recordsJsonText, Encoding.Default); } catch (Exception e) { } }
/// <summary> /// 读取所有记录 /// </summary> /// <param name="_modeType">项目模式的类型</param> public void LoadRecords(ModeType _modeType) { try { //判断项目的模式 switch (_modeType) { //如果项目是[默认模式]:所有的Record,都在/Record/Records.json文件中 case ModeType.Default: //Record文件的路径(文件夹+文件名+后缀) string _recordsFilePath = RecordFolderPath + "/Records" + AppManager.Systems.ProjectSystem.OtherFileSuffix; //FileInfo类 用于读取文件信息 FileInfo _recordsFileInfo = new FileInfo(_recordsFilePath); /* 判断文件是否存在 */ if (_recordsFileInfo.Exists == true) //如果存在 { //读取[Record]的Json文本中的内容 string _recordsJsonText = File.ReadAllText(_recordsFilePath); //然后把Json文本解析成RecordBaseData对象 List <RecordBaseData> _recordBaseDatas = JsonMapper.ToObject <List <RecordBaseData> >(_recordsJsonText); //创建RecordData对象的集合 ObservableCollection <RecordData> _recordDatas = new ObservableCollection <RecordData>(); //把BugBaseData对象,转化为BugData对象 if (_recordBaseDatas != null) { for (int i = 0; i < _recordBaseDatas.Count; i++) { RecordData _recordData = RecordBaseData.BaseDataToData(_recordBaseDatas[i]); if (_recordData != null) { _recordDatas.Add(_recordData); } } } //把[RecordData对象]赋值到[列表]中 RecordDatas = _recordDatas; } break; //如果项目是[协同合作模式]:每一个Record,分别在/Record/Record - 20200119080230555.json文件中 case ModeType.Collaboration: //取到Record文件夹 的信息 DirectoryInfo _recordDirectoryInfo = new DirectoryInfo(RecordFolderPath); //获取到Record文件夹内所有的文件 的信息 FileInfo[] _recordFileInfos = _recordDirectoryInfo.GetFiles(); //遍历所有的Record文件 for (int i = 0; i < _recordFileInfos.Length; i++) { //取到Record文件的名字 string _recordFileName = Path.GetFileNameWithoutExtension(_recordFileInfos[i].FullName); //把[Record文件的名字]转换为[RecordId] _recordFileName = _recordFileName.Replace("Record - ", ""); long _recordId = -1; bool _isParseOk = long.TryParse(_recordFileName, out _recordId); //把string转换为long //如果转换成功 if (_isParseOk == true) { //就读取这个Record LoadRecord(ModeType.Collaboration, _recordId); } } break; } } /* 如果有错误,就输出错误 */ catch (Exception e) { //输出错误 AppManager.Uis.ErrorUi.UiControl.TipContent = e.ToString(); AppManager.Uis.ErrorUi.OpenOrClose(true); } }
/// <summary> /// 读取1个记录 /// </summary> /// <param name="_modeType">项目模式的类型</param> /// <param name="_recordId">Record的编号(通过Record的编号,来读取记录)</param> public void LoadRecord(ModeType _modeType, long _recordId) { try { //判断项目的模式 switch (_modeType) { //如果项目是[默认模式]:所有的Record,都在/Record/Records.json文件中 case ModeType.Default: LoadRecords(_modeType); break; //如果项目是[协同合作模式]:每一个Record,分别在/Record/Record - 20200119080230555.json文件中 case ModeType.Collaboration: //Record文件的路径(文件夹+文件名+后缀) string _recordFilePath = RecordFolderPath + "/Record - " + _recordId + AppManager.Systems.ProjectSystem.OtherFileSuffix; //FileInfo类 用于读取文件信息 FileInfo _recordFileInfo = new FileInfo(_recordFilePath); /* 判断文件是否存在 */ if (_recordFileInfo.Exists == true) //如果存在 { //读取[Record]的Json文本中的内容 string _recordJsonText = File.ReadAllText(_recordFilePath); //然后把Json文本解析成RecordBaseData对象 RecordBaseData _recordBaseData = null; try { _recordBaseData = JsonMapper.ToObject <RecordBaseData>(_recordJsonText); } catch (Exception e) { } //把BugBaseData对象,转化为BugData对象 RecordData _recordData = RecordBaseData.BaseDataToData(_recordBaseData); //如果RecordData的完整度为true if (RecordData.VerifyIntegrity(_recordData) == true) { //把[RecordData对象]赋值到[列表]中 RecordData _oldRecordData = GetRecordData(_recordData.Id); //通过BugId获取到旧的Record对象 if (_oldRecordData != null) //如果有旧的Record对象 { //如果旧的Record和新的Record有不同的地方 if (RecordData.Compare(CompareType.All, _recordData, _oldRecordData) == false) { //修改旧的Record对象的值 _oldRecordData.BugId = _recordData.BugId; _oldRecordData.ReplyId = _recordData.ReplyId; _oldRecordData.Content = _recordData.Content; _oldRecordData.Time = _recordData.Time; _oldRecordData.IsDelete = _recordData.IsDelete; _oldRecordData.Images = _recordData.Images; } } else { RecordDatas.Add(_recordData); //把读取到的Record对象,添加到列表中 } } } break; } } catch (Exception e) { //输出错误 AppManager.Uis.ErrorUi.UiControl.TipContent = e.ToString(); AppManager.Uis.ErrorUi.OpenOrClose(true); } }
/// <summary> /// 同步[Record] /// (当有[Record]文件更改时,2秒后,把已经修改的文件,进行同步) /// </summary> /// <param name="_syncRecordIds">需要同步的所有Record编号</param> /// <param name="_changeTypes">改变的类型(key:Record的编号;value:改变的类型)</param> /// <param name="_logs">同步的日志</param> private void SyncRecord(List <long> _syncRecordIds, out Dictionary <long, ChangeType> _changeTypes, out List <string> _logs) { //out _logs = new List <string>(); //所有的日志 _changeTypes = new Dictionary <long, ChangeType>(); //改变的类型 //遍历所有需要同步的Record for (int i = 0; i < _syncRecordIds.Count; i++) { //Record的编号 long _recordId = _syncRecordIds[i]; /* 取到当前的数据 */ //取到当前Record的数据 RecordData _oldRecordData = RecordData.Copy(AppManager.Systems.RecordSystem.GetRecordData(_recordId)); /* 读取Record数据 */ //Record文件的路径(文件夹+文件名+后缀) string _recordFilePath = AppManager.Systems.ProjectSystem.RecordFolderPath + "/Record - " + _recordId + AppManager.Systems.ProjectSystem.OtherFileSuffix; string _recordJsonText; RecordBaseData _recordBaseData = null; RecordData _newRecordData = null; try { //读取[Record]的Json文本中的内容 _recordJsonText = File.ReadAllText(_recordFilePath); //然后把Json文本解析成RecordBaseData对象 _recordBaseData = JsonMapper.ToObject <RecordBaseData>(_recordJsonText); //把RecordBaseData对象,转化为RecordData对象(取到新的Record数据) _newRecordData = RecordBaseData.BaseDataToData(_recordBaseData); } catch (Exception e) { } /* 对比2个Record的修改 */ //如果是添加一个Record,或者是修改一个Record,或者是IsDelete为true的删除Record if ((_oldRecordData != null && _newRecordData != null) || (_oldRecordData == null && _newRecordData != null)) { //如果新的Record是完整的 if (RecordData.VerifyIntegrity(_newRecordData)) { //如果旧的Record和新的Record有区别 if (RecordData.Compare(CompareType.All, _oldRecordData, _newRecordData) == false) { //读取这个Record文件 AppManager.Systems.RecordSystem.LoadRecord(ModeType, _recordId); //获取Record的更改 ChangeType _changeType = CompareOldRecordAndNewRecord(_oldRecordData, _newRecordData); //获取Log string _log = GenerateRecordSyncLogText(_oldRecordData, _newRecordData, _changeType); //把改变的类型加入到字典中 _changeTypes.Add(_recordId, _changeType); //把_log加入到_logs中 if (_log != null && _log != "") { _logs.Add(_log); } } } } //如果是直接删除文件的删除Record else if (_oldRecordData != null && _newRecordData == null) { //判断Record文件是否存在 FileInfo _recordFileInfo = new FileInfo(_recordFilePath); //如果Record文件已被删除 if (_recordFileInfo.Exists == false) { //标记为已删除 AppManager.Systems.RecordSystem.GetRecordData(_recordId).IsDelete = true; //获取Record的更改 ChangeType _changeType = CompareOldRecordAndNewRecord(_oldRecordData, _newRecordData); //获取Log string _log = GenerateRecordSyncLogText(_oldRecordData, _newRecordData, _changeType); //把改变的类型加入到字典中 _changeTypes.Add(_recordId, _changeType); //把_log加入到_logs中 if (_log != null && _log != "") { _logs.Add(_log); } } } } }