/// <summary> /// 更新账目记录 /// </summary> /// <param name="record">要更新的账目记录</param> /// <param name="oldDate">更新前的账目记录副本</param> public void Update(Record newRecord, Record oldRecord) { if (newRecord.Date < Config.StartDate) { Config.StartDate = newRecord.Date; } if (newRecord.Date.Year == oldRecord.Date.Year && newRecord.Date.Month == oldRecord.Date.Month) { XmlOperate op = new XmlOperate(System.Environment.CurrentDirectory + "\\Data\\" + newRecord.Date.ToString(@"yyyy/MM") + ".xml", XmlFileOpType.创建, newRecord.Date); XmlDocument doc = op.Get(); XmlElement data = (XmlElement)doc.DocumentElement.SelectSingleNode("/datas/data[@id=" + newRecord.Id.ToString() + "]"); data.RemoveAll(); data.SetAttribute("id", newRecord.Id.ToString()); XmlElement date = doc.CreateElement("date"); XmlText datetx = doc.CreateTextNode(newRecord.Date.ToString("yyyy-MM-dd")); date.AppendChild(datetx); XmlElement spend = doc.CreateElement("spend"); XmlText spendtx = doc.CreateTextNode(newRecord.Spend.ToString("n")); spend.AppendChild(spendtx); XmlElement income = doc.CreateElement("income"); XmlText incometx = doc.CreateTextNode(newRecord.Income.ToString("n")); income.AppendChild(incometx); XmlElement type = doc.CreateElement("type"); XmlText typetx = doc.CreateTextNode(newRecord.Type.Id.ToString()); type.AppendChild(typetx); XmlElement description = doc.CreateElement("description"); XmlCDataSection descriptiontx = doc.CreateCDataSection(newRecord.Description); description.AppendChild(descriptiontx); data.AppendChild(date); data.AppendChild(spend); data.AppendChild(income); data.AppendChild(type); data.AppendChild(description); op.Save(); Config.CurrentBalance = Config.CurrentBalance - oldRecord.Income + oldRecord.Spend + newRecord.Income - newRecord.Spend; } else { Delete(oldRecord); Create(newRecord); } }
/// <summary> /// 删除账目记录 /// </summary> /// <param name="record">要删除的账目记录</param> public void Delete(Record record) { XmlOperate op = new XmlOperate(System.Environment.CurrentDirectory + "\\Data\\" + record.Date.ToString(@"yyyy/MM") + ".xml", XmlFileOpType.创建, record.Date); XmlDocument doc = op.Get(); XmlNode xn = doc.DocumentElement.SelectSingleNode("/datas/data[@id=" + record.Id.ToString() + "]"); if (xn != null) { doc.DocumentElement.RemoveChild(xn); op.Save(); } Config.CurrentBalance = Config.CurrentBalance + record.Spend - record.Income; }
/// <summary> /// 添加记录 /// </summary> /// <param name="record">要添加的账目记录</param> public void Create(Record record) { DirectoryInfo rd = new DirectoryInfo(System.Environment.CurrentDirectory + "\\Data\\" + record.Date.ToString("yyyy")); if (!rd.Exists) { rd.Create(); } XmlOperate op = new XmlOperate(System.Environment.CurrentDirectory + "\\Data\\" + record.Date.ToString(@"yyyy/MM") + ".xml", XmlFileOpType.存在则创建, record.Date); XmlDocument doc = op.Get(); record.Id = ++Config.RecordCount; Config.RecordCount = record.Id; if (record.Date < Config.StartDate) { Config.StartDate = record.Date; } { XmlElement data = doc.CreateElement("data"); data.SetAttribute("id", record.Id.ToString()); XmlElement date = doc.CreateElement("date"); XmlText datetx = doc.CreateTextNode(record.Date.ToString("yyyy-MM-dd")); date.AppendChild(datetx); XmlElement spend = doc.CreateElement("spend"); XmlText spendtx = doc.CreateTextNode(record.Spend.ToString("n")); spend.AppendChild(spendtx); XmlElement income = doc.CreateElement("income"); XmlText incometx = doc.CreateTextNode(record.Income.ToString("n")); income.AppendChild(incometx); XmlElement type = doc.CreateElement("type"); XmlText typetx = doc.CreateTextNode(record.Type == null ? "0" : record.Type.Id.ToString()); type.AppendChild(typetx); XmlElement description = doc.CreateElement("description"); XmlCDataSection descriptiontx = doc.CreateCDataSection(record.Description); description.AppendChild(descriptiontx); data.AppendChild(date); data.AppendChild(spend); data.AppendChild(income); data.AppendChild(type); data.AppendChild(description); XmlNode xn = doc.DocumentElement.SelectSingleNode("/datas/data/date[substring(text(),9,2)>'" + record.Date.ToString("dd") + "']"); if (xn != null) { doc.DocumentElement.InsertBefore(data, xn.ParentNode); } else { doc.DocumentElement.AppendChild(data); } } op.Save(); Config.CurrentBalance = Config.CurrentBalance - record.Spend + record.Income; }