scrm-go

article_category_service.go 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package article_service
  2. import (
  3. "SCRM/models"
  4. "SCRM/service"
  5. "fmt"
  6. "github.com/jinzhu/gorm"
  7. )
  8. func FindArticleCategoryType (userOrgId int64)(*[]models.ArticleCategory,error){
  9. cartegory := &[]models.ArticleCategory{}
  10. err := service.PatientReadDB().Where("status = ? AND user_org_id = ? ", 1, userOrgId).Find(cartegory).Error
  11. return cartegory,err
  12. }
  13. func FindCategoryList(orgID int64)(cate []*models.ArticleCategory,err error) {
  14. err = service.PatientReadDB().Model(&models.ArticleCategory{}).Where("user_org_id =? AND status =?", orgID, 1).Order("id asc").Find(&cate).Error
  15. return
  16. }
  17. func AddAritcle(articles models.Articles) error {
  18. err := service.PatientWriteDB().Create(&articles).Error
  19. return err
  20. }
  21. func FindAllArticle(orgID int64, page int64,limit int64, searchKey string, classId int64) (articles []*models.Articles,total int64, err error) {
  22. fmt.Println("机构ID",orgID,"搜搜索",searchKey)
  23. db := service.PatientReadDB().Table("sgj_patient_articles as a ").Where("a.status=1")
  24. if(orgID>0){
  25. db = db.Where("a.user_org_id=?" ,orgID)
  26. }
  27. if(classId>0){
  28. db = db.Joins("JOIN sgj_patient_articles_menu as s ON s.id = a.class_id AND s.status=1 AND user_org_id=?",orgID)
  29. }
  30. if len(searchKey) > 0 {
  31. searchKey = "%" + searchKey + "%"
  32. db = db.Where("a.title LIKE ?", searchKey)
  33. }
  34. offset := (page - 1) * limit
  35. err = db.Count(&total).Order("a.ctime desc").Offset(offset).Limit(limit).
  36. Preload("Comments", func(db *gorm.DB) *gorm.DB {
  37. return db.Where(" status = ?",1)
  38. }).
  39. 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
  40. fmt.Println("err是啥呢?",err)
  41. if err != nil {
  42. return
  43. }
  44. return
  45. }
  46. func AddCategory(category models.ArticleCategory) error {
  47. err := service.PatientWriteDB().Create(&category).Error
  48. return err
  49. }
  50. func GetCategorys(page int64,limit int64,orgID int64) ( categorys []*models.ArticleCategory,total int64,err error) {
  51. db := service.PatientReadDB().Table("sgj_patient_articles_menu as a").Where("a.status=1")
  52. if(orgID > 0){
  53. db = db.Where("a.user_org_id=?" ,orgID)
  54. }
  55. offset := (page - 1) * limit
  56. err = db.Count(&total).Order("a.ctime desc").Offset(offset).Limit(limit).
  57. 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
  58. if err != nil {
  59. return
  60. }
  61. return
  62. }
  63. func GetCategorysByID(orgID int64,id int64)(categorys []*models.ArticleCategory,err error){
  64. err = service.PatientReadDB().Where("user_org_id = ? AND id =? AND status =1", orgID, id).Find(&categorys).Error
  65. if err == gorm.ErrRecordNotFound {
  66. return nil, nil
  67. }
  68. if err != nil {
  69. return nil, err
  70. }
  71. return categorys,err
  72. }
  73. func EditCategory(category models.ArticleCategory)(err error) {
  74. err = service.PatientWriteDB().Save(&category).Error
  75. return err
  76. }