background/model/blog.go

196 lines
4.5 KiB
Go
Raw Normal View History

2019-05-16 18:05:20 +08:00
package model
import (
"background/db"
"background/logs"
"fmt"
"strings"
"time"
2020-08-24 02:08:26 +08:00
2020-09-26 12:30:23 +08:00
"github.com/pkg/errors"
2020-08-24 02:08:26 +08:00
"qiniupkg.com/x/log.v7"
2019-05-16 18:05:20 +08:00
)
type Doc struct {
2020-08-24 02:08:26 +08:00
ID int64 `sql:"id" json:"id"`
Title string `sql:"title" json:"title"`
Type int64 `sql:"type" json:"type"`
Content string `sql:"content" json:"content"`
Author string `sql:"author" json:"author"`
IsPublic int `sql:"is_public" json:"is_public"`
2019-05-17 17:45:50 +08:00
}
2019-05-16 18:05:20 +08:00
2020-07-23 00:06:07 +08:00
type DocGroup struct {
2020-08-24 02:08:26 +08:00
Int int32 `sql:"int"`
2020-07-23 00:06:07 +08:00
Name string `sql:"name"`
}
2019-05-17 17:45:50 +08:00
type ArticleType struct {
2020-08-24 02:08:26 +08: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-23 00:06:07 +08:00
GroupName string `json:"group_name"`
2019-05-17 17:45:50 +08:00
}
2019-06-30 22:59:00 +08:00
func GetArticlesType() []ArticleType {
2019-05-17 17:45:50 +08:00
ret := []ArticleType{}
2020-01-25 22:26:41 +08:00
sql := fmt.Sprintf("select * from doc_type")
2020-09-26 12:30:23 +08:00
e := db.GetMysqlClient().Query2(sql, &ret)
log.Print(ret)
2020-08-24 02:08:26 +08:00
for k, _ := range ret {
2020-07-23 00:06:07 +08:00
group := []DocGroup{}
2020-08-24 02:08:26 +08:00
sql = fmt.Sprintf("select * from doc_group where doc_group.int = %d", ret[k].Group)
2020-09-26 12:30:23 +08:00
db.GetMysqlClient().Query2(sql, &group)
2020-08-24 02:08:26 +08:00
if len(group) > 0 {
2020-07-23 00:06:07 +08:00
ret[k].GroupName = group[0].Name
}
}
2019-06-30 22:59:00 +08:00
if nil != e {
2019-05-17 17:45:50 +08:00
logs.Error(e.Error())
return nil
2019-05-16 18:05:20 +08:00
}
2019-05-17 17:45:50 +08:00
return ret
}
/*
CreateDoc 新建文档
*/
2019-05-17 17:45:50 +08:00
func CreateDoc(doc Doc) error {
sql := fmt.Sprintf(`INSERT INTO doc ( doc.title, doc.content, doc.author, doc.type,doc.is_public,doc.create_time) SELECT
2020-01-20 19:19:08 +08:00
'%s',
'%s',
'%s',
2020-03-26 20:15:04 +08:00
%d ,
%d,
'%s'
2020-01-20 19:19:08 +08:00
FROM
DUAL
WHERE
2020-03-26 20:15:04 +08:00
NOT EXISTS ( SELECT * FROM doc WHERE doc.title = '%s' );`, doc.Title, strings.Replace(doc.Content, "'", "\\'", -1),
doc.Author, doc.Type, doc.IsPublic,time.Now().Format("2006-01-02 15:04:05"), doc.Title)
2019-06-30 22:59:00 +08:00
_, e := db.GetMysqlClient().Query(sql)
if nil != e {
log.Print(sql)
2019-05-16 18:05:20 +08:00
logs.Error(e.Error())
return e
}
return nil
2019-06-30 22:59:00 +08:00
}
/*
UpdateDoc 更新文档
*/
2020-08-24 02:08:26 +08:00
func UpdateDoc(doc Doc) error {
sql := fmt.Sprintf(`update doc set doc.author = '%s' ,doc.title = '%s',doc.type = '%d',doc.content = '%s' ,doc.update_time = '%s' where doc.id = '%d'; `,
2020-08-24 02:08:26 +08:00
doc.Author, doc.Title, doc.Type,
strings.Replace(doc.Content, "'", "\\'", -1), time.Now().Format("2006-01-02 15:04:05"),doc.ID)
2019-08-01 22:52:57 +08:00
_, e := db.GetMysqlClient().Query(sql)
if nil != e {
logs.Error(e.Error())
return e
}
return nil
}
/*
DeleteDoc 删除文档
*/
2020-08-24 02:08:26 +08:00
func DeleteDoc(id int64) error {
sql := fmt.Sprintf(`delete from doc where id = %d`, id)
2019-09-06 15:53:04 +08:00
_, e := db.GetMysqlClient().Query(sql)
if nil != e {
logs.Error(e.Error())
return e
}
return nil
2019-09-07 00:31:01 +08:00
}
/*
AddArticleType 添加文档类型
*/
2020-08-24 02:08:26 +08: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 15:03:42 +08:00
log.Print(sql)
2020-01-25 00:07:37 +08:00
_, e := db.GetMysqlClient().Query(sql)
if nil != e {
logs.Error(e.Error())
return e
}
return nil
2020-01-26 01:39:31 +08:00
}
/*
UpdateArticleType 更新文档类型
*/
2020-08-24 02:08:26 +08: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 19:11:48 +08:00
_, e := db.GetMysqlClient().Query(sql)
if nil != e {
logs.Error(e.Error())
return e
}
return nil
2020-08-24 02:08:26 +08:00
2020-01-26 19:11:48 +08:00
}
/*
DeleteArticleType 删除文档类型
*/
2020-08-24 02:08:26 +08:00
func DeleteArticleType(id int32) error {
sql := fmt.Sprintf("delete from doc_type where id = '%d'", id)
2020-01-26 01:39:31 +08:00
_, e := db.GetMysqlClient().Query(sql)
if nil != e {
logs.Error(e.Error())
return e
}
return nil
}
2020-08-24 02:08:26 +08:00
func GetAllDocs() ([]Doc, error) {
ret := []Doc{}
sql := fmt.Sprintf("select * from doc")
2020-08-24 02:08:26 +08:00
e := db.GetMysqlClient().Query2(sql, &ret)
if nil != e {
logs.Error(e.Error())
2020-08-24 02:08:26 +08:00
return nil, e
}
2020-08-24 02:08:26 +08:00
return ret, nil
2020-07-26 15:03:42 +08:00
}
2020-08-24 02:08:26 +08:00
func GetAllGroup() ([]DocGroup, error) {
2020-07-26 15:03:42 +08:00
ret := []DocGroup{}
sql := fmt.Sprintf("select * from doc_group")
2020-08-24 02:08:26 +08:00
e := db.GetMysqlClient().Query2(sql, &ret)
2020-07-26 15:03:42 +08:00
if nil != e {
logs.Error(e.Error())
2020-08-24 02:08:26 +08:00
return nil, e
2020-07-26 15:03:42 +08:00
}
2020-08-24 02:08:26 +08:00
return ret, nil
}
2020-09-26 12:30:23 +08: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")
}
}
2020-09-26 12:30:23 +08: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")
}
}