Copy 拷贝书籍项目 1. 创建新的书籍,设置书籍的父级id为被拷贝的项目,并同步数据信息 2. 迁移章节内容,包括 md_documents 和 md_document_store 3. 替换内容中的书籍标识 4. 迁移书籍相关的图片等资源文件
(sourceBookIdentify string)
| 746 | // 3. 替换内容中的书籍标识 |
| 747 | // 4. 迁移书籍相关的图片等资源文件 |
| 748 | func (m *Book) Copy(sourceBookIdentify string) (err error) { |
| 749 | var ( |
| 750 | sourceBook Book |
| 751 | sourceDocs []Document |
| 752 | sourceDocStores []DocumentStore |
| 753 | existBook Book |
| 754 | sourceDocId []interface{} |
| 755 | docMap = make(map[int]int) // map[old_doc_id]new_doc_id |
| 756 | ) |
| 757 | o := orm.NewOrm() |
| 758 | o.Begin() |
| 759 | defer func() { |
| 760 | if err == nil { |
| 761 | o.Commit() |
| 762 | m.ResetDocumentNumber(m.BookId) |
| 763 | } else { |
| 764 | o.Rollback() |
| 765 | } |
| 766 | }() |
| 767 | |
| 768 | o.QueryTable(m).Filter("identify", sourceBookIdentify).One(&sourceBook, "book_id") |
| 769 | if sourceBook.BookId <= 0 { |
| 770 | return errors.New("拷贝的书籍不存在") |
| 771 | } |
| 772 | |
| 773 | o.QueryTable(m).Filter("identify", m.Identify).One(&existBook, "book_id") |
| 774 | if existBook.BookId > 0 { |
| 775 | return errors.New("已存在相同标识的书籍,请更换书籍标识") |
| 776 | } |
| 777 | |
| 778 | if m.Cover != "" { |
| 779 | newCover := strings.ReplaceAll(filepath.Join(filepath.Dir(m.Cover), fmt.Sprintf("cover-%v%v", m.Identify, filepath.Ext(m.Cover))), "\\", "/") |
| 780 | if utils.StoreType == utils.StoreOss { |
| 781 | store.ModelStoreOss.CopyFile(m.Cover, newCover) |
| 782 | } else { |
| 783 | utils.CopyFile(newCover, m.Cover) |
| 784 | } |
| 785 | m.Cover = newCover |
| 786 | } |
| 787 | |
| 788 | m.ParentId = sourceBook.BookId |
| 789 | if _, err = o.Insert(m); err != nil { |
| 790 | beego.Error(err) |
| 791 | return errors.New("新建书籍失败:" + err.Error()) |
| 792 | } |
| 793 | |
| 794 | relationship := NewRelationship() |
| 795 | relationship.BookId = m.BookId |
| 796 | relationship.RoleId = 0 |
| 797 | relationship.MemberId = m.MemberId |
| 798 | |
| 799 | if _, err = o.Insert(relationship); err != nil { |
| 800 | beego.Error("插入项目与用户关联 => ", err) |
| 801 | return |
| 802 | } |
| 803 | |
| 804 | document := NewDocument() |
| 805 |
no test coverage detected