实现项目复制功能

pull/358/head
lifei6671 2018-07-10 18:53:41 +08:00
parent 483df1f67b
commit c5fb0c760b
7 changed files with 181 additions and 24 deletions

View File

@ -415,13 +415,13 @@ func (c *BookController) Users() {
func (c *BookController) Create() {
if c.Ctx.Input.IsPost() {
book_name := strings.TrimSpace(c.GetString("book_name", ""))
bookName := strings.TrimSpace(c.GetString("book_name", ""))
identify := strings.TrimSpace(c.GetString("identify", ""))
description := strings.TrimSpace(c.GetString("description", ""))
privatelyOwned, _ := strconv.Atoi(c.GetString("privately_owned"))
comment_status := c.GetString("comment_status")
commentStatus := c.GetString("comment_status")
if book_name == "" {
if bookName == "" {
c.JsonResult(6001, "项目名称不能为空")
}
if identify == "" {
@ -439,8 +439,8 @@ func (c *BookController) Create() {
if privatelyOwned != 0 && privatelyOwned != 1 {
privatelyOwned = 1
}
if comment_status != "open" && comment_status != "closed" && comment_status != "group_only" && comment_status != "registered_only" {
comment_status = "closed"
if commentStatus != "open" && commentStatus != "closed" && commentStatus != "group_only" && commentStatus != "registered_only" {
commentStatus = "closed"
}
book := models.NewBook()
book.Cover = conf.GetDefaultCover()
@ -477,11 +477,11 @@ func (c *BookController) Create() {
c.JsonResult(6006, "项目标识已存在")
}
book.BookName = book_name
book.BookName = bookName
book.Description = description
book.CommentCount = 0
book.PrivatelyOwned = privatelyOwned
book.CommentStatus = comment_status
book.CommentStatus = commentStatus
book.Identify = identify
book.DocCount = 0
book.MemberId = c.Member.MemberId
@ -509,6 +509,26 @@ func (c *BookController) Create() {
}
c.JsonResult(6001, "error")
}
//复制项目
func (c *BookController) Copy(){
if c.Ctx.Input.IsPost() {
identify := strings.TrimSpace(c.GetString("identify", ""))
if identify == "" {
c.JsonResult(6001,"参数错误")
}
book := models.NewBook()
err := book.Copy(identify)
if err != nil {
c.JsonResult(6002,"复制项目出错")
}else{
bookResult, err := models.NewBookResult().FindByIdentify(book.Identify, c.Member.MemberId)
if err != nil {
beego.Error("查询失败")
}
c.JsonResult(0,"ok",bookResult)
}
}
}
//导入zip压缩包
func (c *BookController) Import() {

View File

@ -159,6 +159,112 @@ func (book *Book) Update(cols ...string) error {
return err
}
//复制项目
func (book *Book) Copy(identify string) error {
o := orm.NewOrm()
err := o.QueryTable(book.TableNameWithPrefix()).Filter("identify",identify).One(book)
if err != nil {
beego.Error("查询项目时出错 -> ",err)
return err
}
if err := o.Begin();err != nil {
beego.Error("开启事物时出错 -> ",err)
return err
}
bookId := book.BookId
book.BookId = 0
book.Identify = book.Identify + fmt.Sprintf("%s-%s",identify,strconv.FormatInt(time.Now().UnixNano(), 32))
book.BookName = book.BookName + "[副本]"
book.CreateTime = time.Now()
book.CommentCount = 0
book.HistoryCount = 0
if _,err := o.Insert(book);err != nil {
beego.Error("复制项目时出错 -> ",err)
o.Rollback()
return err
}
var rels []*Relationship
if _,err := o.QueryTable(NewRelationship().TableNameWithPrefix()).Filter("book_id",bookId).All(&rels); err != nil {
beego.Error("复制项目关系时出错 -> ",err)
o.Rollback()
return err
}
for _,rel := range rels {
rel.BookId = book.BookId
rel.RelationshipId = 0
if _,err := o.Insert(rel);err != nil {
beego.Error("复制项目关系时出错 -> ",err)
o.Rollback()
return err
}
}
var docs []*Document
if _,err := o.QueryTable(NewDocument().TableNameWithPrefix()).Filter("book_id",bookId).Filter("parent_id",0).All(&docs);err != nil && err != orm.ErrNoRows {
beego.Error("读取项目文档时出错 -> ",err)
o.Rollback()
return err
}
if len(docs) > 0 {
if err := recursiveInsertDocument(docs,o,book.BookId,0);err != nil {
beego.Error("复制项目时出错 -> ",err)
o.Rollback()
return err
}
}
return o.Commit()
}
//递归的复制文档
func recursiveInsertDocument(docs []*Document,o orm.Ormer,bookId int,parentId int) error {
for _,doc := range docs {
docId := doc.DocumentId
doc.DocumentId = 0
doc.ParentId = parentId
doc.BookId = bookId
doc.Version = time.Now().Unix()
if _,err := o.Insert(doc);err != nil {
beego.Error("插入项目时出错 -> ",err)
return err
}
var attachList []*Attachment
//读取所有附件列表
if _,err := o.QueryTable(NewAttachment().TableNameWithPrefix()).Filter("document_id",docId).All(&attachList); err == nil {
for _,attach := range attachList {
attach.BookId = bookId
attach.DocumentId = doc.DocumentId
attach.AttachmentId = 0
if _,err := o.Insert(attach);err != nil {
return err
}
}
}
var subDocs []*Document
if _,err := o.QueryTable(NewDocument().TableNameWithPrefix()).Filter("parent_id",docId).All(&subDocs);err != nil && err != orm.ErrNoRows {
beego.Error("读取文档时出错 -> ",err)
return err
}
if len(subDocs) > 0{
if err := recursiveInsertDocument(subDocs,o,bookId,doc.DocumentId);err != nil {
return err
}
}
}
return nil
}
//根据指定字段查询结果集.
func (book *Book) FindByField(field string, value interface{},cols ...string) ([]*Book, error) {
o := orm.NewOrm()
@ -465,6 +571,7 @@ func (book *Book) ResetDocumentNumber(bookId int) {
}
}
//导入项目
func (book *Book) ImportBook(zipPath string) error {
if !filetil.FileExists(zipPath) {
return errors.New("文件不存在 => " + zipPath)

View File

@ -96,6 +96,7 @@ func (m *Document) InsertOrUpdate(cols ...string) error {
m.Identify = fmt.Sprintf("%s-%s",identify,strconv.FormatInt(time.Now().UnixNano(), 32))
}
if m.OrderSort == 0{
sort,_ := o.QueryTable(m.TableNameWithPrefix()).Filter("book_id",m.BookId).Filter("parent_id",m.ParentId).Count()
m.OrderSort = int(sort) + 1

View File

@ -25,11 +25,13 @@ func init() {
beego.Router("/manager/books", &controllers.ManagerController{}, "*:Books")
beego.Router("/manager/books/edit/:key", &controllers.ManagerController{}, "*:EditBook")
beego.Router("/manager/books/delete", &controllers.ManagerController{}, "*:DeleteBook")
beego.Router("/manager/comments", &controllers.ManagerController{}, "*:Comments")
beego.Router("/manager/books/token", &controllers.ManagerController{}, "post:CreateToken")
beego.Router("/manager/setting", &controllers.ManagerController{}, "*:Setting")
beego.Router("/manager/books/token", &controllers.ManagerController{}, "post:CreateToken")
beego.Router("/manager/books/transfer", &controllers.ManagerController{}, "post:Transfer")
beego.Router("/manager/books/open", &controllers.ManagerController{}, "post:PrivatelyOwned")
beego.Router("/manager/attach/list", &controllers.ManagerController{}, "*:AttachList")
beego.Router("/manager/attach/detailed/:id", &controllers.ManagerController{}, "*:AttachDetailed")
beego.Router("/manager/attach/delete", &controllers.ManagerController{}, "post:AttachDelete")
@ -52,6 +54,7 @@ func init() {
beego.Router("/book/users/change", &controllers.BookMemberController{}, "post:ChangeRole")
beego.Router("/book/users/delete", &controllers.BookMemberController{}, "post:RemoveMember")
beego.Router("/book/users/import", &controllers.BookController{},"post:Import")
beego.Router("/book/users/copy", &controllers.BookController{},"post:Copy")
beego.Router("/book/setting/save", &controllers.BookController{}, "post:SaveBook")
beego.Router("/book/setting/open", &controllers.BookController{}, "post:PrivatelyOwned")

View File

@ -74,6 +74,7 @@
</template>
<template v-if="item.role_id == 0">
<li><a :href="'javascript:deleteBook(\''+item.identify+'\');'">删除</a></li>
<li><a :href="'javascript:copyBook(\''+item.identify+'\');'">复制</a></li>
</template>
</ul>
@ -269,6 +270,7 @@
<script src="{{cdnjs "/static/js/jquery.form.js"}}" type="text/javascript"></script>
<script src="{{cdnjs "/static/bootstrap/plugins/bootstrap-fileinput/4.4.7/js/fileinput.min.js"}}"></script>
<script src="{{cdnjs "/static/bootstrap/plugins/bootstrap-fileinput/4.4.7/js/locales/zh.js"}}"></script>
<script src="{{cdnjs "/static/layer/layer.js"}}" type="text/javascript" ></script>
<script src="{{cdnjs "/static/js/main.js"}}" type="text/javascript"></script>
<script type="text/javascript">
/**
@ -364,6 +366,28 @@
$("#deleteBookModal").find("input[name='identify']").val($id);
$("#deleteBookModal").modal("show");
}
function copyBook($id){
var index = layer.load()
$.ajax({
url : "{{urlfor "BookController.Copy"}}" ,
data : {"identify":$id},
type : "POST",
dataType : "json",
success : function ($res) {
layer.close(index);
if ($res.errcode === 0) {
window.app.lists.splice(0, 0, $res.data);
$("#addBookDialogModal").modal("hide");
} else {
layer.msg($res.message);
}
},
error : function () {
layer.close(index);
layer.msg('服务器异常');
}
});
}
$(function () {
$("#addBookDialogModal").on("show.bs.modal",function () {

View File

@ -41,25 +41,27 @@
<div class="manual-list">
{{range $index,$item := .Lists}}
<div class="list-item">
<dl class="manual-item-standard">
<dt>
<a href="{{urlfor "DocumentController.Index" ":key" $item.Identify}}" title="{{$item.BookName}}-{{$item.CreateName}}" target="_blank">
<img src="{{cdnimg $item.Cover}}" class="cover" alt="{{$item.BookName}}-{{$item.CreateName}}" onerror="this.src='{{cdnimg "static/images/book.jpg"}}';">
</a>
</dt>
<dd>
<a href="{{urlfor "DocumentController.Index" ":key" $item.Identify}}" class="name" title="{{$item.BookName}}-{{$item.CreateName}}" target="_blank">{{$item.BookName}}</a>
</dd>
<dd>
<div class="list-item">
<dl class="manual-item-standard">
<dt>
<a href="{{urlfor "DocumentController.Index" ":key" $item.Identify}}" title="{{$item.BookName}}-{{$item.CreateName}}" target="_blank">
<img src="{{cdnimg $item.Cover}}" class="cover" alt="{{$item.BookName}}-{{$item.CreateName}}" onerror="this.src='{{cdnimg "static/images/book.jpg"}}';">
</a>
</dt>
<dd>
<a href="{{urlfor "DocumentController.Index" ":key" $item.Identify}}" class="name" title="{{$item.BookName}}-{{$item.CreateName}}" target="_blank">{{$item.BookName}}</a>
</dd>
<dd>
<span class="author">
<b class="text">作者</b>
<b class="text">-</b>
<b class="text">{{if eq $item.RealName "" }}{{$item.CreateName}}{{else}}{{$item.RealName}}{{end}}</b>
</span>
</dd>
</dl>
</div>
</dd>
</dl>
</div>
{{else}}
<div class="text-center" style="height: 200px;margin: 100px;font-size: 28px;">暂无项目</div>
{{end}}
<div class="clearfix"></div>
</div>

View File

@ -1,11 +1,11 @@
<div class="footer">
<div class="container">
<div class="row text-center border-top">
<span><a href="https://www.iminho.me">MinDoc</a></span>
<span><a href="https://www.iminho.me" target="_blank">MinDoc</a></span>
<span>&nbsp;·&nbsp;</span>
<span><a href="https://github.com/lifei6671/mindoc/issues" target="_blank">意见反馈</a></span>
<span>&nbsp;·&nbsp;</span>
<span><a href="https://github.com/lifei6671/mindoc">Github</a></span>
<span><a href="https://github.com/lifei6671/mindoc" target="_blank">Github</a></span>
</div>
{{if ne .site_beian ""}}
<div class="row text-center">