2019-05-16 10:05:20 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"background/db"
|
|
|
|
"background/logs"
|
|
|
|
"fmt"
|
2021-02-05 17:46:59 +00:00
|
|
|
"strings"
|
2021-01-18 15:15:39 +00:00
|
|
|
"time"
|
2020-08-23 18:08:26 +00:00
|
|
|
|
2020-09-26 04:30:23 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-08-23 18:08:26 +00:00
|
|
|
"qiniupkg.com/x/log.v7"
|
2019-05-16 10:05:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Doc struct {
|
2023-09-26 16:32:29 +00:00
|
|
|
ID int64 `json:"id" gorm:"column:id" sql:"id"`
|
|
|
|
Title string `json:"title" gorm:"column:title" sql:"title"`
|
|
|
|
Type int32 `json:"type" gorm:"column:type" sql:"type"`
|
|
|
|
Content string `json:"content" gorm:"column:content" sql:"content"`
|
|
|
|
Author string `json:"author" gorm:"column:author" sql:"author"`
|
|
|
|
CreateTime time.Time `json:"create_time" gorm:"column:create_time" sql:"create_time"`
|
|
|
|
UpdateTime time.Time `json:"update_time" gorm:"column:update_time" sql:"update_time"`
|
|
|
|
DeleteTime time.Time `json:"delete_time" gorm:"column:delete_time" sql:"delete_time"`
|
|
|
|
Version int32 `json:"version" gorm:"column:version" sql:"version"`
|
|
|
|
IsPublic int32 `json:"is_public" gorm:"column:is_public" sql:"is_public"`
|
|
|
|
Deleted int32 `json:"deleted" gorm:"column:deleted" sql:"deleted"`
|
|
|
|
OriginUrl string `json:"origin_url" gorm:"column:origin_url" sql:"origin_url"`
|
|
|
|
EsId string `json:"es_id" gorm:"column:es_id" sql:"es_id"`
|
|
|
|
DeletedAt time.Time `json:"deleted_at" gorm:"column:deleted_at" sql:"deleted_at"`
|
|
|
|
Father int32 `json:"father" gorm:"column:father" sql:"father"`
|
|
|
|
Level int32 `json:"level" gorm:"column:level" sql:"level"`
|
2022-04-12 17:24:39 +00:00
|
|
|
}
|
2022-04-14 06:37:36 +00:00
|
|
|
|
|
|
|
type DocTree struct {
|
2023-09-26 16:32:29 +00:00
|
|
|
ID int64 `json:"id" gorm:"column:id" sql:"id"`
|
|
|
|
Title string `json:"title" gorm:"column:title" sql:"title"`
|
|
|
|
Type int32 `json:"type" gorm:"column:type" sql:"type"`
|
|
|
|
Content string `json:"content" gorm:"column:content" sql:"content"`
|
|
|
|
Author string `json:"author" gorm:"column:author" sql:"author"`
|
|
|
|
CreateTime time.Time `json:"create_time" gorm:"column:create_time" sql:"create_time"`
|
|
|
|
UpdateTime time.Time `json:"update_time" gorm:"column:update_time" sql:"update_time"`
|
|
|
|
DeleteTime time.Time `json:"delete_time" gorm:"column:delete_time" sql:"delete_time"`
|
|
|
|
Version int32 `json:"version" gorm:"column:version" sql:"version"`
|
|
|
|
IsPublic int32 `json:"is_public" gorm:"column:is_public" sql:"is_public"`
|
|
|
|
Deleted int32 `json:"deleted" gorm:"column:deleted" sql:"deleted"`
|
|
|
|
OriginUrl string `json:"origin_url" gorm:"column:origin_url" sql:"origin_url"`
|
|
|
|
EsId string `json:"es_id" gorm:"column:es_id" sql:"es_id"`
|
|
|
|
DeletedAt time.Time `json:"deleted_at" gorm:"column:deleted_at" sql:"deleted_at"`
|
|
|
|
Father int32 `json:"father" gorm:"column:father" sql:"father"`
|
|
|
|
Level int32 `json:"level" gorm:"column:level" sql:"level"`
|
2022-04-14 06:37:36 +00:00
|
|
|
}
|
2023-09-26 16:32:29 +00:00
|
|
|
|
2022-04-14 06:37:36 +00:00
|
|
|
// 设置User的表名为`profiles`
|
|
|
|
func (DocTree) TableName() string {
|
|
|
|
return "doc_copy1"
|
|
|
|
}
|
|
|
|
|
2023-09-26 16:32:29 +00:00
|
|
|
type DocType struct {
|
|
|
|
Id int32 `json:"id" gorm:"column:id" sql:"id"`
|
|
|
|
TypeName string `json:"type_name" gorm:"column:type_name" sql:"type_name"`
|
|
|
|
Group int32 `json:"group" gorm:"column:group" sql:"group"`
|
2019-05-17 09:45:50 +00:00
|
|
|
}
|
2023-09-26 16:32:29 +00:00
|
|
|
type DocHistory struct {
|
|
|
|
ID int32 `json:"id" gorm:"column:id" sql:"id"`
|
|
|
|
Title string `json:"title" gorm:"column:title" sql:"title"`
|
|
|
|
DocId int32 `json:"doc_id" gorm:"column:doc_id" sql:"doc_id"`
|
|
|
|
Content string `json:"content" gorm:"column:content" sql:"content"`
|
|
|
|
EditTime string `json:"edit_time" gorm:"column:edit_time" sql:"edit_time"`
|
2022-03-31 19:27:07 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 16:06:07 +00:00
|
|
|
type DocGroup struct {
|
2020-08-23 18:08:26 +00:00
|
|
|
Int int32 `sql:"int"`
|
2020-07-22 16:06:07 +00:00
|
|
|
Name string `sql:"name"`
|
|
|
|
}
|
2019-05-17 09:45:50 +00:00
|
|
|
type ArticleType struct {
|
2020-08-23 18:08:26 +00:00
|
|
|
Id int64 `sql:"id" json:"id"`
|
|
|
|
Name string `sql:"type_name" json:"type_name"`
|
|
|
|
Author string `sql:"author" json:"author"`
|
|
|
|
Group int32 `sql:"group" json:"group"`
|
2020-07-22 16:06:07 +00:00
|
|
|
GroupName string `json:"group_name"`
|
2019-05-17 09:45:50 +00:00
|
|
|
}
|
|
|
|
|
2019-06-30 14:59:00 +00:00
|
|
|
func GetArticlesType() []ArticleType {
|
2019-05-17 09:45:50 +00:00
|
|
|
ret := []ArticleType{}
|
2020-01-25 14:26:41 +00:00
|
|
|
sql := fmt.Sprintf("select * from doc_type")
|
2020-09-26 04:30:23 +00:00
|
|
|
e := db.GetMysqlClient().Query2(sql, &ret)
|
|
|
|
log.Print(ret)
|
|
|
|
|
2020-08-23 18:08:26 +00:00
|
|
|
for k, _ := range ret {
|
2020-07-22 16:06:07 +00:00
|
|
|
group := []DocGroup{}
|
2020-08-23 18:08:26 +00:00
|
|
|
sql = fmt.Sprintf("select * from doc_group where doc_group.int = %d", ret[k].Group)
|
2020-09-26 04:30:23 +00:00
|
|
|
db.GetMysqlClient().Query2(sql, &group)
|
2020-08-23 18:08:26 +00:00
|
|
|
if len(group) > 0 {
|
2020-07-22 16:06:07 +00:00
|
|
|
ret[k].GroupName = group[0].Name
|
|
|
|
}
|
|
|
|
}
|
2019-06-30 14:59:00 +00:00
|
|
|
if nil != e {
|
2019-05-17 09:45:50 +00:00
|
|
|
logs.Error(e.Error())
|
|
|
|
return nil
|
2019-05-16 10:05:20 +00:00
|
|
|
}
|
2019-05-17 09:45:50 +00:00
|
|
|
return ret
|
|
|
|
}
|
2021-02-08 04:05:38 +00:00
|
|
|
|
2021-01-18 15:15:39 +00:00
|
|
|
/*
|
|
|
|
CreateDoc 新建文档
|
|
|
|
*/
|
2019-05-17 09:45:50 +00:00
|
|
|
func CreateDoc(doc Doc) error {
|
2021-01-18 15:15:39 +00:00
|
|
|
sql := fmt.Sprintf(`INSERT INTO doc ( doc.title, doc.content, doc.author, doc.type,doc.is_public,doc.create_time) SELECT
|
2020-01-20 11:19:08 +00:00
|
|
|
'%s',
|
|
|
|
'%s',
|
|
|
|
'%s',
|
2020-03-26 12:15:04 +00:00
|
|
|
%d ,
|
2021-01-18 15:15:39 +00:00
|
|
|
%d,
|
|
|
|
'%s'
|
2020-01-20 11:19:08 +00:00
|
|
|
FROM
|
|
|
|
DUAL
|
|
|
|
WHERE
|
2020-03-26 12:15:04 +00:00
|
|
|
NOT EXISTS ( SELECT * FROM doc WHERE doc.title = '%s' );`, doc.Title, strings.Replace(doc.Content, "'", "\\'", -1),
|
2021-02-08 04:05:38 +00:00
|
|
|
doc.Author, doc.Type, doc.IsPublic, time.Now().Format("2006-01-02 15:04:05"), doc.Title)
|
2019-06-30 14:59:00 +00:00
|
|
|
_, e := db.GetMysqlClient().Query(sql)
|
|
|
|
if nil != e {
|
2021-01-18 15:15:39 +00:00
|
|
|
log.Print(sql)
|
2019-05-16 10:05:20 +00:00
|
|
|
logs.Error(e.Error())
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
return nil
|
2019-06-30 14:59:00 +00:00
|
|
|
}
|
2021-02-08 04:05:38 +00:00
|
|
|
|
2022-04-14 06:37:36 +00:00
|
|
|
/*
|
|
|
|
CreateDoc 新建文档
|
|
|
|
*/
|
|
|
|
func CreateDocTree(doc DocTree) error {
|
|
|
|
sql := fmt.Sprintf(`INSERT INTO doc_copy1 ( doc_copy1.title, doc_copy1.content,
|
|
|
|
doc_copy1.author, doc_copy1.type,doc_copy1.is_public,doc_copy1.create_time,father,level) SELECT
|
|
|
|
'%s',
|
|
|
|
'%s',
|
|
|
|
'%s',
|
|
|
|
%d ,
|
|
|
|
%d,
|
|
|
|
'%s',
|
|
|
|
%d,
|
|
|
|
%d
|
|
|
|
FROM
|
|
|
|
DUAL
|
|
|
|
WHERE
|
|
|
|
NOT EXISTS ( SELECT * FROM doc_copy1 WHERE doc_copy1.title = '%s' );`, doc.Title, strings.Replace(doc.Content, "'", "\\'", -1),
|
2023-09-26 16:32:29 +00:00
|
|
|
doc.Author, doc.Type, doc.IsPublic, time.Now().Format("2006-01-02 15:04:05"), doc.Father, doc.Level, doc.Title)
|
2022-04-14 06:37:36 +00:00
|
|
|
_, e := db.GetMysqlClient().Query(sql)
|
|
|
|
if nil != e {
|
|
|
|
log.Print(sql)
|
|
|
|
logs.Error(e.Error())
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-26 16:32:29 +00:00
|
|
|
func GetIdFromTitle(title string) int {
|
|
|
|
|
|
|
|
motors := []DocTree{}
|
|
|
|
db.GetOrm().LogMode(true)
|
|
|
|
db.GetOrm().Model(&DocTree{}).Find(&motors, fmt.Sprintf("title = '%s'", title))
|
|
|
|
if len(motors) == 0 {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
db.GetOrm().LogMode(false)
|
|
|
|
|
|
|
|
return int(motors[0].ID)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-18 15:15:39 +00:00
|
|
|
/*
|
2022-03-31 19:27:07 +00:00
|
|
|
UpdateDoc 更新文档
|
2021-01-18 15:15:39 +00:00
|
|
|
*/
|
2020-08-23 18:08:26 +00:00
|
|
|
func UpdateDoc(doc Doc) error {
|
2021-03-04 13:46:04 +00:00
|
|
|
sql := fmt.Sprintf(`update doc set doc.author = '%s' ,doc.title = '%s',doc.type = '%d',doc.content = '%s' ,doc.update_time = '%s' ,doc.version = '%d' where doc.id = '%d'; `,
|
2020-08-23 18:08:26 +00:00
|
|
|
doc.Author, doc.Title, doc.Type,
|
2023-09-26 16:32:29 +00:00
|
|
|
strings.Replace(doc.Content, "'", "\\'", -1), time.Now().Format("2006-01-02 15:04:05"), doc.Version, doc.ID)
|
2019-08-01 14:52:57 +00:00
|
|
|
_, e := db.GetMysqlClient().Query(sql)
|
|
|
|
if nil != e {
|
|
|
|
logs.Error(e.Error())
|
|
|
|
return e
|
|
|
|
}
|
2022-11-13 16:13:42 +00:00
|
|
|
// sql = fmt.Sprintf(`insert into doc_history(title,doc_id,content,edit_time) values('%s',%d,'%s','%s');`,
|
|
|
|
// doc.Title,doc.ID,strings.Replace(doc.Content, "'", "\\'", -1), time.Now().Format("2006-01-02 15:04:05"))
|
|
|
|
// _, er := db.GetMysqlClient().Query(sql)
|
|
|
|
// if nil != er {
|
|
|
|
// logs.Error(e.Error())
|
|
|
|
// return e
|
|
|
|
// }
|
2019-08-01 14:52:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-02-08 04:05:38 +00:00
|
|
|
|
2022-11-13 16:13:42 +00:00
|
|
|
// 添加文件版本。
|
2023-09-26 16:32:29 +00:00
|
|
|
func InsertDocHistory(doc DocTree) error {
|
2022-11-10 16:10:58 +00:00
|
|
|
sql := fmt.Sprintf(`insert into doc_history(title,doc_id,content,edit_time) values('%s',%d,'%s','%s');`,
|
2023-09-26 16:32:29 +00:00
|
|
|
doc.Title, doc.ID, strings.Replace(doc.Content, "'", "\\'", -1), time.Now().Format("2006-01-02 15:04:05"))
|
2022-11-10 16:10:58 +00:00
|
|
|
_, er := db.GetMysqlClient().Query(sql)
|
|
|
|
if nil != er {
|
|
|
|
logs.Error(er.Error())
|
|
|
|
return er
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-04-14 06:37:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
UpdateDoc 更新文档
|
|
|
|
*/
|
|
|
|
func UpdateDocTree(doc DocTree) error {
|
|
|
|
sql := fmt.Sprintf(`update doc_copy1 set doc_copy1.author = '%s' ,doc_copy1.title = '%s',
|
|
|
|
doc_copy1.type = '%d',doc_copy1.content = '%s' ,doc_copy1.update_time = '%s' ,
|
|
|
|
doc_copy1.version = '%d' where doc_copy1.id = '%d'; `,
|
|
|
|
doc.Author, doc.Title, doc.Type,
|
2023-09-26 16:32:29 +00:00
|
|
|
strings.Replace(doc.Content, "'", "\\'", -1), time.Now().Format("2006-01-02 15:04:05"), doc.Version, doc.ID)
|
2022-04-14 06:37:36 +00:00
|
|
|
_, e := db.GetMysqlClient().Query(sql)
|
|
|
|
if nil != e {
|
|
|
|
logs.Error(e.Error())
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-18 15:15:39 +00:00
|
|
|
/*
|
|
|
|
DeleteDoc 删除文档
|
|
|
|
*/
|
2020-08-23 18:08:26 +00:00
|
|
|
func DeleteDoc(id int64) error {
|
|
|
|
sql := fmt.Sprintf(`delete from doc where id = %d`, id)
|
2019-09-06 07:53:04 +00:00
|
|
|
_, e := db.GetMysqlClient().Query(sql)
|
|
|
|
if nil != e {
|
|
|
|
logs.Error(e.Error())
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
return nil
|
2019-09-06 16:31:01 +00:00
|
|
|
}
|
2021-02-08 04:05:38 +00:00
|
|
|
|
2022-04-14 06:37:36 +00:00
|
|
|
/*
|
|
|
|
DeleteDoc 删除文档
|
|
|
|
*/
|
|
|
|
func DeleteDocTree(id int64) error {
|
|
|
|
sql := fmt.Sprintf(`delete from doc_copy1 where id = %d`, id)
|
|
|
|
_, e := db.GetMysqlClient().Query(sql)
|
|
|
|
if nil != e {
|
|
|
|
logs.Error(e.Error())
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-18 15:15:39 +00:00
|
|
|
/*
|
|
|
|
AddArticleType 添加文档类型
|
|
|
|
*/
|
2020-08-23 18:08:26 +00:00
|
|
|
func AddArticleType(t ArticleType) error {
|
|
|
|
sql := fmt.Sprintf("insert into doc_type(id,type_name,`group`) values ('%d','%s','%d')", t.Id, t.Name, t.Group)
|
2020-07-26 07:03:42 +00:00
|
|
|
log.Print(sql)
|
2020-01-24 16:07:37 +00:00
|
|
|
_, e := db.GetMysqlClient().Query(sql)
|
|
|
|
if nil != e {
|
|
|
|
logs.Error(e.Error())
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
return nil
|
2020-01-25 17:39:31 +00:00
|
|
|
}
|
2021-02-08 04:05:38 +00:00
|
|
|
|
2021-01-18 15:15:39 +00:00
|
|
|
/*
|
|
|
|
UpdateArticleType 更新文档类型
|
|
|
|
*/
|
2020-08-23 18:08:26 +00:00
|
|
|
func UpdateArticleType(t ArticleType) error {
|
|
|
|
sql := fmt.Sprintf("update doc_type set type_name = '%s' and group = '%d' where id = %d", t.Name, t.Group, t.Id)
|
2020-01-26 11:11:48 +00:00
|
|
|
_, e := db.GetMysqlClient().Query(sql)
|
|
|
|
if nil != e {
|
|
|
|
logs.Error(e.Error())
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
return nil
|
2020-08-23 18:08:26 +00:00
|
|
|
|
2020-01-26 11:11:48 +00:00
|
|
|
}
|
2021-02-08 04:05:38 +00:00
|
|
|
|
2021-01-18 15:15:39 +00:00
|
|
|
/*
|
|
|
|
DeleteArticleType 删除文档类型
|
|
|
|
*/
|
2020-08-23 18:08:26 +00:00
|
|
|
func DeleteArticleType(id int32) error {
|
|
|
|
sql := fmt.Sprintf("delete from doc_type where id = '%d'", id)
|
2020-01-25 17:39:31 +00:00
|
|
|
_, e := db.GetMysqlClient().Query(sql)
|
|
|
|
if nil != e {
|
|
|
|
logs.Error(e.Error())
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
2020-05-03 18:05:04 +00:00
|
|
|
}
|
2023-09-26 16:32:29 +00:00
|
|
|
func GetAllDocs() ([]DocTree, error) {
|
|
|
|
ret := []DocTree{}
|
|
|
|
sql := fmt.Sprintf("select * from doc_copy1")
|
2020-08-23 18:08:26 +00:00
|
|
|
e := db.GetMysqlClient().Query2(sql, &ret)
|
2020-05-03 18:05:04 +00:00
|
|
|
if nil != e {
|
|
|
|
logs.Error(e.Error())
|
2020-08-23 18:08:26 +00:00
|
|
|
return nil, e
|
2020-05-03 18:05:04 +00:00
|
|
|
}
|
2020-08-23 18:08:26 +00:00
|
|
|
return ret, nil
|
2020-07-26 07:03:42 +00:00
|
|
|
}
|
2022-03-31 19:27:07 +00:00
|
|
|
|
2020-08-23 18:08:26 +00:00
|
|
|
func GetAllGroup() ([]DocGroup, error) {
|
2020-07-26 07:03:42 +00:00
|
|
|
ret := []DocGroup{}
|
|
|
|
sql := fmt.Sprintf("select * from doc_group")
|
2020-08-23 18:08:26 +00:00
|
|
|
e := db.GetMysqlClient().Query2(sql, &ret)
|
2020-07-26 07:03:42 +00:00
|
|
|
if nil != e {
|
|
|
|
logs.Error(e.Error())
|
2020-08-23 18:08:26 +00:00
|
|
|
return nil, e
|
2020-07-26 07:03:42 +00:00
|
|
|
}
|
2020-08-23 18:08:26 +00:00
|
|
|
return ret, nil
|
|
|
|
}
|
2020-09-26 04:30:23 +00:00
|
|
|
|
|
|
|
func GetTypeGroup(id int32) (DocGroup, error) {
|
|
|
|
ret := []DocGroup{}
|
|
|
|
sql := fmt.Sprintf("select * from doc_group where doc_group = ?", id)
|
|
|
|
e := db.GetMysqlClient().Query2(sql, &ret)
|
|
|
|
if nil != e {
|
|
|
|
logs.Error(e.Error())
|
|
|
|
return DocGroup{}, e
|
|
|
|
}
|
|
|
|
if len(ret) > 0 {
|
|
|
|
return ret[0], nil
|
|
|
|
} else {
|
|
|
|
return DocGroup{}, errors.New("no existed")
|
|
|
|
}
|
|
|
|
}
|
2021-01-18 15:15:39 +00:00
|
|
|
|
2020-09-26 04:30:23 +00:00
|
|
|
func GetGroupTypes(id int32) ([]ArticleType, error) {
|
|
|
|
ret := []ArticleType{}
|
|
|
|
sql := fmt.Sprintf("select * from doc_type where doc_type.group = %d", id)
|
|
|
|
log.Print(sql)
|
|
|
|
e := db.GetMysqlClient().Query2(sql, &ret)
|
|
|
|
if nil != e {
|
|
|
|
logs.Error(e.Error())
|
|
|
|
return nil, e
|
|
|
|
}
|
|
|
|
if len(ret) > 0 {
|
|
|
|
return ret, nil
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("no existed")
|
|
|
|
}
|
|
|
|
}
|