package model import ( "background/db" "background/logs" "fmt" "strings" "time" "github.com/pkg/errors" "qiniupkg.com/x/log.v7" ) type Doc struct { 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"` } type DocGroup struct { Int int32 `sql:"int"` Name string `sql:"name"` } type ArticleType struct { 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"` GroupName string `json:"group_name"` } func GetArticlesType() []ArticleType { ret := []ArticleType{} sql := fmt.Sprintf("select * from doc_type") e := db.GetMysqlClient().Query2(sql, &ret) log.Print(ret) for k, _ := range ret { group := []DocGroup{} sql = fmt.Sprintf("select * from doc_group where doc_group.int = %d", ret[k].Group) db.GetMysqlClient().Query2(sql, &group) if len(group) > 0 { ret[k].GroupName = group[0].Name } } if nil != e { logs.Error(e.Error()) return nil } return ret } /* CreateDoc 新建文档 */ 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 '%s', '%s', '%s', %d , %d, '%s' FROM DUAL WHERE 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) _, e := db.GetMysqlClient().Query(sql) if nil != e { log.Print(sql) logs.Error(e.Error()) return e } return nil } /* UpdateDoc 更新文档 */ 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'; `, doc.Author, doc.Title, doc.Type, strings.Replace(doc.Content, "'", "\\'", -1), time.Now().Format("2006-01-02 15:04:05"), doc.ID) _, e := db.GetMysqlClient().Query(sql) if nil != e { logs.Error(e.Error()) return e } return nil } /* DeleteDoc 删除文档 */ func DeleteDoc(id int64) error { sql := fmt.Sprintf(`delete from doc where id = %d`, id) _, e := db.GetMysqlClient().Query(sql) if nil != e { logs.Error(e.Error()) return e } return nil } /* AddArticleType 添加文档类型 */ 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) log.Print(sql) _, e := db.GetMysqlClient().Query(sql) if nil != e { logs.Error(e.Error()) return e } return nil } /* UpdateArticleType 更新文档类型 */ 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) _, e := db.GetMysqlClient().Query(sql) if nil != e { logs.Error(e.Error()) return e } return nil } /* DeleteArticleType 删除文档类型 */ func DeleteArticleType(id int32) error { sql := fmt.Sprintf("delete from doc_type where id = '%d'", id) _, e := db.GetMysqlClient().Query(sql) if nil != e { logs.Error(e.Error()) return e } return nil } func GetAllDocs() ([]Doc, error) { ret := []Doc{} sql := fmt.Sprintf("select * from doc") e := db.GetMysqlClient().Query2(sql, &ret) if nil != e { logs.Error(e.Error()) return nil, e } return ret, nil } func GetAllGroup() ([]DocGroup, error) { ret := []DocGroup{} sql := fmt.Sprintf("select * from doc_group") e := db.GetMysqlClient().Query2(sql, &ret) if nil != e { logs.Error(e.Error()) return nil, e } return ret, nil } 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") } } 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") } }