插入和更新文档. 存在文档id或者文档标识,则表示更新文档内容
(cols ...string)
| 129 | //插入和更新文档. |
| 130 | //存在文档id或者文档标识,则表示更新文档内容 |
| 131 | func (m *Document) InsertOrUpdate(cols ...string) (id int64, err error) { |
| 132 | o := orm.NewOrm() |
| 133 | id = int64(m.DocumentId) |
| 134 | |
| 135 | m.DocumentName = strings.TrimSpace(m.DocumentName) |
| 136 | if m.DocumentId > 0 { //文档id存在,则更新 |
| 137 | _, err = o.Update(m, cols...) |
| 138 | return |
| 139 | } |
| 140 | |
| 141 | var mm Document |
| 142 | //直接查询一个字段,优化MySQL IO |
| 143 | o.QueryTable("md_documents").Filter("identify", m.Identify).Filter("book_id", m.BookId).One(&mm, "document_id") |
| 144 | if mm.DocumentId == 0 { |
| 145 | m.CreateTime = time.Now() |
| 146 | m.ModifyTime = m.CreateTime |
| 147 | id, err = o.Insert(m) |
| 148 | NewBook().ResetDocumentNumber(m.BookId) |
| 149 | } else { //identify存在,则执行更新 |
| 150 | _, err = o.Update(m) |
| 151 | id = int64(mm.DocumentId) |
| 152 | } |
| 153 | return |
| 154 | } |
| 155 | |
| 156 | //根据指定字段查询一条文档. |
| 157 | func (m *Document) FindByFieldFirst(field string, v interface{}) (*Document, error) { |
no test coverage detected