예제 #1
0
        /// <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);
            }
        }
예제 #2
0
        /// <summary>
        /// 新增一行.
        /// </summary>
        /// <param name="line"></param>
        /// <returns></returns>
        public bool NewOneLine(Line line)
        {
            try
            {
                using (MyTranslateContext context = new MyTranslateContext())
                {
                    line.BeforeInsertOperation();


                    context.Lines.Add(line);


                    // 物理保存.
                    context.SaveChanges();

                    return(true);
                }
            }
            catch (Exception ex)
            {
                ResultMessage = ex.Message;

                return(false);
            }
        }
        /// <summary>
        /// 插入或更新 自动替换数据.
        /// </summary>
        /// <param name="autoReplaceData"></param>
        /// <returns></returns>
        public bool InsertOrUpdateAutoReplace(AutoReplace autoReplaceData)
        {
            try
            {
                if (String.IsNullOrEmpty(autoReplaceData.SourceText))
                {
                    ResultMessage = "原始文本不能为空!";
                    return(false);
                }
                if (String.IsNullOrEmpty(autoReplaceData.MachineText))
                {
                    ResultMessage = "机翻文本不能为空!";
                    return(false);
                }
                if (String.IsNullOrEmpty(autoReplaceData.TranslateText))
                {
                    ResultMessage = "结果文本不能为空!";
                    return(false);
                }



                using (MyTranslateContext context = new MyTranslateContext())
                {
                    AutoReplace dbData = context.AutoReplaces.FirstOrDefault(p => p.SourceText == autoReplaceData.SourceText);

                    if (dbData == null)
                    {
                        // 数据库中不存在.
                        // 新增.
                        autoReplaceData.BeforeInsertOperation();

                        // 插入.
                        context.AutoReplaces.Add(autoReplaceData);
                    }
                    else
                    {
                        dbData.MachineText   = autoReplaceData.MachineText;
                        dbData.TranslateText = autoReplaceData.TranslateText;
                        dbData.Status        = autoReplaceData.Status;

                        dbData.BeforeUpdateOperation();
                    }


                    // 物理保存.
                    context.SaveChanges();
                }


                return(true);
            }
            catch (Exception ex)
            {
                ResultMessage = ex.Message;
                return(false);
            }
        }
예제 #4
0
        /// <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);
            }
        }
예제 #5
0
        /// <summary>
        /// 删除章节.
        /// </summary>
        /// <param name="chapter"></param>
        /// <returns></returns>
        public bool Delete(Chapter chapter)
        {
            if (chapter.ChapterCode.Contains("NAMES"))
            {
                ResultMessage = "命名章节不能删除";
                return(false);
            }

            try
            {
                using (MyTranslateContext context = new MyTranslateContext())
                {
                    // 先判断 数据是否存在.

                    Chapter dbChapter = context.Chapters.Include("Lines").FirstOrDefault(p => p.ChapterCode == chapter.ChapterCode);

                    if (dbChapter == null)
                    {
                        // 数据 不存在.
                        ResultMessage = String.Format("未能检索到代码为 {0} 的章节", chapter.ChapterCode);
                        return(false);
                    }
                    else
                    {
                        // 删除逻辑.

                        // 首先删除该章节下面的 行.
                        context.Lines.RemoveRange(dbChapter.Lines);

                        // 删除章节.
                        context.Chapters.Remove(dbChapter);
                    }

                    // 物理保存.
                    context.SaveChanges();

                    return(true);
                }
            }
            catch (Exception ex)
            {
                ResultMessage = ex.Message;

                return(false);
            }
        }
예제 #6
0
        /// <summary>
        /// 更新一行.
        /// </summary>
        /// <param name="line"></param>
        /// <returns></returns>
        public bool UpdateOneLine(Line line)
        {
            try
            {
                using (MyTranslateContext context = new MyTranslateContext())
                {
                    Line oldLine = context.Lines.Find(line.LineID);

                    if (oldLine == null)
                    {
                        ResultMessage = "数据不存在!!!";

                        return(false);
                    }


                    // 只更新 译文 与 状态.
                    oldLine.TranslateText = line.TranslateText;
                    oldLine.Status        = line.Status;


                    // 如果是名称行。 额外更新.
                    if (line.ChapterCode.Contains("NAMES") && !String.IsNullOrEmpty(line.MachineText))
                    {
                        oldLine.MachineText = line.MachineText;
                    }


                    oldLine.BeforeUpdateOperation();


                    // 物理保存.
                    context.SaveChanges();

                    return(true);
                }
            }
            catch (Exception ex)
            {
                ResultMessage = ex.Message;

                return(false);
            }
        }
        /// <summary>
        /// 新增一行.
        /// </summary>
        /// <param name="line"></param>
        /// <returns></returns>
        public bool NewOneLine(Line line)
        {
            try
            {
                using (MyTranslateContext context = new MyTranslateContext())
                {
                    // 名称需要先去除空格.
                    line.SourceText = line.SourceText.Trim();

                    // 对于名称, 需要先判断,  名称是否已存在
                    Line dbLine = context.Lines.FirstOrDefault(p => p.ChapterCode == line.ChapterCode && p.SourceText == line.SourceText);

                    if (dbLine != null)
                    {
                        ResultMessage = String.Format("名词 {0} 已存在!", line.SourceText);

                        return(false);
                    }



                    line.BeforeInsertOperation();


                    context.Lines.Add(line);


                    // 物理保存.
                    context.SaveChanges();

                    return(true);
                }
            }
            catch (Exception ex)
            {
                ResultMessage = ex.Message;

                return(false);
            }
        }
예제 #8
0
        /// <summary>
        /// 更新书籍数据.
        /// </summary>
        /// <param name="book"></param>
        /// <returns></returns>
        public bool UpdateBook(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);
                    }

                    // 书名.
                    oldBook.BookName = book.BookName;

                    // Url 地址.
                    oldBook.BookUrl = book.BookUrl;

                    // 有效性.
                    oldBook.IsActive = book.IsActive;


                    // 更新前处理.
                    oldBook.BeforeUpdateOperation();


                    // 物理保存.
                    context.SaveChanges();

                    return(true);
                }
            }
            catch (Exception ex)
            {
                ResultMessage = ex.Message;

                return(false);
            }
        }
        /// <summary>
        /// 更新一行.
        /// </summary>
        /// <param name="line"></param>
        /// <returns></returns>
        public bool UpdateOneLine(Line line)
        {
            try
            {
                using (MyTranslateContext context = new MyTranslateContext())
                {
                    Line oldLine = context.Lines.Find(line.LineID);

                    if (oldLine == null)
                    {
                        ResultMessage = "数据不存在!!!";

                        return(false);
                    }


                    // 只更新 译文 与 状态.
                    oldLine.TranslateText = line.TranslateText;
                    oldLine.Status        = line.Status;


                    oldLine.BeforeUpdateOperation();


                    // 物理保存.
                    context.SaveChanges();

                    return(true);
                }
            }
            catch (Exception ex)
            {
                ResultMessage = ex.Message;

                return(false);
            }
        }
예제 #10
0
        /// <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);
            }
        }
예제 #11
0
        /// <summary>
        /// 机翻.
        /// </summary>
        /// <param name="chapterCode"></param>
        /// <param name="lineTextArray"></param>
        /// <returns></returns>
        public bool AutoTrans(string chapterCode, string[] lineTextArray)
        {
            try
            {
                using (MyTranslateContext context = new MyTranslateContext())
                {
                    Chapter oldChapter = context.Chapters.Find(chapterCode);
                    if (oldChapter == null)
                    {
                        ResultMessage = String.Format("章节代码 {0} 不存在!", chapterCode);
                        return(false);
                    }


                    // 取得 行信息.
                    List <Line> lineList = oldChapter.Lines.OrderBy(p => p.LineNumber).ToList();


                    // 取得机翻行.
                    List <Line> transLineList = BuildAutoTransLineList(lineTextArray);


                    // 取得自动替换信息.
                    List <AutoReplace> autoReplaceList = context.AutoReplaces.Where(p => p.Status == AutoReplace.STATUS_IS_ACTIVE).ToList();


                    foreach (Line line in lineList)
                    {
                        // 空行不需要翻译.
                        if (line.IsBlank)
                        {
                            continue;
                        }

                        // 检索机翻行.
                        Line transLine = transLineList.FirstOrDefault(p => p.SourceText == line.SourceText);

                        // 找到机翻行.
                        if (transLine != null)
                        {
                            // 更新.
                            line.MachineText = transLine.MachineText;


                            if (String.IsNullOrEmpty(line.TranslateText))
                            {
                                // 为非空的情况下, 提前设置.
                                line.TranslateText = line.MachineText;
                            }


                            // 检测是否存在 自动替换的标志.
                            foreach (var replaceData in autoReplaceList)
                            {
                                if (line.SourceText.Contains(replaceData.SourceText) &&
                                    line.TranslateText.Contains(replaceData.MachineText))
                                {
                                    line.TranslateText = line.TranslateText.Replace(replaceData.MachineText, replaceData.TranslateText);
                                }
                            }

                            // 机翻标志.
                            line.GotoMachine();
                        }
                    }

                    // 物理保存.
                    context.SaveChanges();

                    return(true);
                }
            }
            catch (Exception ex)
            {
                ResultMessage = ex.Message;

                return(false);
            }
        }
예제 #12
0
        /// <summary>
        /// 新增或更新 书籍与章节..
        /// </summary>
        /// <param name="book"></param>
        /// <param name="replaceAll"></param>
        /// <returns></returns>
        public bool InsertOrUpdateBookAndChapter(Book book, bool replaceAll = true)
        {
            try
            {
                using (MyTranslateContext context = new MyTranslateContext())
                {
                    Book oldBook = context.Books.Find(book.BookCode);
                    if (oldBook == null)
                    {
                        // 数据库中,数据不存在.

                        // 新增逻辑.


                        // 新增前处理.
                        book.BeforeInsertOperation();
                        foreach (Chapter chapter in book.Chapters)
                        {
                            // 新增前处理.
                            chapter.BeforeInsertOperation();

                            foreach (Line line in chapter.Lines)
                            {
                                // 新增前处理.
                                line.BeforeInsertOperation();
                            }
                        }


                        context.Books.Add(book);
                    }
                    else
                    {
                        // 数据库中, 数据已存在,

                        // 更新逻辑.

                        // 书名.
                        oldBook.BookName = book.BookName;
                        // Url 地址.
                        oldBook.BookUrl = book.BookUrl;
                        // 有效性.
                        oldBook.IsActive = book.IsActive;

                        // 更新前处理.
                        oldBook.BeforeUpdateOperation();



                        foreach (Chapter chapter in book.Chapters)
                        {
                            // 检查数据库中, 是否存在指定章节.
                            Chapter dbChapter = context.Chapters.Include("Lines").FirstOrDefault(p => p.ChapterCode == chapter.ChapterCode);

                            if (dbChapter != null && !replaceAll)
                            {
                                // 数据库中, 已存在章节。
                                // 但是本次操作, 是只更新。
                                // 那么 本章节则忽略.
                                continue;
                            }



                            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;

                                dbChapter.BeforeUpdateOperation();


                                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);
            }
        }