package article_service import ( "SCRM/models" "SCRM/service" "fmt" "github.com/jinzhu/gorm" "time" ) func FindArticleCategoryType (userOrgId int64)(*[]models.ArticleCategory,error){ cartegory := &[]models.ArticleCategory{} err := service.PatientReadDB().Where("status = ? AND user_org_id = ? ", 1, userOrgId).Find(cartegory).Error return cartegory,err } func FindCategoryList(orgID int64)(cate []*models.ArticleCategory,err error) { err = service.PatientReadDB().Model(&models.ArticleCategory{}).Where("user_org_id =? AND status =?", orgID, 1).Order("id asc").Find(&cate).Error return } func AddAritcle(articles models.Articles) error { err := service.PatientWriteDB().Create(&articles).Error return err } func FindAllArticle(orgID int64, page int64,limit int64, searchKey string,classId int64) (articles []*models.Articles,total int64, err error) { fmt.Println("机构ID",orgID,"搜搜索",searchKey) db := service.PatientReadDB().Table("sgj_patient_articles as a ").Where("a.status=1") if(orgID>0){ db = db.Where("a.user_org_id=?" ,orgID) } if(classId > 0){ db = db.Where("class_id = ?",classId) } if len(searchKey) > 0 { searchKey = "%" + searchKey + "%" db = db.Where("a.title LIKE ?", searchKey) } offset := (page - 1) * limit err = db.Count(&total).Order("a.ctime desc").Offset(offset).Limit(limit). Preload("Comments", func(db *gorm.DB) *gorm.DB { return db.Where(" status = ?",1) }).Where("article_status <> ?",3). Select("a.id, a.title, a.summary, a.content, a.type, a.num, a.mtime, a.real_read_num, a.ctime, a.class_id, a.author, a.status, a.reason,a.star_num, a.comment_num,a.user_org_id, a.article_status, a.imgs, a.video_url, a.source, a.category_id, a.ttid, a.ttype, a.toid").Find(&articles).Error fmt.Println("err是啥呢?",err) if err != nil { return } return } func AddCategory(name string)(*models.ArticleCategory,error ){ var art models.ArticleCategory var err error err = service.PatientReadDB().Model(&models.ArticleCategory{}).Where("name = ? AND status = ?", name, 1).Find(&art).Error fmt.Println("错误",err) if err == gorm.ErrRecordNotFound{ return nil,err } if err != nil { return nil,err } return &art,err } func SaveCategory(category models.ArticleCategory) error { err := service.PatientWriteDB().Create(&category).Error return err } func GetCategorys(page int64,limit int64,orgID int64) ( categorys []*models.ArticleCategory,total int64,err error) { db := service.PatientReadDB().Table("sgj_patient_articles_menu as a").Where("a.status=1") if(orgID > 0){ db = db.Where("a.user_org_id=?" ,orgID) } offset := (page - 1) * limit err = db.Count(&total).Order("a.ctime desc").Offset(offset).Limit(limit). Select("a.id,a.name,a.order,a.status,a.summary,a.type,a.user_org_id,a.mtime,a.ctime,a.num").Find(&categorys).Error if err != nil { return } //rows, err := db.Raw("SELECT t.name,(SELECT count(id) from sgj_patient_articles as a WHERE a.class_id = t.id) as count from sgj_patient_articles_menu as t").Rows() //fmt.Println("错误",err) //fmt.Println("rows是谁很忙",rows) return } func GetCategorysByID(orgID int64,id int64)(categorys []*models.ArticleCategory,err error){ err = service.PatientReadDB().Where("user_org_id = ? AND id =? AND status =1", orgID, id).Find(&categorys).Error if err == gorm.ErrRecordNotFound { return nil, nil } if err != nil { return nil, err } return categorys,err } func EditCategory(cat *models.ArticleCategory,orgID int64,id int64){ service.PatientWriteDB().Model(cat).Where("user_org_id =? AND id =? AND status = ?", orgID, id, 1).Update(map[string]interface{}{"Name": cat.Name, "Summary": cat.Summary, "Order": cat.Order, "mtime": cat.Mtime}) } func DeleteCategorys(orgID int64,ids[] int64)(err error){ if len(ids) ==1{ err = service.PatientWriteDB().Model(&models.ArticleCategory{}).Where( "id=? and user_org_id = ?",ids[0],orgID).Update(map[string]interface{}{"Status":0,"mtime":time.Now().Unix()}).Error }else { err = service.PatientWriteDB().Model(&models.ArticleCategory{}).Where("id IN (?) and user_org_id = ?",ids,orgID).Update(map[string]interface{}{"Status":0,"mtime":time.Now().Unix()}).Error } return } func AddVido(articles models.Articles) error { err := service.PatientWriteDB().Create(&articles).Error return err } func AddPrviewArticle(articles models.Articles)(err error) { err = service.PatientWriteDB().Create(&articles).Error return err } func GetArtilcePreview(OrgID int64)(models.Articles,error) { article := models.Articles{} err := service.PatientReadDB().Where("user_org_id = ? AND article_status = ? AND type = ?", OrgID, 3, 1).Last(&article).Error return article, err } func AddDraft(articles models.Articles) error { err := service.PatientReadDB().Create(&articles).Error return err } func GetPublished(orgID int64,page int64,limit int64, searchKey string)(articles []*models.Articles,total int64, err error) { db := service.PatientReadDB().Table("sgj_patient_articles as a ").Where("a.status=1") if (orgID > 0) { db = db.Where("a.user_org_id=? AND article_status = ?", orgID,1) } if len(searchKey) > 0 { searchKey = "%" + searchKey + "%" db = db.Where("a.title LIKE ?", searchKey) } offset := (page - 1) * limit err = db.Count(&total).Order("a.ctime desc").Offset(offset).Limit(limit). Preload("Comments", func(db *gorm.DB) *gorm.DB { return db.Where(" status = ?",1) }).Where("status = ?",1). Select("a.id, a.title, a.summary, a.content, a.type, a.num, a.mtime, a.real_read_num, a.ctime, a.class_id, a.author, a.status, a.reason,a.star_num, a.comment_num,a.user_org_id, a.article_status, a.imgs, a.video_url, a.source, a.category_id, a.ttid, a.ttype, a.toid").Find(&articles).Error fmt.Println("err是啥呢?",err) if err != nil { return } return } //func GetAllartic(page int64,limit int64,orgID int64)(articles []*models.Articles,total int64, err error) { // db := service.PatientReadDB().Table("sgj_patient_articles as a ").Where("a.status=1") // if (orgID > 0) { // db = db.Where("a.user_org_id=? AND article_status = ?", orgID,1) // } // offset := (page - 1) * limit // err = db.Count(&total).Order("a.ctime desc").Offset(offset).Limit(limit). // Preload("Comments", func(db *gorm.DB) *gorm.DB { // return db.Where(" status = ?",1) // }).Select("a.id, a.title, a.summary, a.content, a.type, a.num, a.mtime, a.real_read_num, a.ctime, a.class_id, a.author, a.status, a.reason,a.star_num, a.comment_num,a.user_org_id, a.article_status, a.imgs, a.video_url, a.source, a.category_id, a.ttid, a.ttype, a.toid").Find(&articles).Error // fmt.Println("err是啥呢?",err) // if err != nil { // return // } // return //} func GetDraftbox(orgID int64,page int64,limit int64, searchKey string)(articles []*models.Articles,total int64, err error) { db := service.PatientReadDB().Table("sgj_patient_articles as a ").Where("a.status=1") if (orgID > 0) { db = db.Where("a.user_org_id=? AND article_status = ?", orgID,2) } if len(searchKey) > 0 { searchKey = "%" + searchKey + "%" db = db.Where("a.title LIKE ?", searchKey) } offset := (page - 1) * limit err = db.Count(&total).Order("a.ctime desc").Offset(offset).Limit(limit). Preload("Comments", func(db *gorm.DB) *gorm.DB { return db.Where(" status = ?",1) }).Where("article_status = ? AND status = ?",2,1). Select("a.id, a.title, a.summary, a.content, a.type, a.num, a.mtime, a.real_read_num, a.ctime, a.class_id, a.author, a.status, a.reason,a.star_num, a.comment_num,a.user_org_id, a.article_status, a.imgs, a.video_url, a.source, a.category_id, a.ttid, a.ttype, a.toid").Find(&articles).Error fmt.Println("err是啥呢?",err) if err != nil { return } return } func GetNoPass(orgID int64,page int64,limit int64, searchKey string)(articles []*models.Articles,total int64, err error) { db := service.PatientReadDB().Table("sgj_patient_articles as a ").Where("a.status=1") if (orgID > 0) { db = db.Where("a.user_org_id=? AND article_status = ?", orgID,4) } if len(searchKey) > 0 { searchKey = "%" + searchKey + "%" db = db.Where("a.title LIKE ?", searchKey) } offset := (page - 1) * limit err = db.Count(&total).Order("a.ctime desc").Offset(offset).Limit(limit). Preload("Comments", func(db *gorm.DB) *gorm.DB { return db.Where(" status = ?",1) }).Where("article_status = ? AND status = ?",4,1). Select("a.id, a.title, a.summary, a.content, a.type, a.num, a.mtime, a.real_read_num, a.ctime, a.class_id, a.author, a.status, a.reason,a.star_num, a.comment_num,a.user_org_id, a.article_status, a.imgs, a.video_url, a.source, a.category_id, a.ttid, a.ttype, a.toid").Find(&articles).Error fmt.Println("err是啥呢?",err) if err != nil { return } return } func GetArticleInfo(orgID int64,id int64)(articles models.Articles,err error){ err = service.PatientReadDB().Where("user_org_id = ? AND id = ? AND status = ?", orgID, id, 1).Find(&articles).Error return articles,err } func DeleteArticle(ids[] int64,orgID int64)(err error) { if len(ids) ==1{ err = service.PatientWriteDB().Model(&models.Articles{}).Where( "id=? and user_org_id = ?",ids[0],orgID).Update(map[string]interface{}{"status":0,"mtime":time.Now().Unix()}).Error }else { err = service.PatientWriteDB().Model(&models.Articles{}).Where("id IN (?) and user_org_id = ?",ids,orgID).Update(map[string]interface{}{"status":0,"mtime":time.Now().Unix()}).Error } return } func DeleteArticles(id int64,orgID int64)( err error) { err =service.PatientWriteDB().Model(&models.Articles{}).Where("id = ? and user_org_id = ?", id, orgID).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error return } func GetMenus(OrgID int64)(categorys []*models.ArticleCategory,err error) { err = service.PatientReadDB().Where("user_org_id = ? AND status = ?", OrgID,1).Find(&categorys).Error return categorys ,err } func UpdataArticleInfo(art *models.Articles,orgID int64,id int64){ service.PatientWriteDB().Model(art).Where("user_org_id = ? AND id = ? AND status = ?",orgID,id,1).Update(map[string]interface{}{"title":art.Title,"content":art.Content,"imgs":art.Imgs,"video_url":art.VideoUrl,"class_id":art.ClassId,"mtime":art.Mtime,"article_status":art.ArticleStatus}) } func SaveVidoDraft(art *models.Articles,orgID int64,id int64) { service.PatientWriteDB().Model(art).Where("user_org_id =? AND id = ? AND status = ?",orgID,id,1).Update(map[string]interface{}{"title":art.Title,"imgs":art.Imgs,"class_id":art.ClassId,"mtime":art.Mtime,"article_status":art.ArticleStatus}) } func PreviewEditArticle(art models.Articles,orgID int64,id int64) { service.PatientWriteDB().Model(art).Where("user_org_id = ? AND id = ? AND status = ?",orgID,id,1).Update(map[string]interface{}{"title":art.Title,"content":art.Content,"imgs":art.Imgs,"class_id":art.ClassId,"mtime":art.Mtime,"article_status":art.ArticleStatus}) } //func GetPreviewInfo(orgID int64)(models.Articles,error) { // article := models.Articles{} // err := service.PatientReadDB().Where("user_org_id = ? AND article_status = ? AND type = ?", orgID, 3, 1).Last(&article).Error // return article, err //} func GetPreviewInfoById(id int64,orgid int64)(models.Articles,error){ articles := models.Articles{} err := service.PatientReadDB().Model(articles).Where("id = ? AND user_org_id = ?",id,orgid).Find(&articles).Error return articles,err } func GetAllComment(page int64,limit int64,orgID int64)(articles []*models.Articles,total int64, err error) { db := service.PatientReadDB().Table("sgj_patient_articles as a ").Where("a.status=1") if (orgID > 0) { db = db.Where("a.user_org_id=? AND article_status = ?", orgID,1) } offset := (page - 1) * limit err = db.Count(&total).Order("a.ctime desc").Offset(offset).Limit(limit). Preload("Comments", func(db *gorm.DB) *gorm.DB { return db.Where(" status = ?",1) }). Select("a.id, a.title, a.summary, a.content, a.type, a.num, a.mtime, a.real_read_num, a.ctime, a.class_id, a.author, a.status, a.reason,a.star_num, a.comment_num,a.user_org_id, a.article_status, a.imgs, a.video_url, a.source, a.category_id, a.ttid, a.ttype, a.toid").Find(&articles).Error fmt.Println("err是啥呢?",err) if err != nil { return } return } func GetArticleCommentDetail(articleId int64,orgID int64)(*models.Articles, error){ article := &models.Articles{} err :=service.PatientReadDB().Where("id = ? AND user_org_id = ? AND status = ?", articleId, orgID,1).Find(article).Error tm := time.Unix(article.Ctime, 0) article.PublicTime = tm.Format("2006-01-02 15:04:05") return article, err } func FindAllComments(page int64,limit int64,articleId int64)(comment []*models.Comment,total int64,err error) { db := service.PatientReadDB().Table("sgj_patient_articles_comment as a").Where("a.status = 1") offset := (page - 1) * limit err = db.Where("article_id = ? AND parent_id = ?", articleId, 0).Count(&total).Order("a.ctime desc").Offset(offset).Limit(limit). Select("a.id,a.article_id,a.comment_user_id,a.parent_id,a.content,a.ctime,a.status,a.star_num,a.comment_num,a.comment_id,a.comment_user_name,a.comment_user_avater,a.source,a.ttid,a.tuser_id,a.taid,a.tpid,a.tcid").Find(&comment).Error fmt.Println("err是啥呢?",err) if err != nil { return } return } func AddComment(comment *models.Comment)(error) { err := service.PatientReadDB().Create(comment).Error return err } func GetReplyAllComents(orgid int64,page int64,limit int64)(comment []*models.Comment,total int64,err error) { db := service.PatientReadDB().Table("sgj_patient_articles_comment as a").Where("a.status = ? AND a.user_org_id = ?",1,orgid) offset := (page - 1) * limit err = db.Where("parent_id = ?",0).Count(&total).Order("a.ctime desc").Offset(offset).Limit(limit). Select("a.id,a.article_id,a.comment_user_id,a.parent_id,a.content,a.ctime,a.status,a.star_num,a.comment_num,a.comment_id,a.comment_user_name,a.comment_user_avater,a.source,a.ttid,a.tuser_id,a.taid,a.tpid,a.tcid").Find(&comment).Error fmt.Println("err是啥呢?",err) if err != nil { return } return } func GetAllReplyFormId(articleID int64, commentId int64)([]models.Comment, error) { var comments []models.Comment errs := service.PatientReadDB().Model(&models.Comment{}). Where("article_id = ? AND parent_id = ? AND status = ?", articleID, commentId, 1). Find(&comments).Error return comments, errs } func ClearReplyInfo(id int64)(error){ err := service.PatientWriteDB().Model(&models.Comment{}).Where("id = ?", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error return err } func DeleteReply(id int64)(error){ err := service.PatientWriteDB().Model(&models.Comment{}).Where("id = ?", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error return err } func GetQueryReplyInfo(parentid int64,articleID int64)([]models.Comment,error) { var comments []models.Comment errs := service.PatientWriteDB().Model(&models.Comment{}). Where("article_id = ? AND parent_id = ? AND status = ?", articleID, parentid, 1).Find(&comments).Error return comments,errs } func DeleteAllReply(ids[] int64)(err error) { if len(ids) ==1{ err = service.PatientWriteDB().Model(&models.Comment{}).Where( "id=?",ids[0]).Update(map[string]interface{}{"status":0,"mtime":time.Now().Unix()}).Error }else { err = service.PatientWriteDB().Model(&models.Comment{}).Where("id IN (?)",ids).Update(map[string]interface{}{"status":0,"mtime":time.Now().Unix()}).Error } return } func DeleteAllArticles(ids[] int64)(err error) { if len(ids) ==1{ err = service.PatientWriteDB().Model(&models.Articles{}).Where( "id=?",ids[0]).Update(map[string]interface{}{"status":0,"mtime":time.Now().Unix()}).Error }else { err = service.PatientWriteDB().Model(&models.Articles{}).Where("id IN (?)",ids).Update(map[string]interface{}{"status":0,"mtime":time.Now().Unix()}).Error } return } func GetArticleDetailById(id int64,orgid int64)(models.Articles,error) { articles := models.Articles{} err := service.PatientReadDB().Model(articles).Where("id = ? AND user_org_id = ? AND status = ?", id, orgid, 1).Find(&articles).Error return articles,err }