/// <summary> /// 新章节. /// </summary> /// <param name="chapter"></param> /// <param name="lineTextArray"></param> /// <returns></returns> public bool NewChapter(Chapter chapter, string[] lineTextArray) { try { using (MyTranslateContext context = new MyTranslateContext()) { Chapter oldChapter = context.Chapters.Find(chapter.ChapterCode); if (oldChapter != null) { ResultMessage = String.Format("章节代码 {0} 已存在!", chapter.ChapterCode); return(false); } chapter.IsActive = true; chapter.BeforeInsertOperation(); context.Chapters.Add(chapter); List <Line> lineList = BuildSourceLineList(lineTextArray); foreach (Line line in lineList) { line.ChapterCode = chapter.ChapterCode; if (line.IsBlank) { // 空行不需后续翻译操作. line.MachineText = ""; line.TranslateText = ""; line.GotoDone(); } else { line.GotoWait(); } line.BeforeInsertOperation(); context.Lines.Add(line); } context.SaveChanges(); return(true); } } catch (Exception ex) { ResultMessage = ex.Message; return(false); } }
/// <summary> /// 新增书籍数据. /// </summary> /// <param name="book"></param> /// <returns></returns> public bool NewBook(Book book) { try { using (MyTranslateContext context = new MyTranslateContext()) { Book oldBook = context.Books.Find(book.BookCode); if (oldBook != null) { ResultMessage = String.Format("书籍代码 {0} 已存在!", book.BookCode); return(false); } book.IsActive = true; book.BeforeInsertOperation(); context.Books.Add(book); // 同时, 新增 “名称” 章节. Chapter nameChapter = new Chapter() { // 书代码. BookCode = book.BookCode, // 章节代码. ChapterCode = book.BookCode + "/NAMES", ChapterName = "命名", }; nameChapter.IsActive = true; nameChapter.BeforeInsertOperation(); context.Chapters.Add(nameChapter); context.SaveChanges(); return(true); } } catch (Exception ex) { ResultMessage = ex.Message; return(false); } }
/// <summary> /// 新增或更新章节. /// </summary> /// <param name="chapter"></param> /// <returns></returns> public bool InsertOrUpdate(Chapter chapter) { try { using (MyTranslateContext context = new MyTranslateContext()) { // 先判断 书籍是否存在. Book book = context.Books.Find(chapter.BookCode); if (book == null) { ResultMessage = String.Format("未检索到代码为{0}的书籍!", chapter.BookCode); return(false); } // 先判断是 新增还是更新. Chapter dbChapter = context.Chapters.Include("Lines").FirstOrDefault(p => p.ChapterCode == chapter.ChapterCode); if (dbChapter == null) { // 新增逻辑. // 新增前处理. chapter.BeforeInsertOperation(); foreach (Line line in chapter.Lines) { // 新增前处理. line.BeforeInsertOperation(); } context.Chapters.Add(chapter); } else { // 更新逻辑. // 名称. dbChapter.ChapterName = chapter.ChapterName; dbChapter.ChapterTranslateName = chapter.ChapterTranslateName; List <Line> newLineList = new List <Line>(); // 行. foreach (Line line in chapter.Lines) { Line dbLine = null; if (chapter.ChapterCode.Contains("NAMES")) { // 命名章节. // 按照 原始文本进行匹配. dbLine = dbChapter.Lines.FirstOrDefault(p => p.SourceText == line.SourceText); } else { // 普通章节. // 按照 行号匹配. dbLine = dbChapter.Lines.FirstOrDefault(p => p.LineNumber == line.LineNumber); } if (dbLine == null) { // 新增 newLineList.Add(line); } else { // 更新. dbLine.SourceText = line.SourceText; dbLine.MachineText = line.MachineText; dbLine.TranslateText = line.TranslateText; dbLine.Status = line.Status; dbLine.BeforeUpdateOperation(); } } if (newLineList.Count > 0) { foreach (Line newLine in newLineList) { // 新增的,需要重置 流水ID. newLine.LineID = 0; newLine.Chapter = null; newLine.CreateUser = null; newLine.BeforeInsertOperation(); dbChapter.Lines.Add(newLine); } } } // 物理保存. context.SaveChanges(); return(true); } } catch (Exception ex) { ResultMessage = ex.Message; return(false); } }